source: rtems/cpukit/libnetworking/libc/gethostnamadr.c @ b2b143f4

4.104.114.84.95
Last change on this file since b2b143f4 was b2b143f4, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 17:58:51

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/rtems_rpc.c, librpc/src/rpc/svc.c, sapi/include/confdefs.h, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c:
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*-
2 * Copyright (c) 1994, Garrett Wollman
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#if defined(LIBC_SCCS) && !defined(lint)
27static char sccsid[] = "@(#)$Id$";
28static char rcsid[] = "$Id$";
29#endif /* LIBC_SCCS and not lint */
30
31/* Since we compile with strict ANSI we need to undef it to get
32 * prototypes for extensions
33 */
34#undef __STRICT_ANSI__
35
36#include <sys/param.h>
37#include <sys/socket.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <netdb.h>
41#include <stdio.h>
42#include <ctype.h>
43#include <errno.h>
44#include <string.h>
45#include <arpa/nameser.h>               /* XXX hack for _res */
46#include <resolv.h>                     /* XXX hack for _res */
47
48#define _PATH_HOSTCONF  "/etc/host.conf"
49
50enum service_type {
51  SERVICE_NONE = 0,
52  SERVICE_BIND,
53  SERVICE_HOSTS,
54  SERVICE_NIS };
55#define SERVICE_MAX     SERVICE_NIS
56
57static struct {
58  const char *name;
59  enum service_type type;
60} service_names[] = {
61  { "hosts", SERVICE_HOSTS },
62  { "/etc/hosts", SERVICE_HOSTS },
63  { "hosttable", SERVICE_HOSTS },
64  { "htable", SERVICE_HOSTS },
65  { "bind", SERVICE_BIND },
66  { "dns", SERVICE_BIND },
67  { "domain", SERVICE_BIND },
68  { "yp", SERVICE_NIS },
69  { "yellowpages", SERVICE_NIS },
70  { "nis", SERVICE_NIS },
71  { 0, SERVICE_NONE }
72};
73
74static enum service_type service_order[SERVICE_MAX + 1];
75static int service_done = 0;
76
77static enum service_type
78get_service_name(const char *name) {
79        int i;
80        for(i = 0; service_names[i].type != SERVICE_NONE; i++) {
81                if(!strcasecmp(name, service_names[i].name)) {
82                        return service_names[i].type;
83                }
84        }
85        return SERVICE_NONE;
86}
87
88static void
89init_services()
90{
91        char *cp, *p, buf[BUFSIZ];
92        register int cc = 0;
93        FILE *fd;
94
95        if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) {
96                                /* make some assumptions */
97                service_order[0] = SERVICE_BIND;
98                service_order[1] = SERVICE_HOSTS;
99                service_order[2] = SERVICE_NONE;
100        } else {
101                while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {
102                        if(buf[0] == '#')
103                                continue;
104
105                        p = buf;
106                        while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
107                                ;
108                        if (cp == NULL)
109                                continue;
110                        do {
111                                if (isalpha((int)cp[0])) {
112                                        service_order[cc] = get_service_name(cp);
113                                        if(service_order[cc] != SERVICE_NONE)
114                                                cc++;
115                                }
116                                while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
117                                        ;
118                        } while(cp != NULL && cc < SERVICE_MAX);
119                }
120                service_order[cc] = SERVICE_NONE;
121                fclose(fd);
122        }
123        service_done = 1;
124}
125
126struct hostent *
127gethostbyname(const char *name)
128{
129        struct hostent *hp;
130
131        if (_res.options & RES_USE_INET6) {             /* XXX */
132                hp = gethostbyname2(name, AF_INET6);    /* XXX */
133                if (hp)                                 /* XXX */
134                        return (hp);                    /* XXX */
135        }                                               /* XXX */
136        return (gethostbyname2(name, AF_INET));
137}
138
139struct hostent *
140gethostbyname2(const char *name, int type)
141{
142        struct hostent *hp = 0;
143        int nserv = 0;
144
145        if (!service_done)
146                init_services();
147
148        while (!hp) {
149                switch (service_order[nserv]) {
150                      case SERVICE_NONE:
151                        return NULL;
152                      case SERVICE_HOSTS:
153                        hp = _gethostbyhtname(name, type);
154                        break;
155                      case SERVICE_BIND:
156                        hp = _gethostbydnsname(name, type);
157                        break;
158                      case SERVICE_NIS:
159                        hp = _gethostbynisname(name, type);
160                        break;
161                }
162                nserv++;
163        }
164        return hp;
165}
166
167struct hostent *
168gethostbyaddr(const char *addr, int len, int type)
169{
170        struct hostent *hp = 0;
171        int nserv = 0;
172
173        if (!service_done)
174                init_services();
175
176        while (!hp) {
177                switch (service_order[nserv]) {
178                      case SERVICE_NONE:
179                        return 0;
180                      case SERVICE_HOSTS:
181                        hp = _gethostbyhtaddr(addr, len, type);
182                        break;
183                      case SERVICE_BIND:
184                        hp = _gethostbydnsaddr(addr, len, type);
185                        break;
186                      case SERVICE_NIS:
187                        hp = _gethostbynisaddr(addr, len, type);
188                        break;
189                }
190                nserv++;
191        }
192        return hp;
193}
194
195#ifdef _THREAD_SAFE
196struct hostent_data;
197
198/*
199 * Temporary function (not thread safe)
200 */
201int gethostbyaddr_r(const char *addr, int len, int type,
202        struct hostent *result, struct hostent_data *buffer)
203{
204        struct hostent *hp;
205        int ret;
206        if ((hp = gethostbyaddr(addr, len, type)) == NULL) {
207                ret = -1;
208        } else {
209                memcpy(result, hp, sizeof(struct hostent));
210                ret = 0;
211        }
212        return(ret);
213}
214#endif
215
216void
217sethostent(stayopen)
218        int stayopen;
219{
220        _sethosthtent(stayopen);
221        _sethostdnsent(stayopen);
222}
223
224void
225endhostent()
226{
227        _endhosthtent();
228        _endhostdnsent();
229}
Note: See TracBrowser for help on using the repository browser.