af_inet.c revision 1.4 1 /* $NetBSD: af_inet.c,v 1.4 2006/08/26 18:14:28 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet.c,v 1.4 2006/08/26 18:14:28 christos Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44
45 #include <arpa/inet.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <ifaddrs.h>
50 #include <netdb.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <util.h>
55
56 #include "extern.h"
57 #include "af_inet.h"
58
59 struct in_aliasreq in_addreq;
60
61 int setipdst;
62
63 void
64 setifipdst(const char *addr, int d)
65 {
66
67 in_getaddr(addr, DSTADDR);
68 setipdst++;
69 clearaddr = 0;
70 newaddr = 0;
71 }
72
73 void
74 in_alias(struct ifreq *creq)
75 {
76 struct sockaddr_in *iasin;
77 int alias;
78
79 if (lflag)
80 return;
81
82 alias = 1;
83
84 /* Get the non-alias address for this interface. */
85 getsock(AF_INET);
86 if (s < 0) {
87 if (errno == EAFNOSUPPORT)
88 return;
89 err(EXIT_FAILURE, "socket");
90 }
91 (void) memset(&ifr, 0, sizeof(ifr));
92 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
93 if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
94 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
95 return;
96 } else
97 warn("SIOCGIFADDR");
98 }
99 /* If creq and ifr are the same address, this is not an alias. */
100 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
101 sizeof(creq->ifr_addr)) == 0)
102 alias = 0;
103 (void) memset(&in_addreq, 0, sizeof(in_addreq));
104 estrlcpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
105 memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
106 sizeof(in_addreq.ifra_addr));
107 if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
108 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
109 return;
110 } else
111 warn("SIOCGIFALIAS");
112 }
113
114 iasin = &in_addreq.ifra_addr;
115 printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
116
117 if (flags & IFF_POINTOPOINT) {
118 iasin = &in_addreq.ifra_dstaddr;
119 printf(" -> %s", inet_ntoa(iasin->sin_addr));
120 }
121
122 iasin = &in_addreq.ifra_mask;
123 printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
124
125 if (flags & IFF_BROADCAST) {
126 iasin = &in_addreq.ifra_broadaddr;
127 printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
128 }
129 printf("\n");
130 }
131
132 void
133 in_status(int force)
134 {
135 struct ifaddrs *ifap, *ifa;
136 struct ifreq isifr;
137
138 if (getifaddrs(&ifap) != 0)
139 err(EXIT_FAILURE, "getifaddrs");
140 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
141 if (strcmp(name, ifa->ifa_name) != 0)
142 continue;
143 if (ifa->ifa_addr->sa_family != AF_INET)
144 continue;
145 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
146 continue;
147
148 memset(&isifr, 0, sizeof(isifr));
149 estrlcpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
150 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
151 in_alias(&isifr);
152 }
153 freeifaddrs(ifap);
154 }
155
156 #define SIN(x) ((struct sockaddr_in *) &(x))
157 struct sockaddr_in *sintab[] = {
158 SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
159 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
160
161 void
162 in_getaddr(const char *str, int which)
163 {
164 struct sockaddr_in *gasin = sintab[which];
165 struct hostent *hp;
166 struct netent *np;
167
168 gasin->sin_len = sizeof(*gasin);
169 if (which != MASK)
170 gasin->sin_family = AF_INET;
171
172 if (which == ADDR) {
173 char *p = NULL;
174 if ((p = strrchr(str, '/')) != NULL) {
175 *p = '\0';
176 in_getprefix(p + 1, MASK);
177 }
178 }
179
180 if (inet_aton(str, &gasin->sin_addr) == 0) {
181 if ((hp = gethostbyname(str)) != NULL)
182 (void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
183 else if ((np = getnetbyname(str)) != NULL)
184 gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
185 else
186 errx(EXIT_FAILURE, "%s: bad value", str);
187 }
188 }
189
190 void
191 in_getprefix(const char *plen, int which)
192 {
193 struct sockaddr_in *igsin = sintab[which];
194 u_char *cp;
195 int len = strtol(plen, (char **)NULL, 10);
196
197 if ((len < 0) || (len > 32))
198 errx(EXIT_FAILURE, "%s: bad value", plen);
199 igsin->sin_len = sizeof(*igsin);
200 if (which != MASK)
201 igsin->sin_family = AF_INET;
202 if ((len == 0) || (len == 32)) {
203 memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
204 return;
205 }
206 memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
207 for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
208 *cp++ = 0xff;
209 if (len)
210 *cp = 0xff << (8 - len);
211 }
212