BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / amd / amd / wire.c
CommitLineData
760c99e3 1/*
760c99e3
JSP
2 * Copyright (c) 1990 Jan-Simon Pendry
3 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry at Imperial College, London.
9 *
af359dea
C
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)wire.c 5.3 (Berkeley) 5/12/91
39 *
40 * $Id: wire.c,v 5.2.1.5 91/05/07 22:14:21 jsp Alpha $
760c99e3 41 *
760c99e3
JSP
42 */
43
44/*
45 * This routine returns the subnet (address&netmask) for the primary network
46 * interface. If the resulting address has an entry in the hosts file, the
47 * corresponding name is retuned, otherwise the address is returned in
48 * standard internet format.
49 *
50 * From: Paul Anderson (23/4/90)
51 */
52
53#include "am.h"
54
55#include <sys/ioctl.h>
56
57#define NO_SUBNET "notknown"
58
59#ifdef SIOCGIFFLAGS
60#include <net/if.h>
61#include <netdb.h>
62
63#if defined(IFF_LOCAL_LOOPBACK) && !defined(IFF_LOOPBACK)
64#define IFF_LOOPBACK IFF_LOCAL_LOOPBACK
65#endif
66
67#define GFBUFLEN 1024
68#define clist (ifc.ifc_ifcu.ifcu_req)
69#define count (ifc.ifc_len/sizeof(struct ifreq))
70
71char *getwire P((void));
72char *getwire()
73{
74 struct hostent *hp;
75 struct netent *np;
76 struct ifconf ifc;
77 struct ifreq *ifr;
78 caddr_t cp, cplim;
79 unsigned long address, netmask, subnet;
80 char buf[GFBUFLEN], *s;
81 int sk = -1;
82
83 /*
84 * Get suitable socket
85 */
86 if ((sk = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
87 goto out;
88
89 /*
90 * Fill in ifconf details
91 */
92 ifc.ifc_len = sizeof buf;
93 ifc.ifc_buf = buf;
94
95 /*
96 * Get network interface configurations
97 */
98 if (ioctl(sk, SIOCGIFCONF, (caddr_t) &ifc) < 0)
99 goto out;
100
101 /*
102 * Upper bound on array
103 */
104 cplim = buf + ifc.ifc_len;
105
106 /*
107 * This is some magic to cope with both "traditional" and the
108 * new 4.4BSD-style struct sockaddrs. The new structure has
109 * variable length and a size field to support longer addresses.
110 * AF_LINK is a new definition for 4.4BSD.
111 */
112#ifdef AF_LINK
113#define max(a, b) ((a) > (b) ? (a) : (b))
af359dea 114#define size(ifr) (max((ifr)->ifr_addr.sa_len, sizeof((ifr)->ifr_addr)) + sizeof(ifr->ifr_name))
760c99e3 115#else
af359dea 116#define size(ifr) sizeof(*ifr)
760c99e3
JSP
117#endif
118 /*
119 * Scan the list looking for a suitable interface
120 */
af359dea 121 for (cp = buf; cp < cplim; cp += size(ifr)) {
760c99e3
JSP
122 ifr = (struct ifreq *) cp;
123
124 if (ifr->ifr_addr.sa_family != AF_INET)
125 continue;
126 else
127 address = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
128
129 /*
130 * Get interface flags
131 */
132 if (ioctl(sk, SIOCGIFFLAGS, (caddr_t) ifr) < 0)
133 goto out;
134
135 /*
136 * If the interface is a loopback, or its not running
137 * then ignore it.
138 */
139 if ((ifr->ifr_flags & IFF_LOOPBACK) != 0)
140 continue;
141 if ((ifr->ifr_flags & IFF_RUNNING) == 0)
142 continue;
143
144 /*
145 * Get the netmask of this interface
146 */
147 if (ioctl(sk, SIOCGIFNETMASK, (caddr_t) ifr) < 0)
148 goto out;
149
150 netmask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
151
152 (void) close(sk);
153
154 /*
155 * Figure out the subnet's network address
156 */
157 subnet = address & netmask;
af359dea
C
158#ifdef IN_CLASSA
159 if (IN_CLASSA(subnet))
160 subnet >>= IN_CLASSA_NSHIFT;
161 else if (IN_CLASSB(subnet))
162 subnet >>= IN_CLASSB_NSHIFT;
163 else if (IN_CLASSC(subnet))
164 subnet >>= IN_CLASSC_NSHIFT;
165#endif
760c99e3
JSP
166 /*
167 * Now get a usable name.
168 * First use the network database,
169 * then the host database,
170 * and finally just make a dotted quad.
171 */
172 np = getnetbyaddr(subnet, AF_INET);
173 if (np)
174 s = np->n_name;
175 else {
af359dea 176 subnet = address & netmask;
760c99e3
JSP
177 hp = gethostbyaddr((char *) &subnet, 4, AF_INET);
178 if (hp)
179 s = hp->h_name;
180 else
181 s = inet_dquad(buf, subnet);
182 }
183
184 return strdup(s);
185 }
186
187out:
188 if (sk >= 0)
189 (void) close(sk);
190 return strdup(NO_SUBNET);
191}
192
193#else
194
195char *getwire P((void));
196char *getwire()
197{
198 return strdup(NO_SUBNET);
199}
200#endif /* SIOCGIFFLAGS */