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-2008 Marc Lehmann <gvpe@schmorp.de> |
5 | 2003-2013 Marc Lehmann <gvpe@schmorp.de> |
6 | |
6 | |
7 | This file is part of GVPE. |
7 | This file is part of GVPE. |
8 | |
8 | |
9 | GVPE is free software; you can redistribute it and/or modify it |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU General Public License as published by the |
10 | under the terms of the GNU General Public License as published by the |
… | |
… | |
33 | |
33 | |
34 | #ifndef UTIL_H__ |
34 | #ifndef UTIL_H__ |
35 | #define UTIL_H__ |
35 | #define UTIL_H__ |
36 | |
36 | |
37 | #include <cstring> |
37 | #include <cstring> |
|
|
38 | #include <sys/types.h> |
38 | |
39 | |
39 | #include <openssl/rsa.h> |
40 | #include <openssl/rsa.h> |
40 | |
41 | |
41 | #include "gettext.h" |
42 | #include "gettext.h" |
42 | |
43 | |
43 | #include "slog.h" |
44 | #include "slog.h" |
44 | #include "ev_cpp.h" |
45 | #include "ev_cpp.h" |
45 | #include "callback.h" |
46 | #include "callback.h" |
|
|
47 | #include "global.h" |
46 | |
48 | |
47 | typedef ev_tstamp tstamp; |
49 | typedef ev_tstamp tstamp; |
48 | |
50 | |
49 | /* |
51 | /* |
50 | * check for an existing gvpe for this net, and write pid to pidfile |
52 | * check for an existing gvpe for this net, and write pid to pidfile |
… | |
… | |
83 | { |
85 | { |
84 | memset (v, -1, sizeof v); |
86 | memset (v, -1, sizeof v); |
85 | seq = seqno; |
87 | seq = seqno; |
86 | } |
88 | } |
87 | |
89 | |
88 | bool recv_ok (u32 seqno) |
90 | // 0 == ok, 1 == far history, 2 == duplicate in-window, 3 == far future |
|
|
91 | int seqno_classify (u32 seqno) |
89 | { |
92 | { |
90 | if (seqno <= seq - WINDOWSIZE) |
93 | if (seqno <= seq - WINDOWSIZE) |
91 | slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" |
94 | return 1; |
92 | "possible replay attack, or just massive packet reordering"), seqno, seq + 1); |
|
|
93 | else if (seqno > seq + WINDOWSIZE * 4) |
95 | else if (seqno > seq + WINDOWSIZE * 16) |
94 | slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
96 | return 3; |
95 | "possible replay attack, or just massive packet loss"), seqno, seq + 1); |
|
|
96 | else |
97 | else |
97 | { |
98 | { |
98 | while (seqno > seq) |
99 | while (seqno > seq) |
99 | { |
100 | { |
100 | seq++; |
101 | seq++; |
… | |
… | |
109 | u32 s = seqno % WINDOWSIZE; |
110 | u32 s = seqno % WINDOWSIZE; |
110 | u32 *cell = v + (s >> 5); |
111 | u32 *cell = v + (s >> 5); |
111 | u32 mask = 1 << (s & 31); |
112 | u32 mask = 1 << (s & 31); |
112 | |
113 | |
113 | if (*cell & mask) |
114 | if (*cell & mask) |
114 | slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
115 | return 2; |
115 | "possible replay attack, or just packet duplication"), seqno, seq + 1); |
|
|
116 | else |
116 | else |
117 | { |
117 | { |
118 | *cell |= mask; |
118 | *cell |= mask; |
119 | return true; |
119 | return 0; |
120 | } |
120 | } |
121 | } |
121 | } |
122 | |
|
|
123 | return false; |
|
|
124 | } |
122 | } |
125 | }; |
123 | }; |
126 | |
124 | |
127 | typedef callback<const char * ()> run_script_cb; |
125 | typedef callback<const char *()> run_script_cb; |
128 | |
126 | |
129 | // run a shell script (or actually an external program). |
127 | // run a shell script (or actually an external program). |
130 | bool run_script (const run_script_cb &cb, bool wait); |
128 | pid_t run_script (const run_script_cb &cb, bool wait); |
|
|
129 | |
|
|
130 | void hexdump (const char *header, void *data, int len); |
131 | |
131 | |
132 | #if ENABLE_HTTP_PROXY |
132 | #if ENABLE_HTTP_PROXY |
133 | u8 *base64_encode (const u8 *data, unsigned int len); |
133 | u8 *base64_encode (const u8 *data, unsigned int len); |
134 | #endif |
134 | #endif |
135 | |
135 | |
136 | /*****************************************************************************/ |
136 | /*****************************************************************************/ |
137 | |
137 | |
138 | typedef u8 rsaclear[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data; |
138 | // run work_cb in another thread, call done_cb in main thread when finished |
139 | typedef u8 rsacrypt[RSA_KEYLEN]; // encrypted challenge |
139 | // only one work_cb will execute at any one time. |
140 | |
140 | void async (callback<void ()> work_cb, callback<void ()> done_cb); |
141 | static inline void |
|
|
142 | rsa_encrypt (RSA *key, const rsaclear &chg, rsacrypt &encr) |
|
|
143 | { |
|
|
144 | if (RSA_public_encrypt (sizeof chg, |
|
|
145 | (unsigned char *)&chg, (unsigned char *)&encr, |
|
|
146 | key, RSA_PKCS1_OAEP_PADDING) < 0) |
|
|
147 | fatal ("RSA_public_encrypt error"); |
|
|
148 | } |
|
|
149 | |
|
|
150 | static inline bool |
|
|
151 | rsa_decrypt (RSA *key, const rsacrypt &encr, rsaclear &chg) |
|
|
152 | { |
|
|
153 | return RSA_private_decrypt (sizeof encr, |
|
|
154 | (unsigned char *)&encr, (unsigned char *)&chg, |
|
|
155 | key, RSA_PKCS1_OAEP_PADDING) > 0; |
|
|
156 | } |
|
|
157 | |
141 | |
158 | #endif |
142 | #endif |
159 | |
143 | |