Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - nslay

Pages: 1 2 3 [4] 5
46
General Discussion / Microsoft's print system
« on: January 07, 2008, 04:54:06 am »
It's terrible!  And when you print to file, you get a driver-dependent file.

Call me impractical, but when I print to file, I kinda expect to be able to view the file or print it somewhere else.  Instead I got a bunch of machine code sludge for which no viewer exists, and only a specific printer can print!  Everyone else uses postscript...call me crazy, but that makes more sense!

A couple years ago, I wrote about CUPS on vL forum.  Boy, I thought that was a pain in the ass.  But in retrospect, I understand the rationale for it.  You have a daemon that listens for print requests and calls a filter that translates documents to the printer's format.  Many printers are so similar, you need only one filter that outputs with minor modifications.  CUPS uses ppd files that describes those minor differences.  When you install a filter, you usually get ppd files for thousands of printers...you can easily add printers anywhere you go in minutes!

Windows uses drivers and those things usually only work for a specific printer and come with crappy VB software to pollute your workspace and filesystem (and registry)!  Why do printers need a kernel-space driver...nobody else does it this way!  What are those jerk offs thinking!?

CUPS is elegant, its portable, extensible, and it can use varieties of mediums without much hassle.  And surprise, its owned by Apple!

So the story is:
Tomorrow is first day of classes.  I was at a friends house and printed my schedule to file on Windows XP and scp'd (he has cygwin) it home.  I assumed it was postscript because everything on Unix outputs postscript files when you print to file...quite handy to save paper and keep online payment receipts, tickets, etc...NAH, that would make too much sense!  Instead, I got a Lexmark EMF file...its a proprietary format and nothing can read it...and no, its not EMF.  It can probably only print to my friend's printer.  Now, the database that holds the schedule is offline and I cannot print it again.

Windows:  Easy to install, Easy to use, Easy to break, Hard to fix (so hard, companies and products exist to fix it!), and often not reliable!
Unix: Custom to install, Verbose to use, Hard to break, Easy to fix, often very reliable (FreeBSD and Linux hold records on netcraft)!

Quote
   "They say when you play a Microsoft CD backwards you can hear satanic messages...but that's nothing, if you play it forward it will install Windows!"
- churchofbsd.org

I indulge in reading John C. Dvorak's PC-MAG columns on annoyances in copying files in Windows.  They can't even get that one right, let alone kept computer technology in the DOS era into the 21st century.

47
Botdev / IRC Battle.net gateway
« on: October 26, 2007, 11:10:29 pm »
Hi everybody,
Haven't worked much on ircbnet since a couple days ago ... I did the bare minimum to make BNX work.  I've thought of a better user interface and API for it but I wonder...would any of you
a) Have use for it
b) Expand it to use other battle.net protocols

If not, I don't really see any reason to finish it.

Here are some ideas for an improved interface:
- Allow multiple connections...each connection has its own IRC context
This would be accomplished by creating a fake login sequence and joining the client to "The Void" initially...the client then can issue a "/server <irc server> [<irc port>]" command to invoke a connect.  Could also add other connect-time commands.
- Generalized protocol API
This would be accomplished in C using a struct of generalized bnet protocol event function pointers (relevant to chatting!)...this way the IRC portion of the code can transparently translate IRC events to Battle.net events.  This is how bunker bnet operates...except without the IRC portion.
- More intelligent IRC context
Presently, the IRC code in ircbnet just does simple translations ... it does not store information about IRC (e.g. like the users in the channel)

One very big problem with this interface is that bots like BNX do not know to send "/server" command on login...

Thoughts, ideas, comments?

48
Botdev / Brining a piece of history back to life
« on: October 25, 2007, 03:02:11 am »
I've been thinking about writing a protocol/OS portable, feature complete, BNX clone with initial support for IRC.  Notably, Bacca's BNX clone is not only not feature complete, but because it uses greetbot as its base which is not protocol or OS portable.  Anyhow, I chose BNX in particular because it is not only a hallmark to ancient Battle.net history, but it is incredibly simple with some fun features that made it unique, even to the later more sophisticated bots.  I feel BNX or a BNX/clone, with some additional features and maintenance has the potential to be one of best all-round chat bots ever made.  My only problem is that I lack the time to really sit down and design and write it.  Earlier this evening, I thought it would be fun to bring the original BNX back to life and wrote a IRC <-> Battle.net gateway for it - dubbed 'ircbnet'.  It's not quite complete but has the essentials for most bots to run, though, if you chatted through it, you'd notice some stuff missing.  Hopefully, if/when I finish it, I can post it here for you folks who still enjoy messing with battle.net bots.

Here is a picture of BNX 1.02 running on EFNet...it sits in #thecabal if you'd like to play around with it (it does have primitive chatting abilities).  And yes, I am fully aware anti-idle is not necessary on IRC because of the PING/PONG mechanisms, but tried to implement everything as it should be.


At present, ircbnet only supports thet text protocol (which is the only thing I know) but hopefully I can make the design flexible enough for someone, who cared, to add other protocols (e.g. like the so called binary protocols).
Feel free to comment or post questions...

