--- gvpe/src/util.h 2003/03/01 15:53:03 1.1 +++ gvpe/src/util.h 2005/03/23 21:55:39 1.17 @@ -2,9 +2,11 @@ util.h -- process management and other utility functions Copyright (C) 1998-2002 Ivo Timmermans 2000-2002 Guus Sliepen - 2003 Marc Lehmannn + 2003 Marc Lehmann - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -15,29 +17,27 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef UTIL_H__ #define UTIL_H__ -#include -#include - -#include +#include -#include "device.h" +#include "gettext.h" -#define SOCKADDR sockaddr_in // this is lame, I know +#include "slog.h" +#include "iom.h" /* - * check for an existing vped for this net, and write pid to pidfile + * check for an existing gvpe for this net, and write pid to pidfile */ extern int write_pidfile (void); /* - * kill older vped + * kill older gvpe */ extern int kill_other (int signal); @@ -47,120 +47,98 @@ extern int detach (int do_detach); /* - * Set all files and paths according to netname - */ -extern void make_names (void); - -/* * check wether the given path is an absolute pathname */ #define ABSOLUTE_PATH(c) ((c)[0] == '/') -static inline void -id2mac (unsigned int id, void *m) -{ - mac &p = *(mac *)m; - - p[0] = 0xfe; - p[1] = 0xfd; - p[2] = 0x80; - p[3] = 0x00; - p[4] = id >> 8; - p[5] = id; -} - -#define mac2id(p) (p[0] & 0x01 ? 0 : (p[4] << 8) | p[5]) - -// a very simple fifo pkt-queue -class pkt_queue - { - tap_packet *queue[QUEUEDEPTH]; - int i, j; +/*****************************************************************************/ - public: +typedef u8 mac[6]; - void put (tap_packet *p); - tap_packet *get (); +extern void id2mac (unsigned int id, void *m); - pkt_queue (); - ~pkt_queue (); - }; +#define mac2id(p) ((p)[0] & 0x01 ? 0 : ((p)[4] << 8) | (p)[5]) -struct sockinfo - { - u32 host; - u16 port; +struct sliding_window { + u32 v[(WINDOWSIZE + 31) / 32]; + u32 seq; - void set (const SOCKADDR *sa) + void reset (u32 seqno) { - host = sa->sin_addr.s_addr; - port = sa->sin_port; + memset (v, -1, sizeof v); + seq = seqno; } - sockinfo() + bool recv_ok (u32 seqno) { - host = port = 0; + if (seqno <= seq - WINDOWSIZE) + slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" + "possible replay attack, or just massive packet reordering"), seqno, seq + 1);//D + else if (seqno > seq + WINDOWSIZE) + slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" + "possible replay attack, or just massive packet loss"), seqno, seq + 1);//D + else + { + while (seqno > seq) + { + seq++; + + u32 s = seq % WINDOWSIZE; + u32 *cell = v + (s >> 5); + u32 mask = 1 << (s & 31); + + *cell &= ~mask; + } + + u32 s = seqno % WINDOWSIZE; + u32 *cell = v + (s >> 5); + u32 mask = 1 << (s & 31); + + if (*cell & mask) + { + slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" + "possible replay attack, or just packet duplication"), seqno, seq + 1);//D + return false; + } + else + { + *cell |= mask; + return true; + } + } } +}; - sockinfo(const SOCKADDR &sa) - { - set (&sa); - } - - sockinfo(const SOCKADDR *sa) - { - set (sa); - } +typedef callback0 run_script_cb; - SOCKADDR *sa() - { - static SOCKADDR sa; +// run a shell script (or actually an external program). +bool run_script (const run_script_cb &cb, bool wait); - sa.sin_family = AF_INET; - sa.sin_port = port; - sa.sin_addr.s_addr = host; +#if ENABLE_HTTP_PROXY +u8 *base64_encode (const u8 *data, unsigned int len); +#endif - return &sa; - } +/*****************************************************************************/ - operator const char *(); - }; +typedef u8 rsaclear[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data; +typedef u8 rsacrypt[RSA_KEYLEN]; // encrypted challenge -inline bool -operator == (const sockinfo &a, const sockinfo &b) +static inline void +rsa_encrypt (RSA *key, const rsaclear &chg, rsacrypt &encr) { - return a.host == b.host && a.port == b.port; + if (RSA_public_encrypt (sizeof chg, + (unsigned char *)&chg, (unsigned char *)&encr, + key, RSA_PKCS1_OAEP_PADDING) < 0) + fatal ("RSA_public_encrypt error"); } -inline bool -operator < (const sockinfo &a, const sockinfo &b) +static inline bool +rsa_decrypt (RSA *key, const rsacrypt &encr, rsaclear &chg) { - return a.host < b.host - || (a.host == b.host && a.port < b.port); + return RSA_private_decrypt (sizeof encr, + (unsigned char *)&encr, (unsigned char *)&chg, + key, RSA_PKCS1_OAEP_PADDING) > 0; } -// only do action once every x seconds per host. -// currently this is quite a slow implementation, -// but suffices for normal operation. -struct u32_rate_limiter : private map - { - int every; - - bool can (u32 host); - - u32_rate_limiter (time_t every = 1) - { - this->every = every; - } - }; - -struct net_rate_limiter : u32_rate_limiter - { - bool can (SOCKADDR *sa) { return u32_rate_limiter::can((u32)sa->sin_addr.s_addr); } - bool can (sockinfo &si) { return u32_rate_limiter::can((u32)si.host); } - - net_rate_limiter (time_t every) : u32_rate_limiter (every) {} - }; - #endif