ifconfig(8): Add '-f' option to print in more formats
[dragonfly.git] / sbin / ifconfig / af_inet.c
CommitLineData
ca74a0a2
SZ
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
ca74a0a2
SZ
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: src/sbin/ifconfig/af_inet.c,v 1.2 2005/06/16 19:37:09 ume Exp $
ca74a0a2
SZ
30 */
31
32#include <sys/types.h>
33#include <sys/ioctl.h>
34#include <sys/socket.h>
35#include <net/if.h>
46158ff5 36#include <net/if_var.h> /* for struct ifaddr */
ca74a0a2 37#include <net/route.h> /* for RTX_IFA */
46158ff5
AL
38#include <netinet/in.h>
39#include <netinet/in_var.h>
40#include <arpa/inet.h>
41#include <netdb.h>
ca74a0a2
SZ
42
43#include <err.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48
ca74a0a2
SZ
49#include "ifconfig.h"
50
51static struct ifaliasreq in_addreq;
52static struct ifreq in_ridreq;
51a3d09e 53static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */
ca74a0a2
SZ
54
55static void
51a3d09e 56in_status(int s __unused, const struct rt_addrinfo *info)
ca74a0a2
SZ
57{
58 struct sockaddr_in *sin, null_sin;
51a3d09e 59 int error, n_flags;
46158ff5 60
ca74a0a2
SZ
61 memset(&null_sin, 0, sizeof(null_sin));
62
63 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
64 if (sin == NULL)
65 return;
66
51a3d09e
AL
67 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
68 n_flags = 0;
69 else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
70 n_flags = NI_NOFQDN;
71 else
72 n_flags = NI_NUMERICHOST;
73
74 error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf,
75 sizeof(addr_buf), NULL, 0, n_flags);
76 if (error != 0)
77 inet_ntop(AF_INET, &sin->sin_addr, addr_buf, sizeof(addr_buf));
78
79 printf("\tinet %s", addr_buf);
ca74a0a2
SZ
80
81 if (flags & IFF_POINTOPOINT) {
82 /* note RTAX_BRD overlap with IFF_BROADCAST */
83 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
84 if (!sin)
85 sin = &null_sin;
51a3d09e 86 printf(" --> %s", inet_ntoa(sin->sin_addr));
ca74a0a2
SZ
87 }
88
89 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
90 if (!sin)
91 sin = &null_sin;
51a3d09e
AL
92 if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) {
93 int cidr = 32;
94 unsigned long smask = ntohl(sin->sin_addr.s_addr);
95
96 while ((smask & 1) == 0) {
97 smask >>= 1;
98 cidr--;
99 if (cidr == 0)
100 break;
101 }
102 printf("/%d", cidr);
103 } else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0) {
104 printf(" netmask %s", inet_ntoa(sin->sin_addr));
105 } else {
106 printf(" netmask 0x%lx",
107 (unsigned long)ntohl(sin->sin_addr.s_addr));
108 }
ca74a0a2
SZ
109
110 if (flags & IFF_BROADCAST) {
111 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
112 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
113 if (sin && sin->sin_addr.s_addr != 0)
51a3d09e 114 printf(" broadcast %s", inet_ntoa(sin->sin_addr));
ca74a0a2
SZ
115 }
116 putchar('\n');
117}
118
119#define SIN(x) ((struct sockaddr_in *) &(x))
120static struct sockaddr_in *sintab[] = {
121 SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
122 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
123};
124
125static void
126in_getaddr(const char *s, int which)
127{
ca74a0a2
SZ
128 struct sockaddr_in *sin = sintab[which];
129 struct hostent *hp;
130 struct netent *np;
131
132 sin->sin_len = sizeof(*sin);
133 if (which != MASK)
134 sin->sin_family = AF_INET;
135
136 if (which == ADDR) {
137 char *p = NULL;
138
139 if((p = strrchr(s, '/')) != NULL) {
140 /* address is `name/masklen' */
141 int masklen;
142 int ret;
143 struct sockaddr_in *min = sintab[MASK];
144 *p = '\0';
145 ret = sscanf(p+1, "%u", &masklen);
146 if(ret != 1 || (masklen < 0 || masklen > 32)) {
147 *p = '/';
148 errx(1, "%s: bad value", s);
149 }
150 min->sin_len = sizeof(*min);
3f7b7260 151 min->sin_addr.s_addr = htonl(rounddown2(0xffffffff, 1LL << (32 - masklen)));
ca74a0a2
SZ
152 }
153 }
154
155 if (inet_aton(s, &sin->sin_addr))
156 return;
678e8cc6 157 if ((hp = gethostbyname(s)) != NULL)
46158ff5
AL
158 bcopy(hp->h_addr, (char *)&sin->sin_addr,
159 MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
678e8cc6 160 else if ((np = getnetbyname(s)) != NULL)
ca74a0a2
SZ
161 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
162 else
163 errx(1, "%s: bad value", s);
ca74a0a2
SZ
164}
165
166static void
167in_status_tunnel(int s)
168{
169 char src[NI_MAXHOST];
170 char dst[NI_MAXHOST];
171 struct ifreq ifr;
172 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
173
174 memset(&ifr, 0, sizeof(ifr));
80d2947b 175 strlcpy(ifr.ifr_name, name, IFNAMSIZ);
ca74a0a2
SZ
176
177 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
178 return;
179 if (sa->sa_family != AF_INET)
180 return;
181 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
182 src[0] = '\0';
183
184 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
185 return;
186 if (sa->sa_family != AF_INET)
187 return;
188 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
189 dst[0] = '\0';
190
191 printf("\ttunnel inet %s --> %s\n", src, dst);
192}
193
194static void
195in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
196{
197 struct ifaliasreq addreq;
198
199 memset(&addreq, 0, sizeof(addreq));
80d2947b 200 strlcpy(addreq.ifra_name, name, IFNAMSIZ);
ca74a0a2
SZ
201 memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
202 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
203
204 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
205 warn("SIOCSIFPHYADDR");
206}
207
208static struct afswtch af_inet = {
209 .af_name = "inet",
210 .af_af = AF_INET,
211 .af_status = in_status,
212 .af_getaddr = in_getaddr,
213 .af_status_tunnel = in_status_tunnel,
214 .af_settunnel = in_set_tunnel,
215 .af_difaddr = SIOCDIFADDR,
216 .af_aifaddr = SIOCAIFADDR,
217 .af_ridreq = &in_ridreq,
218 .af_addreq = &in_addreq,
219};
220
d44b223a 221static __constructor(101) void
ca74a0a2
SZ
222inet_ctor(void)
223{
224 af_register(&af_inet);
225}