/*
 * brcmd.c does bridging ioctls in 1.2.13.
 *
 * Get the necessary include files from the Linux v1.2.13 tree.
 */
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/socket.h>
#include <linux/if.h>

void
usage()
{
    fprintf(stderr,
	    "usage: brcmd up <internal iface> <external iface>\n"
	    "                <internal iface> must have an IP address.\n"
	    "                <external iface> must NOT have an IP address.\n"
	    "       brcmd down <interface>\n");
    exit(1);
}

int
main(int argc, char **argv)
{
    struct ifreq ifr;
    int s;
    int rval;

    if (argc < 2)
	usage();

    if (!strcmp(argv[1], "up")) {
	if (argc != 4)
	    usage();
	strncpy(ifr.ifr_name, argv[2], sizeof(ifr.ifr_name));
	strncpy(ifr.ifr_slave, argv[3], sizeof(ifr.ifr_slave));
	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
	ifr.ifr_slave[sizeof(ifr.ifr_slave) - 1] = 0;
    }
    else if (!strcmp(argv[1], "down")) {
	if (argc != 3)
	    usage();
	strncpy(ifr.ifr_name, argv[2], sizeof(ifr.ifr_name));
	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
	ifr.ifr_slave[0] = 0;
    }
    else
	usage();

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) {
	perror("brcmd: socket");
	exit(1);
    }
    rval = ioctl(s, SIOCSIFBR, &ifr);
    if (rval < 0) {
	perror("brcmd: ioctl SIOCSIFBR");
	exit(1);
    }
    exit(0);
}
