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