crossroads

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

ipfmatch.c (1460B)


      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_match (IpFilter f) {
      9     static int client_ip_nr;
     10     int counter, val;
     11     char *tmp, *cp;
     12     
     13     /* Build up the actual client IP as an int.
     14      * We need to do this in reverse order; e.g. 1.2.3.4 needs to become
     15      * 4<<24 | 3<<16 | 2<<8 | 1. Reason is that when mask /16 applies,
     16      * we can OR this value with 16, and we'll keep 2<<8 | 1, the only
     17      * significant digits. */
     18     if (!client_ip_nr) {
     19 	tmp = xstrdup (client_ip);
     20 	counter = 0;
     21 	for (counter = 0, cp = strtok (tmp, ".");
     22 	     cp;
     23 	     counter += 8, cp = strtok (0, ".")) {
     24 	    if (sscanf (cp, "%d", &val) < 1) {
     25 		free (tmp);
     26 		return (1);
     27 	    }
     28 	    val <<= counter;
     29 	    client_ip_nr |= val;
     30 	    /* msg ("ipf_match: val 0x%x, client_ip_nr 0x%x",
     31 	     * 	     val, client_ip_nr); */
     32 	}
     33     }
     34     free (tmp);
     35 
     36     /* Here's the comparison. */
     37     /* msg ("ipf_match: filter ip/mask 0x%x/0x%x, "
     38      *      "filter res 0x%x, client res 0x%x",
     39      *      f.ip, f.mask, 
     40      *      (f.ip & f.mask), (client_ip_nr & f.mask));
     41      */
     42     return ( (f.ip & f.mask) == (client_ip_nr & f.mask) );
     43 }