changeset 1106:30a210bae3e9

Sceond cleanup pass on vconfig
author Rob Landley <rob@landley.net>
date Sat, 02 Nov 2013 15:10:55 -0500
parents 1bca28705a87
children bbed38cf7236
files toys/other/vconfig.c
diffstat 1 files changed, 40 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/vconfig.c	Sat Nov 02 14:24:54 2013 -0500
+++ b/toys/other/vconfig.c	Sat Nov 02 15:10:55 2013 -0500
@@ -27,86 +27,72 @@
 #include <linux/if_vlan.h>
 #include <linux/sockios.h>
 
-static int strtorange(char *str, int min, int max)
+static long strtorange(char *str, long min, long max)
 {
   char *end = 0;
   long val = strtol(str, &end, 10);
 
   if (end && *end && end != str && val >= min && val <= max) return val;
 
-  perror_exit("%s not %d-%d\n", str, min, max);
+  perror_exit("%s not %ld-%ld\n", str, min, max);
 }
 
 void vconfig_main(void)
 {
-#define MAX_VLAN_ID 4094
   struct vlan_ioctl_args request;
-  char *interface_name = NULL;
-  unsigned int name_type = VLAN_NAME_TYPE_PLUS_VID;
   char *cmd;
-  int fd = 0;
-  int vlan_id = 0;
+  int fd;
 
-  if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror_exit("Can't open socket"); //Use socket instead of open
-  memset(&request, 0, sizeof(struct vlan_ioctl_args));		// Null set all the VLAN info's.
-  cmd = toys.optargs[0];					// Fetch cmd and proceed.
-  if(strcmp(cmd, "set_name_type") == 0) {
-    if(strcmp(toys.optargs[1], "VLAN_PLUS_VID") == 0) {
-      name_type = VLAN_NAME_TYPE_PLUS_VID;
-    } else if(strcmp(toys.optargs[1], "VLAN_PLUS_VID_NO_PAD") == 0) {
-      name_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD;
-    } else if(strcmp(toys.optargs[1], "DEV_PLUS_VID") == 0) {
-      name_type = VLAN_NAME_TYPE_RAW_PLUS_VID;
-    } else if(strcmp(toys.optargs[1], "DEV_PLUS_VID_NO_PAD") == 0) {
-      name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
-    } else perror_exit("ERROR: Invalid name type");
+  fd = xsocket(AF_INET, SOCK_STREAM, 0);
+  memset(&request, 0, sizeof(struct vlan_ioctl_args));
+  cmd = toys.optargs[0];
 
-    request.u.name_type = name_type;
+  if (!strcmp(cmd, "set_name_type")) {
+    char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
+                     "DEV_PLUS_VID_NO_PAD"};
+    int i, j = sizeof(types)/sizeof(*types);
+
+    for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
+    if (i == j) {
+      for (i=0; i<j; i++) puts(types[i]);
+      error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
+    }
+
+    request.u.name_type = i;
     request.cmd = SET_VLAN_NAME_TYPE_CMD;
-    if(ioctl(fd, SIOCSIFVLAN, &request) == 0) {
-      xprintf("Successful set_name_type for VLAN subsystem\n");
-      exit(EXIT_SUCCESS);
-    }
-    else perror_exit("Failed to set set_name_type:");
-  } else {
-    interface_name = toys.optargs[1]; // Store interface name.
-    if(strlen(interface_name) > 15) perror_exit("ERROR:if_name length can not be greater than 15");
-    strcpy(request.device1, interface_name); //we had exited if interface_name length greater than 15, so here it never overflows.
+    xioctl(fd, SIOCSIFVLAN, &request);
+    return;
   }
 
-  if(strcmp(cmd, "add") == 0) {
+  // Store interface name
+  xstrncpy(request.device1, toys.optargs[1], 16);
+
+  if (!strcmp(cmd, "add")) {
     request.cmd = ADD_VLAN_CMD;
-    if(toys.optargs[2]) vlan_id = strtorange(toys.optargs[2], 0, MAX_VLAN_ID);
-    else vlan_id = 0;
-    request.u.VID = vlan_id;
-  } else if(strcmp(cmd, "rem") == 0) {
-    request.cmd = DEL_VLAN_CMD;
-  } else if(strcmp(cmd, "set_flag") == 0) {
+    if (toys.optargs[2]) request.u.VID = strtorange(toys.optargs[2], 0, 4094);
+    if (request.u.VID == 1)
+      xprintf("WARNING: VLAN 1 does not work with many switches.\n");
+  } else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
+  else if (!strcmp(cmd, "set_flag")) {
     request.cmd = SET_VLAN_FLAG_CMD;
-    if(toys.optargs[2]) request.u.flag = strtorange(toys.optargs[2], 0, 1);
-    else request.u.flag = 0;
-    if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
-    else request.vlan_qos = 0;
+    if (toys.optargs[2]) request.u.flag = strtorange(toys.optargs[2], 0, 1);
+    if (toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
   } else if(strcmp(cmd, "set_egress_map") == 0) {
     request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
-    if(toys.optargs[2]) request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX);
-    else request.u.skb_priority = 0;
-    if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
-    else request.vlan_qos = 0;
+    if (toys.optargs[2])
+      request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX);
+    if (toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
   } else if(strcmp(cmd, "set_ingress_map") == 0) {
     request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
-    if(toys.optargs[2]) request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX);
-    else request.u.skb_priority = 0;
+    if (toys.optargs[2])
+      request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX);
     //To set flag we must have to set vlan_qos
-    if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
-    else request.vlan_qos = 0;
+    if (toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7);
   } else {
     xclose(fd);
     perror_exit("Unknown command %s", cmd);
   }
-  if(ioctl(fd, SIOCSIFVLAN, &request) == 0) {
-    if(strcmp(cmd, "add") == 0 && vlan_id == 1)
-      xprintf("WARNING: VLAN 1 does not work with many switches,consider another number if you have problems.\n");
-    xprintf("Successful %s on device %s\n", cmd, interface_name);
-  } else perror_exit("Failed to %s, on vlan subsystem %s.", cmd, interface_name);
+
+  xioctl(fd, SIOCSIFVLAN, &request);
+  xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);
 }