|
|
1.1 ! root 1: #include <ctype.h> ! 2: #include <sys/inet/in.h> ! 3: #include <ipc.h> ! 4: ! 5: extern in_addr in_aton(); ! 6: ! 7: /* Get the next token in a string, returning a pointer the the byte ! 8: * following the token. ! 9: */ ! 10: char * ! 11: in_getw(buf, w) ! 12: char *buf, *w; ! 13: { ! 14: *w = 0; ! 15: while(isspace(*buf)) buf++; ! 16: if(*buf == '\0') ! 17: return(0); ! 18: while(!isspace(*buf) && *buf) ! 19: *w++ = *buf++; ! 20: *w = 0; ! 21: return(buf); ! 22: } ! 23: ! 24: /* get the network that a host is on */ ! 25: in_addr ! 26: in_netof(x) ! 27: in_addr x; ! 28: { ! 29: if(IN_CLASSC(x)) ! 30: return(x&IN_CLASSC_NET); ! 31: else if(IN_CLASSB(x)) ! 32: return(x&IN_CLASSB_NET); ! 33: else ! 34: return(x&IN_CLASSA_NET); ! 35: } ! 36: ! 37: /* ! 38: * get the subnetwork that a host is on. if the mask is not a superset ! 39: * the classmask, just use the classmask. ! 40: */ ! 41: in_addr ! 42: in_subnetof(x, mask) ! 43: in_addr x; ! 44: in_addr mask; ! 45: { ! 46: in_addr cmask; ! 47: ! 48: if(IN_CLASSC(x)) ! 49: cmask = IN_CLASSC_NET; ! 50: else if(IN_CLASSB(x)) ! 51: cmask = IN_CLASSB_NET; ! 52: else ! 53: cmask = IN_CLASSA_NET; ! 54: if((cmask&mask) != cmask) ! 55: return x & cmask; ! 56: else ! 57: return x & mask; ! 58: } ! 59: ! 60: /* ! 61: * get the ip address for `name' on same net as `naddr' ! 62: */ ! 63: in_addr ! 64: in_addronnet(name, naddr, mask) ! 65: char *name; ! 66: in_addr naddr; ! 67: in_addr mask; ! 68: { ! 69: in_addr taddr; ! 70: Qset *sp, *tsp; ! 71: Qtuple *tp; ! 72: ! 73: /* ! 74: * get the subnetwork naddr is on ! 75: */ ! 76: naddr = in_subnetof(naddr, mask); ! 77: ! 78: /* ! 79: * if `name' is numeric, don't call the name server ! 80: */ ! 81: if(*name >= '0' && *name <= '9'){ ! 82: taddr = in_aton(name); ! 83: if(in_subnetof(taddr, mask)!=naddr) ! 84: taddr = 0; ! 85: return taddr; ! 86: } ! 87: ! 88: /* ! 89: * ask name server for a set of possibilities ! 90: */ ! 91: sp = qset(name, (char *)0); ! 92: if (!sp) ! 93: return 0; ! 94: ! 95: /* ! 96: * Look for an inet address that matches the network. ! 97: * If none found return 0. ! 98: */ ! 99: for(tsp=sp; tsp; tsp=tsp->next) { ! 100: for(tp=tsp->this; tp; tp=tp->next) ! 101: if(tp->type && strcmp(tp->type, "in")==0){ ! 102: taddr = in_aton(tp->value); ! 103: if(in_subnetof(taddr, mask)==naddr){ ! 104: freeQset(sp); ! 105: return taddr; ! 106: } ! 107: } ! 108: } ! 109: freeQset(sp); ! 110: return 0; ! 111: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.