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

libipg.h File Reference

ipgeo library interface More...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

Go to the source code of this file.

Data Structures

struct  ipgeo_context

Defines

#define LIBIPG_VERSION   "1.0"
#define LIBIPG_BIG_ENDIAN   1
#define IPGEO_BUF_SIZE   512
#define IPGEO_ERRBUF_SIZE   256

Typedefs

typedef ipgeo_context ipgeo_t

Functions

ipgeo_tipgeo_init (char *file, u_int8_t flags, char *err_buf)
void ipgeo_destroy (ipgeo_t *ipg)
u_int32_t ipgeo_ipa2ipn (char *ip)
int ipgeo_lookup (u_int32_t ipn, u_int8_t flags, ipgeo_t *ipg)
char * ipgeo_get_cc (ipgeo_t *ipg)
char * ipgeo_get_country (ipgeo_t *ipg)
char * ipgeo_get_region (ipgeo_t *ipg)
char * ipgeo_get_city (ipgeo_t *ipg)
char * ipgeo_get_isp (ipgeo_t *ipg)
double ipgeo_get_lat (ipgeo_t *ipg)
double ipgeo_get_long (ipgeo_t *ipg)
char * ipgeo_geterror (ipgeo_t *ipg)
int ipgeo_getfd (ipgeo_t *ipg)
int ipgeo_getdbt (ipgeo_t *ipg)


Detailed Description

ipgeo library interface

Definition in file libipg.h.


Define Documentation

#define LIBIPG_VERSION   "1.0"
 

current libipg version

Definition at line 82 of file libipg.h.

#define LIBIPG_BIG_ENDIAN   1
 

libipg works with multibyte numbers and needs to know how they're ordered

Definition at line 85 of file libipg.h.

#define IPGEO_BUF_SIZE   512
 

internal work buffers

Definition at line 88 of file libipg.h.

Referenced by ipgeo_lookup().


Typedef Documentation

typedef struct ipgeo_context ipgeo_t
 

ipgeo_t is the monolithic datatype describing an ipgeo session

Definition at line 110 of file libipg.h.

Referenced by ipgeo_destroy(), ipgeo_get_cc(), ipgeo_get_city(), ipgeo_get_country(), ipgeo_get_isp(), ipgeo_get_lat(), ipgeo_get_long(), ipgeo_get_region(), ipgeo_getdbt(), ipgeo_geterror(), ipgeo_getfd(), ipgeo_init(), and ipgeo_lookup().


Function Documentation

ipgeo_t* ipgeo_init char *  file,
u_int8_t  flags,
char *  err_buf
 

only CSV db type currently supported

control flags are currently undefined

Definition at line 49 of file libipg_init.c.

References ipgeo_context::db, ipgeo_context::dbt, ipgeo_context::flags, and ipgeo_t.

00050 {
00051     ipgeo_t *ipg = NULL;
00052 
00053     ipg = (ipgeo_t *)malloc(sizeof (ipgeo_t));
00054     if (ipg == NULL)
00055     {
00056         snprintf(err_buf, IPGEO_ERRBUF_SIZE, "malloc(): %s\n", strerror(errno));
00057         goto bad;
00058     }
00059 
00060     memset(ipg, 0, sizeof (ipg));
00061 
00062     ipg->db = fopen(file, "r");
00063     if (ipg->db == NULL)
00064     {
00065         snprintf(err_buf, IPGEO_ERRBUF_SIZE, "fopen() (%s): %s\n", file, 
00066                 strerror(errno));
00067         goto bad;
00068     }
00069 
00071     ipg->dbt = 6;
00072 
00074     ipg->flags = flags;
00075 
00076     return (ipg);
00077 
00078 bad:
00079     if (ipg)
00080     {
00081         if (ipg->db)
00082         {
00083             fclose(ipg->db);
00084         }
00085         free (ipg);
00086     }
00087     return (NULL);
00088 }

void ipgeo_destroy ipgeo_t ipg  ) 
 

Shuts down the libipg session and frees all memory referenced by ipg.

Parameters:
ipg pointer to a ipgeo context

Definition at line 91 of file libipg_init.c.

References ipgeo_context::db, and ipgeo_t.

00092 {
00093     if (ipg)
00094     {
00095         if (ipg->db)
00096         {
00097             fclose(ipg->db);
00098         }
00099         free (ipg);
00100     }
00101 }

u_int32_t ipgeo_ipa2ipn char *  ip  ) 
 

currently we don't deal with hostnames; ip has to be dots n decimals

compute first byte (256^3 * o1)

compute second byte (256^2 * o2)

compute third byte (256 * o3)

compute fourth byte (o4)

Definition at line 50 of file libipg_lookup.c.

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 }

int ipgeo_lookup u_int32_t  ipn,
u_int8_t  flags,
ipgeo_t ipg
 

