attempt to regularize atoi mess.
[oweals/busybox.git] / networking / vconfig.c
index d58c375f7918a1654cb83ee64f9ba47d01be1658..003c1a8f7fef84bf174bd32feb455de90153417a 100644 (file)
@@ -4,36 +4,51 @@
  *
  * Copyright (C) 2001  Manuel Novoa III  <mjn3@codepoet.org>
  *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 /* BB_AUDIT SUSv3 N/A */
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
-#include <linux/if_vlan.h>
-#include <string.h>
-#include <limits.h>
 #include "busybox.h"
+#include <net/if.h>
+
+/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
+enum vlan_ioctl_cmds {
+       ADD_VLAN_CMD,
+       DEL_VLAN_CMD,
+       SET_VLAN_INGRESS_PRIORITY_CMD,
+       SET_VLAN_EGRESS_PRIORITY_CMD,
+       GET_VLAN_INGRESS_PRIORITY_CMD,
+       GET_VLAN_EGRESS_PRIORITY_CMD,
+       SET_VLAN_NAME_TYPE_CMD,
+       SET_VLAN_FLAG_CMD
+};
+enum vlan_name_types {
+       VLAN_NAME_TYPE_PLUS_VID, /* Name will look like:  vlan0005 */
+       VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like:  eth1.0005 */
+       VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like:  vlan5 */
+       VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like:  eth0.5 */
+       VLAN_NAME_TYPE_HIGHEST
+};
+
+struct vlan_ioctl_args {
+       int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
+       char device1[24];
+
+       union {
+               char device2[24];
+               int VID;
+               unsigned int skb_priority;
+               unsigned int name_type;
+               unsigned int bind_type;
+               unsigned int flag; /* Matches vlan_dev_info flags */
+       } u;
+
+       short vlan_qos;
+};
 
 #define VLAN_GROUP_ARRAY_LEN 4096
-#define SIOCSIFVLAN    0x8983          /* Set 802.1Q VLAN options      */
+#define SIOCSIFVLAN    0x8983          /* Set 802.1Q VLAN options */
 
 /* On entry, table points to the length of the current string plus
  * nul terminator plus data length for the subsequent entry.  The
@@ -57,7 +72,7 @@ static const char cmds[] = {
        's', 'e', 't', '_',
        'n', 'a', 'm', 'e', '_',
        't', 'y', 'p', 'e', 0,
-       4, SET_VLAN_FLAG_CMD, 12,
+       5, SET_VLAN_FLAG_CMD, 12,
        's', 'e', 't', '_',
        'f', 'l', 'a', 'g', 0,
        5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
@@ -76,7 +91,7 @@ static const char name_types[] = {
        '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
        0,
        VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
-       'V', 'L', 'A', 'N', 
+       'V', 'L', 'A', 'N',
        '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
        '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
        VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
@@ -102,9 +117,8 @@ int vconfig_main(int argc, char **argv)
        }
 
        /* Don't bother closing the filedes.  It will be closed on cleanup. */
-       if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
-           bb_perror_msg_and_die("open %s", conf_file_name);
-       }
+       /* Will die if 802.1q is not present */
+       xopen(conf_file_name, O_RDONLY);
 
        memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
 
@@ -119,7 +133,7 @@ int vconfig_main(int argc, char **argv)
                ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
        } else {
                if (strlen(argv[1]) >= IF_NAMESIZE) {
-                       bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
+                       bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE);
                }
                strcpy(ifr.device1, argv[1]);
                p = argv[2];
@@ -130,21 +144,21 @@ int vconfig_main(int argc, char **argv)
                 * doing so wouldn't save that much space and would also make maintainence
                 * more of a pain. */
                if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
-                       ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
+                       ifr.u.flag = xatoul_range(p, 0, 1);
+                       /* DM: in order to set reorder header, qos must be set */
+                       ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
                } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
-                       ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
+                       ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
                } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
-                       ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
-                       ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
+                       ifr.u.skb_priority = xatou(p);
+                       ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
                }
        }
 
-       if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-               || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
-               ) {
-               bb_perror_msg_and_die("socket or ioctl error for %s", *argv);
+       fd = xsocket(AF_INET, SOCK_STREAM, 0);
+       if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) {
+               bb_perror_msg_and_die("ioctl error for %s", *argv);
        }
 
        return 0;
 }
-