]>
git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_link.c
2 * Copyright (c) 2009 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
58 #include <sys/types.h>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/ethernet.h>
75 static struct ifreq link_ridreq
;
78 link_status(int s __unused
, const struct ifaddrs
*ifa
)
80 /* XXX no const 'cuz LLADDR is defined wrong */
81 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) ifa
->ifa_addr
;
83 if (sdl
!= NULL
&& sdl
->sdl_alen
> 0) {
85 if (sdl
->sdl_type
== IFT_ETHER
&&
86 sdl
->sdl_alen
== ETHER_ADDR_LEN
)
87 printf("\tether %s\n",
88 ether_ntoa((struct ether_addr
*)LLADDR(sdl
)));
90 int n
= sdl
->sdl_nlen
> 0 ? sdl
->sdl_nlen
+ 1 : 0;
92 printf("\tlladdr %s\n", link_ntoa(sdl
) + n
);
95 char *cp
= (char *)LLADDR(sdl
);
96 int n
= sdl
->sdl_alen
;
98 if (sdl
->sdl_type
== IFT_ETHER
)
101 printf ("\tlladdr ");
103 printf("%02x%c",*cp
++ & 0xff, n
>0? ':' : ' ');
110 link_getaddr(const char *addr
, int which
)
113 struct sockaddr_dl sdl
;
114 struct sockaddr
*sa
= &link_ridreq
.ifr_addr
;
115 size_t slen
= strlen(addr
);
118 errx(1, "can't set link-level netmask or broadcast");
119 if ((temp
= malloc(slen
+ 2)) == NULL
)
120 errx(1, "malloc failed");
122 strlcpy(temp
+ 1, addr
, slen
+ 1);
123 sdl
.sdl_len
= sizeof(sdl
);
124 link_addr(temp
, &sdl
);
126 if (sdl
.sdl_alen
> sizeof(sa
->sa_data
))
127 errx(1, "malformed link-level address");
128 sa
->sa_family
= AF_LINK
;
129 sa
->sa_len
= sdl
.sdl_alen
;
130 bcopy(LLADDR(&sdl
), sa
->sa_data
, sdl
.sdl_alen
);
133 static struct afswtch af_link
= {
136 .af_status
= link_status
,
137 .af_getaddr
= link_getaddr
,
138 .af_aifaddr
= SIOCSIFLLADDR
,
139 .af_addreq
= &link_ridreq
,
141 static struct afswtch af_ether
= {
144 .af_status
= link_status
,
145 .af_getaddr
= link_getaddr
,
146 .af_aifaddr
= SIOCSIFLLADDR
,
147 .af_addreq
= &link_ridreq
,
149 static struct afswtch af_lladdr
= {
152 .af_status
= link_status
,
153 .af_getaddr
= link_getaddr
,
154 .af_aifaddr
= SIOCSIFLLADDR
,
155 .af_addreq
= &link_ridreq
,
158 static __constructor
void
161 af_register(&af_link
);
162 af_register(&af_ether
);
163 af_register(&af_lladdr
);