source: rtems/cpukit/libnetworking/libc/getnetnamadr.c @ e005f6e8

4.104.11
Last change on this file since e005f6e8 was e005f6e8, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2010 at 5:53:22 AM

Add HAVE_STRINGS_H for better POSIX compliance.

  • Property mode set to 100644
File size: 4.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 HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include <sys/param.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34#include <netdb.h>
35#include <stdio.h>
36#include <ctype.h>
37#include <errno.h>
38#include <string.h>
39#ifdef HAVE_STRINGS_H
40#include <strings.h> /* for strcasecmp */
41#endif
42
43#ifndef _PATH_NETCONF
44#define _PATH_NETCONF   "/etc/host.conf"
45#endif
46
47enum service_type {
48  SERVICE_NONE = 0,
49  SERVICE_BIND,
50  SERVICE_TABLE,
51  SERVICE_NIS };
52#define SERVICE_MAX     SERVICE_NIS
53
54static struct {
55  const char *name;
56  enum service_type type;
57} service_names[] = {
58  { "hosts", SERVICE_TABLE },
59  { "/etc/hosts", SERVICE_TABLE },
60  { "hosttable", SERVICE_TABLE },
61  { "htable", SERVICE_TABLE },
62  { "bind", SERVICE_BIND },
63  { "dns", SERVICE_BIND },
64  { "domain", SERVICE_BIND },
65  { "yp", SERVICE_NIS },
66  { "yellowpages", SERVICE_NIS },
67  { "nis", SERVICE_NIS },
68  { 0, SERVICE_NONE }
69};
70
71static enum service_type service_order[SERVICE_MAX + 1];
72static int service_done = 0;
73
74static enum service_type
75get_service_name(const char *name) {
76        int i;
77        for(i = 0; service_names[i].type != SERVICE_NONE; i++) {
78                if(!strcasecmp(name, service_names[i].name)) {
79                        return service_names[i].type;
80                }
81        }
82        return SERVICE_NONE;
83}
84
85static void
86init_services(void)
87{
88        char *cp, *p, buf[BUFSIZ];
89        register int cc = 0;
90        FILE *fd;
91
92        if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) {
93                                /* make some assumptions */
94                service_order[0] = SERVICE_TABLE;
95                service_order[1] = SERVICE_NONE;
96        } else {
97                while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {
98                        if(buf[0] == '#')
99                                continue;
100
101                        p = buf;
102                        while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
103                                ;
104                        if (cp == NULL)
105                                continue;
106                        do {
107                                if (isalpha((unsigned char)cp[0])) {
108                                        service_order[cc] = get_service_name(cp);
109                                        if(service_order[cc] != SERVICE_NONE)
110                                                cc++;
111                                }
112                                while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
113                                        ;
114                        } while(cp != NULL && cc < SERVICE_MAX);
115                }
116                service_order[cc] = SERVICE_NONE;
117                fclose(fd);
118        }
119        service_done = 1;
120}
121
122struct netent *
123getnetbyname(const char *name)
124{
125        struct netent *hp = 0;
126        int nserv = 0;
127
128        if (!service_done)
129                init_services();
130
131        while (!hp) {
132                switch (service_order[nserv]) {
133                      case SERVICE_NONE:
134                        return NULL;
135                      case SERVICE_TABLE:
136                        hp = _getnetbyhtname(name);
137                        break;
138                      case SERVICE_BIND:
139                        hp = _getnetbydnsname(name);
140                        break;
141                      case SERVICE_NIS:
142                        hp = _getnetbynisname(name);
143                        break;
144                }
145                nserv++;
146        }
147        return hp;
148}
149
150struct netent *
151getnetbyaddr(uint32_t addr, int af)
152{
153        struct netent *hp = 0;
154        int nserv = 0;
155
156        if (!service_done)
157                init_services();
158
159        while (!hp) {
160                switch (service_order[nserv]) {
161                      case SERVICE_NONE:
162                        return 0;
163                      case SERVICE_TABLE:
164                        hp = _getnetbyhtaddr(addr, af);
165                        break;
166                      case SERVICE_BIND:
167                        hp = _getnetbydnsaddr(addr, af);
168                        break;
169                      case SERVICE_NIS:
170                        hp = _getnetbynisaddr(addr, af);
171                        break;
172                }
173                nserv++;
174        }
175        return hp;
176}
177
178void
179setnetent(int stayopen)
180{
181        _setnethtent(stayopen);
182        _setnetdnsent(stayopen);
183}
184
185void
186endnetent(void)
187{
188        _endnethtent();
189        _endnetdnsent();
190}
Note: See TracBrowser for help on using the repository browser.