1 |
/* |
2 |
util.h -- process management and other utility functions |
3 |
Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 |
2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 |
2003 Marc Lehmann <pcg@goof.com> |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
#ifndef UTIL_H__ |
23 |
#define UTIL_H__ |
24 |
|
25 |
#include <openssl/rsa.h> |
26 |
|
27 |
#include "gettext.h" |
28 |
|
29 |
#include "slog.h" |
30 |
#include "iom.h" |
31 |
|
32 |
/* |
33 |
* check for an existing vped for this net, and write pid to pidfile |
34 |
*/ |
35 |
extern int write_pidfile (void); |
36 |
|
37 |
/* |
38 |
* kill older vped |
39 |
*/ |
40 |
extern int kill_other (int signal); |
41 |
|
42 |
/* |
43 |
* Detach from current terminal, write pidfile, kill parent |
44 |
*/ |
45 |
extern int detach (int do_detach); |
46 |
|
47 |
/* |
48 |
* check wether the given path is an absolute pathname |
49 |
*/ |
50 |
#define ABSOLUTE_PATH(c) ((c)[0] == '/') |
51 |
|
52 |
/*****************************************************************************/ |
53 |
|
54 |
typedef u8 mac[6]; |
55 |
|
56 |
extern void id2mac (unsigned int id, void *m); |
57 |
|
58 |
#define mac2id(p) ((p)[0] & 0x01 ? 0 : ((p)[4] << 8) | (p)[5]) |
59 |
|
60 |
struct sliding_window { |
61 |
u32 v[(WINDOWSIZE + 31) / 32]; |
62 |
u32 seq; |
63 |
|
64 |
void reset (u32 seqno) |
65 |
{ |
66 |
memset (v, -1, sizeof v); |
67 |
seq = seqno; |
68 |
} |
69 |
|
70 |
bool recv_ok (u32 seqno) |
71 |
{ |
72 |
if (seqno <= seq - WINDOWSIZE) |
73 |
slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" |
74 |
"possible replay attack, or just massive packet reordering"), seqno, seq + 1);//D |
75 |
else if (seqno > seq + WINDOWSIZE) |
76 |
slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
77 |
"possible replay attack, or just massive packet loss"), seqno, seq + 1);//D |
78 |
else |
79 |
{ |
80 |
while (seqno > seq) |
81 |
{ |
82 |
seq++; |
83 |
|
84 |
u32 s = seq % WINDOWSIZE; |
85 |
u32 *cell = v + (s >> 5); |
86 |
u32 mask = 1 << (s & 31); |
87 |
|
88 |
*cell &= ~mask; |
89 |
} |
90 |
|
91 |
u32 s = seqno % WINDOWSIZE; |
92 |
u32 *cell = v + (s >> 5); |
93 |
u32 mask = 1 << (s & 31); |
94 |
|
95 |
if (*cell & mask) |
96 |
{ |
97 |
slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
98 |
"possible replay attack, or just packet duplication"), seqno, seq + 1);//D |
99 |
return false; |
100 |
} |
101 |
else |
102 |
{ |
103 |
*cell |= mask; |
104 |
return true; |
105 |
} |
106 |
} |
107 |
} |
108 |
}; |
109 |
|
110 |
typedef callback0<const char *> run_script_cb; |
111 |
|
112 |
// run a shell script (or actually an external program). |
113 |
void run_script (const run_script_cb &cb, bool wait); |
114 |
|
115 |
#if ENABLE_HTTP_PROXY |
116 |
u8 *base64_encode (const u8 *data, unsigned int len); |
117 |
#endif |
118 |
|
119 |
/*****************************************************************************/ |
120 |
|
121 |
typedef u8 rsaclear[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data; |
122 |
typedef u8 rsacrypt[RSA_KEYLEN]; // encrypted challenge |
123 |
|
124 |
static inline void |
125 |
rsa_encrypt (RSA *key, const rsaclear &chg, rsacrypt &encr) |
126 |
{ |
127 |
if (RSA_public_encrypt (sizeof chg, |
128 |
(unsigned char *)&chg, (unsigned char *)&encr, |
129 |
key, RSA_PKCS1_OAEP_PADDING) < 0) |
130 |
fatal ("RSA_public_encrypt error"); |
131 |
} |
132 |
|
133 |
static inline bool |
134 |
rsa_decrypt (RSA *key, const rsacrypt &encr, rsaclear &chg) |
135 |
{ |
136 |
return RSA_private_decrypt (sizeof encr, |
137 |
(unsigned char *)&encr, (unsigned char *)&chg, |
138 |
key, RSA_PKCS1_OAEP_PADDING) > 0; |
139 |
} |
140 |
|
141 |
#endif |
142 |
|