Main Page | Data Structures | File List | Data Fields | Globals

libipg_lookup.c

Go to the documentation of this file.
00001 /*
00002  *  $Id: libipg_lookup.c,v 1.2 2003/12/29 08:10:10 mike Exp $
00003  *
00004  *  libipg
00005  *  libipb_lookup.c - lookup routines
00006  *
00007  *  Copyright (c) 2003 - 2004 Mike D. Schiffman <mike@infonexus.com>
00008  *  All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  *
00031  */
00032 
00043 #if (HAVE_CONFIG_H)
00044 #include "../include/config.h"
00045 #endif
00046 
00047 #include "./libipg.h"
00048 
00049 u_int32_t
00050 ipgeo_ipa2ipn(char *ip)
00051 {
00052     char *p = ip;
00053     u_int32_t o1, o2, o3, o4;
00054 
00056     if (!isdigit(ip[0]))
00057     {
00058         return (0);
00059     }
00060 
00062     o1 = 16777216 * atol(strsep(&p, "."));
00063 
00065     o2 = 65536 * atol(strsep(&p, "."));
00066 
00068     o3 = 256 * atol(strsep(&p, "."));
00069 
00071     o4 = atol(strsep(&p, "."));
00072 
00073     return (o1 + o2 + o3 + o4);
00074 }
00075 
00076 int
00077 ipgeo_lookup(u_int32_t ipn, u_int8_t flags, ipgeo_t *ipg)
00078 {
00079     int n;
00080     char *p, *q;
00081     u_int32_t min, max;
00082     char buf[IPGEO_BUF_SIZE];
00083 
00084     if (ipg == NULL)
00085     {
00086         return (-1);
00087     }
00088 
00089 #if (LIBIPG_LIL_ENDIAN)
00090     ipn = htonl(ipn);
00091 #endif
00092 
00093     /* perform a linear search through the CSV file */
00094     for (; fgets(buf, IPGEO_BUF_SIZE - 1, ipg->db); )
00095     {
00096         if (buf[0] == '#')
00097         {
00098             /* ignore comments */
00099             continue;
00100         }
00101 
00102         p = buf;
00103 
00104         /* step over quote */
00105         p += 1;
00106         min = strtoul(strsep(&p, ","), (char **)NULL, 10);
00107 
00108         /* step over quote */
00109         p += 1;
00110         max = strtoul(strsep(&p, ","), (char **)NULL, 10);
00111 
00112         if (ipn >= min && ipn <= max)
00113         {
00114             p += 1;
00115             q = strsep(&p, ",");
00116             for (n = 0; n < sizeof(ipg->cc) - 1; n++)
00117             {
00118                 if (q[n] == '"')
00119                 {
00120                     ipg->cc[n] = NULL;
00121                     break;
00122                 }
00123                 ipg->cc[n] = q[n];
00124             }
00125 
00126             /* get cc */
00127             p += 1;
00128             q = strsep(&p, ",");
00129             for (n = 0; n < sizeof(ipg->country) - 1; n++)
00130             {
00131                 if (q[n] == '"')
00132                 {
00133                     ipg->country[n] = NULL;
00134                     break;
00135                 }
00136                 ipg->country[n] = q[n];
00137             }
00138 
00139             /* get country */
00140             p += 1;
00141             q = strsep(&p, ",");
00142             for (n = 0; n < sizeof(ipg->region) - 1; n++)
00143             {
00144                 if (q[n] == '"')
00145                 {
00146                     ipg->region[n] = NULL;
00147                     break;
00148                 }
00149                 ipg->region[n] = q[n];
00150             }
00151 
00152             /* get city */
00153             p += 1;
00154             q = strsep(&p, ",");
00155             for (n = 0; n < sizeof(ipg->city) - 1; n++)
00156             {
00157                 if (q[n] == '"')
00158                 {
00159                     ipg->city[n] = NULL;
00160                     break;
00161                 }
00162                 ipg->city[n] = q[n];
00163             }
00164 
00165             /* get latitude */
00166             p += 1;
00167             ipg->latitude = strtod(strsep(&p, ","), (char **)NULL);
00168 
00169             /* get longitude */
00170             p += 1;
00171             ipg->longitude = strtod(strsep(&p, ","), (char **)NULL);
00172 
00173             /* get isp */
00174             p += 1;
00175             q = strsep(&p, ",");
00176             for (n = 0; n < sizeof(ipg->isp) - 1; n++)
00177             {
00178                 if (q[n] == '"')
00179                 {
00180                     ipg->isp[n] = NULL;
00181                     break;
00182                 }
00183                 ipg->isp[n] = q[n];
00184             }
00185 
00186             rewind(ipg->db);
00187             return (1);
00188         }
00189     }
00190     /* no match */
00191     return (0);
00192 }
00193 
00194 char *
00195 ipgeo_get_cc(ipgeo_t *ipg)
00196 {
00197     if (ipg->cc[0] == '-')
00198     {
00199         return("UNKNOWN");
00200     }
00201     return (ipg->cc);
00202 }
00203 
00204 char *
00205 ipgeo_get_country(ipgeo_t *ipg)
00206 {
00207     if (ipg->country[0] == '-')
00208     {
00209         return("UNKNOWN");
00210     }
00211     return (ipg->country);
00212 }
00213 
00214 char *
00215 ipgeo_get_region(ipgeo_t *ipg)
00216 {
00217     if (ipg->region[0] == '-')
00218     {
00219         return("UNKNOWN");
00220     }
00221     return (ipg->region);
00222 }
00223 
00224 char *
00225 ipgeo_get_city(ipgeo_t *ipg)
00226 {
00227     if (ipg->city[0] == '-')
00228     {
00229         return("UNKNOWN");
00230     }
00231     return (ipg->city);
00232 }
00233 
00234 char *
00235 ipgeo_get_isp(ipgeo_t *ipg)
00236 {
00237     if (ipg->isp[0] == '-')
00238     {
00239         return("UNKNOWN");
00240     }
00241     return (ipg->isp);
00242 }
00243 
00244 double
00245 ipgeo_get_lat(ipgeo_t *ipg)
00246 {
00247     return (ipg->latitude);
00248 }
00249 
00250 double
00251 ipgeo_get_long(ipgeo_t *ipg)
00252 {
00253     return (ipg->longitude);
00254 }
00255 
00256 /* EOF */

Generated on Wed Dec 31 12:06:49 2003 for libipg by doxygen 1.3.4