/*
 *   Copyright (c) 2000-2002 David Parsons. All rights reserved.
 *   
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``AS IS'' AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
 *  PARSONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 *  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 *  THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * brcmd.c brings the bridge up or down.
 */
#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 (!strcasecmp(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 (!strcasecmp(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);
}
