--- gvpe/src/util.C 2003/03/01 15:53:03 1.1 +++ gvpe/src/util.C 2003/10/14 15:48:15 1.8 @@ -1,7 +1,7 @@ /* util.C -- process management and other utility functions - Most of these are taken from tinc, see the AUTHORS file. + Some of these are taken from tinc, see the AUTHORS file. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,18 +26,13 @@ #include #include #include +#include #include #include -#include -#include -#include #include -#include -#include -#include -#include +#include "netcompat.h" #include "gettext.h" #include "pidfile.h" @@ -45,10 +40,8 @@ #include "global.h" #include "conf.h" +#include "util.h" #include "slog.h" -#include "protocol.h" - -time_t now; int write_pidfile (void) @@ -144,75 +137,72 @@ asprintf (&confbase, "%s/vpe", CONFDIR); } -void pkt_queue::put (tap_packet *p) +void run_script (const run_script_cb &cb, bool wait) { - if (queue[i]) + int pid; + + if ((pid = fork ()) == 0) { - delete queue[i]; - j = (j + 1) % QUEUEDEPTH; + char *filename; + asprintf (&filename, "%s/%s", confbase, cb()); + execl (filename, filename, (char *) 0); + exit (255); } - - queue[i] = p; - - i = (i + 1) % QUEUEDEPTH; -} - -tap_packet *pkt_queue::get () -{ - tap_packet *p = queue[j]; - - if (p) + else if (pid > 0) { - queue[j] = 0; - j = (j + 1) % QUEUEDEPTH; + if (wait) + { + waitpid (pid, 0, 0); + /* TODO: check status */ + } } - - return p; -} - -pkt_queue::pkt_queue () -{ - memset (queue, 0, sizeof (queue)); - i = 0; - j = 0; } -pkt_queue::~pkt_queue () +#if ENABLE_HTTP_PROXY +// works like strdup +u8 * +base64_encode (const u8 *data, unsigned int len) { - for (i = QUEUEDEPTH; --i > 0; ) - delete queue[i]; -} + const static char base64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -sockinfo::operator const char *() -{ - static char hostport[15 + 1 + 5 + 1]; - in_addr ia = { host }; - - sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); - - return hostport; -} + unsigned int t, i; + const u8 *end = data + len; + u8 *res = new u8 [4 * ((len + 2) / 3) + 1]; + u8 *out = res; -bool u32_rate_limiter::can (u32 host) -{ - iterator i; - - for (i = begin (); i != end (); ) - if (i->second <= now) - { - erase (i); - i = begin (); - } - else - ++i; + 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]; + } - i = find (host); + for (t = 0, i = 0; data < end; i++) + t = (t << 8) | *data++; - if (i != end ()) - return false; + 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; + } - insert (value_type (host, now + every)); + *out++ = 0; - return true; + return res; } +#endif