📄 nameaddr.c
字号:
callbuf.addr.maxlen = netbufp->maxlen; callbuf.opt.buf = NULL; callbuf.opt.len = 0; callbuf.opt.maxlen = 0; callbuf.udata.buf = NULL; callbuf.udata.len = 0; callbuf.udata.maxlen = 0; if ((fd = t_open(clonedev, O_RDWR, NULL)) < 0) { fprintf(stderr, "Cannot open %s\n", clonedev); return(-1); } #ifdef DEBUG fprintf(stderr, "Connecting to <%s> through device %s\n", callbuf.addr.buf, clonedev);#endif if(t_bind(fd, NULL, NULL) < 0) { t_error("t_bind failed"); t_close(fd); return(-1); } if(t_connect(fd, &callbuf, NULL) < 0) { t_error("t_connect failed"); checkNewEvent(fd); t_close(fd); return(-1); } return(fd);}/*extern char *calloc(), *realloc();*/extern char *program;static int network;static int nextentry;static char *makePacket();static char *staticalloc();static char *TheEnd;static char *inbuf;static int inlen;static int nhosts;static int nHosts;static int flags = 0;static struct netconfig *netconfigp = NULL; static intCallTheNameServer(service, nettype, arg1, arg2, arg3) int service; struct netconfig *nettype; char **arg1, **arg2; int *arg3;{ int n,m, len; char *ptr, *net; int *iptr; flags = service; netconfigp = nettype; ptr = *arg1; iptr = (int *) ptr; inlen = iptr[0];#ifdef DEBUGfprintf(stderr,"inlen = %d\n", inlen);#endif ptr += sizeof(int); nhosts = iptr[1];#ifdef DEBUGfprintf(stderr,"nhosts = %d\n", nhosts);#endif inbuf = ptr + sizeof(int); TheEnd = &inbuf[inlen];#ifdef DEBUG write(2, inbuf, inlen);#endif nextentry = ((xHostEntry *) inbuf)->length; *arg2 = (char *) makePacket(&len); if(arg3 != NULL) *arg3 = nHosts;#ifdef DEBUGfprintf(stderr, "CallTheNameserver return %d\n", len);#endif return(len);}static int bufsize = 512;static char *getnextentry();static struct nd_addrlist *GetHostServiceByName(host, dispno) char *host; int dispno;{ struct nd_hostserv nd_hostserv; struct nd_addrlist *nd_addrlistp = NULL; struct netbuf *netbufp = NULL; char service[MAX_AUTO_BUF_LEN]; sprintf(service , "xserver%d", dispno); nd_hostserv.h_host = host; nd_hostserv.h_serv = service; if(netdir_getbyname(netconfigp, &nd_hostserv, &nd_addrlistp) == 0) return(nd_addrlistp); else return(NULL); }static intConvertName(pktptr, n, entry, len) char **pktptr, *entry; int n, len;{ struct hostent *hp; unsigned long address; int port; char *ptr; int rndlen; struct nd_addrlist *nd_addrlistp = NULL; struct netbuf *netbufp; char *addr;#ifdef DEBUG fprintf(stderr, "in ConvertName %s\n", entry);#endif if((nd_addrlistp = GetHostServiceByName(entry, atoi(dispno))) == NULL) return(n); netbufp = nd_addrlistp->n_addrs; /* the netbufs */ /* xhost needs only the last four bytes of the address over TCP/IP. */ if( strcmp(netconfigp->nc_protofmly, "inet") == 0 && strcmp(netconfigp->nc_proto , "tcp") == 0 ){ addr = &netbufp->buf[4]; len = 4; family = 1; } else { family = 0; addr = netbufp->buf; len = netbufp->len; } rndlen = ((sizeof(xHostEntry) + len + 3) >> 2) << 2; if((*pktptr = staticalloc(*pktptr, n+rndlen)) == NULL) { (void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST); return(-1); } ptr = &(*pktptr)[n]; ((xHostEntry *)ptr)->family = family; ((xHostEntry *)ptr)->length = len; ptr += sizeof(xHostEntry); memcpy(ptr, addr, len); netdir_free((char *)nd_addrlistp, ND_ADDRLIST); #ifdef DEBUG ptr[len] = '\0'; fprintf(stderr, "creating address for host %s address<%d>\n", entry, ptr);#endif return(n+rndlen);}static struct nd_hostservlist *GetHostServiceByAddr(addr, len) char *addr; int len;{ struct nd_hostservlist *nd_hostservlist; struct netbuf netbuf; netbuf.buf = addr; netbuf.len = len; netbuf.maxlen = len; if(netdir_getbyaddr(netconfigp, &nd_hostservlist, &netbuf) == 0) return(nd_hostservlist); else return(NULL);}static intConvertCallToName(pktptr, n, entry, len) char **pktptr, *entry; int n, len;{ int l, rl; char *ptr; struct nd_hostservlist *nd_hostservlist; if((nd_hostservlist = GetHostServiceByAddr(entry, len)) == NULL) return(n); l = strlen(nd_hostservlist->h_hostservs->h_host); rl = ((sizeof(xHostEntry) + l + 3) >> 2) << 2; if((*pktptr = staticalloc(*pktptr, n+rl)) == NULL) { (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST); return(-1); } ptr = &(*pktptr)[n]; ((xHostEntry *)ptr)->family = 0; ((xHostEntry *)ptr)->length = l; ptr += sizeof(xHostEntry); sprintf(ptr, nd_hostservlist->h_hostservs->h_host); (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST); #ifdef DEBUG fprintf(stderr, "getting the name for host %s\n", ptr);#endif return(rl+n);}static intConvertAddress(pktptr, n, entry, len) char **pktptr, *entry; int n, len;{ register i; char *ptr; int l, rl; struct nd_hostservlist *nd_hostservlist; char *name; char tcpaddr[8], *addr; char addrbuf[MAX_AUTO_BUF_LEN]; #ifdef DEBUG entry[len] = '\0'; fprintf(stderr, "Converting address %s in %s format\n", entry, netconfigp->nc_netid);#endif if( strcmp(netconfigp->nc_protofmly, "inet") == 0 && strcmp(netconfigp->nc_proto , "tcp") == 0 ){ addr = tcpaddr; memcpy(tcpaddr, tcphalf, 4); memcpy(&tcpaddr[4], entry, 4); len = 8;#ifdef DEBUG fprintf(stderr, "port %d, family %d\n", *(short *) &tcpaddr[2], *(short *) tcpaddr);#endif } else { addr = entry; } if((nd_hostservlist = GetHostServiceByAddr(addr, len)) == NULL) { int i; for(i=0; i< len; i++) if(entry[i] < ' ' || entry[i] > 0177) break; if(i < len) { sprintf(addrbuf, "%d", *(int *) entry); name = addrbuf; len = strlen(name); } else name = entry; entry[len] = '\0'; l = len + 1; } else { name = nd_hostservlist->h_hostservs->h_host; l = strlen(name) +1; } rl = ((sizeof(xHostEntry) + l + 3) >> 2) << 2; if((*pktptr = staticalloc(*pktptr, n+rl)) == NULL) { if(nd_hostservlist != NULL) (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST); return(-1); } ptr = &(*pktptr)[n]; ((xHostEntry *)ptr)->family = 0; ((xHostEntry *)ptr)->length = l; ptr += sizeof(xHostEntry); memcpy(ptr, name, l); if(nd_hostservlist != NULL) (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST); #ifdef DEBUG fprintf(stderr, "getting the name for host %s\n", name);#endif return(n+rl);}static char *getnextentry(plen) int *plen;{ char *ptr; int n = nextentry; #ifdef DEBUG fprintf(stderr,"In getnextentry()\n");#endif if(inbuf >= TheEnd) {#ifdef DEBUG fprintf(stderr,"In getnextentry() end of buffer\n");#endif *plen = -1; return(NULL); } *plen = nextentry; family = ((xHostEntry *) inbuf)->family; ptr = inbuf + sizeof(xHostEntry); inbuf += ((sizeof(xHostEntry) + *plen + 3) >> 2) << 2; nextentry = ((xHostEntry *) inbuf)->length; ptr[*plen] = '\0'; return(ptr);}static char *makePacket(plen) int *plen;{ char *pktptr = NULL, *ptr; int len; int n = 0, m; #ifdef DEBUG fprintf(stderr,"In makePacket()\n");#endif for(nHosts = 0; nHosts < nhosts;) { ptr = getnextentry(&len); if(len < 0) break; if(len == 0 || ptr == NULL) continue; m = addentry(&pktptr, n, ptr, len); if(m > n){ nHosts++; n = m; } }#ifdef DEBUG fprintf(stderr, "packet size is %d\n", n);#endif *plen = n; return(pktptr);}static intaddentry(pktptr, n, entry, len) char **pktptr, *entry; int n, len;{ #ifdef DEBUG fprintf(stderr, "in addEntry %s\n", entry);#endif switch(flags) { case ConvertNameToNetAddr: return(ConvertName(pktptr, n, entry, len)); case ConvertNetAddrToName: return(ConvertAddress(pktptr, n, entry, len)); case ConvertTliCallToName: return(ConvertCallToName(pktptr, n, entry, len)); } return(-1);}static char *staticalloc(ptr, size) char *ptr; int size;{ if(ptr == NULL) { if(bufsize < size) bufsize = size; ptr = malloc(bufsize); } if(bufsize < size) { bufsize = size + 512; ptr = realloc(ptr, bufsize); } return(ptr);}static intOpenVirtualCircuit(lfd) int lfd;{ char *clonedev; int fd; clonedev = Network._net[_XsTypeOfStream[lfd]]->nc_device; if ((fd = t_open(clonedev, O_RDWR, NULL)) < 0) { fprintf(stderr, "Cannot open %s\n", clonedev); return(-1); } if(t_bind(fd, NULL, NULL) < 0) { t_error("t_bind failed"); t_close(fd); return(-1); } return(fd);}#endif /* XSTREAMS_COMPILE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -