提交 51c4e451 编写于 作者: S Szabolcs Nagy

fix __ipparse to parse the generic numbers-and-dots IPv4 format correctly

* allow at most 4 parts
* bounds check the parts correctly
* disallow leading whitespace and sign
* check the address family before falling back to IPv6
上级 59b8dc08
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "__dns.h" #include "__dns.h"
#include <stdio.h>
int __ipparse(void *dest, int family, const char *s0) int __ipparse(void *dest, int family, const char *s0)
{ {
const char *s = s0; const char *s = s0;
unsigned char *d = dest; unsigned char *d = dest;
unsigned long a[16] = { 0 }; unsigned long a[16] = { 0 };
const char *z; char *z;
int i; int i;
if (family == AF_INET6) goto not_v4; if (family == AF_INET6) goto not_v4;
for (i=0; i<4; i++) { for (i=0; i<4; i++) {
a[i] = strtoul(s, (char **)&z, 0); a[i] = strtoul(s, &z, 0);
if (z==s || (*z && *z != '.')) goto not_v4; if (z==s || (*z && *z != '.') || !isdigit(*s)) {
if (family == AF_INET) return -1;
goto not_v4;
}
if (!*z) break; if (!*z) break;
s=z+1; s=z+1;
} }
if (i==4) return -1;
switch (i) { switch (i) {
case 0: case 0:
a[1] = a[0] & 0xffffff; a[1] = a[0] & 0xffffff;
...@@ -35,7 +39,10 @@ int __ipparse(void *dest, int family, const char *s0) ...@@ -35,7 +39,10 @@ int __ipparse(void *dest, int family, const char *s0)
} }
((struct sockaddr_in *)d)->sin_family = AF_INET; ((struct sockaddr_in *)d)->sin_family = AF_INET;
d = (void *)&((struct sockaddr_in *)d)->sin_addr; d = (void *)&((struct sockaddr_in *)d)->sin_addr;
for (i=0; i<4; i++) d[i] = a[i]; for (i=0; i<4; i++) {
if (a[i] > 255) return -1;
d[i] = a[i];
}
return 0; return 0;
not_v4: not_v4:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册