aboutsummaryrefslogtreecommitdiff
path: root/networking/nameif.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-01-16 11:37:57 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-01-16 11:37:57 +0000
commit77c60e5c667e1293f92b65056f107c92edcc3965 (patch)
treed4eadc7c5981c0da97e5d44bfaf777373316af1d /networking/nameif.c
parent8573704097440769c232f3edb6f347f016353dfb (diff)
downloadbusybox-w32-77c60e5c667e1293f92b65056f107c92edcc3965.tar.gz
busybox-w32-77c60e5c667e1293f92b65056f107c92edcc3965.tar.bz2
busybox-w32-77c60e5c667e1293f92b65056f107c92edcc3965.zip
Use vsyslog instead of syslog, improve initalisation and cleanup of
variables, save 300 Bytes or so, patch by Vladimir N. Oleynik
Diffstat (limited to 'networking/nameif.c')
-rw-r--r--networking/nameif.c78
1 files changed, 36 insertions, 42 deletions
diff --git a/networking/nameif.c b/networking/nameif.c
index 886ff49a8..cd18b4c18 100644
--- a/networking/nameif.c
+++ b/networking/nameif.c
@@ -1,4 +1,4 @@
1/* 1/*
2 * nameif.c - Naming Interfaces based on MAC address for busybox. 2 * nameif.c - Naming Interfaces based on MAC address for busybox.
3 * 3 *
4 * Writen 2000 by Andi Kleen. 4 * Writen 2000 by Andi Kleen.
@@ -38,7 +38,7 @@
38/* take from linux/sockios.h */ 38/* take from linux/sockios.h */
39#define SIOCSIFNAME 0x8923 /* set interface name */ 39#define SIOCSIFNAME 0x8923 /* set interface name */
40 40
41/* Octets in one ethernet addr, from <linux/if_ether.h> */ 41/* Octets in one ethernet addr, from <linux/if_ether.h> */
42#define ETH_ALEN 6 42#define ETH_ALEN 6
43 43
44#ifndef ifr_newname 44#ifndef ifr_newname
@@ -52,18 +52,22 @@ typedef struct mactable_s {
52 struct ether_addr *mac; 52 struct ether_addr *mac;
53} mactable_t; 53} mactable_t;
54 54
55static void serror(const char use_syslog, const char *s, ...) 55static unsigned char use_syslog;
56
57static void serror(const char *s, ...) __attribute__ ((noreturn));
58
59static void serror(const char *s, ...)
56{ 60{
57 va_list ap; 61 va_list ap;
58 62
59 va_start(ap, s); 63 va_start(ap, s);
60 64
61 if (use_syslog) { 65 if (use_syslog) {
62 openlog("nameif", 0, LOG_LOCAL0); 66 openlog(applet_name, 0, LOG_LOCAL0);
63 syslog(LOG_ERR, s, ap); 67 vsyslog(LOG_ERR, s, ap);
64 closelog(); 68 closelog();
65 } else { 69 } else {
66 vfprintf(stderr, s, ap); 70 verror_msg(s, ap);
67 putc('\n', stderr); 71 putc('\n', stderr);
68 } 72 }
69 73
@@ -73,14 +77,14 @@ static void serror(const char use_syslog, const char *s, ...)
73} 77}
74 78
75/* Check ascii str_macaddr, convert and copy to *mac */ 79/* Check ascii str_macaddr, convert and copy to *mac */
76struct ether_addr *cc_macaddr(char *str_macaddr, unsigned char use_syslog) 80struct ether_addr *cc_macaddr(char *str_macaddr)
77{ 81{
78 struct ether_addr *lmac, *mac; 82 struct ether_addr *lmac, *mac;
79 83
80 lmac = ether_aton(str_macaddr); 84 lmac = ether_aton(str_macaddr);
81 if (lmac == NULL) 85 if (lmac == NULL)
82 serror(use_syslog, "cannot parse MAC %s", str_macaddr); 86 serror("cannot parse MAC %s", str_macaddr);
83 mac = xcalloc(1, ETH_ALEN); 87 mac = xmalloc(ETH_ALEN);
84 memcpy(mac, lmac, ETH_ALEN); 88 memcpy(mac, lmac, ETH_ALEN);
85 89
86 return mac; 90 return mac;
@@ -90,13 +94,12 @@ int nameif_main(int argc, char **argv)
90{ 94{
91 mactable_t *clist = NULL; 95 mactable_t *clist = NULL;
92 FILE *ifh; 96 FILE *ifh;
93 char *fname = "/etc/mactab"; 97 const char *fname = "/etc/mactab";
94 char *line; 98 char *line;
95 unsigned char use_syslog = 0; 99 int ctl_sk;
96 int ctl_sk = -1;
97 int opt; 100 int opt;
98 int if_index = 1; 101 int if_index = 1;
99 mactable_t *ch = NULL; 102 mactable_t *ch;
100 103
101 static struct option opts[] = { 104 static struct option opts[] = {
102 {"syslog", 0, NULL, 's'}, 105 {"syslog", 0, NULL, 's'},
@@ -121,23 +124,18 @@ int nameif_main(int argc, char **argv)
121 show_usage(); 124 show_usage();
122 125
123 if (optind < argc) { 126 if (optind < argc) {
124 while (optind < argc) { 127 char **a = argv + optind;
125 128
126 if (strlen(argv[optind]) > IF_NAMESIZE) 129 while (*a) {
127 serror(use_syslog, "interface name `%s' too long",
128 argv[optind]);
129 optind++;
130 130
131 if (strlen(*a) > IF_NAMESIZE)
132 serror("interface name `%s' too long", *a);
131 ch = xcalloc(1, sizeof(mactable_t)); 133 ch = xcalloc(1, sizeof(mactable_t));
132 ch->next = NULL; 134 ch->ifname = xstrdup(*a++);
133 ch->prev = NULL; 135 ch->mac = cc_macaddr(*a++);
134 ch->ifname = strdup(argv[optind - 1]);
135 ch->mac = cc_macaddr(argv[optind], use_syslog);
136 optind++;
137 if (clist) 136 if (clist)
138 clist->prev = ch->next; 137 clist->prev = ch;
139 ch->next = clist; 138 ch->next = clist;
140 ch->prev = clist;
141 clist = ch; 139 clist = ch;
142 } 140 }
143 } else { 141 } else {
@@ -145,24 +143,22 @@ int nameif_main(int argc, char **argv)
145 143
146 while ((line = get_line_from_file(ifh)) != NULL) { 144 while ((line = get_line_from_file(ifh)) != NULL) {
147 char *line_ptr; 145 char *line_ptr;
148 unsigned short name_length; 146 size_t name_length;
149 147
150 line_ptr = line + strspn(line, " \t"); 148 line_ptr = line + strspn(line, " \t");
151 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) 149 if ((line_ptr[0] == '#') || (line_ptr[0] == '\n'))
152 continue; 150 continue;
153 name_length = strcspn(line_ptr, " \t"); 151 name_length = strcspn(line_ptr, " \t");
154 if (name_length > IF_NAMESIZE)
155 serror(use_syslog, "interface name `%s' too long",
156 argv[optind]);
157 ch = xcalloc(1, sizeof(mactable_t)); 152 ch = xcalloc(1, sizeof(mactable_t));
158 ch->next = NULL; 153 ch->ifname = xstrndup(line_ptr, name_length);
159 ch->prev = NULL; 154 if (name_length > IF_NAMESIZE)
160 ch->ifname = strndup(line_ptr, name_length); 155 serror("interface name `%s' too long",
156 ch->ifname);
161 line_ptr += name_length; 157 line_ptr += name_length;
162 line_ptr += strspn(line_ptr, " \t"); 158 line_ptr += strspn(line_ptr, " \t");
163 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); 159 name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
164 line_ptr[name_length] = '\0'; 160 line_ptr[name_length] = '\0';
165 ch->mac = cc_macaddr(line_ptr, use_syslog); 161 ch->mac = cc_macaddr(line_ptr);
166 if (clist) 162 if (clist)
167 clist->prev = ch; 163 clist->prev = ch;
168 ch->next = clist; 164 ch->next = clist;
@@ -173,7 +169,7 @@ int nameif_main(int argc, char **argv)
173 } 169 }
174 170
175 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1) 171 if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
176 serror(use_syslog, "socket: %s", strerror(errno)); 172 serror("socket: %m");
177 173
178 while (clist) { 174 while (clist) {
179 struct ifreq ifr; 175 struct ifreq ifr;
@@ -201,8 +197,8 @@ int nameif_main(int argc, char **argv)
201 197
202 strcpy(ifr.ifr_newname, ch->ifname); 198 strcpy(ifr.ifr_newname, ch->ifname);
203 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) 199 if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
204 serror(use_syslog, "cannot change ifname %s to %s: %s", 200 serror("cannot change ifname %s to %s: %m",
205 ifr.ifr_name, ch->ifname, strerror(errno)); 201 ifr.ifr_name, ch->ifname);
206 202
207 /* Remove list entry of renamed interface */ 203 /* Remove list entry of renamed interface */
208 if (ch->prev != NULL) { 204 if (ch->prev != NULL) {
@@ -212,13 +208,11 @@ int nameif_main(int argc, char **argv)
212 } 208 }
213 if (ch->next != NULL) 209 if (ch->next != NULL)
214 (ch->next)->prev = ch->prev; 210 (ch->next)->prev = ch->prev;
211#ifdef CONFIG_FEATURE_CLEAN_UP
212 free(ch->ifname);
213 free(ch->mac);
215 free(ch); 214 free(ch);
216 } 215#endif
217
218 while (clist) {
219 ch = clist;
220 clist = clist->next;
221 free(ch);
222 } 216 }
223 217
224 return 0; 218 return 0;