1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2006 Max Laier. All rights reserved. |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifndef lint |
---|
29 | static const char rcsid[] = |
---|
30 | "$FreeBSD$"; |
---|
31 | #endif /* not lint */ |
---|
32 | |
---|
33 | #ifdef __rtems__ |
---|
34 | #include <machine/rtems-bsd-program.h> |
---|
35 | #endif /* __rtems__ */ |
---|
36 | #include <rtems/bsd/sys/types.h> |
---|
37 | #include <sys/ioctl.h> |
---|
38 | #include <sys/socket.h> |
---|
39 | #include <net/if.h> |
---|
40 | |
---|
41 | #include <ctype.h> |
---|
42 | #include <err.h> |
---|
43 | #include <errno.h> |
---|
44 | #include <stdio.h> |
---|
45 | #include <stdlib.h> |
---|
46 | #include <string.h> |
---|
47 | #include <unistd.h> |
---|
48 | |
---|
49 | #include "ifconfig.h" |
---|
50 | |
---|
51 | /* ARGSUSED */ |
---|
52 | static void |
---|
53 | setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp) |
---|
54 | { |
---|
55 | struct ifgroupreq ifgr; |
---|
56 | |
---|
57 | memset(&ifgr, 0, sizeof(ifgr)); |
---|
58 | strlcpy(ifgr.ifgr_name, name, IFNAMSIZ); |
---|
59 | |
---|
60 | if (group_name[0] && isdigit((unsigned char)group_name[strlen(group_name) - 1])) |
---|
61 | errx(1, "setifgroup: group names may not end in a digit"); |
---|
62 | |
---|
63 | if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ) |
---|
64 | errx(1, "setifgroup: group name too long"); |
---|
65 | if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST) |
---|
66 | err(1," SIOCAIFGROUP"); |
---|
67 | } |
---|
68 | |
---|
69 | /* ARGSUSED */ |
---|
70 | static void |
---|
71 | unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp) |
---|
72 | { |
---|
73 | struct ifgroupreq ifgr; |
---|
74 | |
---|
75 | memset(&ifgr, 0, sizeof(ifgr)); |
---|
76 | strlcpy(ifgr.ifgr_name, name, IFNAMSIZ); |
---|
77 | |
---|
78 | if (group_name[0] && isdigit((unsigned char)group_name[strlen(group_name) - 1])) |
---|
79 | errx(1, "unsetifgroup: group names may not end in a digit"); |
---|
80 | |
---|
81 | if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ) |
---|
82 | errx(1, "unsetifgroup: group name too long"); |
---|
83 | if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT) |
---|
84 | err(1, "SIOCDIFGROUP"); |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | getifgroups(int s) |
---|
89 | { |
---|
90 | int len, cnt; |
---|
91 | struct ifgroupreq ifgr; |
---|
92 | struct ifg_req *ifg; |
---|
93 | |
---|
94 | if (!verbose) |
---|
95 | return; |
---|
96 | |
---|
97 | memset(&ifgr, 0, sizeof(ifgr)); |
---|
98 | strlcpy(ifgr.ifgr_name, name, IFNAMSIZ); |
---|
99 | |
---|
100 | if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) { |
---|
101 | if (errno == EINVAL || errno == ENOTTY) |
---|
102 | return; |
---|
103 | else |
---|
104 | err(1, "SIOCGIFGROUP"); |
---|
105 | } |
---|
106 | |
---|
107 | len = ifgr.ifgr_len; |
---|
108 | ifgr.ifgr_groups = |
---|
109 | (struct ifg_req *)calloc(len / sizeof(struct ifg_req), |
---|
110 | sizeof(struct ifg_req)); |
---|
111 | if (ifgr.ifgr_groups == NULL) |
---|
112 | err(1, "getifgroups"); |
---|
113 | if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) |
---|
114 | err(1, "SIOCGIFGROUP"); |
---|
115 | |
---|
116 | cnt = 0; |
---|
117 | ifg = ifgr.ifgr_groups; |
---|
118 | for (; ifg && len >= sizeof(struct ifg_req); ifg++) { |
---|
119 | len -= sizeof(struct ifg_req); |
---|
120 | if (strcmp(ifg->ifgrq_group, "all")) { |
---|
121 | if (cnt == 0) |
---|
122 | printf("\tgroups: "); |
---|
123 | cnt++; |
---|
124 | printf("%s ", ifg->ifgrq_group); |
---|
125 | } |
---|
126 | } |
---|
127 | if (cnt) |
---|
128 | printf("\n"); |
---|
129 | } |
---|
130 | |
---|
131 | static void |
---|
132 | printgroup(const char *groupname) |
---|
133 | { |
---|
134 | struct ifgroupreq ifgr; |
---|
135 | struct ifg_req *ifg; |
---|
136 | int len, cnt = 0; |
---|
137 | int s; |
---|
138 | |
---|
139 | s = socket(AF_LOCAL, SOCK_DGRAM, 0); |
---|
140 | if (s == -1) |
---|
141 | err(1, "socket(AF_LOCAL,SOCK_DGRAM)"); |
---|
142 | bzero(&ifgr, sizeof(ifgr)); |
---|
143 | strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name)); |
---|
144 | if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) { |
---|
145 | close(s); |
---|
146 | if (errno == EINVAL || errno == ENOTTY || |
---|
147 | errno == ENOENT) |
---|
148 | exit(0); |
---|
149 | else |
---|
150 | err(1, "SIOCGIFGMEMB"); |
---|
151 | } |
---|
152 | |
---|
153 | len = ifgr.ifgr_len; |
---|
154 | if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) { |
---|
155 | close(s); |
---|
156 | err(1, "printgroup"); |
---|
157 | } |
---|
158 | if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) { |
---|
159 | free(ifgr.ifgr_groups); |
---|
160 | close(s); |
---|
161 | err(1, "SIOCGIFGMEMB"); |
---|
162 | } |
---|
163 | |
---|
164 | for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req); |
---|
165 | ifg++) { |
---|
166 | len -= sizeof(struct ifg_req); |
---|
167 | printf("%s\n", ifg->ifgrq_member); |
---|
168 | cnt++; |
---|
169 | } |
---|
170 | free(ifgr.ifgr_groups); |
---|
171 | close(s); |
---|
172 | |
---|
173 | exit(0); |
---|
174 | } |
---|
175 | |
---|
176 | static struct cmd group_cmds[] = { |
---|
177 | DEF_CMD_ARG("group", setifgroup), |
---|
178 | DEF_CMD_ARG("-group", unsetifgroup), |
---|
179 | }; |
---|
180 | static struct afswtch af_group = { |
---|
181 | .af_name = "af_group", |
---|
182 | .af_af = AF_UNSPEC, |
---|
183 | .af_other_status = getifgroups, |
---|
184 | }; |
---|
185 | static struct option group_gopt = { "g:", "[-g groupname]", printgroup }; |
---|
186 | |
---|
187 | #ifndef __rtems__ |
---|
188 | static __constructor void |
---|
189 | #else /* __rtems__ */ |
---|
190 | void |
---|
191 | #endif /* __rtems__ */ |
---|
192 | group_ctor(void) |
---|
193 | { |
---|
194 | #define N(a) (sizeof(a) / sizeof(a[0])) |
---|
195 | int i; |
---|
196 | |
---|
197 | for (i = 0; i < N(group_cmds); i++) |
---|
198 | cmd_register(&group_cmds[i]); |
---|
199 | af_register(&af_group); |
---|
200 | opt_register(&group_gopt); |
---|
201 | #undef N |
---|
202 | } |
---|