--- gvpe/src/util.C 2008/08/07 19:07:03 1.23 +++ gvpe/src/util.C 2014/01/09 08:15:05 1.28 @@ -1,6 +1,6 @@ /* util.C -- process management and other utility functions - Copyright (C) 2003-2008 Marc Lehmann + Copyright (C) 2003-2011 Marc Lehmann Some of these are taken from tinc, see the AUTHORS file. @@ -37,6 +37,8 @@ #include #include +#include + #include #include #include @@ -44,6 +46,12 @@ #include #include +#if ENABLE_PTHREADS +# include +#endif + +#include + #include "netcompat.h" #include "gettext.h" @@ -134,11 +142,13 @@ else log_to (LOGTO_SYSLOG | LOGTO_STDERR); - slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); + slog (L_INFO, _("gvpe daemon %s (%s %s) starting up."), VERSION, __DATE__, __TIME__); return 0; } +/*****************************************************************************/ + pid_t run_script (const run_script_cb &cb, bool wait) { @@ -193,6 +203,113 @@ return pid; } +/*****************************************************************************/ + +#if 0 /* not yet used */ + +#if ENABLE_PTHREADS +struct async_cb +{ + callback work_cb; + callback done_cb; +}; + +static ev::async async_done_w; +static std::queue< callback > async_q; + +static callback work_cb; + +static void * +async_exec (void *) +{ + work_cb (); + async_done_w.send (); + + return 0; +} + +static void +async_q_next () +{ + work_cb = async_q.front (); async_q.pop (); + + sigset_t fullsigset, oldsigset; + pthread_attr_t attr; + pthread_t tid; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + //pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); + sigfillset (&fullsigset); + pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); + + if (pthread_create (&tid, &attr, async_exec, 0)) + async_exec (0); + + pthread_sigmask (SIG_SETMASK, &oldsigset, 0); + pthread_attr_destroy (&attr); +} + +namespace { + void + async_done (ev::async &w, int revents) + { + callback done_cb = async_q.front (); async_q.pop (); + + if (async_q.empty ()) + async_done_w.stop (); + else + async_q_next (); + + done_cb (); + } +}; + +void +async (callback work_cb, callback done_cb) +{ + bool was_empty = async_q.empty (); + + async_q.push (work_cb); + async_q.push (done_cb); + + if (was_empty) + { + async_done_w.set (); + async_done_w.start (); + async_q_next (); + } +} + +#else + +void +async (callback work_cb, callback done_cb) +{ + work_cb (); + done_cb (); +} + +#endif + +#endif + +/*****************************************************************************/ + +void hexdump (const char *header, void *data, int len) +{ + u8 *p = (u8 *)data; + + printf ("%s:", header); + + while (len--) + printf (" %02x", *p++); + + printf ("\n"); +} + +/*****************************************************************************/ + #if ENABLE_HTTP_PROXY // works like strdup u8 * @@ -241,6 +358,19 @@ } #endif +bool +slow_memeq (const void *a, const void *b, int len) +{ + volatile const u8 *pa = (const u8 *)a; + volatile const u8 *pb = (const u8 *)b; + u8 diff = 0; + + while (len--) + diff |= *pa++ ^ *pb++; + + return !diff; +} + void id2mac (unsigned int id, void *m) { @@ -266,3 +396,17 @@ } } +/*****************************************************************************/ + +void rand_fill (void *data, int len) +{ + int l = RAND_bytes ((unsigned char *)data, len); + + if (l > 0) + return; + else if (l == 0) + slog (L_WARN, _("Not enough random entropy to generate secure keys. Using weaker pseudo-random session keys.")); + else + fatal (_("RAND_bytes failed, aborting.")); +} +