pthread: Implement pthread_attr_{get,set}affinity_np
[dragonfly.git] / sbin / ifconfig / af_inet.c
blob419b5ad40e7b1201a90d21e6631056f9bf5e881b
1 /*
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
7 * are met:
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 * 3. 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
27 * SUCH DAMAGE.
29 * $FreeBSD: src/sbin/ifconfig/af_inet.c,v 1.2 2005/06/16 19:37:09 ume Exp $
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #include <net/route.h> /* for RTX_IFA */
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include <netinet/in.h>
45 #include <net/if_var.h> /* for struct ifaddr */
46 #include <netinet/in_var.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
50 #include "ifconfig.h"
52 static struct ifaliasreq in_addreq;
53 static struct ifreq in_ridreq;
55 static void
56 in_status(int s __unused, const struct rt_addrinfo * info)
58 struct sockaddr_in *sin, null_sin;
60 memset(&null_sin, 0, sizeof(null_sin));
62 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
63 if (sin == NULL)
64 return;
66 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
68 if (flags & IFF_POINTOPOINT) {
69 /* note RTAX_BRD overlap with IFF_BROADCAST */
70 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
71 if (!sin)
72 sin = &null_sin;
73 printf("--> %s ", inet_ntoa(sin->sin_addr));
76 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
77 if (!sin)
78 sin = &null_sin;
79 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
81 if (flags & IFF_BROADCAST) {
82 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
83 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
84 if (sin && sin->sin_addr.s_addr != 0)
85 printf("broadcast %s", inet_ntoa(sin->sin_addr));
87 putchar('\n');
90 #define SIN(x) ((struct sockaddr_in *) &(x))
91 static struct sockaddr_in *sintab[] = {
92 SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
93 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
96 static void
97 in_getaddr(const char *s, int which)
99 struct sockaddr_in *sin = sintab[which];
100 struct hostent *hp;
101 struct netent *np;
103 sin->sin_len = sizeof(*sin);
104 if (which != MASK)
105 sin->sin_family = AF_INET;
107 if (which == ADDR) {
108 char *p = NULL;
110 if((p = strrchr(s, '/')) != NULL) {
111 /* address is `name/masklen' */
112 int masklen;
113 int ret;
114 struct sockaddr_in *min = sintab[MASK];
115 *p = '\0';
116 ret = sscanf(p+1, "%u", &masklen);
117 if(ret != 1 || (masklen < 0 || masklen > 32)) {
118 *p = '/';
119 errx(1, "%s: bad value", s);
121 min->sin_len = sizeof(*min);
122 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
123 0xffffffff);
127 if (inet_aton(s, &sin->sin_addr))
128 return;
129 if ((hp = gethostbyname(s)) != NULL)
130 bcopy(hp->h_addr, (char *)&sin->sin_addr,
131 MIN(hp->h_length, sizeof(sin->sin_addr)));
132 else if ((np = getnetbyname(s)) != NULL)
133 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
134 else
135 errx(1, "%s: bad value", s);
138 static void
139 in_status_tunnel(int s)
141 char src[NI_MAXHOST];
142 char dst[NI_MAXHOST];
143 struct ifreq ifr;
144 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
146 memset(&ifr, 0, sizeof(ifr));
147 strncpy(ifr.ifr_name, name, IFNAMSIZ);
149 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
150 return;
151 if (sa->sa_family != AF_INET)
152 return;
153 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
154 src[0] = '\0';
156 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
157 return;
158 if (sa->sa_family != AF_INET)
159 return;
160 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
161 dst[0] = '\0';
163 printf("\ttunnel inet %s --> %s\n", src, dst);
166 static void
167 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
169 struct ifaliasreq addreq;
171 memset(&addreq, 0, sizeof(addreq));
172 strncpy(addreq.ifra_name, name, IFNAMSIZ);
173 memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
174 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
176 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
177 warn("SIOCSIFPHYADDR");
180 static struct afswtch af_inet = {
181 .af_name = "inet",
182 .af_af = AF_INET,
183 .af_status = in_status,
184 .af_getaddr = in_getaddr,
185 .af_status_tunnel = in_status_tunnel,
186 .af_settunnel = in_set_tunnel,
187 .af_difaddr = SIOCDIFADDR,
188 .af_aifaddr = SIOCAIFADDR,
189 .af_ridreq = &in_ridreq,
190 .af_addreq = &in_addreq,
193 static __constructor(101) void
194 inet_ctor(void)
196 af_register(&af_inet);