source: rtems/cpukit/libnetworking/libc/getnetbynis.c @ d8fce03

4.8
Last change on this file since d8fce03 was 09fdb5e8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/30/07 at 05:15:58

Eliminate SCCS, LINT. Add HAVE_CONFIG_H.

  • Property mode set to 100644
File size: 4.0 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 <stdlib.h>
37#include <ctype.h>
38#include <errno.h>
39#include <string.h>
40#include <arpa/nameser.h>
41#ifdef YP
42#include <rpc/rpc.h>
43#include <rpcsvc/yp_prot.h>
44#include <rpcsvc/ypclnt.h>
45#endif
46
47#define MAXALIASES      35
48#define MAXADDRS        35
49
50#ifdef YP
51static char *host_aliases[MAXALIASES];
52#endif /* YP */
53
54static struct netent *
55_getnetbynis(name, map, af)
56        const char *name;
57        char *map;
58        int af;
59{
60#ifdef YP
61        register char *cp, **q;
62        static char *result;
63        int resultlen;
64        static struct netent h;
65        static char *domain = (char *)NULL;
66        static char ypbuf[YPMAXRECORD + 2];
67
68        switch(af) {
69        case AF_INET:
70                break;
71        default:
72        case AF_INET6:
73                errno = EAFNOSUPPORT;
74                return NULL;
75        }
76
77        if (domain == (char *)NULL)
78                if (yp_get_default_domain (&domain))
79                        return (NULL);
80
81        if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
82                return (NULL);
83
84        bcopy((char *)result, (char *)&ypbuf, resultlen);
85        ypbuf[resultlen] = '\0';
86        free(result);
87        result = (char *)&ypbuf;
88
89        if ((cp = index(result, '\n')))
90                *cp = '\0';
91
92        cp = strpbrk(result, " \t");
93        *cp++ = '\0';
94        h.n_name = result;
95
96        while (*cp == ' ' || *cp == '\t')
97                cp++;
98
99        h.n_net = inet_network(cp);
100        h.n_addrtype = AF_INET;
101
102        q = h.n_aliases = host_aliases;
103        cp = strpbrk(cp, " \t");
104        if (cp != NULL)
105                *cp++ = '\0';
106        while (cp && *cp) {
107                if (*cp == ' ' || *cp == '\t') {
108                        cp++;
109                        continue;
110                }
111                if (q < &host_aliases[MAXALIASES - 1])
112                        *q++ = cp;
113                cp = strpbrk(cp, " \t");
114                if (cp != NULL)
115                        *cp++ = '\0';
116        }
117        *q = NULL;
118        return (&h);
119#else
120        return (NULL);
121#endif
122}
123
124struct netent *
125_getnetbynisname(name)
126        const char *name;
127{
128        return _getnetbynis(name, "networks.byname", AF_INET);
129}
130
131struct netent *
132_getnetbynisaddr(addr, af)
133        unsigned long addr;
134        int af;
135{
136        char *str, *cp;
137        unsigned long net2;
138        int nn;
139        unsigned int netbr[4];
140        char buf[MAXDNAME];
141
142        if (af != AF_INET) {
143                errno = EAFNOSUPPORT;
144                return (NULL);
145        }
146
147        for (nn = 4, net2 = addr; net2; net2 >>= 8) {
148                netbr[--nn] = net2 & 0xff;
149        }
150
151        switch (nn) {
152        case 3:         /* Class A */
153                sprintf(buf, "%u", netbr[3]);
154                break;
155        case 2:         /* Class B */
156                sprintf(buf, "%u.%u", netbr[2], netbr[3]);
157                break;
158        case 1:         /* Class C */
159                sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
160                break;
161        case 0:         /* Class D - E */
162                sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
163                        netbr[2], netbr[3]);
164                break;
165        }
166
167        str = (char *)&buf;
168        cp = str + (strlen(str) - 2);
169
170        while(!strcmp(cp, ".0")) {
171                *cp = '\0';
172                cp = str + (strlen(str) - 2);
173        }
174
175        return _getnetbynis(str, "networks.byaddr", af);
176}
Note: See TracBrowser for help on using the repository browser.