source: rtems-libbsd/freebsd/sbin/ifconfig/ifclone.c @ 3d1e767

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d1e767 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[d48955b]1#include <machine/rtems-bsd-user-space.h>
2
[ebbe3cc]3/*
4 * Copyright (c) 1983, 1993
5 *      The Regents of the University of California.  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 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char rcsid[] =
34  "$FreeBSD$";
35#endif /* not lint */
36
[56e9de9]37#ifdef __rtems__
38#include <machine/rtems-bsd-program.h>
39#endif /* __rtems__ */
[ebbe3cc]40#include <sys/queue.h>
[3d1e767]41#include <sys/types.h>
[ebbe3cc]42#include <sys/ioctl.h>
43#include <sys/socket.h>
44#include <net/if.h>
45
46#include <err.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51
52#include "ifconfig.h"
53
54static void
55list_cloners(void)
56{
57        struct if_clonereq ifcr;
58        char *cp, *buf;
59        int idx;
60        int s;
61
62        s = socket(AF_LOCAL, SOCK_DGRAM, 0);
63        if (s == -1)
64                err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
65
66        memset(&ifcr, 0, sizeof(ifcr));
67
68        if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
69                err(1, "SIOCIFGCLONERS for count");
70
71        buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
72        if (buf == NULL)
73                err(1, "unable to allocate cloner name buffer");
74
75        ifcr.ifcr_count = ifcr.ifcr_total;
76        ifcr.ifcr_buffer = buf;
77
[b833cc4]78        if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) {
79                free(buf);
[ebbe3cc]80                err(1, "SIOCIFGCLONERS for names");
[b833cc4]81        }
[ebbe3cc]82
83        /*
84         * In case some disappeared in the mean time, clamp it down.
85         */
86        if (ifcr.ifcr_count > ifcr.ifcr_total)
87                ifcr.ifcr_count = ifcr.ifcr_total;
88
89        for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
90                if (idx > 0)
91                        putchar(' ');
92                printf("%s", cp);
93        }
94
95        putchar('\n');
96        free(buf);
97}
98
99struct clone_defcb {
100        char ifprefix[IFNAMSIZ];
101        clone_callback_func *clone_cb;
102        SLIST_ENTRY(clone_defcb) next;
103};
104
105static SLIST_HEAD(, clone_defcb) clone_defcbh =
106   SLIST_HEAD_INITIALIZER(clone_defcbh);
107
108void
109clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
110{
111        struct clone_defcb *dcp;
112
113        dcp = malloc(sizeof(*dcp));
[b833cc4]114#ifndef __rtems__
115        if (dcp == NULL) {
116                errx(1, "unable to allocate clone");
117        }
118#endif /* __rtems__ */
[ebbe3cc]119        strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
120        dcp->clone_cb = p;
121        SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
122}
123
124/*
125 * Do the actual clone operation.  Any parameters must have been
126 * setup by now.  If a callback has been setup to do the work
127 * then defer to it; otherwise do a simple create operation with
128 * no parameters.
129 */
130static void
131ifclonecreate(int s, void *arg)
132{
133        struct ifreq ifr;
134        struct clone_defcb *dcp;
135        clone_callback_func *clone_cb = NULL;
136
137        memset(&ifr, 0, sizeof(ifr));
138        (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
139
140        if (clone_cb == NULL) {
141                /* Try to find a default callback */
142                SLIST_FOREACH(dcp, &clone_defcbh, next) {
143                        if (strncmp(dcp->ifprefix, ifr.ifr_name,
144                            strlen(dcp->ifprefix)) == 0) {
145                                clone_cb = dcp->clone_cb;
146                                break;
147                        }
148                }
149        }
150        if (clone_cb == NULL) {
151                /* NB: no parameters */
152                if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
153                        err(1, "SIOCIFCREATE2");
154        } else {
155                clone_cb(s, &ifr);
156        }
157
158        /*
159         * If we get a different name back than we put in, print it.
160         */
161        if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
162                strlcpy(name, ifr.ifr_name, sizeof(name));
163                printf("%s\n", name);
164        }
165}
166
167static
168DECL_CMD_FUNC(clone_create, arg, d)
169{
170        callback_register(ifclonecreate, NULL);
171}
172
173static
174DECL_CMD_FUNC(clone_destroy, arg, d)
175{
176        (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
177        if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
178                err(1, "SIOCIFDESTROY");
179}
180
181static struct cmd clone_cmds[] = {
182        DEF_CLONE_CMD("create", 0,      clone_create),
183        DEF_CMD("destroy",      0,      clone_destroy),
184        DEF_CLONE_CMD("plumb",  0,      clone_create),
185        DEF_CMD("unplumb",      0,      clone_destroy),
186};
187
188static void
189clone_Copt_cb(const char *optarg __unused)
190{
191        list_cloners();
192        exit(0);
193}
194static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
195
[60618d5]196#ifndef __rtems__
[ebbe3cc]197static __constructor void
[60618d5]198#else /* __rtems__ */
199void
200#endif /* __rtems__ */
[ebbe3cc]201clone_ctor(void)
202{
[60618d5]203#ifdef __rtems__
204        SLIST_INIT(&clone_defcbh);
205#endif /* __rtems__ */
[ebbe3cc]206#define N(a)    (sizeof(a) / sizeof(a[0]))
207        size_t i;
208
209        for (i = 0; i < N(clone_cmds);  i++)
210                cmd_register(&clone_cmds[i]);
211        opt_register(&clone_Copt);
212#undef N
213}
[b833cc4]214#ifdef __rtems__
215void
216clone_dtor(void)
217{
218        struct clone_defcb *dcp;
219        struct clone_defcb *dcp_tmp;
220
221        SLIST_FOREACH_SAFE(dcp, &clone_defcbh, next, dcp_tmp) {
222                free(dcp);
223        }
224}
225#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.