void test_ip4_parsing(void) { unsigned host, mask, not_flag; PORTSET portset; char **curip; int ret; IPADDRESS *adp; char *ips[] = { "138.26.1.24:25", "1.1.1.1/255.255.255.0:444", "1.1.1.1/16:25-28", "1.1.1.1/255.255.255.255:25 27-29", "z/24", "0/0", "0.0.0.0/0.0.0.0:25-26 28-29 31", "0.0.0.0/0.0.2.0", NULL }; for(curip = ips; curip[0] != NULL; curip++) { portset_init(&portset); /* network byte order stuff */ if((ret = ip4_parse(curip[0], 1, ¬_flag, &host, &mask, &portset)) != 0) { fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret); } else { printf("%c", not_flag ? '!' : ' '); printf("%s/", inet_ntoa(*(struct in_addr *) &host)); printf("%s", inet_ntoa(*(struct in_addr *) &mask)); printf(" parsed successfully!\n"); } /* host byte order stuff */ if((ret = ip4_parse(curip[0], 0, ¬_flag, &host, &mask, &portset)) != 0) { fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret); } else { adp = ip_new(IPV4_FAMILY); ip_set(adp, &host, IPV4_FAMILY); ip_fprint(stdout, adp); fprintf(stdout, "*****************\n"); ip_free(adp); } } return; }
int ip4_setparse(IPSET *ipset, char *ipstr) { char *s_copy, *startIP, *endIP; int parse_count = 0; int set_not_flag = 0; int item_not_flag; unsigned host, mask; PORTSET portset; s_copy = strdup(ipstr); if(!s_copy) return -2; if (*s_copy == '!') { set_not_flag = 1; s_copy++; } startIP = s_copy; while (startIP) { while (isspace((int)*startIP) || (*startIP == '[') ) { startIP++; } if ((*startIP == ']') || (*startIP == '\0')) break; /* if not found, endIP will be NULL */ endIP = strstr(startIP, ","); if (endIP) *endIP = '\0'; portset_init(&portset); if(ip4_parse(startIP, 0, &item_not_flag, &host, &mask, &portset) != 0) { free(s_copy); return -5; } if(ipset_add(ipset, &host, &mask, &portset, (item_not_flag ^ set_not_flag), IPV4_FAMILY) != 0) { free(s_copy); return -6; } parse_count++; if (endIP) endIP++; startIP = endIP; } free(s_copy); if (!parse_count) return -7; return 0; }
int ip4_setparse(IPSET *ipset, char *ipstr) { char *s_copy, *startIP, *endIP; int parse_count = 0; int set_not_flag = 0; int item_not_flag; unsigned host, mask; PORTSET portset; s_copy = strdup(ipstr); if(!s_copy) return -2; if (*s_copy == '!') { set_not_flag = 1; s_copy++; } startIP = s_copy; while (startIP) { while (isspace((int)*startIP) || (*startIP == '[') ) { startIP++; } if ((*startIP == ']') || (*startIP == '\0')) break; endIP = startIP; /* The following two loops and conditional address bug 30042 */ /* Traverse the IP */ while(isdigit((int)*endIP) || (*endIP == '.') || (*endIP == '/')) { endIP++; } /* Skip any whitespace after the IP or CIDR block */ while(isspace((int)*endIP) || (*endIP == '[') || (*endIP == ']')) { endIP++; } if(*endIP != ',' && *endIP) { FatalError("ip4_setparse: only commas are allowed as " "delimiters in the IP list: %s\n", ipstr); } portset_init(&portset); if(ip4_parse(startIP, 0, &item_not_flag, &host, &mask, &portset) != 0) { free(s_copy); return -5; } if(ipset_add(ipset, &host, &mask, &portset, (item_not_flag ^ set_not_flag), IPV4_FAMILY) != 0) { free(s_copy); return -6; } parse_count++; if(*endIP) { endIP++; } startIP = endIP; } free(s_copy); if (!parse_count) return -7; return 0; }