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