Mercurial > hg > toybox
comparison toys/other/vconfig.c @ 1103:bc09fa708d94 draft
Cleanup vconfig.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 02 Nov 2013 11:07:25 -0500 |
parents | 786841fdb1e0 |
children | e11684e3bbc5 |
comparison
equal
deleted
inserted
replaced
1102:e1f30cbf79f9 | 1103:bc09fa708d94 |
---|---|
1 /* vconfig.c - Creates virtual ethernet devices. | 1 /* vconfig.c - Creates virtual ethernet devices. |
2 * | 2 * |
3 * Copyright 2012 Sandeep Sharma <sandeep.jack2756@gmail.com>, Kyungwan Han <asura321@gmail.com> | 3 * Copyright 2012 Sandeep Sharma <sandeep.jack2756@gmail.com> |
4 * Copyright 2012 Kyungwan Han <asura321@gmail.com> | |
4 * | 5 * |
5 * Not in SUSv4. | 6 * No standard |
6 | 7 |
7 USE_VCONFIG(NEWTOY(vconfig, "<2>4", TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) | 8 USE_VCONFIG(NEWTOY(vconfig, "<2>4", TOYFLAG_NEEDROOT|TOYFLAG_SBIN)) |
8 | 9 |
9 config VCONFIG | 10 config VCONFIG |
10 bool "vconfig" | 11 bool "vconfig" |
24 | 25 |
25 #include "toys.h" | 26 #include "toys.h" |
26 #include "toynet.h" | 27 #include "toynet.h" |
27 #include <linux/if_vlan.h> | 28 #include <linux/if_vlan.h> |
28 #include <linux/sockios.h> | 29 #include <linux/sockios.h> |
29 /* | 30 |
30 * convert str to long within given range | 31 static int strtorange(char *str, int min, int max) |
31 */ | |
32 static int strtol_range(char *str, int min, int max) | |
33 { | 32 { |
34 char *endptr = NULL; | 33 char *end = 0; |
35 errno = 0; | 34 long val = strtol(str, &end, 10); |
36 long ret_value = strtol(str, &endptr, 10); | |
37 | 35 |
38 if(errno) perror_exit("Invalid num %s", str); | 36 if (end && *end && end != str && val >= min && val <= max) return val; |
39 else if(endptr && (*endptr != '\0' || endptr == str)) | 37 |
40 perror_exit("Not a valid num %s", str); | 38 perror_exit("%s not %d-%d\n", str, min, max); |
41 if(ret_value >= min && ret_value <= max) return ret_value; | |
42 else perror_exit("Number %s is not in valid [%d-%d] Range\n", str, min, max); | |
43 } | 39 } |
44 /* | 40 |
45 * vconfig main function. | |
46 */ | |
47 void vconfig_main(void) | 41 void vconfig_main(void) |
48 { | 42 { |
49 #define MAX_VLAN_ID 4094 | 43 #define MAX_VLAN_ID 4094 |
50 struct vlan_ioctl_args request; | 44 struct vlan_ioctl_args request; |
51 char *interface_name = NULL; | 45 char *interface_name = NULL; |
58 memset(&request, 0, sizeof(struct vlan_ioctl_args)); // Null set all the VLAN info's. | 52 memset(&request, 0, sizeof(struct vlan_ioctl_args)); // Null set all the VLAN info's. |
59 cmd = toys.optargs[0]; // Fetch cmd and proceed. | 53 cmd = toys.optargs[0]; // Fetch cmd and proceed. |
60 if(strcmp(cmd, "set_name_type") == 0) { | 54 if(strcmp(cmd, "set_name_type") == 0) { |
61 if(strcmp(toys.optargs[1], "VLAN_PLUS_VID") == 0) { | 55 if(strcmp(toys.optargs[1], "VLAN_PLUS_VID") == 0) { |
62 name_type = VLAN_NAME_TYPE_PLUS_VID; | 56 name_type = VLAN_NAME_TYPE_PLUS_VID; |
63 } | 57 } else if(strcmp(toys.optargs[1], "VLAN_PLUS_VID_NO_PAD") == 0) { |
64 else if(strcmp(toys.optargs[1], "VLAN_PLUS_VID_NO_PAD") == 0) { | |
65 name_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD; | 58 name_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD; |
66 } | 59 } else if(strcmp(toys.optargs[1], "DEV_PLUS_VID") == 0) { |
67 else if(strcmp(toys.optargs[1], "DEV_PLUS_VID") == 0) { | |
68 name_type = VLAN_NAME_TYPE_RAW_PLUS_VID; | 60 name_type = VLAN_NAME_TYPE_RAW_PLUS_VID; |
69 } | 61 } else if(strcmp(toys.optargs[1], "DEV_PLUS_VID_NO_PAD") == 0) { |
70 else if(strcmp(toys.optargs[1], "DEV_PLUS_VID_NO_PAD") == 0) { | |
71 name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; | 62 name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; |
72 } | 63 } else perror_exit("ERROR: Invalid name type"); |
73 else perror_exit("ERROR: Invalid name type"); | |
74 | 64 |
75 request.u.name_type = name_type; | 65 request.u.name_type = name_type; |
76 request.cmd = SET_VLAN_NAME_TYPE_CMD; | 66 request.cmd = SET_VLAN_NAME_TYPE_CMD; |
77 if(ioctl(fd, SIOCSIFVLAN, &request) == 0) { | 67 if(ioctl(fd, SIOCSIFVLAN, &request) == 0) { |
78 xprintf("Successful set_name_type for VLAN subsystem\n"); | 68 xprintf("Successful set_name_type for VLAN subsystem\n"); |
79 exit(EXIT_SUCCESS); | 69 exit(EXIT_SUCCESS); |
80 } | 70 } |
81 else perror_exit("Failed to set set_name_type:"); | 71 else perror_exit("Failed to set set_name_type:"); |
82 } | 72 } else { |
83 else { | |
84 interface_name = toys.optargs[1]; // Store interface name. | 73 interface_name = toys.optargs[1]; // Store interface name. |
85 if(strlen(interface_name) > 15) perror_exit("ERROR:if_name length can not be greater than 15"); | 74 if(strlen(interface_name) > 15) perror_exit("ERROR:if_name length can not be greater than 15"); |
86 strcpy(request.device1, interface_name); //we had exited if interface_name length greater than 15, so here it never overflows. | 75 strcpy(request.device1, interface_name); //we had exited if interface_name length greater than 15, so here it never overflows. |
87 } | 76 } |
88 | 77 |
89 if(strcmp(cmd, "add") == 0) { | 78 if(strcmp(cmd, "add") == 0) { |
90 request.cmd = ADD_VLAN_CMD; | 79 request.cmd = ADD_VLAN_CMD; |
91 if(toys.optargs[2]) vlan_id = strtol_range(toys.optargs[2], 0, MAX_VLAN_ID); | 80 if(toys.optargs[2]) vlan_id = strtorange(toys.optargs[2], 0, MAX_VLAN_ID); |
92 else vlan_id = 0; | 81 else vlan_id = 0; |
93 request.u.VID = vlan_id; | 82 request.u.VID = vlan_id; |
94 } | 83 } else if(strcmp(cmd, "rem") == 0) { |
95 else if(strcmp(cmd, "rem") == 0) { | |
96 request.cmd = DEL_VLAN_CMD; | 84 request.cmd = DEL_VLAN_CMD; |
97 } | 85 } else if(strcmp(cmd, "set_flag") == 0) { |
98 else if(strcmp(cmd, "set_flag") == 0) { | |
99 request.cmd = SET_VLAN_FLAG_CMD; | 86 request.cmd = SET_VLAN_FLAG_CMD; |
100 if(toys.optargs[2]) request.u.flag = strtol_range(toys.optargs[2], 0, 1); | 87 if(toys.optargs[2]) request.u.flag = strtorange(toys.optargs[2], 0, 1); |
101 else request.u.flag = 0; | 88 else request.u.flag = 0; |
102 if(toys.optargs[3]) request.vlan_qos = strtol_range(toys.optargs[3], 0, 7); | 89 if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7); |
103 else request.vlan_qos = 0; | 90 else request.vlan_qos = 0; |
104 } | 91 } else if(strcmp(cmd, "set_egress_map") == 0) { |
105 else if(strcmp(cmd, "set_egress_map") == 0) { | |
106 request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD; | 92 request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD; |
107 if(toys.optargs[2]) request.u.skb_priority = strtol_range(toys.optargs[2], 0, INT_MAX); | 93 if(toys.optargs[2]) request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX); |
108 else request.u.skb_priority = 0; | 94 else request.u.skb_priority = 0; |
109 if(toys.optargs[3]) request.vlan_qos = strtol_range(toys.optargs[3], 0, 7); | 95 if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7); |
110 else request.vlan_qos = 0; | 96 else request.vlan_qos = 0; |
111 } | 97 } else if(strcmp(cmd, "set_ingress_map") == 0) { |
112 else if(strcmp(cmd, "set_ingress_map") == 0) { | |
113 request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD; | 98 request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD; |
114 if(toys.optargs[2]) request.u.skb_priority = strtol_range(toys.optargs[2], 0, INT_MAX); | 99 if(toys.optargs[2]) request.u.skb_priority = strtorange(toys.optargs[2], 0, INT_MAX); |
115 else request.u.skb_priority = 0; | 100 else request.u.skb_priority = 0; |
116 //To set flag we must have to set vlan_qos | 101 //To set flag we must have to set vlan_qos |
117 if(toys.optargs[3]) request.vlan_qos = strtol_range(toys.optargs[3], 0, 7); | 102 if(toys.optargs[3]) request.vlan_qos = strtorange(toys.optargs[3], 0, 7); |
118 else request.vlan_qos = 0; | 103 else request.vlan_qos = 0; |
119 } | 104 } else { |
120 else { | |
121 xclose(fd); | 105 xclose(fd); |
122 perror_exit("Unknown command %s", cmd); | 106 perror_exit("Unknown command %s", cmd); |
123 } | 107 } |
124 if(ioctl(fd, SIOCSIFVLAN, &request) == 0) { | 108 if(ioctl(fd, SIOCSIFVLAN, &request) == 0) { |
125 if(strcmp(cmd, "add") == 0 && vlan_id == 1) | 109 if(strcmp(cmd, "add") == 0 && vlan_id == 1) |
126 xprintf("WARNING: VLAN 1 does not work with many switches,consider another number if you have problems.\n"); | 110 xprintf("WARNING: VLAN 1 does not work with many switches,consider another number if you have problems.\n"); |
127 xprintf("Successful %s on device %s\n", cmd, interface_name); | 111 xprintf("Successful %s on device %s\n", cmd, interface_name); |
128 } | 112 } else perror_exit("Failed to %s, on vlan subsystem %s.", cmd, interface_name); |
129 else perror_exit("Failed to %s, on vlan subsystem %s.", cmd, interface_name); | |
130 } | 113 } |