|
|
1.1 ! root 1: #include <sys/inet/in.h> ! 2: #include <ctype.h> ! 3: ! 4: /* ! 5: * Convert network-format internet address ! 6: * to base 256 d.d.d.d representation. ! 7: */ ! 8: char * ! 9: in_ntoa(in) ! 10: in_addr in; ! 11: { ! 12: register char *p; ! 13: static char b[18]; ! 14: #define UC(b) (((int)b)&0xff) ! 15: ! 16: /* ! 17: * number of network bytes depends on network class ! 18: */ ! 19: p = (char *)∈ ! 20: if(IN_CLASSC(in)) { ! 21: if((in & IN_CLASSC_HOST)==0){ ! 22: sprintf(b, "%d.%d.%d", UC(p[3]), UC(p[2]), UC(p[1])); ! 23: return b; ! 24: } ! 25: } else if(IN_CLASSB(in)) { ! 26: if((in & IN_CLASSB_HOST)==0){ ! 27: sprintf(b, "%d.%d", UC(p[3]), UC(p[2])); ! 28: return b; ! 29: } ! 30: } else { ! 31: if((in & IN_CLASSA_HOST)==0){ ! 32: sprintf(b, "%d", UC(p[3])); ! 33: return b; ! 34: } ! 35: } ! 36: sprintf(b, "%d.%d.%d.%d", UC(p[3]), UC(p[2]), UC(p[1]), UC(p[0])); ! 37: return b; ! 38: } ! 39: ! 40: /* ! 41: * Convert base 256 d.d.d.d representation ! 42: * to network-format internet address. ! 43: */ ! 44: in_addr ! 45: in_aton(s) ! 46: register char *s; ! 47: { ! 48: register int b; ! 49: register int x, i; ! 50: in_addr h, n; ! 51: ! 52: /* ! 53: * first bytee determines class of network ! 54: */ ! 55: for(b = 0; *s; s++){ ! 56: if(*s=='.'){ ! 57: s++; ! 58: break; ! 59: } else if(isdigit(*s)) { ! 60: b = b*10 + (*s - '0'); ! 61: } else ! 62: return INADDR_ANY; ! 63: } ! 64: ! 65: /* ! 66: * number of network bytes depends on network class ! 67: */ ! 68: n = b<<24; ! 69: if(IN_CLASSC(n)) ! 70: x = 0; /* 2 more net bytes */ ! 71: else if(IN_CLASSB(n)) ! 72: x = 1; /* 1 more net bytes */ ! 73: else ! 74: x = 2; /* no more net bytes */ ! 75: ! 76: /* ! 77: * get network bytes ! 78: */ ! 79: for(i = 2; i>x; i--){ ! 80: for(b = 0; *s; s++){ ! 81: if(*s=='.'){ ! 82: s++; ! 83: break; ! 84: } ! 85: b = b*10 + (*s - '0'); ! 86: } ! 87: n |= b<<(8*i); ! 88: } ! 89: ! 90: /* ! 91: * get host bytes ! 92: */ ! 93: h = 0; ! 94: while(*s){ ! 95: for(b = 0; *s; s++){ ! 96: if(*s=='.'){ ! 97: s++; ! 98: break; ! 99: } ! 100: b = b*10 + (*s - '0'); ! 101: } ! 102: h = (h<<8) + b; ! 103: } ! 104: ! 105: return h | n; ! 106: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.