pthread: Implement pthread_attr_{get,set}affinity_np
[dragonfly.git] / sbin / ifconfig / ifcarp.c
blobabb2ae1047db9ac61815e3cad814164f53185d3b
1 /*
2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. 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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sbin/ifconfig/ifcarp.c,v 1.2 2005/02/22 14:07:47 glebius Exp $
28 * $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <netinet/ip_carp.h>
43 #include <net/route.h>
45 #include <arpa/inet.h>
47 #include <ctype.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <err.h>
51 #include <errno.h>
53 #include "ifconfig.h"
55 static const char *carp_states[] = { CARP_STATES };
57 void carp_status(int s);
58 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
59 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
60 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
61 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
63 void
64 carp_status(int s)
66 const char *state;
67 struct carpreq carpr;
68 struct ifdrv ifd;
69 char devname[IFNAMSIZ];
71 memset((char *)&carpr, 0, sizeof(struct carpreq));
72 ifr.ifr_data = (caddr_t)&carpr;
74 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
75 return;
77 if (carpr.carpr_vhid > 0) {
78 if (carpr.carpr_state > CARP_MAXSTATE)
79 state = "<UNKNOWN>";
80 else
81 state = carp_states[carpr.carpr_state];
83 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
84 state, carpr.carpr_vhid, carpr.carpr_advbase,
85 carpr.carpr_advskew);
88 memset(&ifd, 0, sizeof(ifd));
89 strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
90 ifd.ifd_cmd = CARPGDEVNAME;
91 ifd.ifd_len = sizeof(devname);
92 ifd.ifd_data = devname;
93 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
94 strlcpy(devname, "none", sizeof(devname));
95 if (devname[0] != '\0')
96 printf("\tcarpdev: %s\n", devname);
99 void
100 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
102 struct carpreq carpr;
104 memset((char *)&carpr, 0, sizeof(struct carpreq));
105 ifr.ifr_data = (caddr_t)&carpr;
107 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
108 err(1, "SIOCGVH");
110 /* XXX Should hash the password into the key here, perhaps? */
111 strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
113 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
114 err(1, "SIOCSVH");
117 void
118 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
120 int vhid;
121 struct carpreq carpr;
123 vhid = atoi(val);
125 if (vhid <= 0)
126 errx(1, "vhid must be greater than 0");
128 memset((char *)&carpr, 0, sizeof(struct carpreq));
129 ifr.ifr_data = (caddr_t)&carpr;
131 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
132 err(1, "SIOCGVH");
134 carpr.carpr_vhid = vhid;
136 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
137 err(1, "SIOCSVH");
140 void
141 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
143 int advskew;
144 struct carpreq carpr;
146 advskew = atoi(val);
148 memset((char *)&carpr, 0, sizeof(struct carpreq));
149 ifr.ifr_data = (caddr_t)&carpr;
151 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
152 err(1, "SIOCGVH");
154 carpr.carpr_advskew = advskew;
156 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
157 err(1, "SIOCSVH");
160 void
161 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
163 int advbase;
164 struct carpreq carpr;
166 advbase = atoi(val);
168 memset((char *)&carpr, 0, sizeof(struct carpreq));
169 ifr.ifr_data = (caddr_t)&carpr;
171 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
172 err(1, "SIOCGVH");
174 carpr.carpr_advbase = advbase;
176 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
177 err(1, "SIOCSVH");
180 static void
181 getcarp_vhaddr(const char *val, int d, int s, const struct afswtch *afp)
183 #define VHADDR_PFMT "%-15s %-15s %s\n"
185 struct ifdrv ifd;
186 struct ifcarpvhaddr *carpa;
187 int count, i;
189 memset(&ifd, 0, sizeof(ifd));
190 strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
191 ifd.ifd_cmd = CARPGVHADDR;
192 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
193 return;
194 if (ifd.ifd_len != 0) {
195 carpa = malloc(ifd.ifd_len);
196 if (carpa == NULL)
197 return;
199 ifd.ifd_cmd = CARPGVHADDR;
200 ifd.ifd_data = carpa;
201 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0) {
202 free(carpa);
203 return;
205 } else {
206 carpa = NULL;
208 count = ifd.ifd_len / sizeof(*carpa);
209 if (count != 0)
210 printf(VHADDR_PFMT, "virtual addr", "backing addr", "flags");
211 for (i = 0; i < count; ++i) {
212 char flags[16];
213 char baddr[INET_ADDRSTRLEN];
214 int a = 0;
216 memset(flags, 0, sizeof(flags));
217 flags[a] = '*';
218 if (carpa[i].carpa_flags & CARP_VHAF_OWNER)
219 flags[a++] = 'O';
221 memset(baddr, 0, sizeof(baddr));
222 baddr[0] = '*';
223 if (carpa[i].carpa_baddr.sin_addr.s_addr != INADDR_ANY) {
224 inet_ntop(AF_INET, &carpa[i].carpa_baddr.sin_addr,
225 baddr, sizeof(baddr));
228 printf(VHADDR_PFMT, inet_ntoa(carpa[i].carpa_addr.sin_addr),
229 baddr, flags);
231 if (carpa != NULL)
232 free(carpa);
234 #undef VHADDR_PFMT
237 static struct cmd carp_cmds[] = {
238 DEF_CMD_ARG("advbase", setcarp_advbase),
239 DEF_CMD_ARG("advskew", setcarp_advskew),
240 DEF_CMD_ARG("pass", setcarp_passwd),
241 DEF_CMD_ARG("vhid", setcarp_vhid),
242 DEF_CMD("vhaddr", 1, getcarp_vhaddr)
244 static struct afswtch af_carp = {
245 .af_name = "af_carp",
246 .af_af = AF_UNSPEC,
247 .af_other_status = carp_status,
250 static __constructor(101) void
251 carp_ctor(void)
253 #define N(a) (sizeof(a) / sizeof(a[0]))
254 int i;
256 for (i = 0; i < N(carp_cmds); i++)
257 cmd_register(&carp_cmds[i]);
258 af_register(&af_carp);
259 #undef N