1 /* $Id: nameaddr.c 246 2007-05-12 01:03:53Z jon $ */
2 /* $NCDId: @(#)nameaddr.c,v 1.2 1994/05/02 17:42:06 greg Exp $ */
3 /* $XConsortium: nameaddr.c,v 1.5 91/09/12 13:41:04 rws Exp $ */
4 /* nameaddr.c - included by Astreams.c */
5 /* Used for System V Release 4.0 networking code */
6
7 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
8 * Copyright 1991 Massachusetts Institute of Technology
9 * Copyright 1988, 1989 AT&T, Inc.
10 *
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby
13 * granted, provided that the above copyright notice appear in all
14 * copies and that both that copyright notice and this permission
15 * notice appear in supporting documentation, and that the name of
16 * AT&T, USL, or MIT not be used in advertising or publicity
17 * pertaining to distribution of the software without specific,
18 * written prior permission. AT&T, USL, and MIT make no
19 * representations about the suitability of this software for any
20 * purpose. It is provided "as is" without express or implied
21 * warranty.
22 *
23 * AT&T, USL, AND MIT DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
25 * NO EVENT SHALL AT&T, USL, OR MIT BE LIABLE FOR ANY SPECIAL,
26 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
27 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
28 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 */
31
32 #ifdef AuSTREAMS_COMPILE /* magic symbol to avoid lint problems */
33
34 static void *handlep = NULL;
35 static int family = 0;
36 static char tcphalf[4];
37 static int *tcpfamilyp = NULL;
38 static char **tcpremoteaddrp = NULL;
39 static int *tcpremoteaddrlenp = NULL;
40
41 static int SetupNetworkStream();
42 static int BindAndListen();
43 static int BindAndConnect();
44 static int addentry();
45
46 /* Routines for handling TLI streams */
47 static int
InitializeNetPath(void)48 InitializeNetPath(void)
49 {
50 if(handlep == NULL && (handlep = setnetpath()) == NULL)
51 {
52 nc_perror("Cannot set network selection path\n");
53 return(-1);
54 }
55 return(1);
56
57 }
58
_AusSetupTliStream(char * display,void * dummy)59 _AusSetupTliStream(
60 char *display,
61 void *dummy
62 )
63 {
64 int i, n;
65 int fd, type;
66 struct utsname machine;
67 struct listenCall *tmp;
68 int nameserver();
69 static int firstime=1;
70
71 PRMSG("Calling SetupTliStream()\n",0,0);
72
73 if(InitializeNetPath() < 0)
74 return(-1);
75
76 dispno = display;
77
78 if(uname(&machine) < 0){
79 perror("Cannot get nodename");
80 return(-2);
81 }
82
83 bind_req.addr.buf = req_buf;
84
85 /* pcc */
86 if (firstime) {
87 Network._nnets = Au_TLI_STREAM;
88 firstime = 0;
89 }
90
91 /* type = Network._nnets; */
92
93 bind_ret.addr.buf = ret_buf;
94 call.addr.buf = call_buf;
95 bind_req.addr.maxlen = MAXLEN;
96 bind_ret.addr.maxlen = MAXLEN;
97 call.addr.maxlen = MAXLEN;
98
99 fd = SetupNetworkStream(
100 machine.nodename,
101 atoi(display),
102 BindAndListen,
103 &type,
104 handlep
105 );
106
107 if( fd < 0)
108 {
109 PRMSG("SetupNetworkStream failed\n",0,0);
110 return(-1);
111 }
112
113 _AusTypeOfStream[fd] = type;
114
115 /*
116 * set up call save list for general network listen service
117 */
118 for (i = 0; i < LISTEN_QUE_SIZE; ++i)
119 {
120 if((tmp = (struct listenCall *) malloc(sizeof(struct listenCall))) == NULL)
121 {
122 PRMSG( "malloc failed\n",0,0);
123 exit(1);
124 }
125 if((tmp->CurrentCall = (struct t_call *) t_alloc(fd,T_CALL,T_ALL)) == NULL)
126 {
127 PRMSG( "t_alloc failed\n",0,0);
128 exit(1);
129 }
130 Que(&Network.FreeList[type], tmp, CLEAR);
131 }
132
133 if(Network._npeers > 0 && Network._peer == NULL)
134 {
135 register i;
136 register char *ptr;
137 int n;
138
139 n = (Network._npeers + 1) *
140 (sizeof(int) + sizeof(char *) + (1 + UNAME_LENGTH));
141
142 PRMSG("Allocating %d chars for %d peeers names",
143 n, Network._npeers);
144
145 if((ptr = malloc(n)) == NULL){
146 fprintf(stderr,"Cannot malloc space for peers names\n");
147 exit(1);
148 }
149
150
151 Network._peerlen = (int *) ptr;
152 ptr += Network._npeers * sizeof(int);
153 Network._peer = (char **) ptr;
154 ptr += Network._npeers * sizeof(char *);
155 for(i= 0; i< Network._npeers; i++)
156 {
157 Network._peerlen[i] = 0;
158 Network._peer[i] = ptr;
159 #ifdef DEBUG
160 /*
161 fprintf(stderr, "peer[%d] is %u; peerlen[%d] is %u\n",
162 i, Network._peer[i], i, &Network._peerlen[i]);
163 */
164 #endif
165 ptr += (1 + UNAME_LENGTH);
166 }
167 }
168 PRMSG("SetupTliStream () (success) fd = %d\n", fd,0);
169 return(fd);
170 }
171
172 int
_AusCallTliServer(char * host,int idisplay)173 _AusCallTliServer(
174 char *host,
175 int idisplay
176 )
177 {
178 int fd, type;
179 int nameserver ();
180 static char firsttime = 1;
181
182 PRMSG("Calling CallTliServer()\n",0,0);
183
184 if(firsttime == 1)
185 {
186 firsttime = 0;
187 SetupNetworkInfo();
188 Network._nnets = Au_TLI_STREAM;
189 }
190
191 sprintf(_dispno, "%d", idisplay);
192 dispno = _dispno;
193
194
195 if((fd = SetupNetworkStream(
196 host,
197 idisplay,
198 BindAndConnect,
199 &type,
200 handlep)) < 0)
201 {
202 PRMSG("SetupNetworkStream failed\n",0,0);
203 return(-1);
204 }
205
206 if(ioctl(fd, I_POP, "timod") < 0)
207 {
208 PRMSG("failed to pop timod\n",0,0);
209 }
210 if(ioctl(fd, I_PUSH, "tirdwr") < 0)
211 {
212 t_close(fd);
213 return(-1);
214 }
215
216 PRMSG("A Connection has been established to %s ... \n", host,0);
217 _AusTypeOfStream[fd] = type;
218 if (_AusInputBuffer[fd].DataBuffer == NULL)
219 if ((_AusInputBuffer[fd].DataBuffer = (char *) malloc(BUFFERSIZE)) == NULL)
220 {
221 errno = ENOMEM;
222 perror("Client can't connect to remote server");
223 return (-1);
224 }
225 _AusInputBuffer[fd].LastBytePtr = 0;
226 _AusInputBuffer[fd].FirstBytePtr = 0;
227 PRMSG("CallTliServer() returns success\n",0,0);
228
229 return(fd);
230 }
231
232
_AuMakeStreamsConnection(char * name,int * iserverp,AuBool xname,int retries,int * familyp,int * serveraddrlenp,char ** serveraddrp)233 int _AuMakeStreamsConnection (
234 char *name,
235 int *iserverp, /* can be RETURN */
236 AuBool xname,
237 int retries,
238 int *familyp, /* return */
239 int *serveraddrlenp, /* return */
240 char **serveraddrp /* return */
241 )
242 {
243 char *procname = "Aulib/_AuMakeStreamsConnection";
244 struct utsname machine;
245 int fd;
246 int idisplay;
247
248 PRMSG("GetConnectionType(%s)\n", name, 0);
249
250 if(uname(&machine) < 0){
251 perror("Cannot get nodename");
252 return(-1);
253 }
254 if(
255 name == NULL ||
256 strcmp(name, "") == 0 ||
257 strcmp(name, "unix") == 0 ||
258 strcmp(name, "local") == 0 ||
259 strcmp(name, machine.nodename) == 0
260 )
261 {
262 fd = ((*_AusStream[Au_NAMED_STREAM].CallTheListener)
263 ("unix", *iserverp, "local"));
264
265 if (fd >= 0) {
266 *familyp = FamilyLocal;
267 *serveraddrlenp = strlen (machine.nodename);
268 *serveraddrp = (char *) Aumalloc ((*serveraddrlenp) + 1);
269 if (!*serveraddrp) {
270 *serveraddrlenp = 0;
271 } else {
272 strcpy (*serveraddrp, machine.nodename);
273 }
274 }
275 return fd;
276 }
277
278 if((handlep = setnetpath()) == NULL)
279 {
280 nc_perror("Cannot set network selection path\n");
281 return(-1);
282 }
283 /* For backward compatibility, we have to pass authorization
284 data in global variables. Ugh. */
285 tcpfamilyp = familyp;
286 tcpremoteaddrp = serveraddrp;
287 tcpremoteaddrlenp = serveraddrlenp;
288
289 if (xname) /* name came from X */
290 *iserverp += AU_DEFAULT_TCP_PORT;
291 fd = (*_AusStream[Au_TLI_STREAM].CallTheListener)(name, *iserverp);
292 return(fd);
293 }
294
295
296 static int
SetupNetworkStream(char * host,int dispno,int (* action)(int,int),int * typtr,void * handlep)297 SetupNetworkStream(
298 char *host,
299 int dispno,
300 int (*action)(int, int), /* JET - check this */
301 int *typtr,
302 void *handlep
303 )
304 {
305 int i;
306 char service[MAX_AUTO_BUF_LEN];
307 int fd, type;
308 struct nd_hostserv nd_hostserv;
309 struct netconfig *netconfigp = NULL;
310 struct nd_addrlist *nd_addrlistp = NULL;
311 struct netbuf *netbufp = NULL;
312
313
314 #ifdef DEBUG
315 fprintf(stderr, "Calling SetupNetworkStream(%s, %d)\n", host, dispno);
316 #endif
317
318 sprintf(service , "auserver%d", dispno);
319 nd_hostserv.h_host = host;
320 nd_hostserv.h_serv = service;
321 #ifdef DEBUG
322 fprintf(stderr, "Trying to get the binding address for service %s on %s\n",
323 service, host);
324 #endif
325 while((netconfigp = getnetpath(handlep)) != NULL)
326 {
327 #ifdef DEBUG
328 fprintf(stderr, "Trying to bind using %s\n", netconfigp->nc_device);
329 #endif
330 if(netdir_getbyname(netconfigp, &nd_hostserv, &nd_addrlistp) == 0)
331 {
332
333 #ifdef DEBUG
334 fprintf(stderr, "There are %d ways\n", nd_addrlistp->n_cnt);
335 #endif
336 netbufp = nd_addrlistp->n_addrs;
337 for(i=0; i< nd_addrlistp->n_cnt; i++)
338 {
339
340 #ifdef DEBUG
341 fprintf(stderr, "Address: len %d maxlen %d \n",
342 netbufp->len, netbufp->maxlen);
343 #endif
344 if( strcmp(netconfigp->nc_netid, "starlan") == 0 )
345 {
346 char *from, *to;
347 int i, len;
348
349 from = to = netbufp->buf;
350 len = 0;
351 for(i=0; i< netbufp->len; i++, from++)
352 if(*from != '.')
353 {
354 *to++ = *from;
355 len++;
356 }
357 *to = '\0';
358 netbufp->len = len;
359 }
360
361 #ifdef DEBUG
362 fprintf(stderr, "Address: maxlen %d buf ", netbufp->maxlen);
363 dumpBytes(netbufp->len, netbufp->buf);
364 #endif
365 if((fd = (*action)(netconfigp->nc_device, netbufp)) < 0)
366 {
367 netbufp++;
368 continue;
369 }
370 if(
371 strcmp(netconfigp->nc_protofmly, "inet") == 0 &&
372 strcmp(netconfigp->nc_proto , "tcp") == 0
373 )
374 {
375 memcpy(tcphalf, netbufp->buf, 4);
376 if (tcpfamilyp != NULL) {
377 *tcpfamilyp = AuNetworkInternet;
378 *tcpremoteaddrlenp = 4;
379 *tcpremoteaddrp = Aumalloc(*tcpremoteaddrlenp);
380 /* This is a kludge. What is the right way to get
381 this info out? */
382 memcpy(*tcpremoteaddrp, netbufp->buf+4,
383 *tcpremoteaddrlenp);
384 #ifdef DEBUG
385 fprintf(stderr, "tcp remote addr = %0x\n",
386 *(long *)*tcpremoteaddrp);
387 #endif
388 }
389 }
390 type = 0;
391 for(i=Au_TLI_STREAM; i< Network._nnets; i++)
392 if(strcmp(Network._net[i]->nc_netid, netconfigp->nc_netid) == 0)
393 {
394 type = i;
395 break;
396 }
397 if(type == 0)
398 {
399 Network._net[Network._nnets] = netconfigp;
400 type = Network._nnets++;
401 }
402 *typtr = type;
403 /* free(netconfigp) the right way */
404 (void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST);
405
406 return(fd);
407 }
408 /* free(nd_addrlistp) the right way */
409 (void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST);
410 }
411 #ifdef DEBUG
412 else netdir_perror("netdir_getbyname() failed");
413 #endif
414 }
415 return(-1);
416 }
417
418
419 static int
BindAndListen(char * clonedev,struct netbuf * netbufp)420 BindAndListen(
421 char *clonedev,
422 struct netbuf *netbufp
423 )
424 {
425 int fd;
426 struct t_bind bindbuf;
427
428
429 bindbuf.addr.buf = netbufp->buf;
430 bindbuf.addr.len = netbufp->len;
431 bindbuf.addr.maxlen = netbufp->maxlen;
432
433
434 if ((fd = t_open(clonedev, O_RDWR, NULL)) < 0)
435 {
436 fprintf(stderr, "Cannot open %s\n", clonedev);
437 return(-1);
438 }
439 #ifdef DEBUG
440 fprintf(stderr, "opening device %s\n", clonedev);
441 #endif
442
443 bindbuf.qlen = 8;
444 if(t_bind (fd, &bindbuf, NULL) < 0)
445 {
446 t_error("t_bind failed");
447 close(fd);
448 return(-1);
449 }
450 return(fd);
451 }
452
453 static int
BindAndConnect(char * clonedev,struct netbuf * netbufp)454 BindAndConnect(
455 char *clonedev,
456 struct netbuf *netbufp
457 )
458 {
459 int fd;
460 struct t_call callbuf;
461
462 callbuf.addr.buf = netbufp->buf;
463 callbuf.addr.len = netbufp->len;
464 callbuf.addr.maxlen = netbufp->maxlen;
465
466 callbuf.opt.buf = NULL;
467 callbuf.opt.len = 0;
468 callbuf.opt.maxlen = 0;
469
470 callbuf.udata.buf = NULL;
471 callbuf.udata.len = 0;
472 callbuf.udata.maxlen = 0;
473
474 if ((fd = t_open(clonedev, O_RDWR, NULL)) < 0)
475 {
476 fprintf(stderr, "Cannot open %s\n", clonedev);
477 return(-1);
478 }
479
480 #ifdef DEBUG
481 fprintf(stderr, "Connecting to <%s> through device %s\n",
482 callbuf.addr.buf, clonedev);
483 #endif
484 if(t_bind(fd, NULL, NULL) < 0)
485 {
486 t_error("t_bind failed");
487 t_close(fd);
488 return(-1);
489 }
490 if(t_connect(fd, &callbuf, NULL) < 0)
491 {
492 t_error("t_connect failed");
493 checkNewEvent(fd);
494 t_close(fd);
495 return(-1);
496 }
497 return(fd);
498 }
499
500
501
502 /*extern char *calloc(), *realloc();*/
503 extern char *program;
504 static int network;
505 static int nextentry;
506
507 static char *makePacket();
508 static char *staticalloc();
509 static char *TheEnd;
510 static char *inbuf;
511 static int inlen;
512 static int nhosts;
513 static int nHosts;
514 static int flags = 0;
515 static struct netconfig *netconfigp = NULL;
516
517 static int
CallTheNameServer(int service,struct netconfig * nettype,char ** arg1,char ** arg2,int * arg3)518 CallTheNameServer(
519 int service,
520 struct netconfig *nettype,
521 char **arg1,
522 char **arg2,
523 int *arg3
524 )
525 {
526 int n,m, len;
527 char *ptr, *net;
528 int *iptr;
529
530 flags = service;
531 netconfigp = nettype;
532 ptr = *arg1;
533
534 iptr = (int *) ptr;
535 inlen = iptr[0];
536
537 #ifdef DEBUG
538 fprintf(stderr,"inlen = %d\n", inlen);
539 #endif
540 ptr += sizeof(int);
541 nhosts = iptr[1];
542 #ifdef DEBUG
543 fprintf(stderr,"nhosts = %d\n", nhosts);
544 #endif
545
546 inbuf = ptr + sizeof(int);
547 TheEnd = &inbuf[inlen];
548 #ifdef DEBUG
549 write(2, inbuf, inlen);
550 #endif
551 nextentry = ((xHostEntry *) inbuf)->length;
552 *arg2 = (char *) makePacket(&len);
553 if(arg3 != NULL)
554 *arg3 = nHosts;
555
556 #ifdef DEBUG
557 fprintf(stderr, "CallTheNameserver return %d\n", len);
558 #endif
559 return(len);
560 }
561
562
563
564 static int bufsize = 512;
565
566 static char *getnextentry();
567
568 static struct nd_addrlist *
GetHostServiceByName(char * host,int dispno)569 GetHostServiceByName(
570 char *host,
571 int dispno
572 )
573 {
574 struct nd_hostserv nd_hostserv;
575 struct nd_addrlist *nd_addrlistp = NULL;
576 struct netbuf *netbufp = NULL;
577 char service[MAX_AUTO_BUF_LEN];
578
579 sprintf(service , "auserver%d", dispno);
580 nd_hostserv.h_host = host;
581 nd_hostserv.h_serv = service;
582
583 if(netdir_getbyname(netconfigp, &nd_hostserv, &nd_addrlistp) == 0)
584 return(nd_addrlistp);
585 else return(NULL);
586 }
587
588 static int
ConvertName(char ** pktptr,char * entry,int n,int len)589 ConvertName(
590 char **pktptr,
591 char *entry,
592 int n,
593 int len
594 )
595 {
596 struct hostent *hp;
597 unsigned long address;
598 int port;
599 char *ptr;
600 int rndlen;
601 struct nd_addrlist *nd_addrlistp = NULL;
602 struct netbuf *netbufp;
603 char *addr;
604 #ifdef DEBUG
605 fprintf(stderr, "in ConvertName %s\n", entry);
606 #endif
607 if((nd_addrlistp = GetHostServiceByName(entry, atoi(dispno))) == NULL)
608 return(n);
609 netbufp = nd_addrlistp->n_addrs; /* the netbufs */
610 /*
611 xhost needs only the last four bytes of the address
612 over TCP/IP.
613 */
614 if(
615 strcmp(netconfigp->nc_protofmly, "inet") == 0 &&
616 strcmp(netconfigp->nc_proto , "tcp") == 0
617 ){
618 addr = &netbufp->buf[4];
619 len = 4;
620 family = 1;
621 }
622 else
623 {
624 family = 0;
625 addr = netbufp->buf;
626 len = netbufp->len;
627 }
628 rndlen = ((sizeof(xHostEntry) + len + 3) >> 2) << 2;
629
630 if((*pktptr = staticalloc(*pktptr, n+rndlen)) == NULL)
631 {
632 (void) netdir_free((char *)nd_addrlistp, ND_ADDRLIST);
633 return(-1);
634 }
635
636 ptr = &(*pktptr)[n];
637 ((xHostEntry *)ptr)->family = family;
638 ((xHostEntry *)ptr)->length = len;
639 ptr += sizeof(xHostEntry);
640
641 memcpy(ptr, addr, len);
642 netdir_free((char *)nd_addrlistp, ND_ADDRLIST);
643
644 #ifdef DEBUG
645 ptr[len] = '\0';
646 fprintf(stderr, "creating address for host %s address<%d>\n", entry, ptr);
647 #endif
648
649 return(n+rndlen);
650 }
651
652 static struct nd_hostservlist *
GetHostServiceByAddr(char * addr,int len)653 GetHostServiceByAddr(
654 char *addr,
655 int len
656 )
657 {
658 struct nd_hostservlist *nd_hostservlist;
659 struct netbuf netbuf;
660
661 netbuf.buf = addr;
662 netbuf.len = len;
663 netbuf.maxlen = len;
664
665 if(netdir_getbyaddr(netconfigp, &nd_hostservlist, &netbuf) == 0)
666 return(nd_hostservlist);
667 else return(NULL);
668 }
669
670
671 static int
ConvertCallToName(char ** pktptr,char * entry,int n,int len)672 ConvertCallToName(
673 char **pktptr,
674 char *entry,
675 int n,
676 int len
677 )
678 {
679 int l, rl;
680 char *ptr;
681 struct nd_hostservlist *nd_hostservlist;
682
683 if((nd_hostservlist = GetHostServiceByAddr(entry, len)) == NULL)
684 return(n);
685
686 l = strlen(nd_hostservlist->h_hostservs->h_host);
687
688 rl = ((sizeof(xHostEntry) + l + 3) >> 2) << 2;
689
690 if((*pktptr = staticalloc(*pktptr, n+rl)) == NULL)
691 {
692 (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST);
693 return(-1);
694 }
695
696 ptr = &(*pktptr)[n];
697 ((xHostEntry *)ptr)->family = 0;
698 ((xHostEntry *)ptr)->length = l;
699
700 ptr += sizeof(xHostEntry);
701
702 sprintf(ptr, nd_hostservlist->h_hostservs->h_host);
703 (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST);
704
705 #ifdef DEBUG
706 fprintf(stderr, "getting the name for host %s\n", ptr);
707 #endif
708
709 return(rl+n);
710 }
711
712 static int
ConvertAddress(char ** pktptr,char * entry,int n,int len)713 ConvertAddress(
714 char **pktptr,
715 char *entry,
716 int n,
717 int len
718 )
719 {
720 i;
721 char *ptr;
722 int l, rl;
723 struct nd_hostservlist *nd_hostservlist;
724 char *name;
725 char tcpaddr[8], *addr;
726 char addrbuf[MAX_AUTO_BUF_LEN];
727
728 #ifdef DEBUG
729 entry[len] = '\0';
730 fprintf(stderr, "Converting address %s in %s format\n",
731 entry, netconfigp->nc_netid);
732 #endif
733 if(
734 strcmp(netconfigp->nc_protofmly, "inet") == 0 &&
735 strcmp(netconfigp->nc_proto , "tcp") == 0
736 ){
737 addr = tcpaddr;
738 memcpy(tcpaddr, tcphalf, 4);
739 memcpy(&tcpaddr[4], entry, 4);
740 len = 8;
741 #ifdef DEBUG
742 fprintf(stderr, "port %d, family %d\n",
743 *(short *) &tcpaddr[2], *(short *) tcpaddr);
744 #endif
745 }
746 else
747 {
748 addr = entry;
749 }
750
751 if((nd_hostservlist = GetHostServiceByAddr(addr, len)) == NULL)
752 {
753 int i;
754
755 for(i=0; i< len; i++)
756 if((unsigned char)entry[i] < ' ' || (unsigned char)entry[i] > 0177)
757 break;
758 if(i < len)
759 {
760 sprintf(addrbuf, "%d", *(int *) entry);
761 name = addrbuf;
762 len = strlen(name);
763 }
764 else name = entry;
765 entry[len] = '\0';
766 l = len + 1;
767 }
768 else
769 {
770 name = nd_hostservlist->h_hostservs->h_host;
771 l = strlen(name) +1;
772 }
773 rl = ((sizeof(xHostEntry) + l + 3) >> 2) << 2;
774
775 if((*pktptr = staticalloc(*pktptr, n+rl)) == NULL)
776 {
777 if(nd_hostservlist != NULL)
778 (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST);
779 return(-1);
780 }
781
782 ptr = &(*pktptr)[n];
783 ((xHostEntry *)ptr)->family = 0;
784 ((xHostEntry *)ptr)->length = l;
785 ptr += sizeof(xHostEntry);
786
787 memcpy(ptr, name, l);
788 if(nd_hostservlist != NULL)
789 (void) netdir_free((char *)nd_hostservlist, ND_HOSTSERVLIST);
790
791 #ifdef DEBUG
792 fprintf(stderr, "getting the name for host %s\n", name);
793 #endif
794
795 return(n+rl);
796 }
797
798 static char *
getnextentry(int * plen)799 getnextentry(int *plen)
800 {
801 char *ptr;
802 int n = nextentry;
803
804 #ifdef DEBUG
805 fprintf(stderr,"In getnextentry()\n");
806 #endif
807 if(inbuf >= TheEnd)
808 {
809 #ifdef DEBUG
810 fprintf(stderr,"In getnextentry() end of buffer\n");
811 #endif
812 *plen = -1;
813 return(NULL);
814 }
815
816 *plen = nextentry;
817 family = ((xHostEntry *) inbuf)->family;
818 ptr = inbuf + sizeof(xHostEntry);
819 inbuf += ((sizeof(xHostEntry) + *plen + 3) >> 2) << 2;
820 nextentry = ((xHostEntry *) inbuf)->length;
821 ptr[*plen] = '\0';
822 return(ptr);
823 }
824
825 static char *
makePacket(int * plen)826 makePacket(int *plen)
827 {
828 char *pktptr = NULL, *ptr;
829 int len;
830 int n = 0, m;
831
832
833 #ifdef DEBUG
834 fprintf(stderr,"In makePacket()\n");
835 #endif
836
837 for(nHosts = 0; nHosts < nhosts;)
838 {
839 ptr = getnextentry(&len);
840 if(len < 0)
841 break;
842 if(len == 0 || ptr == NULL)
843 continue;
844 m = addentry(&pktptr, n, ptr, len);
845 if(m > n){
846 nHosts++;
847 n = m;
848 }
849 }
850 #ifdef DEBUG
851 fprintf(stderr, "packet size is %d\n", n);
852 #endif
853
854 *plen = n;
855
856 return(pktptr);
857 }
858
859 static int
addentry(char ** pktptr,char * entry,int n,int len;)860 addentry(
861 char **pktptr,
862 char *entry,
863 int n,
864 int len;
865 )
866 {
867
868 #ifdef DEBUG
869 fprintf(stderr, "in addEntry %s\n", entry);
870 #endif
871
872 switch(flags)
873 {
874 case ConvertNameToNetAddr:
875 return(ConvertName(pktptr, n, entry, len));
876 case ConvertNetAddrToName:
877 return(ConvertAddress(pktptr, n, entry, len));
878 case ConvertTliCallToName:
879 return(ConvertCallToName(pktptr, n, entry, len));
880 }
881 return(-1);
882 }
883
884 static char *
staticalloc(char * ptr,int size)885 staticalloc(
886 char *ptr,
887 int size
888 )
889 {
890
891 if(ptr == NULL)
892 {
893 if(bufsize < size)
894 bufsize = size;
895 ptr = malloc(bufsize);
896 }
897 if(bufsize < size)
898 {
899 bufsize = size + 512;
900 ptr = realloc(ptr, bufsize);
901 }
902 return(ptr);
903 }
904
905 static int
OpenVirtualCircuit(int lfd)906 OpenVirtualCircuit(int lfd)
907 {
908 char *clonedev;
909 int fd;
910
911 clonedev = Network._net[_AusTypeOfStream[lfd]]->nc_device;
912
913 if ((fd = t_open(clonedev, O_RDWR, NULL)) < 0)
914 {
915 fprintf(stderr, "Cannot open %s\n", clonedev);
916 return(-1);
917 }
918
919 if(t_bind(fd, NULL, NULL) < 0)
920 {
921 t_error("t_bind failed");
922 t_close(fd);
923 return(-1);
924 }
925 return(fd);
926 }
927
928 #endif /* AuSTREAMS_COMPILE */
929