Small comma fixes.
[dragonfly.git] / sbin / ifconfig / af_inet.c
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.
13  * 4. Neither the name of the University nor the names of its contributors
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 $
30  * $DragonFly: src/sbin/ifconfig/af_inet.c,v 1.1 2006/04/02 03:33:59 sephe Exp $
31  */
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37 #include <net/route.h>          /* for RTX_IFA */
38
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include <netinet/in.h>
46 #include <net/if_var.h>         /* for struct ifaddr */
47 #include <netinet/in_var.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50
51 #include "ifconfig.h"
52
53 static struct ifaliasreq in_addreq;
54 static struct ifreq in_ridreq;
55
56 static void
57 in_status(int s __unused, const struct rt_addrinfo * info)
58 {
59         struct sockaddr_in *sin, null_sin;
60         
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
67         printf("\tinet %s ", inet_ntoa(sin->sin_addr));
68
69         if (flags & IFF_POINTOPOINT) {
70                 /* note RTAX_BRD overlap with IFF_BROADCAST */
71                 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
72                 if (!sin)
73                         sin = &null_sin;
74                 printf("--> %s ", inet_ntoa(sin->sin_addr));
75         }
76
77         sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
78         if (!sin)
79                 sin = &null_sin;
80         printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
81
82         if (flags & IFF_BROADCAST) {
83                 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
84                 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
85                 if (sin && sin->sin_addr.s_addr != 0)
86                         printf("broadcast %s", inet_ntoa(sin->sin_addr));
87         }
88         putchar('\n');
89 }
90
91 #define SIN(x) ((struct sockaddr_in *) &(x))
92 static struct sockaddr_in *sintab[] = {
93         SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
94         SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
95 };
96
97 static void
98 in_getaddr(const char *s, int which)
99 {
100 #define MIN(a,b)        ((a)<(b)?(a):(b))
101         struct sockaddr_in *sin = sintab[which];
102         struct hostent *hp;
103         struct netent *np;
104
105         sin->sin_len = sizeof(*sin);
106         if (which != MASK)
107                 sin->sin_family = AF_INET;
108
109         if (which == ADDR) {
110                 char *p = NULL;
111
112                 if((p = strrchr(s, '/')) != NULL) {
113                         /* address is `name/masklen' */
114                         int masklen;
115                         int ret;
116                         struct sockaddr_in *min = sintab[MASK];
117                         *p = '\0';
118                         ret = sscanf(p+1, "%u", &masklen);
119                         if(ret != 1 || (masklen < 0 || masklen > 32)) {
120                                 *p = '/';
121                                 errx(1, "%s: bad value", s);
122                         }
123                         min->sin_len = sizeof(*min);
124                         min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 
125                                               0xffffffff);
126                 }
127         }
128
129         if (inet_aton(s, &sin->sin_addr))
130                 return;
131         if ((hp = gethostbyname(s)) != 0)
132                 bcopy(hp->h_addr, (char *)&sin->sin_addr, 
133                     MIN(hp->h_length, sizeof(sin->sin_addr)));
134         else if ((np = getnetbyname(s)) != 0)
135                 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
136         else
137                 errx(1, "%s: bad value", s);
138 #undef MIN
139 }
140
141 static void
142 in_status_tunnel(int s)
143 {
144         char src[NI_MAXHOST];
145         char dst[NI_MAXHOST];
146         struct ifreq ifr;
147         const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
148
149         memset(&ifr, 0, sizeof(ifr));
150         strncpy(ifr.ifr_name, name, IFNAMSIZ);
151
152         if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
153                 return;
154         if (sa->sa_family != AF_INET)
155                 return;
156         if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
157                 src[0] = '\0';
158
159         if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
160                 return;
161         if (sa->sa_family != AF_INET)
162                 return;
163         if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
164                 dst[0] = '\0';
165
166         printf("\ttunnel inet %s --> %s\n", src, dst);
167 }
168
169 static void
170 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
171 {
172         struct ifaliasreq addreq;
173
174         memset(&addreq, 0, sizeof(addreq));
175         strncpy(addreq.ifra_name, name, IFNAMSIZ);
176         memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
177         memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
178
179         if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
180                 warn("SIOCSIFPHYADDR");
181 }
182
183 static struct afswtch af_inet = {
184         .af_name        = "inet",
185         .af_af          = AF_INET,
186         .af_status      = in_status,
187         .af_getaddr     = in_getaddr,
188         .af_status_tunnel = in_status_tunnel,
189         .af_settunnel   = in_set_tunnel,
190         .af_difaddr     = SIOCDIFADDR,
191         .af_aifaddr     = SIOCAIFADDR,
192         .af_ridreq      = &in_ridreq,
193         .af_addreq      = &in_addreq,
194 };
195
196 static __constructor void
197 inet_ctor(void)
198 {
199         af_register(&af_inet);
200 }