agr.c revision 1.7 1 /* $NetBSD: agr.c,v 1.7 2008/05/06 21:16:52 dyoung Exp $ */
2
3 /*-
4 * Copyright (c)2005 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if !defined(lint)
31 __RCSID("$NetBSD: agr.c,v 1.7 2008/05/06 21:16:52 dyoung Exp $");
32 #endif /* !defined(lint) */
33
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36
37 #include <net/if.h>
38 #include <net/agr/if_agrioctl.h>
39
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <util.h>
47
48 #include "env.h"
49 #include "parse.h"
50 #include "extern.h"
51 #include "agr.h"
52
53 static int checkifname(const char *);
54 static void assertifname(const char *);
55
56 static int
57 checkifname(const char *ifname)
58 {
59
60 return strncmp(ifname, "agr", 3) != 0 ||
61 !isdigit((unsigned char)ifname[3]);
62 }
63
64 static void
65 assertifname(const char *ifname)
66 {
67
68 if (checkifname(ifname)) {
69 errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
70 }
71 }
72
73 int
74 agrsetport(prop_dictionary_t env, prop_dictionary_t xenv)
75 {
76 char buf[IFNAMSIZ];
77 struct agrreq ar;
78 int s;
79 const char *port;
80 const char *ifname;
81 struct ifreq ifr;
82 int64_t cmd;
83
84 if ((s = getsock(AF_UNSPEC)) == -1)
85 err(EXIT_FAILURE, "%s: getsock", __func__);
86
87 assertifname(ifr.ifr_name);
88
89 if (!prop_dictionary_get_int64(env, "agrcmd", &cmd)) {
90 warnx("%s.%d", __func__, __LINE__);
91 errno = ENOENT;
92 return -1;
93 }
94
95 if (!prop_dictionary_get_cstring_nocopy(env, "agrport", &port)) {
96 warnx("%s.%d", __func__, __LINE__);
97 errno = ENOENT;
98 return -1;
99 }
100 strlcpy(buf, port, sizeof(buf));
101
102 memset(&ifr, 0, sizeof(ifr));
103 if ((ifname = getifname(env)) == NULL)
104 err(EXIT_FAILURE, "%s: getifname", __func__);
105 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
106 memset(&ar, 0, sizeof(ar));
107 ar.ar_version = AGRREQ_VERSION;
108 ar.ar_cmd = cmd;
109 ar.ar_buf = buf;
110 ar.ar_buflen = strlen(buf);
111 ifr.ifr_data = &ar;
112
113 if (ioctl(s, SIOCSETAGR, &ifr) == -1)
114 err(EXIT_FAILURE, "SIOCSETAGR");
115 return 0;
116 }
117
118 void
119 agr_status(prop_dictionary_t env, prop_dictionary_t oenv)
120 {
121 struct agrreq ar;
122 void *buf = NULL;
123 size_t buflen = 0;
124 struct agrportlist *apl;
125 struct agrportinfo *api;
126 int i;
127 int s;
128 const char *ifname;
129 struct ifreq ifr;
130
131 if ((s = getsock(AF_UNSPEC)) == -1)
132 err(EXIT_FAILURE, "%s: getsock", __func__);
133
134 memset(&ifr, 0, sizeof(ifr));
135 if ((ifname = getifname(env)) == NULL)
136 err(EXIT_FAILURE, "%s: getifname", __func__);
137 if (checkifname(ifname))
138 return;
139 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
140
141 again:
142 memset(&ar, 0, sizeof(ar));
143 ar.ar_version = AGRREQ_VERSION;
144 ar.ar_cmd = AGRCMD_PORTLIST;
145 ar.ar_buf = buf;
146 ar.ar_buflen = buflen;
147 ifr.ifr_data = &ar;
148
149 if (ioctl(s, SIOCGETAGR, &ifr) == -1) {
150 if (errno != E2BIG) {
151 warn("SIOCGETAGR");
152 return;
153 }
154
155 free(buf);
156 buf = NULL;
157 buflen = 0;
158 goto again;
159 }
160
161 if (buf == NULL) {
162 buflen = ar.ar_buflen;
163 buf = malloc(buflen);
164 if (buf == NULL) {
165 err(EXIT_FAILURE, "agr_status");
166 }
167 goto again;
168 }
169
170 apl = buf;
171 api = (void *)(apl + 1);
172
173 for (i = 0; i < apl->apl_nports; i++) {
174 char tmp[256];
175
176 snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
177 printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
178 api++;
179 }
180 }
181