agr.c revision 1.4 1 /* $NetBSD: agr.c,v 1.4 2008/04/22 17:18:11 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.4 2008/04/22 17:18:11 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 <stdlib.h>
45 #include <util.h>
46
47 #include "extern.h"
48 #include "agr.h"
49
50 static int checkifname(const char *);
51 static void assertifname(const char *);
52
53 static int
54 checkifname(const char *ifname)
55 {
56
57 return strncmp(ifname, "agr", 3) != 0 ||
58 !isdigit((unsigned char)ifname[3]);
59 }
60
61 static void
62 assertifname(const char *ifname)
63 {
64
65 if (checkifname(ifname)) {
66 errx(EXIT_FAILURE, "valid only with agr(4) interfaces");
67 }
68 }
69
70 void
71 agraddport(const char *val, int d)
72 {
73 char buf[IFNAMSIZ];
74 struct agrreq ar;
75
76 assertifname(ifr.ifr_name);
77
78 strlcpy(buf, val, sizeof(buf));
79
80 memset(&ar, 0, sizeof(ar));
81 ar.ar_version = AGRREQ_VERSION;
82 ar.ar_cmd = AGRCMD_ADDPORT;
83 ar.ar_buf = buf;
84 ar.ar_buflen = strlen(buf);
85 ifr.ifr_data = &ar;
86
87 if (ioctl(s, SIOCSETAGR, &ifr) == -1)
88 err(EXIT_FAILURE, "SIOCSETAGR");
89 }
90
91 void
92 agrremport(const char *val, int d)
93 {
94 char buf[IFNAMSIZ];
95 struct agrreq ar;
96
97 assertifname(ifr.ifr_name);
98
99 strlcpy(buf, val, sizeof(buf));
100
101 memset(&ar, 0, sizeof(ar));
102 ar.ar_version = AGRREQ_VERSION;
103 ar.ar_cmd = AGRCMD_REMPORT;
104 ar.ar_buf = buf;
105 ar.ar_buflen = strlen(buf);
106 ifr.ifr_data = &ar;
107
108 if (ioctl(s, SIOCSETAGR, &ifr) == -1)
109 err(EXIT_FAILURE, "SIOCSETAGR");
110 }
111
112 void
113 agr_status()
114 {
115 struct agrreq ar;
116 void *buf = NULL;
117 size_t buflen = 0;
118 struct agrportlist *apl;
119 struct agrportinfo *api;
120 int i;
121
122 if (checkifname(ifr.ifr_name)) {
123 return;
124 }
125
126 again:
127 memset(&ar, 0, sizeof(ar));
128 ar.ar_version = AGRREQ_VERSION;
129 ar.ar_cmd = AGRCMD_PORTLIST;
130 ar.ar_buf = buf;
131 ar.ar_buflen = buflen;
132 ifr.ifr_data = (void *)&ar;
133
134 if (ioctl(s, SIOCGETAGR, &ifr) == -1) {
135 if (errno != E2BIG) {
136 warn("SIOCGETAGR");
137 return;
138 }
139
140 free(buf);
141 buf = NULL;
142 buflen = 0;
143 goto again;
144 }
145
146 if (buf == NULL) {
147 buflen = ar.ar_buflen;
148 buf = malloc(buflen);
149 if (buf == NULL) {
150 err(EXIT_FAILURE, "agr_status");
151 }
152 goto again;
153 }
154
155 apl = buf;
156 api = (void *)(apl + 1);
157
158 for (i = 0; i < apl->apl_nports; i++) {
159 char tmp[256];
160
161 snprintb(tmp, sizeof(tmp), AGRPORTINFO_BITS, api->api_flags);
162 printf("\tagrport: %s, flags=%s\n", api->api_ifname, tmp);
163 api++;
164 }
165 }
166