How it works:
One starts ircbnet providing it with an IRC server and port to connect to - there is an optional debug flag as well.  If the debug flag isn't specified, it forks into the background.  It listens on localhost:6112 and upon accepting a connection, does a blocking connect to the IRC server.  First line it reads from the bot is considered the nickname.  It lies to the bot telling it that it joined The Void.  If the bot is not in any channel, any channel text (TALK,EMOTE) it sends is silently dropped.  ircbnet only allows the bot in one channel at a time and upon joining a new channel will auto-part the previous channel.  Nickname changes on IRC are dealt with by LEAVE/JOIN combo (nslay NICK enslay causes nslay to LEAVE and enslay to JOIN).  ircbnet loosely deals with user flags...everyone by default has NOUDP flag, but the operator flag is loosely handled (e.g. upon joining a channel and getting a USER list, those users with 0012 who might talk, will appear to have 0010)...NOUDP and CHANOP are the only flags used so far.  If either the bot or IRC server disconnect, ircbnet drops the other connection and will continue to listen for bot connections.  ircbnet only allows 1 bot to connect at a time.

EDIT: P.S. BNX is running one wine :(

49
Unix / Linux Discussion / pcc imported into Net/OpenBSD
« on: September 25, 2007, 08:05:27 pm »
One of the original C compilers (written at bell labs in 1970s) has been imported in both NetBSD and OpenBSD src trees.  Its of course under the BSDL - one of the main motivations of adopting the compiler.  It's been said to be better designed and faster than the gcc (not surprisingly, since it was designed for the PDP11!).  Of course, and expected, its also presently very limited in features.  OpenBSD developers have modified it into a C99 compliant compiler.  I can see this compiler being a major competitor to gcc and other free compilers a couple years from now.  I expect all BSDs (including OS X) to re-adopt it at some point (apparently it was last used in 4.3 BSD Reno).  Pretty crazy eh?

Links
http://www.ludd.ltu.se/~ragge/pcc/
http://en.wikipedia.org/wiki/Portable_C_Compiler
http://undeadly.org/cgi?action=article&sid=20070915195203&mode=flat&count=0
http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/pcc/

50
Unix / Linux Discussion / NetApp sues Sun over ZFS intellectual property
« on: September 16, 2007, 01:08:55 pm »
For NetApp's side:
http://blogs.netapp.com/dave/2007/09/netapp-sues-sun.html

For a pretty good view of both sides:
http://www.eweek.com/article2/0,1759,2179693,00.asp

Its a dissappointment when patents take something great and ruin it for everyone.  Patents on techniques make about as much sense as patents on techniques to solve math/scientific problems.
"Oops, you're going to have to pay Company X a license fee before you can use Fourier Transform"

51
General Security Information / Vista Content protection
« on: August 19, 2007, 05:05:17 am »
This is a pretty neat, yet disturbing article...
http://www.pcworld.com/article/id,135814-c,windowsbugs/article.html

It's exactly everything I imagined would happened the day I learned of DRM.  That was the same day I heard about Sony's DRM rootkit, and the same day I started looking at alternative operating systems.  I'm really disappointed in Microsoft...I've used the NT series since Windows 2000, beta tested Windows XP as well as Windows 2003 and I've seen a lot of improvement... Vista seems like a step in the wrong direction.  What happened?

DRM used to be confined to Windows, but now it is spreading to hardware and this should be concern to everyone.  The PC is an open architecture and I frankly believe this is taken for granted even by power users...What Microsoft is doing with Vista is threatening the open nature of the PC.  Hopefully open hardware will still prevail (e.g. much like USB vs Microchannel).  There are even implications of this maliciousness outside of this article, one namely comes to mind, AMD.  AMD announced it would release open graphics drivers back in May.  ATi responded that the drivers included content protection code that could not be open sourced.  There are even, now standardized, protocols for Vista to use with hardware to secure high definition content.

Quotes I find most frightening include:
Quote
"If there was any threat modeling at all, it was really badly done," Gutmann, from the University of Auckland, New Zealand, said while giving a talk on Vista content protection. "Once the enemy is the user and not the attacker, standard security thinking falls apart."

Vista requires premium content like high-definition movies to be degraded in quality when sent to high-quality outputs, so users are seeing status codes that say "graphics OPM resolution too high." Gutmann calls this "probably the most bizarre status code ever."
COMMENT:  So buying Vista is like buying a broken television?

Quote
"It's taking this open architecture that IBM created 25 years ago and making it closed again," he said.

Quote
Gutmann said hardware costs will increase because vendors can't provide Vista-approved security functionality unless Hollywood studios like MGM, 20th Century Fox and Disney grant written approval saying the content security meets their standards.
COMMENT:  Who are these corporations to dictate to Microsoft how it should treat its own customers!?

Quote
Gutmann argued that Microsoft placed content protection above all other priorities when building Vista, perhaps to gain favor and money from Hollywood. Microsoft should have instead focused this effort on security features that protect users, Gutmann said, such as encrypted paging to protect user secrets, protected content domains that keep out malware, and anti-debugging techniques to prevent rootkit hooking.

New Zealand's government, which has argued that digital rights management fails to address the rights of people and government, appears to be the only government worldwide to express public concern about Vista's content protection standards, Gutmann said.
COMMENT: I predict the US government, and others, will have to address the media's overwhelming malicious influence on computer technology.  Most people are not thiefs and they need to find a new solution to deal with pirates.

52
General Discussion / Fun Microsoft blog
« on: July 12, 2007, 09:50:05 am »
I was reading these one and two comical articles  about how Vista is a failure, and was linked to this blog, which the article claims is comprised of actual employees of the company.  From a brief skim of the blog, it seems plausible but there is no rubber stamp anywhere.

http://minimsft.blogspot.com/

Things to note for authenticity:
The disclaimer.

Quote
These are sole individual personal points-of-view and the posts and comments by the participants in no way represent the official point-of-view of Microsoft or any other organization. This is a discussion to foster debate and by no means an enactment of policy-violation. These posts are provided "as-is" with no warranties and confer no rights. So chill. And think.

Who has ever had to post a disclaimer when bad mouthing the company?  And what about this "policy-violation"?  There is also mention (and what detail) of internal affairs that are scattered throughout the blog too.

So what about this this blog on Vista?

Some favorite quotes from articles and blog:
Quote
"Vista is the biggest software development failure of all time, IBM's office vision was the previous record holder, with $900 million spent… Vista has cost five or six billion," the poster moaned.

Quote
The other equally monumental Me II failure? Gates in China launching a $3 version of bundled XP. Why is this not altruism? Well, it goes back to piracy and how it helped enforce the MS monopoly. If you can easily pirate Windows, Linux has no price advantage, they both cost zero.
---
So, it did the unthinkable, and dropped the price. I won't bother to hunt down all the exec quotes saying how people can't afford clean water would be overjoyed to sell kidneys to upgrade to the new version of Office, but they are out there. This was a sacred cow, and it is now hamburger backed up against the wall.

Quote
In the meantime, the discussion of how you'd sell Vista in 30-seconds to a non-techy consumer hasn't come up with much Abbie-understandable reasons other than "cooler games!" Sure, Abbie probably spends a lot of time with solitaire and minesweeper, so that's good. But most of it focuses either on issues so deep and technical that the average consumer is going to shrug and say, "Hell, I don't think I need any of that!" or on issues that make you think that XP is a ticking time-bomb of unstable code ready to explode 1s and 0s over anyone who looks at it wrong. And as for Alpha Geeks and super-users, it sounds like LUA is going to be a daily pain in the patootie.

53
Unix / Linux Discussion / The Linux Solution
« on: July 02, 2007, 01:36:37 pm »
I was reading this thread about GEOM providers and newly discovered USB media in freebsd-current@ list (development branch) and I was quite amused by a couple comments

A quote from Poul-Henning Kamp
Quote
In message <4688830C.6050701@root.org>, Nate Lawson writes:

> >camcontrol tur (test unit ready) will return an error or success if the
> >medium is now present.  Kicking off a process to do that when a
> >removable media da(4) device appears seems like a good idea.  It could
> >also be a thread in devd.

I would call that "The Linux Solution"  :-)

