--- gvpe/src/util.C 2003/04/02 03:06:22 1.4 +++ gvpe/src/util.C 2004/04/02 14:42:45 1.13 @@ -1,5 +1,6 @@ /* util.C -- process management and other utility functions + Copyright (C) 2003 Marc Lehmann Some of these are taken from tinc, see the AUTHORS file. @@ -21,6 +22,7 @@ #include "config.h" #include +#include #include #include @@ -29,12 +31,8 @@ #include #include #include -#include -#include -#include - -#include +#include "netcompat.h" #include "gettext.h" #include "pidfile.h" @@ -42,15 +40,15 @@ #include "global.h" #include "conf.h" +#include "util.h" #include "slog.h" -#include "protocol.h" int write_pidfile (void) { int pid; - pid = check_pid (pidfilename); + pid = check_pid (conf.pidfilename); if (pid) { @@ -59,7 +57,7 @@ } /* if it's locked, write-protected, or whatever */ - if (!write_pid (pidfilename)) + if (!write_pid (conf.pidfilename)) return 1; return 0; @@ -70,7 +68,7 @@ { int pid; - pid = read_pid (pidfilename); + pid = read_pid (conf.pidfilename); if (!pid) { @@ -86,7 +84,7 @@ fprintf (stderr, _("The vped is no longer running. ")); fprintf (stderr, _("Removing stale lock file.\n")); - remove_pid (pidfilename); + remove_pid (conf.pidfilename); } return 0; @@ -116,7 +114,7 @@ /* Now UPDATE the pid in the pidfile, because we changed it... */ - if (!write_pid (pidfilename)) + if (!write_pid (conf.pidfilename)) return -1; log_to (LOGTO_SYSLOG); @@ -132,8 +130,8 @@ void make_names (void) { - if (!pidfilename) - pidfilename = LOCALSTATEDIR "/run/vped.pid"; + if (!conf.pidfilename) + conf.pidfilename = strdup (LOCALSTATEDIR "/run/vped.pid"); if (!confbase) asprintf (&confbase, "%s/vpe", CONFDIR); @@ -146,9 +144,9 @@ if ((pid = fork ()) == 0) { char *filename; - asprintf (&filename, "%s/%s", confbase, cb(0)); + asprintf (&filename, "%s/%s", confbase, cb()); execl (filename, filename, (char *) 0); - exit (255); + exit (126); } else if (pid > 0) { @@ -159,3 +157,77 @@ } } } + +#if ENABLE_HTTP_PROXY +// works like strdup +u8 * +base64_encode (const u8 *data, unsigned int len) +{ + const static char base64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + unsigned int t, i; + const u8 *end = data + len; + u8 *res = new u8 [4 * ((len + 2) / 3) + 1]; + u8 *out = res; + + while (data <= end - 3) + { + t = (((data[0] << 8) | data[1]) << 8) | data[2]; + data += 3; + + *out++ = base64[(t >> 18) & 0x3f]; + *out++ = base64[(t >> 12) & 0x3f]; + *out++ = base64[(t >> 6) & 0x3f]; + *out++ = base64[(t ) & 0x3f]; + } + + for (t = 0, i = 0; data < end; i++) + t = (t << 8) | *data++; + + switch (i) + { + case 2: + *out++ = base64[(t >> 10) & 0x3f]; + *out++ = base64[(t >> 4) & 0x3f]; + *out++ = base64[(t << 2) & 0x3f]; + *out++ = '='; + break; + case 1: + *out++ = base64[(t >> 2) & 0x3f]; + *out++ = base64[(t << 4) & 0x3f]; + *out++ = '='; + *out++ = '='; + break; + } + + *out++ = 0; + + return res; +} +#endif + +void +id2mac (unsigned int id, void *m) +{ + mac &p = *(mac *)m; + + if (id) + { + p[0] = 0xfe; + p[1] = 0xfd; + p[2] = 0x80; + p[3] = 0x00; + p[4] = id >> 8; + p[5] = id; + } + else + { + p[0] = 0xff; + p[1] = 0xff; + p[2] = 0xff; + p[3] = 0xff; + p[4] = 0xff; + p[5] = 0xff; + } +} +