Deleted Added
full compact
gethostnamadr.c (1574) gethostnamadr.c (1991)
1/*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 38 unchanged lines hidden (view full) ---

47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#if defined(LIBC_SCCS) && !defined(lint)
1/*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 38 unchanged lines hidden (view full) ---

47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#if defined(LIBC_SCCS) && !defined(lint)
55static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
56static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
55static char sccsid[] = "From: @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
56/*static char rcsid[] = "From: Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel ";*/
57static const char rcsid[] =
58 "$Id$";
57#endif /* LIBC_SCCS and not lint */
58
59#include <sys/param.h>
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
63#include <arpa/nameser.h>
64#include <netdb.h>
65#include <resolv.h>
66#include <stdio.h>
67#include <ctype.h>
68#include <errno.h>
69#include <string.h>
70
71#define MAXALIASES 35
72#define MAXADDRS 35
73
59#endif /* LIBC_SCCS and not lint */
60
61#include <sys/param.h>
62#include <sys/socket.h>
63#include <netinet/in.h>
64#include <arpa/inet.h>
65#include <arpa/nameser.h>
66#include <netdb.h>
67#include <resolv.h>
68#include <stdio.h>
69#include <ctype.h>
70#include <errno.h>
71#include <string.h>
72
73#define MAXALIASES 35
74#define MAXADDRS 35
75
76#define _PATH_HOSTCONF "/etc/host.conf"
77
78enum service_type {
79 SERVICE_NONE = 0,
80 SERVICE_BIND,
81 SERVICE_HOSTS,
82 SERVICE_NIS };
83#define SERVICE_MAX SERVICE_NIS
84
85static struct {
86 const char *name;
87 enum service_type type;
88} service_names[] = {
89 { "hosts", SERVICE_HOSTS },
90 { "/etc/hosts", SERVICE_HOSTS },
91 { "hosttable", SERVICE_HOSTS },
92 { "htable", SERVICE_HOSTS },
93 { "bind", SERVICE_BIND },
94 { "dns", SERVICE_BIND },
95 { "domain", SERVICE_BIND },
96 { "yp", SERVICE_NIS },
97 { "yellowpages", SERVICE_NIS },
98 { "nis", SERVICE_NIS },
99 { 0, SERVICE_NONE }
100};
101
102static enum service_type service_order[SERVICE_MAX + 1];
103static int service_done = 0;
104
74static char *h_addr_ptrs[MAXADDRS + 1];
75
76static struct hostent host;
77static char *host_aliases[MAXALIASES];
78static char hostbuf[BUFSIZ+1];
79static struct in_addr host_addr;
80static FILE *hostf = NULL;
81static char hostaddr[MAXADDRS];
82static char *host_addrs[2];
83static int stayopen = 0;
105static char *h_addr_ptrs[MAXADDRS + 1];
106
107static struct hostent host;
108static char *host_aliases[MAXALIASES];
109static char hostbuf[BUFSIZ+1];
110static struct in_addr host_addr;
111static FILE *hostf = NULL;
112static char hostaddr[MAXADDRS];
113static char *host_addrs[2];
114static int stayopen = 0;
84char *strpbrk();
85
86#if PACKETSZ > 1024
87#define MAXPACKET PACKETSZ
88#else
89#define MAXPACKET 1024
90#endif
91
92typedef union {
93 HEADER hdr;
94 u_char buf[MAXPACKET];
95} querybuf;
96
97typedef union {
98 int32_t al;
99 char ac;
100} align;
101
102extern int h_errno;
103
115
116#if PACKETSZ > 1024
117#define MAXPACKET PACKETSZ
118#else
119#define MAXPACKET 1024
120#endif
121
122typedef union {
123 HEADER hdr;
124 u_char buf[MAXPACKET];
125} querybuf;
126
127typedef union {
128 int32_t al;
129 char ac;
130} align;
131
132extern int h_errno;
133
134static enum service_type
135get_service_name(const char *name) {
136 int i;
137 for(i = 0; service_names[i].type != SERVICE_NONE; i++) {
138 if(!strcasecmp(name, service_names[i].name)) {
139 return service_names[i].type;
140 }
141 }
142 return SERVICE_NONE;
143}
144
145static void
146init_services()
147{
148 char *cp, buf[BUFSIZ];
149 register int cc = 0;
150 FILE *fd;
151
152 if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) {
153 /* make some assumptions */
154 service_order[0] = SERVICE_BIND;
155 service_order[1] = SERVICE_HOSTS;
156 service_order[2] = SERVICE_NONE;
157 } else {
158 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {
159 if(buf[0] == '#')
160 continue;
161
162 cp = strtok(buf, "\n \t,:;");
163 do {
164 if(!isalpha(cp[0])) continue;
165 service_order[cc] = get_service_name(buf);
166 if(service_order[cc] != SERVICE_NONE)
167 cc++;
168 } while((cp = strtok((char *)0, "\n \t,:;"))
169 && (cc < SERVICE_MAX));
170 }
171 service_order[cc] = SERVICE_NONE;
172 fclose(fd);
173 }
174 service_done = 1;
175}
176
104static struct hostent *
105getanswer(answer, anslen, iquery)
106 querybuf *answer;
107 int anslen;
108 int iquery;
109{
110 register HEADER *hp;
111 register u_char *cp;

--- 129 unchanged lines hidden (view full) ---

241 return (&host);
242 } else {
243 h_errno = TRY_AGAIN;
244 return ((struct hostent *) NULL);
245 }
246}
247
248struct hostent *
177static struct hostent *
178getanswer(answer, anslen, iquery)
179 querybuf *answer;
180 int anslen;
181 int iquery;
182{
183 register HEADER *hp;
184 register u_char *cp;

--- 129 unchanged lines hidden (view full) ---

314 return (&host);
315 } else {
316 h_errno = TRY_AGAIN;
317 return ((struct hostent *) NULL);
318 }
319}
320
321struct hostent *
249gethostbyname(name)
322_getdnsbyname(name)
250 const char *name;
251{
252 querybuf buf;
253 register const char *cp;
254 int n;
255 extern struct hostent *_gethtbyname();
256
257 /*

--- 32 unchanged lines hidden (view full) ---

290 break;
291 }
292
293 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
294#ifdef DEBUG
295 if (_res.options & RES_DEBUG)
296 printf("res_search failed\n");
297#endif
323 const char *name;
324{
325 querybuf buf;
326 register const char *cp;
327 int n;
328 extern struct hostent *_gethtbyname();
329
330 /*

--- 32 unchanged lines hidden (view full) ---

363 break;
364 }
365
366 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
367#ifdef DEBUG
368 if (_res.options & RES_DEBUG)
369 printf("res_search failed\n");
370#endif
298 if (errno == ECONNREFUSED)
299 return (_gethtbyname(name));
300 else
301 return ((struct hostent *) NULL);
371 return ((struct hostent *) NULL);
302 }
303 return (getanswer(&buf, n, 0));
304}
305
306struct hostent *
372 }
373 return (getanswer(&buf, n, 0));
374}
375
376struct hostent *
307gethostbyaddr(addr, len, type)
377_getdnsbyaddr(addr, len, type)
308 const char *addr;
309 int len, type;
310{
311 int n;
312 querybuf buf;
313 register struct hostent *hp;
314 char qbuf[MAXDNAME];
315 extern struct hostent *_gethtbyaddr();

--- 6 unchanged lines hidden (view full) ---

322 ((unsigned)addr[1] & 0xff),
323 ((unsigned)addr[0] & 0xff));
324 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
325 if (n < 0) {
326#ifdef DEBUG
327 if (_res.options & RES_DEBUG)
328 printf("res_query failed\n");
329#endif
378 const char *addr;
379 int len, type;
380{
381 int n;
382 querybuf buf;
383 register struct hostent *hp;
384 char qbuf[MAXDNAME];
385 extern struct hostent *_gethtbyaddr();

--- 6 unchanged lines hidden (view full) ---

392 ((unsigned)addr[1] & 0xff),
393 ((unsigned)addr[0] & 0xff));
394 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
395 if (n < 0) {
396#ifdef DEBUG
397 if (_res.options & RES_DEBUG)
398 printf("res_query failed\n");
399#endif
330 if (errno == ECONNREFUSED)
331 return (_gethtbyaddr(addr, len, type));
332 return ((struct hostent *) NULL);
333 }
334 hp = getanswer(&buf, n, 1);
335 if (hp == NULL)
336 return ((struct hostent *) NULL);
337 hp->h_addrtype = type;
338 hp->h_length = len;
339 h_addr_ptrs[0] = (char *)&host_addr;

--- 105 unchanged lines hidden (view full) ---

445
446 _sethtent(0);
447 while (p = _gethtent())
448 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
449 break;
450 _endhtent();
451 return (p);
452}
400 return ((struct hostent *) NULL);
401 }
402 hp = getanswer(&buf, n, 1);
403 if (hp == NULL)
404 return ((struct hostent *) NULL);
405 hp->h_addrtype = type;
406 hp->h_length = len;
407 h_addr_ptrs[0] = (char *)&host_addr;

--- 105 unchanged lines hidden (view full) ---

513
514 _sethtent(0);
515 while (p = _gethtent())
516 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
517 break;
518 _endhtent();
519 return (p);
520}
521
522
523#ifdef YP
524struct hostent *
525_getnishost(name, map)
526 char *name, *map;
527{
528 register char *cp, *dp, **q;
529 char *result;
530 int resultlen;
531 static struct hostent h;
532 static char *domain = (char *)NULL;
533
534 if (domain == (char *)NULL)
535 if (yp_get_default_domain (&domain))
536 return ((struct hostent *)NULL);
537
538 if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
539 return ((struct hostent *)NULL);
540
541 if (cp = index(result, '\n'))
542 *cp = '\0';
543
544 cp = strpbrk(result, " \t");
545 *cp++ = '\0';
546#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
547 h.h_addr_list = host_addrs;
548#endif
549 h.h_addr = hostaddr;
550 *((u_long *)h.h_addr) = inet_addr(result);
551 h.h_length = sizeof(u_long);
552 h.h_addrtype = AF_INET;
553 while (*cp == ' ' || *cp == '\t')
554 cp++;
555 h.h_name = cp;
556 q = h.h_aliases = host_aliases;
557 cp = strpbrk(cp, " \t");
558 if (cp != NULL)
559 *cp++ = '\0';
560 while (cp && *cp) {
561 if (*cp == ' ' || *cp == '\t') {
562 cp++;
563 continue;
564 }
565 if (q < &host_aliases[MAXALIASES - 1])
566 *q++ = cp;
567 cp = strpbrk(cp, " \t");
568 if (cp != NULL)
569 *cp++ = '\0';
570 }
571 *q = NULL;
572 return (&h);
573}
574#endif /* YP */
575
576struct hostent *
577gethostbyname(const char *name)
578{
579 struct hostent *hp = 0;
580 int nserv = 0;
581
582 if(!service_done) {
583 init_services();
584 }
585
586 while(!hp) {
587 switch(service_order[nserv]) {
588 case SERVICE_NONE:
589 return 0;
590 case SERVICE_HOSTS:
591 hp = _gethtbyname(name);
592 break;
593 case SERVICE_BIND:
594 hp = _getdnsbyname(name);
595 break;
596 case SERVICE_NIS:
597#ifdef YP
598 hp = _getnishost(name, "hosts.byname");
599#endif
600 break;
601 }
602 nserv++;
603 }
604 return hp;
605}
606
607struct hostent *
608gethostbyaddr(const char *addr, int len, int type)
609{
610 struct hostent *hp = 0;
611 int nserv = 0;
612
613 if(!service_done) {
614 init_service();
615 }
616
617 while(!hp) {
618 switch(service_order[nserv]) {
619 case SERVICE_NONE:
620 return 0;
621 case SERVICE_HOSTS:
622 hp = _gethtbyaddr(addr, len, type);
623 break;
624 case SERVICE_BIND:
625 hp = _getdnsbyname(addr, len, type);
626 break;
627 case SERVICE_NIS:
628#ifdef YP
629 hp = _getnishost(addr, "hosts.byaddr");
630#endif
631 break;
632 }
633 nserv++;
634 }
635 return hp;
636}
637