📄 ip_test.c
字号:
#include <stdio.h>#include <math.h>int IpFt; //the first byte of the subnet's ipint IsLegal(char * const s){ char *p = s, c; int err = 0, dot = 1, i = 3; while((c = *p++) && !err) { if (c == '.') { if (dot) { err++; break; } //ipaddr->uip.part[i] = part; dot++, i--; } else if (isdigit(c)) dot = 0; else err++; } if (i || *(--p, --p) == '.') err++; return !err;}int Ip_addr_test(char *addr, long int *ip) //test IP whether it is legal or not{ int i, num = 0; // num:the number of the char '.' char *p = addr; if(IsLegal(addr)){ int j, k = 3; while(p != NULL) { j = atoi(p); if(j <= 255 && j >= 0){ // ip address <= 255 and >= 0 if(*ip == 0) IpFt = j; *ip = *ip + j * (1 << (k*8)); // printf("ip: %d.", *ip); } else return -2; p = strchr(p, '.'); if( p != NULL) p++; k--; } return 0; }else return -1;}int Subnet_test(char* subnet, long int* l_subnet, int* i_mask){ int i, flag = 0, ipflag = 0; char * p = subnet; for(i = 0; i < strlen(subnet); i++){ if(subnet[i] == '/') flag++; else if(subnet[i] != '.' && (subnet[i] < '0' || subnet[i] > '9' )) return -1; } if(flag == 1){ p = strchr( p, '/' ); *p = '\0'; p++; *i_mask = atoi(p); if(Ip_addr_test(subnet, l_subnet)) return -1; }else if(flag == 0) return -2; else return -1; return 0;} int Ip_Addr_Type(){//A type return 1, b type return 2, c type return 3 if(IpFt >= 1 && IpFt <= 126) return 1; else if(IpFt >= 127 && IpFt <= 191) return 2; else if(IpFt >= 192 && IpFt <= 223) return 3; else return 4;}int TestMask(long int sn, int m, int *mflag, int *snflag){ /* return -1:ip address type is D, E mflag signs which mask is legal; sn sings which subnet is legal */ int HIpNum = 32 - m; long int n; //printf(" ipft= %d\n type = %d\n",IpFt, Ip_Addr_Type()); switch(Ip_Addr_Type()){ case 1: if(m < 8 || m > 32) *mflag = 1; break; case 2: if(m < 16 || m > 32) *mflag = 1; break; case 3: if(m < 24 || m > 32) *mflag = 1; break; defalut: printf("D. E type IP address!!\n"); return -1; } if(*mflag == 0){ n = (1 << (32 - m )) - 1; //printf("m = %d, n = %08x\n", m , sn); if((sn & n) != 0) *snflag = 1; } //printf("mflag = %d, snflag = %d\n", *mflag, *snflag); return 0; }int Is_subnet(long int ip, long int sn, int m){ int HostIpNum; long int n = -1; HostIpNum = 32 - m; n = (n << HostIpNum) & ip; if(n == sn) return 1; else return 0;}int main(int argc, char **argv){ long int l_ip = 0, l_subnet = 0; int i_mask; int mflag = 0, snflag = 0, flag; // mflag is mask 's check flag; snflag is subnet's check flag if(argc <= 2){ printf("Error:para is too few!\n"); return -1; } flag = Ip_addr_test(argv[2], &l_ip); // test ip /* < -- test ip --> */ if(flag == -1){ printf("IP adress ERROR: IP includes too much dot or doesn't only include digits\n"); return -1; }else if(flag == -2){ printf("IP adress ERROR:the ip adress > 255 or < 0!!\n"); return -1; }else printf("the ip adress is right!!\n"); /* < -- test subnet and mask --> */ if((flag = Subnet_test(argv[1], &l_subnet, &i_mask)) == -1){ //detach and test subnet printf("SUBNET ERROR: IP includes too much dot or doesn't only include digits!\n"); return -1; } else if(flag == -2){ printf("SUBNET ERROR:lack of mask\n"); return -1; } if(! TestMask(l_subnet, i_mask, &mflag, &snflag)){ // test Mask code if(mflag == 1){ printf("MASK ERROR:mask is wrong!\n"); return -1; } if(snflag == 1){ printf("SUBNET ERROR:the subnet doesn't exist!!\n"); return -1; } printf("subnet is legal!!\n"); } /* ip belongs to subnet */ if(Is_subnet(l_ip, l_subnet, i_mask)) printf("\n%s belongs to %s/%d\n", argv[2], argv[1], i_mask); else printf("\n%s doesn't belog to %s/%d\n", argv[2], argv[1], i_mask); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -