crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

ipfparse.c (1323B)


      1 /*************************************************************************
      2  * This file is part of Crosroads 1.23, a load balancer and fail over
      3  * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL.
      4  * Visit http://crossroads.e-tunity.com for information.
      5  *************************************************************************/
      6 #include "crossroads.h"
      7 
      8 int ipf_parse (char const *val, IpFilter *res) {
      9     char *str, *cp;
     10     int counter, nr, i;
     11     
     12     memset (res, 0, sizeof(IpFilter));
     13     str = xstrdup (val);
     14 
     15     for (counter = 0, cp = strtok (str, ".");
     16 	 cp;
     17 	 counter += 8, cp = strtok (0, ".")) {
     18 	if (counter > 24) {
     19 	    warning ("Invalid IP filter specifier '%s' "
     20 		     "(too many network bytes)", val);
     21 	    free (str);
     22 	    return (1);
     23 	}	
     24 	if (sscanf (cp, "%d", &nr) < 1) {
     25 	    warning ("Invalid IP filter specifier '%s' "
     26 		     "('%s' not a number)", val, cp);
     27 	    free (str);
     28 	    return (2);
     29 	}
     30 	nr <<= counter;
     31 	res->ip |= nr;
     32     }
     33     free (str);
     34 
     35     if ( (cp = strchr (val, '/')) ) {
     36 	if (sscanf (cp + 1, "%d", &nr) < 1) {
     37 	    warning ("Invalid IP filter specifier '%s' "
     38 		     "(bad mask '$s')", val, cp + 1);
     39 	    return (3);
     40 	}
     41 	for (i = 1; i <= nr; i++) {
     42 	    res->mask <<= 1;
     43 	    res->mask |= 1;
     44 	}
     45     } else
     46 	res->mask = (unsigned) -1;
     47 
     48     return (0);
     49 }
     50 	
     51