The right place to poll is from scsi_da.
http://lists.freebsd.org/pipermail/freebsd-current/2007-July/074410.html

And even funnier, Scott Long writes
Quote
I'm going to ask that you guys let me know before you adulterate scsi_da
with this.

Scott
http://lists.freebsd.org/pipermail/freebsd-current/2007-July/074416.html

Watching this list is quite fascinating ... its interesting to see the discussion and addition of new features, hardware support, kernel features, and userland tools all in real time.

54
Unix / Linux Discussion / Xorg 7.2 ported to FreeBSD
« on: May 18, 2007, 08:05:30 pm »
Not that anyone here uses FreeBSD as a workstation :P
but Xorg 7.2 was finally ported to FreeBSD ... I guess Xorg 7.x is extremely different than 6.x
http://lists.freebsd.org/pipermail/freebsd-announce/2007-May/001131.html


55
General Programming / bunker bnet
« on: April 18, 2007, 01:58:01 pm »
For channel warring reasons, Rob and I started implementing a battle.net clone, named 'bunker'
It implements text and all game protocols up to (but not including) war 3... but only for chatting purposes.  It does not, and will never support gaming.
bunker is written entirely in ANSI C and aims to be POSIX compliant where possible.
It supports the following
6112 (TCP/UDP) for clients
6113 (TCP) for server linking
/tmp/bunker (AF_UNIX) for IPC clients

Its database backend, for now, is dbm...we chose dbm because it is included in the Single Unix Specification (v2).  Additionally, it does not use a viral license like that of the GPL (our license is not an open source license for obvious reasons).  We might use BDB 1.85 later on.  Authentication takes place over the server link which is in contrast to using a dedicated database server software (MySQL's library also blocks...another reason we don't want to use MySQL...this server relies on non-blocking!).

So, our progress at present
-text protocol is complete
-game protocol is complete but may need cleaning up
-chatting is nearly complete, need to implement all the standard commands
-IPC handling, and IPC client are in the works...IPC client is used for administration and it is given administrator status on connect (new product type [IPCC], Inter Process Communication Client)
-server linking is in the works
-authentication is complete

So we're about half way finished, maybe 40-50%

bunker is modular, everything is as general as possible.  The server core is oblivious to the client's protocol, the protocols are modules that provide callbacks for the server to use.   Bunker, for the time being is multiplexed with select(), but it shouldn't be too difficult to use kqueue/epoll...in fact I might add #ifdefs BSD and Linux platforms to take advantage of those.

