Allows to pass IPv6 in interactive mode (always returns None for now)

This commit is contained in:
Grégory Soutadé 2016-02-04 20:39:50 +01:00
parent 57fce2afe2
commit 501875ae2b
3 changed files with 17 additions and 34 deletions

View File

@ -27,9 +27,9 @@
const char *gengetopt_args_info_purpose = "Convert an IP to country code"; 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[] = { const char *gengetopt_args_info_help[] = {
" -h, --help Print help and exit", " -h, --help Print help and exit",

View File

@ -1,8 +1,8 @@
package "ip_to_geo" package "ip_to_geo"
version "0.1" version "0.1"
purpose "Convert an IP to country code" purpose "Convert an IP to country code"
usage "ip_to_geo --ip <ipv4>|--daemon [--port <port>] [--bind-ip <ip>]" usage "ip_to_geo --ip <ip>|--daemon [--port <port>] [--bind-ip <ip>]"
description "Convert an IP to country code. Currently, onyl IPv4 supported" description "Convert an IP to country code."
section "Interactive mode" section "Interactive mode"
option "ip" i "IP to convert" typestr="ip" string optional option "ip" i "IP to convert" typestr="ip" string optional

View File

@ -86,43 +86,26 @@ const uint8_t* get_country_code(const uint8_t* idx)
return country_codes[*idx]; 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) int interactive(struct gengetopt_args_info* params)
{ {
uint8_t ip[16];
const uint8_t* cc; const uint8_t* cc;
int ret; int ret, ip_size=4;
ret = inet_addr(params->ip_arg); ret = inet_pton(AF_INET, params->ip_arg, ip);
if (ret == INADDR_NONE) if (ret != 1)
{ {
if (!params->quiet_flag) ip_size = 16;
fprintf(stderr, "Invalid IP %s\n", params->ip_arg); ret = inet_pton(AF_INET6, params->ip_arg, ip);
return -1; 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) if (params->quiet_flag)
printf("%s\n", (cc)?(char*)get_country_code(cc):"<none>"); printf("%s\n", (cc)?(char*)get_country_code(cc):"<none>");