source: rtems-libbsd/freebsd/sbin/ifconfig/af_inet.c @ 7eeb079

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 7eeb079 was 7eeb079, checked in by Sebastian Huber <sebastian.huber@…>, on 02/02/15 at 13:27:13

Update to FreeBSD 9.3

  • Property mode set to 100644
File size: 6.0 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
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 * 4. 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#ifndef lint
33static const char rcsid[] =
34  "$FreeBSD$";
35#endif /* not lint */
36
37#include <rtems/bsd/sys/types.h>
38#include <sys/ioctl.h>
39#include <sys/socket.h>
40#include <net/if.h>
41
42#include <ctype.h>
43#include <err.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <ifaddrs.h>
49
50#include <netinet/in.h>
51#include <net/if_var.h>         /* for struct ifaddr */
52#include <netinet/in_var.h>
53#include <arpa/inet.h>
54#include <netdb.h>
55
56#include "ifconfig.h"
57
58static struct in_aliasreq in_addreq;
59static struct ifreq in_ridreq;
60
61static void
62in_status(int s __unused, const struct ifaddrs *ifa)
63{
64        struct sockaddr_in *sin, null_sin;
65       
66        memset(&null_sin, 0, sizeof(null_sin));
67
68        sin = (struct sockaddr_in *)ifa->ifa_addr;
69        if (sin == NULL)
70                return;
71
72        printf("\tinet %s ", inet_ntoa(sin->sin_addr));
73
74        if (ifa->ifa_flags & IFF_POINTOPOINT) {
75                sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
76                if (sin == NULL)
77                        sin = &null_sin;
78                printf("--> %s ", inet_ntoa(sin->sin_addr));
79        }
80
81        sin = (struct sockaddr_in *)ifa->ifa_netmask;
82        if (sin == NULL)
83                sin = &null_sin;
84        printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
85
86        if (ifa->ifa_flags & IFF_BROADCAST) {
87                sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
88                if (sin != NULL && sin->sin_addr.s_addr != 0)
89                        printf("broadcast %s", inet_ntoa(sin->sin_addr));
90        }
91        putchar('\n');
92}
93
94#define SIN(x) ((struct sockaddr_in *) &(x))
95static struct sockaddr_in *sintab[] = {
96        SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
97        SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
98};
99
100static void
101in_getaddr(const char *s, int which)
102{
103#define MIN(a,b)        ((a)<(b)?(a):(b))
104        struct sockaddr_in *sin = sintab[which];
105        struct hostent *hp;
106        struct netent *np;
107
108        sin->sin_len = sizeof(*sin);
109        sin->sin_family = AF_INET;
110
111        if (which == ADDR) {
112                char *p = NULL;
113
114                if((p = strrchr(s, '/')) != NULL) {
115                        const char *errstr;
116                        /* address is `name/masklen' */
117                        int masklen;
118                        struct sockaddr_in *min = sintab[MASK];
119                        *p = '\0';
120                        if (!isdigit((unsigned char)*(p + 1)))
121                                errstr = "invalid";
122                        else
123                                masklen = (int)strtonum(p + 1, 0, 32, &errstr);
124                        if (errstr != NULL) {
125                                *p = '/';
126                                errx(1, "%s: bad value (width %s)", s, errstr);
127                        }
128                        min->sin_family = AF_INET;
129                        min->sin_len = sizeof(*min);
130                        min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
131                                              0xffffffff);
132                }
133        }
134
135        if (inet_aton(s, &sin->sin_addr))
136                return;
137        if ((hp = gethostbyname(s)) != 0)
138                bcopy(hp->h_addr, (char *)&sin->sin_addr,
139                    MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
140        else if ((np = getnetbyname(s)) != 0)
141                sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
142        else
143                errx(1, "%s: bad value", s);
144#undef MIN
145}
146
147static void
148in_status_tunnel(int s)
149{
150        char src[NI_MAXHOST];
151        char dst[NI_MAXHOST];
152        struct ifreq ifr;
153        const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
154
155        memset(&ifr, 0, sizeof(ifr));
156        strncpy(ifr.ifr_name, name, IFNAMSIZ);
157
158        if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
159                return;
160        if (sa->sa_family != AF_INET)
161                return;
162        if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
163                src[0] = '\0';
164
165        if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
166                return;
167        if (sa->sa_family != AF_INET)
168                return;
169        if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
170                dst[0] = '\0';
171
172        printf("\ttunnel inet %s --> %s\n", src, dst);
173}
174
175static void
176in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
177{
178        struct in_aliasreq addreq;
179
180        memset(&addreq, 0, sizeof(addreq));
181        strncpy(addreq.ifra_name, name, IFNAMSIZ);
182        memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
183        memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
184
185        if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
186                warn("SIOCSIFPHYADDR");
187}
188
189static struct afswtch af_inet = {
190        .af_name        = "inet",
191        .af_af          = AF_INET,
192        .af_status      = in_status,
193        .af_getaddr     = in_getaddr,
194        .af_status_tunnel = in_status_tunnel,
195        .af_settunnel   = in_set_tunnel,
196        .af_difaddr     = SIOCDIFADDR,
197        .af_aifaddr     = SIOCAIFADDR,
198        .af_ridreq      = &in_ridreq,
199        .af_addreq      = &in_addreq,
200};
201
202#ifndef __rtems__
203static __constructor void
204#else /* __rtems__ */
205void
206#endif /* __rtems__ */
207inet_ctor(void)
208{
209
210#ifndef RESCUE
211        if (!feature_present("inet"))
212                return;
213#endif
214
215#ifdef __rtems__
216        memset(&in_addreq, 0, sizeof(in_addreq));
217        memset(&in_ridreq, 0, sizeof(in_ridreq));
218#endif /* __rtems__ */
219
220        af_register(&af_inet);
221}
Note: See TracBrowser for help on using the repository browser.