Skip to content

Commit

Permalink
Check of a 'AF_INET' address first.
Browse files Browse the repository at this point in the history
Fix the IPv4 mapped layout of the IPv6 address.
  • Loading branch information
Gisle Vanem committed Oct 21, 2020
1 parent 3ba9091 commit 4dc6dc6
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/Geo-IP/IPFire/libloc/src/libloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,41 @@ LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {

LOC_EXPORT int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {

DEBUG(ctx, "Parsing IP address %s\n", string);
struct in_addr ipv4_address;

loc_init();

// Try parsing this as an IPv6 address
int r = inet_pton(AF_INET6, string, address);

// If inet_pton returns one it has been successful
if (r == 1) {
DEBUG(ctx, "%s is an IPv6 address\n", string);
return 0;
}
DEBUG(ctx, "Parsing IP address %s\n", string);

// Try parsing this as an IPv4 address
struct in_addr ipv4_address;
r = inet_pton(AF_INET, string, &ipv4_address);
if (r == 1) {
if (inet_pton(AF_INET, string, &ipv4_address) == 1) {
DEBUG(ctx, "%s is an IPv4 address\n", string);

// Convert to IPv6-mapped address
#if defined(_WIN32)
*(u_long*) &address->u.Byte[0] = 0;
*(u_long*) &address->u.Byte[4] = 0;
*(u_long*) &address->u.Byte[8] = 0xffff;
*(u_long*) &address->u.Byte[12] = ipv4_address.s_addr;
address->s6_words[0] = 0;
address->s6_words[1] = 0;
address->s6_words[2] = 0;
address->s6_words[3] = 0;
address->s6_words[4] = 0;
address->s6_words[5] = 0xffff;
address->s6_words[6] = 0;
address->s6_words[7] = ipv4_address.s_addr;
#else
address->s6_addr32[0] = htonl(0x0000);
address->s6_addr32[1] = htonl(0x0000);
address->s6_addr32[2] = htonl(0xffff);
address->s6_addr32[3] = ipv4_address.s_addr;
#endif
return 0;
}

// Try parsing this as an IPv6 address
int r = inet_pton(AF_INET6, string, address);

// If inet_pton returns one it has been successful
if (r == 1) {
DEBUG(ctx, "%s is an IPv6 address\n", string);
return 0;
}

Expand Down

0 comments on commit 4dc6dc6

Please sign in to comment.