1: /* 2: * This file implements functions for dealing with names in the XNS 3: * environment. 4: */ 5: 6: /* 7: $Log: names.c,v $ 8: * Revision 2.0 85/11/21 07:22:14 jqj 9: * 4.3BSD standard release 10: * 11: * Revision 1.2 85/10/21 12:58:41 root 12: * Gould version: eliminate various uses of ns_netof(), which doesn't 13: * work on Gould. 14: * 15: * Revision 1.1 85/10/17 10:13:45 bill 16: * Initial revision 17: * 18: * Revision 1.3 85/03/11 16:37:13 jqj 19: * Public alpha-test version, released 11 March 1985 20: * 21: * Revision 1.2 85/01/27 07:37:32 jqj 22: * finished but undebugged version 23: * 24: */ 25: 26: #ifndef lint 27: static char rcsid[] = "$Header: names.c,v 2.0 85/11/21 07:22:14 jqj Exp $"; 28: #endif 29: 30: #include <stdio.h> 31: #include <sys/types.h> /* for ns.h and socket.h */ 32: #include <sys/socket.h> /* for ns.h */ 33: #include <netns/ns.h> /* for XNS addresses */ 34: #include <netdb.h> 35: 36: 37: 38: /*ARGSUSED*/ 39: struct ns_addr * 40: ClearinghouseLookup(name,field) 41: char *name, *field; 42: { 43: /* not yet implemented */ 44: return(NULL); 45: } 46: 47: struct ns_addr * 48: getXNSaddr(name) 49: char *name; 50: { 51: static struct ns_addr addr; 52: long net,net1; 53: int i; 54: int hb[6]; 55: u_short socket; 56: char *netname, *hostname, *socketname; 57: extern char *index(); 58: 59: netname = name; 60: if (NULL == (hostname = index(name,'#'))) { 61: /* no # means just a host name */ 62: netname = "0"; hostname = name; socketname = "0"; 63: } 64: else if (NULL == (socketname = index(++hostname,'#'))) 65: /* one # means net#host */ 66: socketname = "0"; 67: else 68: /* two # means net#host#socket */ 69: socketname += 1; 70: 71: *(u_long *)&addr.x_net = 0; 72: for (i = 0; i < 6; i++) 73: addr.x_host.c_host[i] = (char) 0; 74: addr.x_port = 0; 75: /* 76: * first try 2-273#2-852-151-014#socket 77: */ 78: if (1 < (i = sscanf(hostname,"%d-%d-%d-%d-%d#", 79: &hb[0], &hb[1], &hb[2], &hb[3], &hb[4]))) { 80: cvtbase(1000,256,hb,i,addr.x_host.c_host,6); 81: i = sscanf(netname,"%ld-%ld#", &net, &net1); 82: if (i > 1) 83: net = net*1000+net1; 84: i = sscanf(socketname,"%hd", &socket); 85: *(u_long *)&addr.x_net = htonl(net); 86: addr.x_port = htons(socket); 87: return(&addr); 88: } 89: /* 90: * try form 8E1#0.0.AA.0.5E.E6#socket 91: */ 92: else if (1 < (i = sscanf(hostname,"%x.%x.%x.%x.%x.%x", 93: &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { 94: cvtbase(256,256,hb,i,addr.x_host.c_host,6); 95: i = sscanf(netname,"%lx", &net); 96: i = sscanf(socketname,"%hx", &socket); 97: *(u_long *)&addr.x_net = htonl(net); 98: addr.x_port = htons(socket); 99: return(&addr); 100: } 101: /* code for alternate forms here */ 102: return(NULL); /* no match */ 103: } 104: 105: static 106: cvtbase(oldbase,newbase,input,inlen,result,reslen) 107: int oldbase, newbase; 108: int input[]; 109: int inlen; 110: unsigned char result[]; 111: int reslen; 112: { 113: int d, e; 114: long sum; 115: 116: e = 1; 117: while (e > 0 && reslen > 0) { 118: d = 0; e = 0; sum = 0; 119: /* long division: input=input/newbase */ 120: while (d < inlen) { 121: sum = sum*oldbase + (long) input[d]; 122: e += (sum > 0); 123: input[d++] = sum / newbase; 124: sum %= newbase; 125: } 126: result[--reslen] = sum; /* accumulate remainder */ 127: } 128: for (d=0; d < reslen; d++) 129: result[d] = 0; 130: } 131: 132: 133: /*ARGSUSED*/ 134: struct hostent * 135: getXNShostbyname (name) 136: char *name; 137: { 138: /* can't use gethostent() since only internet addresses are understood */ 139: return(NULL); 140: } 141: 142: 143: 144: struct ns_addr * 145: CourierName( name ) 146: char *name; 147: { 148: struct hostent *haddr; 149: struct ns_addr *paddr; 150: 151: /* first, try an explicit address */ 152: /* like 3#1.2.3.4.5.6#5 */ 153: if ((paddr = getXNSaddr(name)) != NULL) 154: return(paddr); 155: /* second, try a local cache lookup */ 156: if ((haddr = getXNShostbyname(name)) != NULL) { 157: return((struct ns_addr*) haddr->h_addr); 158: } 159: /* finally, try a nonlocal lookup */ 160: return( ClearinghouseLookup(name,(char*)NULL) ); 161: }