Performs a lookup into the ipg database for the given IPv4 number. If successful, the funcion will fill in internal libipg structure members with location information specific to the queried IPv4 address. Currently libipg supports Country Code, Country, Region, City and ISP information. Depending on the IPv4 number queried; none, some or all of this information may be returned. Currently the only db access method is a linear search through the ASCII CSV file. Expect this to change shortly in favor of something faster, along the lines of O(ln n) or so. The database has over 95% accuracy at the Country and ISP level, 70% at the regional level and 65% at the city level, which, according to the IP2LOCATION documentation, is the highest accuracy available. The country-level inaccuracy is due to dynamic IP address allocation by large ISPs such as AOL and MSN TV. AOL, for example, uses a network that routes all of their Internet traffic through Reston, Virginia. As such, all IP-based geo-location databases are unable to determine the state and city for computers on the AOL network. The regional and country level inaccuracy is due to the flexibility given to each ISP to reassign dynamic IP addresses within their service areas.
Parameters:
ipn a network byte ordered IPv4 number, as returned by a function like ipgeo_ipa2ipn() or libnet_name2addr4(). After a successful call to ipgeo_lookup(), one or more calls to ipgeo_get_cc(), ipgeo_get_country(), ipgeo_get_region(), ipgeo_get_city(), and/or ipgeo_get_isp() may be made to obtain lookup information.
flags lookup parameters, or 0 for default
ipg pointer to a ipgeo context
Returns:
1 on success, -1 on error

Definition at line 77 of file libipg_lookup.c.

References ipgeo_context::cc, ipgeo_context::city, ipgeo_context::country, ipgeo_context::db, IPGEO_BUF_SIZE, ipgeo_t, ipgeo_context::isp, ipgeo_context::latitude, ipgeo_context::longitude, and ipgeo_context::region.

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 }

char* ipgeo_get_cc ipgeo_t ipg  ) 
 

Returns the country code for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the country code on success, NULL on error

Definition at line 195 of file libipg_lookup.c.

References ipgeo_context::cc, and ipgeo_t.

00196 {
00197     if (ipg->cc[0] == '-')
00198     {
00199         return("UNKNOWN");
00200     }
00201     return (ipg->cc);
00202 }

char* ipgeo_get_country ipgeo_t ipg  ) 
 

Returns the country for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the country code on success, NULL on error

Definition at line 205 of file libipg_lookup.c.

References ipgeo_context::country, and ipgeo_t.

00206 {
00207     if (ipg->country[0] == '-')
00208     {
00209         return("UNKNOWN");
00210     }
00211     return (ipg->country);
00212 }

char* ipgeo_get_region ipgeo_t ipg  ) 
 

Returns the region for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the country code on success, NULL on error

Definition at line 215 of file libipg_lookup.c.

References ipgeo_t, and ipgeo_context::region.

00216 {
00217     if (ipg->region[0] == '-')
00218     {
00219         return("UNKNOWN");
00220     }
00221     return (ipg->region);
00222 }

char* ipgeo_get_city ipgeo_t ipg  ) 
 

Returns the city for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the country code on success, NULL on error

Definition at line 225 of file libipg_lookup.c.

References ipgeo_context::city, and ipgeo_t.

00226 {
00227     if (ipg->city[0] == '-')
00228     {
00229         return("UNKNOWN");
00230     }
00231     return (ipg->city);
00232 }

char* ipgeo_get_isp ipgeo_t ipg  ) 
 

Returns the isp for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the country code on success, NULL on error

Definition at line 235 of file libipg_lookup.c.

References ipgeo_t, and ipgeo_context::isp.

00236 {
00237     if (ipg->isp[0] == '-')
00238     {
00239         return("UNKNOWN");
00240     }
00241     return (ipg->isp);
00242 }

double ipgeo_get_lat ipgeo_t ipg  ) 
 

Returns the latitude for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the latitude on success, 0 on error

Definition at line 245 of file libipg_lookup.c.

References ipgeo_t, and ipgeo_context::latitude.

00246 {
00247     return (ipg->latitude);
00248 }

double ipgeo_get_long ipgeo_t ipg  ) 
 

Returns the longitude for the most recent ipgeo db lookup. The function must be called after a successful call to ipgeo_lookup().

Parameters:
ipg pointer to a ipgeo context
Returns:
the longitude on success, 0 on error

Definition at line 251 of file libipg_lookup.c.

References ipgeo_t, and ipgeo_context::longitude.

00252 {
00253     return (ipg->longitude);
00254 }

char* ipgeo_geterror ipgeo_t ipg  ) 
 

Returns the last error set inside of the referenced libipg context. This function should be called anytime a function fails or an error condition is detected inside of libipg.

Parameters:
ipg pointer to a libipg context
Returns:
an error string or NULL if no error has occured

Definition at line 104 of file libipg_init.c.

References ipgeo_context::err_buf, and ipgeo_t.

00105 {
00106     return (ipg->err_buf);
00107 }

int ipgeo_getfd ipgeo_t ipg  ) 
 

we should not return -1 here since any error should be caught during initialization

Definition at line 120 of file libipg_init.c.

References ipgeo_context::db, and ipgeo_t.

00121 {
00122     if (ipg == NULL || ipg->db == NULL)
00123     {
00124         return (-1);
00125     }
00126 
00131     return (fileno(ipg->db));
00132 }

int ipgeo_getdbt ipgeo_t ipg  ) 
 

Returns the type of IP2LOCATION db libipg has open.

Parameters:
ipg pointer to a ipgeo context
Returns:
the type of db open

Definition at line 110 of file libipg_init.c.

References ipgeo_context::db, ipgeo_context::dbt, and ipgeo_t.

00111 {
00112     if (ipg == NULL || ipg->db == NULL)
00113     {
00114         return (-1);
00115     }
00116     return (ipg->dbt);
00117 }


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