📄 ipobj.c
字号:
if( p->notflag ) printf("CIDR BLOCK: !%s / %s\n", ip_str,mask_str); else printf("CIDR BLOCK: %s / %s\n", ip_str,mask_str); } } else return -1; return 0;}/* parsing functions to help make life a bit easier *//** * Break an IP4 Address down into its components * * @param ipstr string to parse * @param use network order for return values (defaults to host order) * @param not_flag return value if the ip is negated * @param host ipv4 host argument * @param mask ipv4 mask argument * * @return 0 on sucess, else failure parsing the address * @retval -3 \0 encountered prematurely * @retval -2 strdup failed * @retval -1 null argument * @retval -4 out of range for CIDR notation */int ip4_parse(char *ipstr, int network_order, int *not_flag, unsigned *host, unsigned *mask){ char *saved, *s_copy, *maskptr; struct in_addr addrstuff; if(!ipstr || !not_flag || !host || !mask) return -1; if(*ipstr == '\0') return -3; saved = s_copy = strdup(ipstr); if(!s_copy) { return -2; } else { while(isspace((int)*s_copy)) s_copy++; if(*s_copy == '\0') { free(saved); return -3; } if(*s_copy == '!') { *not_flag = 1; s_copy++; if(*s_copy == '\0') { free(saved); return -3; } } else { *not_flag = 0; } maskptr = strstr(s_copy, "/"); if(!maskptr) { /* assume this is a host */ *mask = 0xFFFFFFFF; } else { *maskptr = '\0'; maskptr++; } if(!strcmp(s_copy, "0") || !strcmp(s_copy, "0.0.0.0")) { *host = 0; } else if((addrstuff.s_addr = inet_addr(s_copy)) == -1) { if(!strncmp(s_copy, "255.255.255.255", 15)) { addrstuff.s_addr = INADDR_BROADCAST; } else { /* invalid ip address! */ free(saved); return -3; } } else { *host = ntohl(addrstuff.s_addr); } if(maskptr) { if(maskptr == '\0') { /* /\0 is the representation */ free(saved); return -3; } if(strstr(maskptr, ".")) { if(!strcmp(maskptr, "0") || !strcmp(maskptr, "0.0.0.0")) { *mask = 0; } else if((addrstuff.s_addr = inet_addr(maskptr)) == -1) { if(!strncmp(maskptr, "255.255.255.255", 15)) { addrstuff.s_addr = INADDR_BROADCAST; } else { /* invalid ip address! */ free(saved); return -3; } } else { memcpy(mask, &addrstuff.s_addr, sizeof(unsigned)); } } else { int blocksize = atoi(maskptr); int i; if(blocksize == 0) { *mask = 0; } else if(blocksize < 1 || blocksize > 32) { free(saved); return -4; } else { *mask = 0; for(i=0;i<blocksize;i++) { (*mask) |= (1 << 31) >> i; } } } } } /* convert the arguments by default */ if(network_order) { *mask = htonl(*mask); *host = htonl(*host); } free(saved); return 0;}int ip4_setparse(IPSET *ipset, char *ipstr){ char *s_copy, *saved, *endp; int parse_count = 0; int set_not_flag = 0; int done = 0; if(!ipset || !ipstr) return -1; while(isspace((int)*ipstr) || (*ipstr == '[')) ipstr++; if(*ipstr == '\0') return -3; endp = saved = s_copy = strdup(ipstr); if(!s_copy) return -2; if(*s_copy == '!') set_not_flag = 1; /* global not flag for the set */ while(*s_copy != '\0' && !done) { unsigned host, mask; int item_not_flag; while((*endp != '\0') && (*endp != ',') && (*endp != ']')) { endp++; } switch(*endp) { case '\0': case ']': done = 1; /* last cases -- fall through */ case ',': if(*endp != '\0') { *endp = '\0'; } if(ip4_parse(s_copy, 0, &item_not_flag, &host, &mask) != 0) { free(saved); return -5; } if(ipset_add(ipset, &host, &mask, (item_not_flag ^ set_not_flag), IPV4_FAMILY) != 0) { free(saved); return -6; } else { endp++; s_copy = endp; parse_count++; } break; default: printf("ip4_setparse: unknown switch condition conditon: %c\n", *endp); exit(1); } } free(saved); if(!parse_count) return -7; return 0;}#ifdef MAIN_IP#include <time.h>#ifndef WIN32#define rand random#define srand srandom#endif#define MAXIP 100 #include "sflsq.c"void test_ip4_parsing(void){ unsigned host, mask, not_flag; char **curip; int ret; IPADDRESS *adp; char *ips[] = { "138.26.1.24", "1.1.1.1", "1.1.1.1/16", "1.1.1.1/255.255.255.255", "z/24", "0/0", "0.0.0.0/0.0.0.0", "0.0.0.0/0.0.2.0", NULL }; for(curip = ips; curip[0] != NULL; curip++) { /* network byte order stuff */ if((ret = ip4_parse(curip[0], 1, ¬_flag, &host, &mask)) != 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)) != 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;}void test_ip4set_parsing(void){ char **curip; int ret; IPADDRESS *adp; int not_flag; int host; int mask; char *ips[] = { "12.24.24.1/32,!24.24.24.1", "[0.0.0.0/0.0.2.0,241.242.241.22]", "138.26.1.24", "1.1.1.1", "1.1.1.1/16", "1.1.1.1/255.255.255.255", "z/24", "0/0", "0.0.0.0/0.0.0.0", "0.0.0.0/0.0.2.0", NULL }; for(curip = ips; curip[0] != NULL; curip++) { IPSET *ipset = ipset_new(IPV4_FAMILY); /* network byte order stuff */ if((ret = ip4_setparse(ipset, curip[0])) != 0) { ipset_free(ipset); fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret); } else { printf("-[%s]\n ", curip[0]); ipset_print(ipset); printf("---------------------\n "); } } return;}// -----------------------------void test_ip(){ int i,k; IPADDRESS * ipa[MAXIP]; unsigned ipaddress,ipx; unsigned short ipaddress6[8], ipx6[8]; printf("IPADDRESS testing\n"); srand( time(0) ); for(i=0;i<MAXIP;i++) { if( i % 2 ) { ipa[i]= ip_new(IPV4_FAMILY); ipaddress = rand() * rand(); ip_set( ipa[i], &ipaddress, IPV4_FAMILY ); if( !ip_equal(ipa[i],&ipaddress, IPV4_FAMILY ) ) printf("error with ip_equal\n"); ip_get( ipa[i], &ipx, IPV4_FAMILY ); if( ipx != ipaddress ) printf("error with ip_get\n"); } else { ipa[i]= ip_new(IPV6_FAMILY); for(k=0;k<8;k++) ipaddress6[k] = rand() % (1<<16); ip_set( ipa[i], ipaddress6, IPV6_FAMILY ); if( !ip_equal(ipa[i],&ipaddress6, IPV6_FAMILY ) ) printf("error with ip6_equal\n"); ip_get( ipa[i], ipx6, IPV6_FAMILY ); for(k=0;k<8;k++) if( ipx6[k] != ipaddress6[k] ) printf("error with ip6_get\n"); } printf("[%d] ",i); ip_fprint(stdout,ipa[i]); printf("\n"); } printf("IP testing completed\n");}// -----------------------------void test_ipset(){ int i,k; IPSET * ipset, * ipset6; IPSET * ipset_copyp, * ipset6_copyp; unsigned ipaddress, mask; unsigned short mask6[8]; unsigned short ipaddress6[8]; printf("IPSET testing\n"); ipset = ipset_new(IPV4_FAMILY); ipset6 = ipset_new(IPV6_FAMILY); srand( time(0) ); for(i=0;i<MAXIP;i++) { if( i % 2 ) { ipaddress = rand() * rand(); mask = 0xffffff00; ipset_add( ipset, &ipaddress, &mask, 0, IPV4_FAMILY ); //class C cidr blocks if( !ipset_contains( ipset, &ipaddress, IPV4_FAMILY ) ) printf("error with ipset_contains\n"); } else { for(k=0;k<8;k++) ipaddress6[k] = rand() % (1<<16); for(k=0;k<8;k++) mask6[k] = 0xffff; ipset_add( ipset6, ipaddress6, mask6, 0, IPV6_FAMILY ); if( !ipset_contains( ipset6, &ipaddress6, IPV6_FAMILY ) ) printf("error with ipset6_contains\n"); } } ipset_copyp = ipset_copy( ipset ); ipset6_copyp = ipset_copy( ipset6 ); printf("-----IP SET-----\n"); ipset_print( ipset ); printf("\n"); printf("-----IP SET6-----\n"); ipset_print( ipset6 ); printf("\n"); printf("-----IP SET COPY -----\n"); ipset_print( ipset_copyp ); printf("\n"); printf("-----IP SET6 COPY -----\n"); ipset_print( ipset6_copyp ); printf("\n"); printf("IP set testing completed\n");}// -----------------------------int main( int argc, char ** argv ){ printf("ipobj \n"); test_ip(); test_ipset(); test_ip4_parsing(); test_ip4set_parsing(); printf("normal pgm completion\n"); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -