diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-19 21:15:42 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-19 21:15:42 +0000 |
commit | d867f32a7aefe6694dbbc4c6370a36a30bfc8318 (patch) | |
tree | 3257bc9edfcce45db949620da0dbc9e3a6b817cd /networking | |
parent | 921799da4ec72eb353240e315b456171d2581d46 (diff) | |
download | busybox-w32-d867f32a7aefe6694dbbc4c6370a36a30bfc8318.tar.gz busybox-w32-d867f32a7aefe6694dbbc4c6370a36a30bfc8318.tar.bz2 busybox-w32-d867f32a7aefe6694dbbc4c6370a36a30bfc8318.zip |
httpd: explain IP/mask parsing, and simplify it a bit.
parse_conf 1258 1247 -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-11) Total: -11 bytes
text data bss dec hex filename
772602 1058 11092 784752 bf970 busybox_old
772594 1058 11092 784744 bf968 busybox_unstripped
Diffstat (limited to 'networking')
-rw-r--r-- | networking/httpd.c | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/networking/httpd.c b/networking/httpd.c index a7473ede9..0b4e305d9 100644 --- a/networking/httpd.c +++ b/networking/httpd.c | |||
@@ -289,17 +289,26 @@ static ALWAYS_INLINE void free_Htaccess_IP_list(Htaccess_IP **pptr) | |||
289 | free_llist((has_next_ptr**)pptr); | 289 | free_llist((has_next_ptr**)pptr); |
290 | } | 290 | } |
291 | 291 | ||
292 | static int scan_ip(const char **ep, unsigned *ip, unsigned char endc) | 292 | //#undef isdigit |
293 | //#define isdigit(a) ((unsigned)((a) - '0') <= 9) | ||
294 | //#define notdigit(a) ((unsigned)((a) - '0') > 9) | ||
295 | |||
296 | /* Returns presumed mask width in bits or < 0 on error. | ||
297 | * Updates strp, stores IP at provided pointer */ | ||
298 | static int scan_ip(const char **strp, unsigned *ipp, unsigned char endc) | ||
293 | { | 299 | { |
294 | const char *p = *ep; | 300 | const char *p = *strp; |
295 | int auto_mask = 8; | 301 | int auto_mask = 8; |
302 | unsigned ip = 0; | ||
296 | int j; | 303 | int j; |
297 | 304 | ||
298 | *ip = 0; | 305 | if (*p == '/') |
306 | return -auto_mask; | ||
307 | |||
299 | for (j = 0; j < 4; j++) { | 308 | for (j = 0; j < 4; j++) { |
300 | unsigned octet; | 309 | unsigned octet; |
301 | 310 | ||
302 | if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != '\0') | 311 | if ((*p < '0' || *p > '9') && *p != '/' && *p) |
303 | return -auto_mask; | 312 | return -auto_mask; |
304 | octet = 0; | 313 | octet = 0; |
305 | while (*p >= '0' && *p <= '9') { | 314 | while (*p >= '0' && *p <= '9') { |
@@ -311,55 +320,61 @@ static int scan_ip(const char **ep, unsigned *ip, unsigned char endc) | |||
311 | } | 320 | } |
312 | if (*p == '.') | 321 | if (*p == '.') |
313 | p++; | 322 | p++; |
314 | if (*p != '/' && *p != '\0') | 323 | if (*p != '/' && *p) |
315 | auto_mask += 8; | 324 | auto_mask += 8; |
316 | *ip = ((*ip) << 8) | octet; | 325 | ip = (ip << 8) | octet; |
317 | } | 326 | } |
318 | if (*p != '\0') { | 327 | if (*p) { |
319 | if (*p != endc) | 328 | if (*p != endc) |
320 | return -auto_mask; | 329 | return -auto_mask; |
321 | p++; | 330 | p++; |
322 | if (*p == '\0') | 331 | if (*p == '\0') |
323 | return -auto_mask; | 332 | return -auto_mask; |
324 | } | 333 | } |
325 | *ep = p; | 334 | *ipp = ip; |
335 | *strp = p; | ||
326 | return auto_mask; | 336 | return auto_mask; |
327 | } | 337 | } |
328 | 338 | ||
329 | static int scan_ip_mask(const char *ipm, unsigned *ip, unsigned *mask) | 339 | /* Returns 0 on success. Stores IP and mask at provided pointers */ |
340 | static int scan_ip_mask(const char *str, unsigned *ipp, unsigned *maskp) | ||
330 | { | 341 | { |
331 | int i; | 342 | int i; |
332 | unsigned msk; | 343 | unsigned mask; |
344 | char *p; | ||
333 | 345 | ||
334 | i = scan_ip(&ipm, ip, '/'); | 346 | i = scan_ip(&str, ipp, '/'); |
335 | if (i < 0) | 347 | if (i < 0) |
336 | return i; | 348 | return i; |
337 | if (*ipm) { | 349 | |
338 | const char *p = ipm; | 350 | if (*str) { |
339 | 351 | /* there is /xxx after dotted-IP address */ | |
340 | i = 0; | 352 | i = bb_strtou(str, &p, 10); |
341 | while (*p) { | 353 | if (*p == '.') { |
342 | if (*p < '0' || *p > '9') { | 354 | /* 'xxx' itself is dotted-IP mask, parse it */ |
343 | if (*p == '.') { | 355 | /* (return 0 (success) only if it has N.N.N.N form) */ |
344 | i = scan_ip(&ipm, mask, 0); | 356 | return scan_ip(&str, maskp, '\0') - 32; |
345 | return i != 32; | ||
346 | } | ||
347 | return -1; | ||
348 | } | ||
349 | i *= 10; | ||
350 | i += *p - '0'; | ||
351 | p++; | ||
352 | } | 357 | } |
358 | if (*p) | ||
359 | return -1; | ||
353 | } | 360 | } |
354 | if (i > 32 || i < 0) | 361 | |
362 | if (i > 32) | ||
355 | return -1; | 363 | return -1; |
356 | msk = 0x80000000; | 364 | |
357 | *mask = 0; | 365 | if (sizeof(unsigned) == 4 && i == 32) { |
358 | while (i > 0) { | 366 | /* mask >>= 32 below may not work */ |
359 | *mask |= msk; | 367 | mask = 0; |
360 | msk >>= 1; | 368 | } else { |
361 | i--; | 369 | mask = 0xffffffff; |
370 | mask >>= i; | ||
362 | } | 371 | } |
372 | /* i == 0 -> *maskp = 0x00000000 | ||
373 | * i == 1 -> *maskp = 0x80000000 | ||
374 | * i == 4 -> *maskp = 0xf0000000 | ||
375 | * i == 31 -> *maskp = 0xfffffffe | ||
376 | * i == 32 -> *maskp = 0xffffffff */ | ||
377 | *maskp = (uint32_t)(~mask); | ||
363 | return 0; | 378 | return 0; |
364 | } | 379 | } |
365 | 380 | ||