I am aiming for the battle.net environment as it was before nbbot (but not including the vulnerabilities it had, as iago was kind enough to mention to me :)).  For example, "The Void" is a valid chatting channel.  There is only allowed one CHAT client per IP (until a split happens, then the split server isn't aware of your other client).  We anticipate real splits for much the same reason IRC servers split.
We will import features like the friend list, and the '/r' command pvpgn has for responding to whispers.
I'll keep you up to date on progress, but it looks terrific so far!
Comments and criticisms are welcome

P.S.  If you have standard error/info responses, screenshots, etc...from those days, I'd really appreciate it if you could share them with me.

http://www.emadness.net/
http://www.projectbunker.net/

56
General Programming / [C++] Recursive auto differentiation library
« on: March 21, 2007, 02:04:50 am »
I wrote a library to perform recursive auto differentiation - In other words, it computes arbitrary order derivatives and partial derivatives exactly.

How it works:
For simple auto differentiation (suppose first order), you define a set on RxR with elements <u,du>
The operators +,-,/,*,sin,cos, etc... act on the pair in accordance to differentiation rules
e.g. <u,du>*<v,dv> = <u*v,u*dv+v*du>
For second order, you define a threee tuple vector and the associated operators, for third, etc...

Some examples:
Two dependent variables
<5,0>*<4,0> = <20,0>
One independent variable
<5,1>*<4,0> = <20,4>
This example can be thought of as x*4...x's realization happens to be 5.
Two independent variables - partial derivatives
<5,<1,0>>*<4,<0,1>> = <20,<4,5>>
This example can be thought of as x*y...x's realization is 5 while y's is 4.  x and y are independent.
d/dx[x*y] = y=4
d/dy[x*y] = x=5
in the derivative vector, x is index 1 while y is index 2
Statically defining derivative rules can be tedious and most libraries I've seen only do up to the hessian (2nd order).

Two extend this idea, I define a set U on RxU ... the set is recursively defined.
u = <x,du> in U, with x in R, and du in U

Now operations are recursive and perform all derivatives.
e.g.
Let u = <x,du>, v = <y,dv>
Define <x,du>*<y,dv> = <x*y,u*dv+v*du>

Notice u*dv and v*du additionally use our multiplication operator...This process repeats forever.

The Implimentation
This is essentially a tree data structure.  Each node holds a double and a pointer of a vector of leaves.  The order of the auto derivative is defined by the depth of the tree ... the top of the tree is reached when our pointer is NULL.  All the operators and most of the math.h functions are overloaded very carefully to ensure no deadlocks or infinite recursions occurr.

Here's the header:
Code: [Select]
/*-
 * Copyright (c) 2007
 *      Nathan Lay <nslay@hotmail.com>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef ADVAR_H
#define ADVAR_H

class ADvar {
// Friends
// NOTE: the optional parameters are for indicating exit conditions in recursion and should be ignored.  Specifying values for the optional parameters can cause undefined behavior!
// NOTE: Some of these functions can exhibit oddities at critical numbers.  For example erf(x)'s derivative is 2/sqrt(pi)*exp(-u**2)*du/dx - If the auto derivative's order is 4, pow(u,2) runs 4 times computing u**-1 at some point.  If u had the value 0, then your 4th derivative wouldn't exist, even though in practice it should be 0
friend ADvar operator+( double nx, const ADvar &ad );
friend ADvar operator-( double nx, const ADvar &ad );
friend ADvar operator*( double nx, const ADvar &ad );
friend ADvar operator/( double nx, const ADvar &ad );
friend ADvar pow( const ADvar &ad, double nx, const ADvar *dx = NULL );
friend ADvar pow( double nx, const ADvar &ad, const ADvar *dx = NULL );
friend ADvar pow( const ADvar &ad1, const ADvar &ad2, const ADvar *dx = NULL );
friend ADvar exp( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar log( const ADvar &ad );
friend ADvar log10( const ADvar &ad );
friend ADvar sqrt( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar cbrt( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar sin( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar cos( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar tan( const ADvar &ad );
friend ADvar sinh( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar cosh( const ADvar &ad, const ADvar *dx = NULL );
friend ADvar tanh( const ADvar &ad );
friend ADvar asin( const ADvar &ad );
friend ADvar acos( const ADvar &ad );
friend ADvar atan( const ADvar &ad );
friend ADvar asinh( const ADvar &ad );
friend ADvar acosh( const ADvar &ad );
friend ADvar atanh( const ADvar &ad );
friend ADvar erf( const ADvar &ad );
friend ADvar fabs( const ADvar &ad, const ADvar *dx = NULL );

private:
unsigned long dim;
double x;
ADvar *dx;

public:
// Constructors
explicit ADvar( double x = 0 );
ADvar( const ADvar &ad );
~ADvar();

// Misc
// This functions sets the order and dimension (i.e. independent variables) of the auto derivative
int SetMode( unsigned long order, unsigned long dim );
// This function sets the value and dependency of the auto derivative
// an index of 0 will set the auto derivative to dependent
// NOTE: index should not be larger than dim! Should index > dim, index will be assumed 0
void SetDep( double x, unsigned long index );

// Accessors
// This function returns the derivative vector
const ADvar *Getdx() const;
// This function returns the dimension of the derivative vector
unsigned long GetDim() const;
// This function returns the order of the auto derivative
unsigned long GetOrder() const;

// Operators
// This operator is a convenience, allowing one to reference derivatives by index, e.g. ad[1][1] would give me the 2nd derivative in respect to index 2 and ad[0][1] would give me the derivative in respect to index 1 and 2.
const ADvar& operator[]( unsigned long n ) const;
ADvar operator-() const;
ADvar operator+( const ADvar &ad ) const;
ADvar operator+( double nx ) const;
ADvar operator-( const ADvar &ad ) const;
ADvar operator-( double nx ) const;
ADvar operator*( const ADvar &ad ) const;
ADvar operator*( double nx ) const;
ADvar operator/( const ADvar &ad ) const;
ADvar operator/( double nx ) const;
ADvar& operator=( const ADvar &ad );
ADvar& operator=( double nx );
ADvar& operator++();
ADvar operator++(int);
ADvar& operator--();
ADvar operator--(int);
ADvar& operator+=( const ADvar &ad );
ADvar& operator+=( double nx );
ADvar& operator-=( const ADvar &ad );
ADvar& operator-=( double nx );
ADvar& operator*=( const ADvar &ad );
ADvar& operator*=( double nx );
ADvar& operator/=( const ADvar &ad );
ADvar& operator/=( double nx );

// Typecasts
operator double() const;
};

#endif

Using the library:
Declare ADvar variables with an optional double
ADvar y; // Creates an order 0 auto derivative with value 0
ADvar x(5.2); // Creates an order 0 auto derivative with value 5.2

Because of limitations in C++ (which are very few!), it is not possible to set the order or dimension of the auto derivatives at constructor...the constructor must be able to recurse itself to initialize an auto derivative of order N...however, since it must new a dx array, it is not possible to call a constructor other than the default when allocating an array (that I know of).
e.g.
ADvar x(5.2,2,3); // Hypothetically create auto derivative of order 2 and dimension 3
The constructor allocates an array of 3 ADvars for dx, however it is not able to explicitly call itself.
e.g.
You cannot do: dx = new ADvar[3](0,1,3); // Create 3 order 1 auto derivatives with dimension 3
or: dx = new ADvar(0,1,3)[3];

We could keep a static counter in ADvar, but then this would not be thread safe.

Therefore, you must manually initialize your auto derivatives using SetMode( unsigned long order, unsigned long dim )

ADvar x, y, z;
x.SetMode( 3, 2 ); // Set x to compute order 3 derivatives with at most 2 independent variables
y.SetMode( 3, 2 );

Now that you have set desired order and dimension, you can now specify dependence.
x.SetDep( 5.2, 1 ); // Set x to be index 1 in derivative vector
y.SetDep( 3.1, 2 ); // Set y to be index 2 in derivative vector
NOTE: A zero index will set the auto derivative to be dependent

z = x*y+pow(cos(x),2.0)+2.0/x;

NOTE: It is important that you always use floats and doubles in your calculations, otherwise the C++ compiler will complain about ambiguity.  It is not practical to define operators for every single type!

You can extract derivatives in two ways ... the easiest is with the [] operator.
cout << "d/dx = " << z[0] << endl;
cout << "d^2/dxdy = " << z[0][1] << endl;
cout << "d^3/dx^3 = " << z[0][0][0] << endl;

Another way is defined since it is not always known in advance how many []s you're allowed to use on an ADvar.
const ADvar *dx = z.Getdx(), *ddx = dx[0].Getdx();
dx[0] describes z[0]
ddx[0] describes z[0][0], while ddx[1] describes z[0][1]

Two accessors are available to get the dimension and order of an ADvar.
unsigned long GetDim() const;
unsigned long GetOrder() const;

Examples
One of the first neat examples that came to mind was to compute arbitrary order taylor series:
Code: [Select]
#include <math.h>
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "advar.h"

#define N 10
#define DX 0.1
#define X0 0.0
#define BEGIN -4.0
#define END 4.0

#define PREFIX "plot"

using namespace std;

double taylor( double ax, double x0, unsigned long n );

int main( int argc, char **argv ) {
unsigned long i;
double x;
FILE *file, *pfile;
char fn[256];

pfile = fopen( "graph", "w" );

fprintf( pfile, "set grid\n" );
fprintf( pfile, "set xlabel 'x'\n" );
fprintf( pfile, "set ylabel 'y'\n" );
fprintf( pfile, "plot cos(x), " );

for ( i = 0; i <= N; ++i ) {
snprintf( fn, sizeof(fn), "%s%d", PREFIX, i );
file = fopen( fn, "w" );
for ( x = BEGIN; x <= END; x+=DX ) {
fprintf( file, "%lf %lf\n", x, taylor(x,X0,i) );
}
fprintf( pfile, "'%s' w linespoints title 'Taylor n=%d'", fn, i );
if ( i < N ) fprintf( pfile, ", " );
fclose( file );
}
fprintf( pfile, "\n" );
fclose( pfile );

return 0;
}

double taylor( double nx, double x0, unsigned long n ) {
unsigned long i, prod;
ADvar x, z;
const ADvar *dx;
double sum;
x.SetMode( n, 1 );
x.SetDep( x0, 1 );

z = cos(x);
for ( i = 0, dx = &z, sum = 0, prod = 1; dx != NULL; ++i, dx = dx->Getdx() ) {
sum += ((double)dx[0])*pow(nx-x0,i)/prod;
prod *= (i+1);
}
return sum;
}

This example computes the taylor expansion for orders 0-10 of cos(x) and outputs datafiles and a GNUplot script that can be executed as follows:
'gnuplot -persist < graph'

Here's the plot


Conclusions
Recursive auto differentiation is extremely inefficient and the derivative operations grow exponentially as order and dimension increase.  I think I did a test with u*v, order 7 and 2 dimensions would take 5 seconds to compute.  Statically defining each order is more efficient but becomes increasingly difficult (e.g. imagining defining order 7 product rule).  Additionally, this library computes all derivatives regardless of symmetry...it is not possible to keep track of symmetry in my implementation (e.g. I cannot compute only the lower triangle of the hessian).  So, with recursion, you gain general usage but lose efficiency...you give and take something.

You can get ADvar library here
Enjoy!

57
Unix / Linux Discussion / FreeBSD 6.2/Quarterly report
« on: January 18, 2007, 12:59:36 am »
FreeBSD 6.2 was released on Monday (1/15) ... its very nice. (EDIT, I went and checked my inbox, it was actually Monday)
So some of the updates include:
Fixes and improvements to iwi
- iwi previously had serious locking problems, probably due to FreeBSD 5.x/6.x transition to SMP from GIANT.  iwi additionally uses the new firmware(4) interface to load firmware to hardware.
new security event auditing feature (a component of TrustedBSD)
- As I understand it, this removes power from root in respects to logging and auditting and allows applications to tap into security-related events also.  This way, even root can't change logged events...the power rests with the kernel.
Again more locking problems fixed in devfs
- devfs previously had deadlocking problems
csup, a new cvsup!
- FreeBSD now has its own cvsup client and it lives in the FreeBSD base (not like cvsup which must be installed via ports/packages).
freebsd-update
- This is an application to do binary-only updates...apparently it allows one to update FreeBSD in minutes (instead of having to buildworld and kernel which can take hours).

There were a few other things...you can check out the release notes here:
http://www.freebsd.org/releases/6.2R/relnotes.html

Additionally, today I got a copy of the FreeBSD Project's quarterly report in FreeBSD-announce mailing list!
One particular article caught my eyes....ZFS is apparently functional in all respects except ACLs...I wonder if 6.3 will have ZFS as an experimental feature, I'm very eager to try it out (and ditch FFS!).

You can see a copy of the quarterly report here:
http://www.freebsd.org/news/status/report-oct-2006-dec-2006.html

58
General Programming / [C] tunsl
« on: January 02, 2007, 03:59:16 pm »
To be consistent with my other postings, I've decided to dedicate a topic of its own to the actual p2p tunneler instead of appending it the P2P VLAN post. This (tunsl) is a working prototype of a p2p tunneler.
tunsl is composed of 9 inter working components:

fdman - The file descriptor manager
timer - The timer manager, it cooperates with fdman ... fdman varies its timeout value to select() with information from timer.
tunman - The tunnel manager, it gives the application a high level interface to doing simple operations like setting the tunnel's IP address, reading from tunnel, and etc.
sudp - This does secure UDP communications over DSS/DH with Blowfish
conf - This is the configurations parser

It uses these data structures:
hash - A generalized hash table (heavily used)
stack - A generalized stack (used with conf)
list - A generlized linked list (used with timer)

And finally, the main component, the meat and the guts
tunsl - This is the route manager ... it does everything from seek out routes to DSS/DH with Blowfish over routes.

The real thing would have more components to manage the protocol and communications with the daemon. 

The purpose of this prototype is to get a feel for how to write this odd program ... its not everyday you're walking down the street and instantly want to write a p2p tunneler.  It's also meant to be used for testing purposes both stability and security-wise.  I think it could be useful to have a decentralized virtualized LAN, as long as one peer exists, the network exists.  No matter which peer you connect to, you are part of the same logical network as they are.

So, just to give you a general idea of how this works.  The tunsl component is the focal point of this program, it is the literally the engine.  It makes use of both sudp and tunman for communications.  tunsl listens on sudp for various events, but in the prototype all that is emphasized is connect/disconnect and read events.  On connect and disconnect, tunsl creates and destroys peer structures that are hung in sudpcon.arg for later use.  On read, tunsl hands the data over to a protocol handler.  The protocol handler does any forwarding necessary and also handles data addressed to us.  tunsl additionally listens on tunman for packets read from the tunnel and each packet read, it calls a function that tries to send the packet ... that is, it looks for a route if none exist, or it uses a random existing route.

So, I did some testing last night between 4 machines: LIGHTBULB, BLENDER, BOTTLE and DOORKNOB.
LIGHTBULB connected to BLENDER
BOTTLE connected to BLENDER
DOORKNOB connected to BLENDER and BOTTLE

I pinged each of these machines by their virtual IP from every other machine (and in some cases, all at the same time).  I had even ssh'd from LIGHTBULB to BOTTLE.  The routing scheme works quite well surprisingly (see Protocol Description post in P2P VLAN).
Here is a screenshot of transactions:


So, some problems I discovered:
a) This network, although it emulates an IP LAN, cannot be bridged or NAT'd to a real IP network.  The reason is, each route must be encrypted ... nodes communicating from outside of the network simply cannot do this.
b) The Blowfish cipher has a chaining effect on the data ... the original intent was to allow the OS to handle lossage, but I have to deal with this too.  Encrypted data cannot go missing and cannot arrive out of order ... but that doesn't mean tunsl isn't ill equipped to handle this scenario, like the sudp library, it will reset the cryptographic parameters to their initial state.
c) There is no checking on the packet level ... you can have a packet of random garbage addressed to a destination on P2P level (this can be fixed)

Any comments on solutions to the above are welcome :)

So here is the source, it is designed for FreeBSD 6.x branch of OS but it might work on 5.x.  Additionally, you will need OpenSSL 0.9.7 or above to use it.
http://nslay.36bit.com/tunsl.tar.gz


59
General Programming / [C] Secure UDP Library (sudp)
« on: December 22, 2006, 04:01:57 am »
The Secure UDP Library (or sudp for short) is a callback driven UDP communications library that handles all aspects of key agreement and encryption.  The purpose of this library is to provide (semi) secure UDP communications for my tunnel project where the data transfered is small pieces of information (usually forwarded packets), but it is general enough to use for other things.  Presently, it uses Diffie-Hellman with DSA signatures for key agreement and Blowfish (CFB mode) for the encryption.

Synopsis
Code: [Select]
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <openssl/dsa.h>
#include <openssl/engine.h>
#include <openssl/sha.h>
#include <openssl/blowfish.h>

#include "fdman.h"
#include "hash.h"

#ifndef SUDP_H
#define SUDP_H

#ifndef SUDP_TIMEOUT
#define SUDP_TIMEOUT 60
#endif

/* Message format */
#define SUDP_MSGSIZE 8192
#define SUDP_HDRSIZE ( sizeof(uint8_t)+sizeof(uint16_t) )
#define SUDP_DATSIZE ( SUDP_MSGSIZE-SUDP_HDRSIZE )

/* States */
#define SUDP_WAITING 0
#define SUDP_WAITING_PUBKEY 1
#define SUDP_WAITING_BIGNUM 2
#define SUDP_WAITING_SIGNATURE 3
#define SUDP_COMPLETE 4

/* Protocol */
#define SUDP_PROTO_RESET 0x00
#define SUDP_PROTO_CONNECT 0x01
#define SUDP_PROTO_DISCONNECT 0x02
#define SUDP_PROTO_PUBKEY 0x03
#define SUDP_PROTO_BIGNUM 0x04
#define SUDP_PROTO_SIGNATURE 0x05
#define SUDP_PROTO_DATA 0x06

/* Callback events */
#define SUDP_RESET 0
#define SUDP_CONNECT 1
#define SUDP_DISCONNECT 2
#define SUDP_PUBKEY 3
#define SUDP_READ 4

struct sudp;
struct sudpcon;

typedef void (*sudp_cb)( struct sudp *sd, struct sudpcon *sdc );

struct sudpcon {
struct sockaddr addr;
socklen_t addrlen;
int state, event, in_num, out_num;
DSA *dsa_pubkey;
unsigned char md[SHA_DIGEST_LENGTH], in_ivec[8], out_ivec[8];
BF_KEY key;
uint16_t in_seq, out_seq;
void *msg, *arg;
size_t msgsz;
time_t to;
};

struct sudp {
int fd;
struct fdman *fdm;
DSA *dsa_privkey;
struct hash_table ht;
sudp_cb cb;
};

int sudp_init( struct sudp *sd, struct fdman *fdm, sudp_cb cb, DSA *dsa_privkey );
void sudp_free( struct sudp *sd );
int sudp_bind( struct sudp *sd, struct sockaddr *addr, socklen_t addrlen );
int sudp_connect( struct sudp *sd, struct sockaddr *addr, socklen_t addrlen );
void sudp_disconnect( struct sudp *sd, struct sudpcon *sdc );
int sudp_send( struct sudp *sd, struct sudpcon *sdc, void *data, size_t sz );
void sudp_timeout( struct sudp *sd );

#endif

Parameters
Code: [Select]
struct sudpcon {
struct sockaddr addr; /* Remote address */
socklen_t addrlen; /* Length of addr */
int state, event, in_num, out_num; /* event indicates the reason for callback */
DSA *dsa_pubkey; /* The remote DSA public key */
unsigned char md[SHA_DIGEST_LENGTH], in_ivec[8], out_ivec[8];
BF_KEY key;
uint16_t in_seq, out_seq;
void *msg, *arg; /* These are used for callback, msg points to decrypted data, arg is arbitrary */
size_t msgsz; /* This is the size of the the decrypted text */
time_t to;
};

struct sudp {
int fd; /* This is the UDP socket descriptor */
struct fdman *fdm; /* This is the fdman (see post on fdman for more information) */
DSA *dsa_privkey; /* DSA private key */
struct hash_table ht; /* Hash table used to store sudpcon descriptors */
sudp_cb cb; /* Callback function */
};

Functions
Code: [Select]
int sudp_init( struct sudp *sd, struct fdman *fdm, sudp_cb cb, DSA *dsa_privkey ); /* This initializes a secure udp manager */
void sudp_free( struct sudp *sd ); /* This frees the data used by the secure udp manager */
int sudp_bind( struct sudp *sd, struct sockaddr *addr, socklen_t addrlen ); /* This binds the secure udp manager to a specific address and/or port */
int sudp_connect( struct sudp *sd, struct sockaddr *addr, socklen_t addrlen ); /* This "connects", or rather, attempts to establish an encrypted session with specified address */
void sudp_disconnect( struct sudp *sd, struct sudpcon *sdc ); /* This "disconnects", or rather alerts the end point of the terminated session */
int sudp_send( struct sudp *sd, struct sudpcon *sdc, void *data, size_t sz ); /* This sends encrypted data to the address described by the sudpcon descriptor */
void sudp_timeout( struct sudp *sd ); /* This cleans up idle "connections" */

Callback events
Code: [Select]
typedef void (*sudp_cb)( struct sudp *sd, struct sudpcon *sdc ); /* The callback function prototype */

/* Callback events */
#define SUDP_RESET 0 /* If any packets are lost, the encryption parameters are automatically reset to maintain the encrypted session ... it lets you know packets were lost, this callback is a convenience! */
#define SUDP_CONNECT 1 /* This callback lets you know an encrypted session was successfully established */
#define SUDP_DISCONNECT 2 /* This callback lets you know remote side terminated the session, or the session timed out */
#define SUDP_PUBKEY 3 /* This lets you know when a public key is received, it is intended to let the application check its cache to see if there is evesdropping */
#define SUDP_READ 4 /* This lets you know data was read from the socket, sdc->msg and sdc->msgsz point to the decrypted data and size respectively */

UDP is extremely unreliable!  Only use this library if you only wish to transfer small pieces of data and do so semi-securely.

Example
This is a simple peer-to-peer chat program.  It uses sudp to encrypt messages between two peers.
Code: [Select]
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/dsa.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <sys/time.h>
#include <time.h>
#include <sysexits.h>
#include "sudp.h"
#include "fdman.h"

#define PORT "3421"

struct sudpcon *chat = NULL;
char *remote_host = NULL;

struct addrinfo hints = { AI_PASSIVE, AF_INET, SOCK_DGRAM, IPPROTO_UDP };

void usage( void ) {
extern char *__progname;
printf( "%s: [-h remote host ]\n", __progname );
exit(EX_USAGE);
}

void chatcb( struct sudp *sd, struct sudpcon *sdc ) {
uint8_t msg[SUDP_DATSIZE+1];
const char *addr = inet_ntoa( ((struct sockaddr_in *)&sdc->addr)->sin_addr );
switch( sdc->event ) {
case SUDP_RESET:
printf( "Packets lost, reset!\n" );
break;
case SUDP_CONNECT:
if ( chat != NULL ) {
printf( "%s connected but we're already connected!\n", addr );
sudp_disconnect( sd, sdc );
break;
}
chat = sdc;
printf( "Connected to %s.\n", addr );
break;
case SUDP_DISCONNECT:
if ( sdc == chat ) chat = NULL;
printf( "Lost connection with %s!\n", addr );
break;
case SUDP_PUBKEY:
printf( "Recieved public key from %s!\n", addr );
break;
case SUDP_READ:
memcpy( msg, sdc->msg, sdc->msgsz );
msg[sdc->msgsz] = '\0';
printf( "%s: %s", addr, msg );
break;
}
}

void kbdcb( struct fdarg *fda ) {
struct sudp *sd = (struct sudp *)(fda->arg);
uint8_t msg[SUDP_DATSIZE];
ssize_t msgsz;

msgsz = read( fda->fd, msg, sizeof(msg) );
if ( chat == NULL ) return;
if ( sudp_send( sd, chat, msg, msgsz ) ) {
printf( "Send error!\n" );
sudp_disconnect( sd, chat );
chat = NULL;
}
}

int main( int argc, char **argv ) {
char c;
DSA *dsa;
struct fdman fdm;
struct fdarg fda;
struct sudp sd;
struct addrinfo *res;
struct timeval to = {0,0}, tv;
int arg;

while ( (c = getopt( argc, argv, "h:" )) != -1 ) {
switch( c ) {
case 'h':
remote_host = strdup(optarg);
hints.ai_flags = AI_CANONNAME;
break;
default:
usage();
}
}

/* Seed the random generator */
srandom( time(NULL) );

/* Generate private DSA key */
dsa = DSA_generate_parameters( 1024, NULL, 0, NULL, NULL, NULL, NULL );
if ( dsa == NULL ) {
printf( "%s\n", ERR_error_string(ERR_get_error(),NULL) );
exit(EX_SOFTWARE);
}
if ( !DSA_generate_key(dsa) ) {
printf( "%s\n", ERR_error_string(ERR_get_error(),NULL) );
exit(EX_SOFTWARE);
}

/* Initiaize fd manager */
fdman_init( &fdm );
if ( sudp_init( &sd, &fdm, chatcb, dsa ) ) {
printf( "Unable to initialize secure UDP!\n" );
exit(EX_SOFTWARE);
}

/* Setup keyboard callback */
fda.fd = STDIN_FILENO;
fda.opt = FDMAN_READ;
fda.arg = &sd;
fda.cb = kbdcb;

fdman_set( &fdm, &fda );

/* Resolve address */
if ( arg = getaddrinfo( remote_host, PORT, &hints, &res ) ) {
printf( "%s\n", gai_strerror(arg) );
exit(EX_OSERR);
}

if ( remote_host == NULL ) {
printf( "Listening on port %s\n", PORT );
if ( sudp_bind( &sd, res->ai_addr, res->ai_addrlen ) ) {
perror( "sudp_bind()" );
exit(EX_OSERR);
}
}
else {
printf( "Attempting to connect to %s\n", remote_host );
if ( sudp_connect( &sd, res->ai_addr, res->ai_addrlen ) ) {
perror( "sudp_connect()" );
exit(EX_OSERR);
}
}

gettimeofday( &tv, NULL );
tv.tv_sec += SUDP_TIMEOUT;
while ( fdman_select( &fdm, &to ) >= 0 ) {
gettimeofday( &to, NULL );
if ( timercmp( &to, &tv, >= ) ) {
/* Kill idle "connections" */
sudp_timeout( &sd );
tv = to;
tv.tv_sec += SUDP_TIMEOUT;
}
timersub( &tv, &to, &to );
}

printf( "Exiting ...\n" );

return 0;
}

Here's a screenshot of the example program in action:


The source: http://nslay.36bit.com/sudp.tar.gz

60
Unix / Linux Discussion / Inspiring Essays
« on: December 11, 2006, 05:52:35 pm »
Aside of the UNIX-Haters handbook.  You might find these essays on Linux inspiring.  It points out everything that is wrong with the Linux community.  Much of what is written in these essays are among reasons I despise Linux ... although I do try to recognize Linux's good points.

These essays are very well written and they appear objective.  They are not your ordinary anti-Linux FUD ... Linux zealots should pay close attention to these essays.  These essays don't necessarily bash Linux or Linux distributions ... they bash the community.

1) Is Windows "Dumbed Down"?
2) Linux is a teenage boy's operating system
3) Are the "Open Source" advocates mentally healthy
4) Open Source hurts the poor nations
5) Linux depends on the disgust of Microsoft

I've only had time to read the first essay, but it really nails the point.  Although, I don't agree with his view on open source applications ... many open source applications are written for fun, or to serve a purpose on a platform that is unsupported by alternatives.

Pages: 1 2 3 [4] 5