Commit 4430aaca authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Return address family from parse_ip and _net.

parent 8337aaee
...@@ -210,7 +210,7 @@ format_prefix(const unsigned char *prefix, unsigned char plen) ...@@ -210,7 +210,7 @@ format_prefix(const unsigned char *prefix, unsigned char plen)
} }
int int
parse_address(const char *address, unsigned char *addr_r) parse_address(const char *address, unsigned char *addr_r, int *af_r)
{ {
struct in_addr ina; struct in_addr ina;
struct in6_addr ina6; struct in6_addr ina6;
...@@ -220,12 +220,14 @@ parse_address(const char *address, unsigned char *addr_r) ...@@ -220,12 +220,14 @@ parse_address(const char *address, unsigned char *addr_r)
if(rc > 0) { if(rc > 0) {
memcpy(addr_r, v4prefix, 12); memcpy(addr_r, v4prefix, 12);
memcpy(addr_r + 12, &ina, 4); memcpy(addr_r + 12, &ina, 4);
if(af_r) *af_r = AF_INET;
return 0; return 0;
} }
rc = inet_pton(AF_INET6, address, &ina6); rc = inet_pton(AF_INET6, address, &ina6);
if(rc > 0) { if(rc > 0) {
memcpy(addr_r, &ina6, 16); memcpy(addr_r, &ina6, 16);
if(af_r) *af_r = AF_INET6;
return 0; return 0;
} }
...@@ -233,12 +235,14 @@ parse_address(const char *address, unsigned char *addr_r) ...@@ -233,12 +235,14 @@ parse_address(const char *address, unsigned char *addr_r)
} }
int int
parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r) parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
int *af_r)
{ {
char buf[INET6_ADDRSTRLEN]; char buf[INET6_ADDRSTRLEN];
char *slash, *end; char *slash, *end;
unsigned char prefix[16]; unsigned char prefix[16];
long plen; long plen;
int af;
struct in_addr ina; struct in_addr ina;
struct in6_addr ina6; struct in6_addr ina6;
int rc; int rc;
...@@ -249,7 +253,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r) ...@@ -249,7 +253,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r)
} else { } else {
slash = strchr(net, '/'); slash = strchr(net, '/');
if(slash == NULL) { if(slash == NULL) {
rc = parse_address(net, prefix); rc = parse_address(net, prefix, &af);
if(rc < 0) if(rc < 0)
return rc; return rc;
plen = 128; plen = 128;
...@@ -266,6 +270,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r) ...@@ -266,6 +270,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r)
if(*end != '\0' || plen < 0 || plen > 32) if(*end != '\0' || plen < 0 || plen > 32)
return -1; return -1;
plen += 96; plen += 96;
af = AF_INET;
} else { } else {
rc = inet_pton(AF_INET6, buf, &ina6); rc = inet_pton(AF_INET6, buf, &ina6);
if(rc > 0) { if(rc > 0) {
...@@ -273,6 +278,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r) ...@@ -273,6 +278,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r)
plen = strtol(slash + 1, &end, 0); plen = strtol(slash + 1, &end, 0);
if(*end != '\0' || plen < 0 || plen > 128) if(*end != '\0' || plen < 0 || plen > 128)
return -1; return -1;
af = AF_INET6;
} else { } else {
return -1; return -1;
} }
...@@ -281,6 +287,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r) ...@@ -281,6 +287,7 @@ parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r)
} }
mask_prefix(prefix_r, prefix, plen); mask_prefix(prefix_r, prefix, plen);
*plen_r = plen; *plen_r = plen;
if(af_r) *af_r = af;
return 0; return 0;
} }
......
...@@ -46,8 +46,9 @@ const unsigned char *mask_prefix(unsigned char *ret, ...@@ -46,8 +46,9 @@ const unsigned char *mask_prefix(unsigned char *ret,
unsigned char plen); unsigned char plen);
const char *format_address(const unsigned char *address); const char *format_address(const unsigned char *address);
const char *format_prefix(const unsigned char *address, unsigned char prefix); const char *format_prefix(const unsigned char *address, unsigned char prefix);
int parse_address(const char *address, unsigned char *addr_r); int parse_address(const char *address, unsigned char *addr_r, int *af_r);
int parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r); int parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
int *af_r);
int wait_for_fd(int direction, int fd, int msecs); int wait_for_fd(int direction, int fd, int msecs);
int martian_prefix(const unsigned char *prefix, int plen); int martian_prefix(const unsigned char *prefix, int plen);
int v4mapped(const unsigned char *address); int v4mapped(const unsigned char *address);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment