add function is_private_ip

pull/5/head
yuqing 2015-10-09 16:12:30 +08:00
parent a9388ab7fe
commit 3a3abaf148
5 changed files with 61 additions and 4 deletions

View File

@ -1,6 +1,8 @@
Version 1.22 2015-09-15 Version 1.22 2015-10-09
* export php function: fastcommon_get_first_local_ip * export php function: fastcommon_get_first_local_ip
* add function is_private_ip
* add function get_next_local_ip
Version 1.21 2015-09-14 Version 1.21 2015-09-14
* ini_file_reader support annotation function * ini_file_reader support annotation function

View File

@ -134,26 +134,49 @@ void print_local_host_ip_addrs()
printf("\n"); printf("\n");
} }
const char *get_first_local_ip() const char *get_next_local_ip(const char *previous_ip)
{ {
char *p; char *p;
char *pEnd; char *pEnd;
bool found;
if (g_local_host_ip_count == 0) if (g_local_host_ip_count == 0)
{ {
load_local_host_ip_addrs(); load_local_host_ip_addrs();
} }
found = (previous_ip == NULL);
pEnd = g_local_host_ip_addrs + \ pEnd = g_local_host_ip_addrs + \
IP_ADDRESS_SIZE * g_local_host_ip_count; IP_ADDRESS_SIZE * g_local_host_ip_count;
for (p=g_local_host_ip_addrs; p<pEnd; p+=IP_ADDRESS_SIZE) for (p=g_local_host_ip_addrs; p<pEnd; p+=IP_ADDRESS_SIZE)
{ {
if (strcmp(p, LOCAL_LOOPBACK_IP) != 0) if (strcmp(p, LOCAL_LOOPBACK_IP) != 0)
{
if (found)
{ {
return p; return p;
} }
else if (strcmp(p, previous_ip) == 0)
{
found = true;
}
}
} }
return NULL;
}
const char *get_first_local_ip()
{
const char *first_ip;
first_ip = get_next_local_ip(NULL);
if (first_ip != NULL)
{
return first_ip;
}
else
{
return LOCAL_LOOPBACK_IP; return LOCAL_LOOPBACK_IP;
} }
}

View File

@ -31,7 +31,10 @@ extern char g_if_alias_prefix[FAST_IF_ALIAS_PREFIX_MAX_SIZE];
void load_local_host_ip_addrs(); void load_local_host_ip_addrs();
bool is_local_host_ip(const char *client_ip); bool is_local_host_ip(const char *client_ip);
const char *get_first_local_ip(); const char *get_first_local_ip();
const char *get_next_local_ip(const char *previous_ip);
int insert_into_local_host_ip(const char *client_ip); int insert_into_local_host_ip(const char *client_ip);
void log_local_host_ip_addrs(); void log_local_host_ip_addrs();
void print_local_host_ip_addrs(); void print_local_host_ip_addrs();

View File

@ -2227,3 +2227,27 @@ double get_line_distance_km(const double lat1, const double lon1,
return sqrt(lat_distance * lat_distance + lng_distance * lng_distance); return sqrt(lat_distance * lat_distance + lng_distance * lng_distance);
} }
bool is_private_ip(const char* ip)
{
if (ip == NULL || (int)strlen(ip) < 8)
{
return false;
}
if (memcmp(ip, "10.", 3) == 0 || memcmp(ip, "192.168.", 8) == 0)
{
return true;
}
if (memcmp(ip, "172.", 4) == 0)
{
int b;
b = atoi(ip + 4);
if (b >= 16 && b < 32)
{
return true;
}
}
return false;
}

View File

@ -515,6 +515,11 @@ int ignore_signal_pipe();
double get_line_distance_km(const double lat1, const double lon1, double get_line_distance_km(const double lat1, const double lon1,
const double lat2, const double lon2); const double lat2, const double lon2);
/** is private ip address for IPv4
* return: true for private ip, otherwise false
*/
bool is_private_ip(const char* ip);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif