]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_inet.c
9ad175e305608b94d0cf14325b269cab1eb62199
2 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static const char rcsid
[] =
60 "$FreeBSD: src/sbin/ifconfig/af_inet.c,v 1.3.6.1 2008/11/25 02:59:29 kensmith Exp $";
63 #include <sys/types.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
75 #include <netinet/in.h>
76 #include <net/if_var.h> /* for struct ifaddr */
77 #include <netinet/in_var.h>
78 #include <arpa/inet.h>
83 static struct ifaliasreq in_addreq
;
84 static struct ifreq in_ridreq
;
87 in_status(int s __unused
, const struct ifaddrs
*ifa
)
89 struct sockaddr_in
*sin
, null_sin
;
91 memset(&null_sin
, 0, sizeof(null_sin
));
93 sin
= (struct sockaddr_in
*)ifa
->ifa_addr
;
97 printf("\tinet %s ", inet_ntoa(sin
->sin_addr
));
99 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
100 sin
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
103 printf("--> %s ", inet_ntoa(sin
->sin_addr
));
106 sin
= (struct sockaddr_in
*)ifa
->ifa_netmask
;
109 printf("netmask 0x%lx ", (unsigned long)ntohl(sin
->sin_addr
.s_addr
));
111 if (ifa
->ifa_flags
& IFF_BROADCAST
) {
112 sin
= (struct sockaddr_in
*)ifa
->ifa_broadaddr
;
113 if (sin
!= NULL
&& sin
->sin_addr
.s_addr
!= 0)
114 printf("broadcast %s", inet_ntoa(sin
->sin_addr
));
119 #define SIN(x) ((struct sockaddr_in *) &(x))
120 static 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
)
126 in_getaddr(const char *s
, int which
)
129 #define MIN(a,b) ((a)<(b)?(a):(b))
131 struct sockaddr_in
*sin
= sintab
[which
];
135 sin
->sin_len
= sizeof(*sin
);
137 sin
->sin_family
= AF_INET
;
142 if((p
= strrchr(s
, '/')) != NULL
) {
143 /* address is `name/masklen' */
146 struct sockaddr_in
*min
= sintab
[MASK
];
148 ret
= sscanf(p
+1, "%u", &masklen
);
149 if(ret
!= 1 || (masklen
< 0 || masklen
> 32)) {
151 errx(1, "%s: bad value", s
);
153 min
->sin_len
= sizeof(*min
);
154 min
->sin_addr
.s_addr
= htonl(~((1LL << (32 - masklen
)) - 1) &
159 if (inet_aton(s
, &sin
->sin_addr
))
161 if ((hp
= gethostbyname(s
)) != 0)
162 bcopy(hp
->h_addr
, (char *)&sin
->sin_addr
,
163 MIN(hp
->h_length
, sizeof(sin
->sin_addr
)));
164 else if ((np
= getnetbyname(s
)) != 0)
165 sin
->sin_addr
= inet_makeaddr(np
->n_net
, INADDR_ANY
);
167 errx(1, "%s: bad value", s
);
172 in_status_tunnel(int s
)
174 char src
[NI_MAXHOST
];
175 char dst
[NI_MAXHOST
];
177 const struct sockaddr
*sa
= (const struct sockaddr
*) &ifr
.ifr_addr
;
179 memset(&ifr
, 0, sizeof(ifr
));
180 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
182 if (ioctl(s
, SIOCGIFPSRCADDR
, (caddr_t
)&ifr
) < 0)
184 if (sa
->sa_family
!= AF_INET
)
186 if (getnameinfo(sa
, sa
->sa_len
, src
, sizeof(src
), 0, 0, NI_NUMERICHOST
) != 0)
189 if (ioctl(s
, SIOCGIFPDSTADDR
, (caddr_t
)&ifr
) < 0)
191 if (sa
->sa_family
!= AF_INET
)
193 if (getnameinfo(sa
, sa
->sa_len
, dst
, sizeof(dst
), 0, 0, NI_NUMERICHOST
) != 0)
196 printf("\ttunnel inet %s --> %s\n", src
, dst
);
200 in_set_tunnel(int s
, struct addrinfo
*srcres
, struct addrinfo
*dstres
)
202 struct ifaliasreq addreq
;
204 memset(&addreq
, 0, sizeof(addreq
));
205 strncpy(addreq
.ifra_name
, name
, IFNAMSIZ
);
206 memcpy(&addreq
.ifra_addr
, srcres
->ai_addr
, srcres
->ai_addr
->sa_len
);
207 memcpy(&addreq
.ifra_dstaddr
, dstres
->ai_addr
, dstres
->ai_addr
->sa_len
);
209 if (ioctl(s
, SIOCSIFPHYADDR
, &addreq
) < 0)
210 warn("SIOCSIFPHYADDR");
214 in_set_router(int s
, int enable
)
218 bzero(&ifr
, sizeof (ifr
));
219 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
220 ifr
.ifr_intval
= enable
;
222 if (ioctl(s
, SIOCSETROUTERMODE
, &ifr
) < 0)
223 warn("SIOCSETROUTERMODE");
226 static struct afswtch af_inet
= {
229 .af_status
= in_status
,
230 .af_getaddr
= in_getaddr
,
231 .af_status_tunnel
= in_status_tunnel
,
232 .af_settunnel
= in_set_tunnel
,
233 .af_setrouter
= in_set_router
,
234 .af_difaddr
= SIOCDIFADDR
,
235 .af_aifaddr
= SIOCAIFADDR
,
236 .af_ridreq
= &in_ridreq
,
237 .af_addreq
= &in_addreq
,
240 static __constructor
void
243 af_register(&af_inet
);