IP to Geo

IP to Geo Commit Details

Date:2016-02-04 20:39:50 (4 years 8 months ago)
Author:Grégory Soutadé
Branch:master
Commit:501875ae2bb5721ab1fb7386b59734b6f3a533ec
Parents: 57fce2afe292b86d208afa393aa02b59d3e88315
Message:Allows to pass IPv6 in interactive mode (always returns None for now)

Changes:
Msrc/cmdline.c (1 diff)
Msrc/cmdline.ggo (1 diff)
Msrc/ip_to_geo.c (1 diff)

File differences

src/cmdline.c
2727
2828
2929
30
30
3131
32
32
3333
3434
3535
const char *gengetopt_args_info_purpose = "Convert an IP to country code";
const char *gengetopt_args_info_usage = "Usage: ip_to_geo --ip <ipv4>|--daemon [--port <port>] [--bind-ip <ip>]";
const char *gengetopt_args_info_usage = "Usage: ip_to_geo --ip <ip>|--daemon [--port <port>] [--bind-ip <ip>]";
const char *gengetopt_args_info_description = "Convert an IP to country code. Currently, onyl IPv4 supported";
const char *gengetopt_args_info_description = "Convert an IP to country code.";
const char *gengetopt_args_info_help[] = {
" -h, --help Print help and exit",
src/cmdline.ggo
11
22
33
4
5
4
5
66
77
88
package "ip_to_geo"
version "0.1"
purpose "Convert an IP to country code"
usage "ip_to_geo --ip <ipv4>|--daemon [--port <port>] [--bind-ip <ip>]"
description "Convert an IP to country code. Currently, onyl IPv4 supported"
usage "ip_to_geo --ip <ip>|--daemon [--port <port>] [--bind-ip <ip>]"
description "Convert an IP to country code."
section "Interactive mode"
option "ip" i "IP to convert" typestr="ip" string optional
src/ip_to_geo.c
8686
8787
8888
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
11289
11390
91
11492
115
93
11694
117
118
95
96
11997
120
121
122
98
99
100
101
102
103
104
105
123106
124107
125
108
126109
127110
128111
return country_codes[*idx];
}
/* int strip_to_int(char* strip_, uint32_t* ip) */
/* { */
/* char* saveptr = NULL; */
/* char* cur; */
/* int i; */
/* char* strip = strdup(strip_); */
/* *ip = 0; */
/* for(i=3; i>=0; i--) */
/* { */
/* cur = strtok_r(strip, ".", &saveptr); */
/* if (!cur) goto end; */
/* *ip += atoi(cur) << (8*i); */
/* strip = NULL; */
/* } */
/* cur = strtok_r(strip, ".", &saveptr); */
/* end: */
/* free(strip); */
/* return (cur)?-1:0; */
/* } */
int interactive(struct gengetopt_args_info* params)
{
uint8_t ip[16];
const uint8_t* cc;
int ret;
int ret, ip_size=4;
ret = inet_addr(params->ip_arg);
if (ret == INADDR_NONE)
ret = inet_pton(AF_INET, params->ip_arg, ip);
if (ret != 1)
{
if (!params->quiet_flag)
fprintf(stderr, "Invalid IP %s\n", params->ip_arg);
return -1;
ip_size = 16;
ret = inet_pton(AF_INET6, params->ip_arg, ip);
if (ret != 1)
{
if (!params->quiet_flag)
fprintf(stderr, "Invalid IP %s\n", params->ip_arg);
return -1;
}
}
cc = ip_to_geo((uint8_t*)&ret, 4);
cc = ip_to_geo((uint8_t*)&ret, ip_size);
if (params->quiet_flag)
printf("%s\n", (cc)?(char*)get_country_code(cc):"<none>");

Archive Download the corresponding diff file

Branches

Tags