]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_inet.c
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 static const char rcsid
[] =
32 "$FreeBSD: src/sbin/ifconfig/af_inet.c,v 1.3.6.1 2008/11/25 02:59:29 kensmith Exp $";
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <net/if_var.h> /* for struct ifaddr */
49 #include <netinet/in_var.h>
50 #include <arpa/inet.h>
55 static struct ifaliasreq in_addreq
;
56 static struct ifreq in_ridreq
;
59 in_status(int s __unused
, const struct ifaddrs
*ifa
)
61 struct sockaddr_in
*sin
, null_sin
;
63 memset(&null_sin
, 0, sizeof(null_sin
));
65 sin
= (struct sockaddr_in
*)ifa
->ifa_addr
;
69 printf("\tinet %s ", inet_ntoa(sin
->sin_addr
));
71 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
72 sin
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
75 printf("--> %s ", inet_ntoa(sin
->sin_addr
));
78 sin
= (struct sockaddr_in
*)ifa
->ifa_netmask
;
81 printf("netmask 0x%lx ", (unsigned long)ntohl(sin
->sin_addr
.s_addr
));
83 if (ifa
->ifa_flags
& IFF_BROADCAST
) {
84 sin
= (struct sockaddr_in
*)ifa
->ifa_broadaddr
;
85 if (sin
!= NULL
&& sin
->sin_addr
.s_addr
!= 0)
86 printf("broadcast %s", inet_ntoa(sin
->sin_addr
));
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
)
98 in_getaddr(const char *s
, int which
)
101 #define MIN(a,b) ((a)<(b)?(a):(b))
103 struct sockaddr_in
*sin
= sintab
[which
];
107 sin
->sin_len
= sizeof(*sin
);
109 sin
->sin_family
= AF_INET
;
114 if((p
= strrchr(s
, '/')) != NULL
) {
115 /* address is `name/masklen' */
118 struct sockaddr_in
*min
= sintab
[MASK
];
120 ret
= sscanf(p
+1, "%u", &masklen
);
121 if(ret
!= 1 || (masklen
< 0 || masklen
> 32)) {
123 errx(1, "%s: bad value", s
);
125 min
->sin_len
= sizeof(*min
);
126 min
->sin_addr
.s_addr
= htonl(~((1LL << (32 - masklen
)) - 1) &
131 if (inet_aton(s
, &sin
->sin_addr
))
133 if ((hp
= gethostbyname(s
)) != 0)
134 bcopy(hp
->h_addr
, (char *)&sin
->sin_addr
,
135 MIN(hp
->h_length
, sizeof(sin
->sin_addr
)));
136 else if ((np
= getnetbyname(s
)) != 0)
137 sin
->sin_addr
= inet_makeaddr(np
->n_net
, INADDR_ANY
);
139 errx(1, "%s: bad value", s
);
144 in_status_tunnel(int s
)
146 char src
[NI_MAXHOST
];
147 char dst
[NI_MAXHOST
];
149 const struct sockaddr
*sa
= (const struct sockaddr
*) &ifr
.ifr_addr
;
151 memset(&ifr
, 0, sizeof(ifr
));
152 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
154 if (ioctl(s
, SIOCGIFPSRCADDR
, (caddr_t
)&ifr
) < 0)
156 if (sa
->sa_family
!= AF_INET
)
158 if (getnameinfo(sa
, sa
->sa_len
, src
, sizeof(src
), 0, 0, NI_NUMERICHOST
) != 0)
161 if (ioctl(s
, SIOCGIFPDSTADDR
, (caddr_t
)&ifr
) < 0)
163 if (sa
->sa_family
!= AF_INET
)
165 if (getnameinfo(sa
, sa
->sa_len
, dst
, sizeof(dst
), 0, 0, NI_NUMERICHOST
) != 0)
168 printf("\ttunnel inet %s --> %s\n", src
, dst
);
172 in_set_tunnel(int s
, struct addrinfo
*srcres
, struct addrinfo
*dstres
)
174 struct ifaliasreq addreq
;
176 memset(&addreq
, 0, sizeof(addreq
));
177 strncpy(addreq
.ifra_name
, name
, IFNAMSIZ
);
178 memcpy(&addreq
.ifra_addr
, srcres
->ai_addr
, srcres
->ai_addr
->sa_len
);
179 memcpy(&addreq
.ifra_dstaddr
, dstres
->ai_addr
, dstres
->ai_addr
->sa_len
);
181 if (ioctl(s
, SIOCSIFPHYADDR
, &addreq
) < 0)
182 warn("SIOCSIFPHYADDR");
185 static struct afswtch af_inet
= {
188 .af_status
= in_status
,
189 .af_getaddr
= in_getaddr
,
190 .af_status_tunnel
= in_status_tunnel
,
191 .af_settunnel
= in_set_tunnel
,
192 .af_difaddr
= SIOCDIFADDR
,
193 .af_aifaddr
= SIOCAIFADDR
,
194 .af_ridreq
= &in_ridreq
,
195 .af_addreq
= &in_addreq
,
198 static __constructor
void
201 af_register(&af_inet
);