1 /* $NetBSD: agr.c,v 1.15 2008/07/15 21:27:58 dyoung Exp $ */
4 * Copyright (c)2005 YAMAMOTO Takashi,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: agr.c,v 1.15 2008/07/15 21:27:58 dyoung Exp $");
32 #endif /* !defined(lint) */
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
38 #include <net/agr/if_agrioctl.h>
53 static int agrsetport(prop_dictionary_t
, prop_dictionary_t
);
54 static void agr_constructor(void) __attribute__((constructor
));
55 static int checkifname(prop_dictionary_t
);
56 static void assertifname(prop_dictionary_t
);
58 static struct piface agrif
= PIFACE_INITIALIZER(&agrif
, "agr interface",
59 agrsetport
, "agrport", &command_root
.pb_parser
);
61 static const struct kwinst agrkw
[] = {
62 {.k_word
= "agrport", .k_type
= KW_T_INT
, .k_int
= AGRCMD_ADDPORT
,
63 .k_nextparser
= &agrif
.pif_parser
}
64 , {.k_word
= "-agrport", .k_type
= KW_T_INT
, .k_int
= AGRCMD_REMPORT
,
65 .k_nextparser
= &agrif
.pif_parser
}
68 struct pkw agr
= PKW_INITIALIZER(&agr
, "agr", NULL
, "agrcmd",
69 agrkw
, __arraycount(agrkw
), NULL
);
72 checkifname(prop_dictionary_t env
)
76 if ((ifname
= getifname(env
)) == NULL
)
79 return strncmp(ifname
, "agr", 3) != 0 ||
80 !isdigit((unsigned char)ifname
[3]);
84 assertifname(prop_dictionary_t env
)
87 errx(EXIT_FAILURE
, "valid only with agr(4) interfaces");
91 agrsetport(prop_dictionary_t env
, prop_dictionary_t oenv
)
98 if (!prop_dictionary_get_int64(env
, "agrcmd", &cmd
)) {
99 warnx("%s.%d", __func__
, __LINE__
);
104 if (!prop_dictionary_get_cstring_nocopy(env
, "agrport", &port
)) {
105 warnx("%s.%d", __func__
, __LINE__
);
109 strlcpy(buf
, port
, sizeof(buf
));
112 memset(&ar
, 0, sizeof(ar
));
113 ar
.ar_version
= AGRREQ_VERSION
;
116 ar
.ar_buflen
= strlen(buf
);
118 if (indirect_ioctl(env
, SIOCSETAGR
, &ar
) == -1)
119 err(EXIT_FAILURE
, "SIOCSETAGR");
124 agr_status(prop_dictionary_t env
, prop_dictionary_t oenv
)
129 struct agrportlist
*apl
;
130 struct agrportinfo
*api
;
133 if (checkifname(env
))
137 memset(&ar
, 0, sizeof(ar
));
138 ar
.ar_version
= AGRREQ_VERSION
;
139 ar
.ar_cmd
= AGRCMD_PORTLIST
;
141 ar
.ar_buflen
= buflen
;
143 if (indirect_ioctl(env
, SIOCGETAGR
, &ar
) == -1) {
144 if (errno
!= E2BIG
) {
156 buflen
= ar
.ar_buflen
;
157 buf
= malloc(buflen
);
159 err(EXIT_FAILURE
, "agr_status");
165 api
= (void *)(apl
+ 1);
167 for (i
= 0; i
< apl
->apl_nports
; i
++) {
170 snprintb(tmp
, sizeof(tmp
), AGRPORTINFO_BITS
, api
->api_flags
);
171 printf("\tagrport: %s, flags=%s\n", api
->api_ifname
, tmp
);
176 static status_func_t status
;
177 static usage_func_t usage
;
178 static cmdloop_branch_t branch
;
181 agr_usage(prop_dictionary_t env
)
183 fprintf(stderr
, "\t[ agrport i ] [ -agrport i ]\n");
187 agr_constructor(void)
189 status_func_init(&status
, agr_status
);
190 usage_func_init(&usage
, agr_usage
);
191 register_status(&status
);
192 register_usage(&usage
);
193 cmdloop_branch_init(&branch
, &agr
.pk_parser
);
194 register_cmdloop_branch(&branch
);