1*b1d757bcSAlan Somers /*
2*b1d757bcSAlan Somers * Copyright (c) 1983, 1993
3*b1d757bcSAlan Somers * The Regents of the University of California. All rights reserved.
4*b1d757bcSAlan Somers *
5*b1d757bcSAlan Somers * Redistribution and use in source and binary forms, with or without
6*b1d757bcSAlan Somers * modification, are permitted provided that the following conditions
7*b1d757bcSAlan Somers * are met:
8*b1d757bcSAlan Somers * 1. Redistributions of source code must retain the above copyright
9*b1d757bcSAlan Somers * notice, this list of conditions and the following disclaimer.
10*b1d757bcSAlan Somers * 2. Redistributions in binary form must reproduce the above copyright
11*b1d757bcSAlan Somers * notice, this list of conditions and the following disclaimer in the
12*b1d757bcSAlan Somers * documentation and/or other materials provided with the distribution.
13*b1d757bcSAlan Somers * 3. Neither the name of the University nor the names of its contributors
14*b1d757bcSAlan Somers * may be used to endorse or promote products derived from this software
15*b1d757bcSAlan Somers * without specific prior written permission.
16*b1d757bcSAlan Somers *
17*b1d757bcSAlan Somers * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*b1d757bcSAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*b1d757bcSAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*b1d757bcSAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*b1d757bcSAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*b1d757bcSAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*b1d757bcSAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*b1d757bcSAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*b1d757bcSAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*b1d757bcSAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*b1d757bcSAlan Somers * SUCH DAMAGE.
28*b1d757bcSAlan Somers */
29*b1d757bcSAlan Somers #include <sys/param.h>
30*b1d757bcSAlan Somers #include <sys/ioctl.h>
31*b1d757bcSAlan Somers
32*b1d757bcSAlan Somers #include <net/if.h>
33*b1d757bcSAlan Somers #include <netinet/in.h>
34*b1d757bcSAlan Somers
35*b1d757bcSAlan Somers #include <errno.h>
36*b1d757bcSAlan Somers #include <ifaddrs.h>
37*b1d757bcSAlan Somers #include <string.h>
38*b1d757bcSAlan Somers #include <strings.h>
39*b1d757bcSAlan Somers
40*b1d757bcSAlan Somers #include "libifconfig.h"
41*b1d757bcSAlan Somers #include "libifconfig_internal.h"
42*b1d757bcSAlan Somers
43*b1d757bcSAlan Somers static const struct sockaddr_in NULL_SIN;
44*b1d757bcSAlan Somers
45*b1d757bcSAlan Somers static int
inet_prefixlen(const struct in_addr * addr)46*b1d757bcSAlan Somers inet_prefixlen(const struct in_addr *addr)
47*b1d757bcSAlan Somers {
48*b1d757bcSAlan Somers int x;
49*b1d757bcSAlan Somers
50*b1d757bcSAlan Somers x = ffs(ntohl(addr->s_addr));
51*b1d757bcSAlan Somers return (x == 0 ? 0 : 33 - x);
52*b1d757bcSAlan Somers }
53*b1d757bcSAlan Somers
54*b1d757bcSAlan Somers int
ifconfig_inet_get_addrinfo(ifconfig_handle_t * h __unused,const char * name __unused,struct ifaddrs * ifa,struct ifconfig_inet_addr * addr)55*b1d757bcSAlan Somers ifconfig_inet_get_addrinfo(ifconfig_handle_t *h __unused,
56*b1d757bcSAlan Somers const char *name __unused, struct ifaddrs *ifa,
57*b1d757bcSAlan Somers struct ifconfig_inet_addr *addr)
58*b1d757bcSAlan Somers {
59*b1d757bcSAlan Somers bzero(addr, sizeof(*addr));
60*b1d757bcSAlan Somers
61*b1d757bcSAlan Somers /* Set the address */
62*b1d757bcSAlan Somers if (ifa->ifa_addr == NULL) {
63*b1d757bcSAlan Somers return (-1);
64*b1d757bcSAlan Somers } else {
65*b1d757bcSAlan Somers addr->sin = (struct sockaddr_in *)ifa->ifa_addr;
66*b1d757bcSAlan Somers }
67*b1d757bcSAlan Somers
68*b1d757bcSAlan Somers /* Set the destination address */
69*b1d757bcSAlan Somers if (ifa->ifa_flags & IFF_POINTOPOINT) {
70*b1d757bcSAlan Somers if (ifa->ifa_dstaddr) {
71*b1d757bcSAlan Somers addr->dst = (struct sockaddr_in *)ifa->ifa_dstaddr;
72*b1d757bcSAlan Somers } else {
73*b1d757bcSAlan Somers addr->dst = &NULL_SIN;
74*b1d757bcSAlan Somers }
75*b1d757bcSAlan Somers }
76*b1d757bcSAlan Somers
77*b1d757bcSAlan Somers /* Set the netmask and prefixlen */
78*b1d757bcSAlan Somers if (ifa->ifa_netmask) {
79*b1d757bcSAlan Somers addr->netmask = (struct sockaddr_in *)ifa->ifa_netmask;
80*b1d757bcSAlan Somers } else {
81*b1d757bcSAlan Somers addr->netmask = &NULL_SIN;
82*b1d757bcSAlan Somers }
83*b1d757bcSAlan Somers addr->prefixlen = inet_prefixlen(&addr->netmask->sin_addr);
84*b1d757bcSAlan Somers
85*b1d757bcSAlan Somers /* Set the broadcast */
86*b1d757bcSAlan Somers if (ifa->ifa_flags & IFF_BROADCAST) {
87*b1d757bcSAlan Somers addr->broadcast = (struct sockaddr_in *)ifa->ifa_broadaddr;
88*b1d757bcSAlan Somers }
89*b1d757bcSAlan Somers
90*b1d757bcSAlan Somers /* Set the vhid */
91*b1d757bcSAlan Somers if (ifa->ifa_data) {
92*b1d757bcSAlan Somers addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid;
93*b1d757bcSAlan Somers }
94*b1d757bcSAlan Somers
95*b1d757bcSAlan Somers return (0);
96*b1d757bcSAlan Somers }
97