/* * $Id: vlanctl.c,v 1.2 2000/02/19 10:12:26 root Exp $ * * Copyright (C) 2000 Lennert Buytenhek * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include #include static int sock = 0; int vlan_ioctl(char *device, int cmd, int vlan) { struct ifreq ifr; if (!sock) sock = socket(PF_INET, SOCK_DGRAM, 0); strncpy(ifr.ifr_name, device, IFNAMSIZ); ifr.ifr_mtu = vlan; return ioctl(sock, cmd, &ifr); } int vlan_add(char *device, int vlan) { return vlan_ioctl(device, SIOCADDVLAN, vlan); } int vlan_del(char *device, int vlan) { return vlan_ioctl(device, SIOCDELVLAN, vlan); } void usage() { fprintf(stderr, "commands:\n"); fprintf(stderr, "add\t\t \t\tadd vlan to device\n"); fprintf(stderr, "del\t\t \t\tdelete vlan from device\n"); exit(-1); } int main(int argc, char *argv[]) { int cmd; int err; int vlan; char *stcmd; char *stdev; if (argc != 4) usage(); stcmd = argv[1]; stdev = argv[2]; if (!strcmp(stcmd, "add")) cmd = SIOCADDVLAN; else if (!strcmp(stcmd, "del")) cmd = SIOCDELVLAN; else usage(); sscanf(argv[3], "%i", &vlan); if ((err = vlan_ioctl(stdev, cmd, vlan)) == 0) return 0; switch (errno) { case ENODEV: fprintf(stderr, "%s: device not found\n", stdev); break; case EBUSY: fprintf(stderr, "%s: vlan %i already present\n", stdev, vlan); break; case ENOMEM: fprintf(stderr, "%s: out of memory\n", stdev); break; case ENFILE: fprintf(stderr, "%s: can't find vlan %i\n", stdev, vlan); break; default: perror("blah"); break; } return 1; }