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-2011 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 |
… | |
… | |
84 | { |
84 | { |
85 | memset (v, -1, sizeof v); |
85 | memset (v, -1, sizeof v); |
86 | seq = seqno; |
86 | seq = seqno; |
87 | } |
87 | } |
88 | |
88 | |
89 | bool recv_ok (u32 seqno) |
89 | // 0 == ok, 1 == far history, 2 == duplicate in-window, 3 == far future |
|
|
90 | int seqno_classify (u32 seqno) |
90 | { |
91 | { |
91 | if (seqno <= seq - WINDOWSIZE) |
92 | if (seqno <= seq - WINDOWSIZE) |
92 | slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" |
93 | return 1; |
93 | "possible replay attack, or just massive packet reordering"), seqno, seq + 1); |
|
|
94 | else if (seqno > seq + WINDOWSIZE * 4) |
94 | else if (seqno > seq + WINDOWSIZE * 16) |
95 | slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
95 | return 3; |
96 | "possible replay attack, or just massive packet loss"), seqno, seq + 1); |
|
|
97 | else |
96 | else |
98 | { |
97 | { |
99 | while (seqno > seq) |
98 | while (seqno > seq) |
100 | { |
99 | { |
101 | seq++; |
100 | seq++; |
… | |
… | |
110 | u32 s = seqno % WINDOWSIZE; |
109 | u32 s = seqno % WINDOWSIZE; |
111 | u32 *cell = v + (s >> 5); |
110 | u32 *cell = v + (s >> 5); |
112 | u32 mask = 1 << (s & 31); |
111 | u32 mask = 1 << (s & 31); |
113 | |
112 | |
114 | if (*cell & mask) |
113 | if (*cell & mask) |
115 | slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
114 | return 2; |
116 | "possible replay attack, or just packet duplication"), seqno, seq + 1); |
|
|
117 | else |
115 | else |
118 | { |
116 | { |
119 | *cell |= mask; |
117 | *cell |= mask; |
120 | return true; |
118 | return 0; |
121 | } |
119 | } |
122 | } |
120 | } |
123 | |
|
|
124 | return false; |
|
|
125 | } |
121 | } |
126 | }; |
122 | }; |
127 | |
123 | |
128 | typedef callback<const char * ()> run_script_cb; |
124 | typedef callback<const char *()> run_script_cb; |
129 | |
125 | |
130 | // run a shell script (or actually an external program). |
126 | // run a shell script (or actually an external program). |
131 | pid_t run_script (const run_script_cb &cb, bool wait); |
127 | pid_t run_script (const run_script_cb &cb, bool wait); |
132 | |
128 | |
133 | #if ENABLE_HTTP_PROXY |
129 | #if ENABLE_HTTP_PROXY |
… | |
… | |
154 | return RSA_private_decrypt (sizeof encr, |
150 | return RSA_private_decrypt (sizeof encr, |
155 | (unsigned char *)&encr, (unsigned char *)&chg, |
151 | (unsigned char *)&encr, (unsigned char *)&chg, |
156 | key, RSA_PKCS1_OAEP_PADDING) > 0; |
152 | key, RSA_PKCS1_OAEP_PADDING) > 0; |
157 | } |
153 | } |
158 | |
154 | |
|
|
155 | /*****************************************************************************/ |
|
|
156 | |
|
|
157 | // run work_cb in another thread, call done_cb in main thread when finished |
|
|
158 | // only one work_cb will execute at any one time. |
|
|
159 | void async (callback<void ()> work_cb, callback<void ()> done_cb); |
|
|
160 | |
159 | #endif |
161 | #endif |
160 | |
162 | |