#ifndef lint
static const char rcsid[] =
"$FreeBSD: src/sbin/ifconfig/ifclone.c,v 1.3.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $";
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ifconfig.h"
static void
list_cloners(void)
{
#ifdef notdef
struct if_clonereq ifcr;
char *cp, *buf;
int idx;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1)
err(1, "socket(AF_INET,SOCK_DGRAM)");
memset(&ifcr, 0, sizeof(ifcr));
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
err(1, "SIOCIFGCLONERS for count");
buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
if (buf == NULL)
err(1, "unable to allocate cloner name buffer");
ifcr.ifcr_count = ifcr.ifcr_total;
ifcr.ifcr_buffer = buf;
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
err(1, "SIOCIFGCLONERS for names");
if (ifcr.ifcr_count > ifcr.ifcr_total)
ifcr.ifcr_count = ifcr.ifcr_total;
for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
if (idx > 0)
putchar(' ');
printf("%s", cp);
}
putchar('\n');
free(buf);
#endif
}
static clone_callback_func *clone_cb = NULL;
void
clone_setcallback(clone_callback_func *p)
{
if (clone_cb != NULL && clone_cb != p)
errx(1, "conflicting device create parameters");
clone_cb = p;
}
static void
ifclonecreate(int s, void *arg)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (clone_cb == NULL) {
#ifdef SIOCIFCREATE2
if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
err(1, "SIOCIFCREATE2");
#else
if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
err(1, "SIOCIFCREATE");
#endif
} else {
clone_cb(s, &ifr);
}
if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
strlcpy(name, ifr.ifr_name, sizeof(name));
printf("%s\n", name);
}
}
static
DECL_CMD_FUNC(clone_create, arg, d)
{
callback_register(ifclonecreate, NULL);
}
static
DECL_CMD_FUNC(clone_destroy, arg, d)
{
(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
err(1, "SIOCIFDESTROY");
}
static struct cmd clone_cmds[] = {
DEF_CLONE_CMD("create", 0, clone_create),
DEF_CMD("destroy", 0, clone_destroy),
DEF_CLONE_CMD("plumb", 0, clone_create),
DEF_CMD("unplumb", 0, clone_destroy),
};
static void
clone_Copt_cb(const char *optarg __unused)
{
list_cloners();
exit(0);
}
static struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
static __constructor void
clone_ctor(void)
{
#define N(a) (sizeof(a) / sizeof(a[0]))
int i;
for (i = 0; i < N(clone_cmds); i++)
cmd_register(&clone_cmds[i]);
opt_register(&clone_Copt);
#undef N
}