1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | Some of these are taken from tinc, see the AUTHORS file. |
5 | Some of these are taken from tinc, see the AUTHORS file. |
5 | |
6 | |
6 | This program is free software; you can redistribute it and/or modify |
7 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
… | |
… | |
27 | #include <signal.h> |
28 | #include <signal.h> |
28 | #include <sys/types.h> |
29 | #include <sys/types.h> |
29 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
30 | #include <unistd.h> |
31 | #include <unistd.h> |
31 | #include <time.h> |
32 | #include <time.h> |
32 | #include <sys/socket.h> |
|
|
33 | #include <netinet/in.h> |
|
|
34 | #include <arpa/inet.h> |
|
|
35 | |
33 | |
36 | |
34 | #include "netcompat.h" |
37 | #include <sys/mman.h> |
|
|
38 | |
35 | |
39 | #include "gettext.h" |
36 | #include "gettext.h" |
40 | #include "pidfile.h" |
37 | #include "pidfile.h" |
41 | #include "dropin.h" |
38 | #include "dropin.h" |
42 | |
39 | |
… | |
… | |
144 | int pid; |
141 | int pid; |
145 | |
142 | |
146 | if ((pid = fork ()) == 0) |
143 | if ((pid = fork ()) == 0) |
147 | { |
144 | { |
148 | char *filename; |
145 | char *filename; |
149 | asprintf (&filename, "%s/%s", confbase, cb(0)); |
146 | asprintf (&filename, "%s/%s", confbase, cb()); |
150 | execl (filename, filename, (char *) 0); |
147 | execl (filename, filename, (char *) 0); |
151 | exit (255); |
148 | exit (255); |
152 | } |
149 | } |
153 | else if (pid > 0) |
150 | else if (pid > 0) |
154 | { |
151 | { |
… | |
… | |
157 | waitpid (pid, 0, 0); |
154 | waitpid (pid, 0, 0); |
158 | /* TODO: check status */ |
155 | /* TODO: check status */ |
159 | } |
156 | } |
160 | } |
157 | } |
161 | } |
158 | } |
|
|
159 | |
|
|
160 | #if ENABLE_HTTP_PROXY |
|
|
161 | // works like strdup |
|
|
162 | u8 * |
|
|
163 | base64_encode (const u8 *data, unsigned int len) |
|
|
164 | { |
|
|
165 | const static char base64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
|
166 | |
|
|
167 | unsigned int t, i; |
|
|
168 | const u8 *end = data + len; |
|
|
169 | u8 *res = new u8 [4 * ((len + 2) / 3) + 1]; |
|
|
170 | u8 *out = res; |
|
|
171 | |
|
|
172 | while (data <= end - 3) |
|
|
173 | { |
|
|
174 | t = (((data[0] << 8) | data[1]) << 8) | data[2]; |
|
|
175 | data += 3; |
|
|
176 | |
|
|
177 | *out++ = base64[(t >> 18) & 0x3f]; |
|
|
178 | *out++ = base64[(t >> 12) & 0x3f]; |
|
|
179 | *out++ = base64[(t >> 6) & 0x3f]; |
|
|
180 | *out++ = base64[(t ) & 0x3f]; |
|
|
181 | } |
|
|
182 | |
|
|
183 | for (t = 0, i = 0; data < end; i++) |
|
|
184 | t = (t << 8) | *data++; |
|
|
185 | |
|
|
186 | switch (i) |
|
|
187 | { |
|
|
188 | case 2: |
|
|
189 | *out++ = base64[(t >> 10) & 0x3f]; |
|
|
190 | *out++ = base64[(t >> 4) & 0x3f]; |
|
|
191 | *out++ = base64[(t << 2) & 0x3f]; |
|
|
192 | *out++ = '='; |
|
|
193 | break; |
|
|
194 | case 1: |
|
|
195 | *out++ = base64[(t >> 2) & 0x3f]; |
|
|
196 | *out++ = base64[(t << 4) & 0x3f]; |
|
|
197 | *out++ = '='; |
|
|
198 | *out++ = '='; |
|
|
199 | break; |
|
|
200 | } |
|
|
201 | |
|
|
202 | *out++ = 0; |
|
|
203 | |
|
|
204 | return res; |
|
|
205 | } |
|
|
206 | #endif |
|
|
207 | |
|
|
208 | void |
|
|
209 | id2mac (unsigned int id, void *m) |
|
|
210 | { |
|
|
211 | mac &p = *(mac *)m; |
|
|
212 | |
|
|
213 | if (id) |
|
|
214 | { |
|
|
215 | p[0] = 0xfe; |
|
|
216 | p[1] = 0xfd; |
|
|
217 | p[2] = 0x80; |
|
|
218 | p[3] = 0x00; |
|
|
219 | p[4] = id >> 8; |
|
|
220 | p[5] = id; |
|
|
221 | } |
|
|
222 | else |
|
|
223 | { |
|
|
224 | p[0] = 0xff; |
|
|
225 | p[1] = 0xff; |
|
|
226 | p[2] = 0xff; |
|
|
227 | p[3] = 0xff; |
|
|
228 | p[4] = 0xff; |
|
|
229 | p[5] = 0xff; |
|
|
230 | } |
|
|
231 | } |
|
|
232 | |