aboutsummaryrefslogtreecommitdiff
path: root/networking/zcip.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-06-22 22:28:29 +0000
committerRob Landley <rob@landley.net>2006-06-22 22:28:29 +0000
commit53433b3ca1fb5593167b6fd66f879d45f8f31ecc (patch)
treeda7773cc2bb55ef637668ee6bd136521962722e9 /networking/zcip.c
parent1fa4a9448b4909fb6baf9953c14760f588b680bc (diff)
downloadbusybox-w32-53433b3ca1fb5593167b6fd66f879d45f8f31ecc.tar.gz
busybox-w32-53433b3ca1fb5593167b6fd66f879d45f8f31ecc.tar.bz2
busybox-w32-53433b3ca1fb5593167b6fd66f879d45f8f31ecc.zip
Clean up.
Diffstat (limited to 'networking/zcip.c')
-rw-r--r--networking/zcip.c87
1 files changed, 13 insertions, 74 deletions
diff --git a/networking/zcip.c b/networking/zcip.c
index b5a904d46..a6b034ea2 100644
--- a/networking/zcip.c
+++ b/networking/zcip.c
@@ -9,10 +9,6 @@
9 */ 9 */
10 10
11/* 11/*
12 * This can build as part of BusyBox or by itself:
13 *
14 * $(CROSS_COMPILE)cc -Os -Wall -DNO_BUSYBOX -DDEBUG -o zcip zcip.c
15 *
16 * ZCIP just manages the 169.254.*.* addresses. That network is not 12 * ZCIP just manages the 169.254.*.* addresses. That network is not
17 * routed at the IP level, though various proxies or bridges can 13 * routed at the IP level, though various proxies or bridges can
18 * certainly be used. Its naming is built over multicast DNS. 14 * certainly be used. Its naming is built over multicast DNS.
@@ -26,22 +22,15 @@
26// - avoid silent script failures, especially under load... 22// - avoid silent script failures, especially under load...
27// - link status monitoring (restart on link-up; stop on link-down) 23// - link status monitoring (restart on link-up; stop on link-down)
28 24
25#include "busybox.h"
29#include <errno.h> 26#include <errno.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h> 27#include <string.h>
33#include <syslog.h> 28#include <syslog.h>
34#include <poll.h> 29#include <poll.h>
35#include <time.h> 30#include <time.h>
36#include <unistd.h>
37 31
38#include <sys/ioctl.h>
39#include <sys/types.h>
40#include <sys/wait.h> 32#include <sys/wait.h>
41#include <sys/socket.h>
42 33
43#include <arpa/inet.h>
44#include <netinet/in.h>
45#include <netinet/ether.h> 34#include <netinet/ether.h>
46#include <net/ethernet.h> 35#include <net/ethernet.h>
47#include <net/if.h> 36#include <net/if.h>
@@ -78,34 +67,20 @@ enum {
78 DEFEND_INTERVAL = 10 67 DEFEND_INTERVAL = 10
79}; 68};
80 69
81static const unsigned char ZCIP_VERSION[] = "0.75 (18 April 2005)";
82static char *prog;
83
84static const struct in_addr null_ip = { 0 }; 70static const struct in_addr null_ip = { 0 };
85static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} }; 71static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
86 72
87static int verbose = 0; 73static int verbose = 0;
88 74
89#ifdef DEBUG
90
91#define DBG(fmt,args...) \
92 fprintf(stderr, "%s: " fmt , prog , ## args)
93#define VDBG(fmt,args...) do { \
94 if (verbose) fprintf(stderr, "%s: " fmt , prog ,## args); \
95 } while (0)
96#else
97
98#define DBG(fmt,args...) \ 75#define DBG(fmt,args...) \
99 do { } while (0) 76 do { } while (0)
100#define VDBG DBG 77#define VDBG DBG
101#endif /* DEBUG */
102 78
103/** 79/**
104 * Pick a random link local IP address on 169.254/16, except that 80 * Pick a random link local IP address on 169.254/16, except that
105 * the first and last 256 addresses are reserved. 81 * the first and last 256 addresses are reserved.
106 */ 82 */
107static void 83static void pick(struct in_addr *ip)
108pick(struct in_addr *ip)
109{ 84{
110 unsigned tmp; 85 unsigned tmp;
111 86
@@ -119,8 +94,7 @@ pick(struct in_addr *ip)
119/** 94/**
120 * Broadcast an ARP packet. 95 * Broadcast an ARP packet.
121 */ 96 */
122static int 97static int arp(int fd, struct sockaddr *saddr, int op,
123arp(int fd, struct sockaddr *saddr, int op,
124 const struct ether_addr *source_addr, struct in_addr source_ip, 98 const struct ether_addr *source_addr, struct in_addr source_ip,
125 const struct ether_addr *target_addr, struct in_addr target_ip) 99 const struct ether_addr *target_addr, struct in_addr target_ip)
126{ 100{
@@ -153,8 +127,7 @@ arp(int fd, struct sockaddr *saddr, int op,
153/** 127/**
154 * Run a script. 128 * Run a script.
155 */ 129 */
156static int 130static int run(char *script, char *arg, char *intf, struct in_addr *ip)
157run(char *script, char *arg, char *intf, struct in_addr *ip)
158{ 131{
159 int pid, status; 132 int pid, status;
160 char *why; 133 char *why;
@@ -182,8 +155,8 @@ run(char *script, char *arg, char *intf, struct in_addr *ip)
182 goto bad; 155 goto bad;
183 } 156 }
184 if (WEXITSTATUS(status) != 0) { 157 if (WEXITSTATUS(status) != 0) {
185 fprintf(stderr, "%s: script %s failed, exit=%d\n", 158 bb_error_msg("script %s failed, exit=%d\n",
186 prog, script, WEXITSTATUS(status)); 159 script, WEXITSTATUS(status));
187 return -errno; 160 return -errno;
188 } 161 }
189 } 162 }
@@ -195,35 +168,11 @@ bad:
195 return status; 168 return status;
196} 169}
197 170
198#ifndef NO_BUSYBOX
199#include "busybox.h"
200#endif
201
202/**
203 * Print usage information.
204 */
205static void ATTRIBUTE_NORETURN
206zcip_usage(const char *msg)
207{
208 fprintf(stderr, "%s: %s\n", prog, msg);
209#ifdef NO_BUSYBOX
210 fprintf(stderr, "Usage: %s [OPTIONS] ifname script\n"
211 "\t-f foreground mode (implied by -v)\n"
212 "\t-q quit after address (no daemon)\n"
213 "\t-r 169.254.x.x request this address first\n"
214 "\t-v verbose; show version\n",
215 prog);
216 exit(0);
217#else
218 bb_show_usage();
219#endif
220}
221 171
222/** 172/**
223 * Return milliseconds of random delay, up to "secs" seconds. 173 * Return milliseconds of random delay, up to "secs" seconds.
224 */ 174 */
225static inline unsigned 175static inline unsigned ms_rdelay(unsigned secs)
226ms_rdelay(unsigned secs)
227{ 176{
228 return lrand48() % (secs * 1000); 177 return lrand48() % (secs * 1000);
229} 178}
@@ -232,12 +181,6 @@ ms_rdelay(unsigned secs)
232 * main program 181 * main program
233 */ 182 */
234 183
235#ifdef NO_BUSYBOX
236int
237main(int argc, char *argv[])
238 __attribute__ ((weak, alias ("zcip_main")));
239#endif
240
241int zcip_main(int argc, char *argv[]) 184int zcip_main(int argc, char *argv[])
242{ 185{
243 char *intf = NULL; 186 char *intf = NULL;
@@ -259,7 +202,6 @@ int zcip_main(int argc, char *argv[])
259 int t; 202 int t;
260 203
261 // parse commandline: prog [options] ifname script 204 // parse commandline: prog [options] ifname script
262 prog = argv[0];
263 while ((t = getopt(argc, argv, "fqr:v")) != EOF) { 205 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
264 switch (t) { 206 switch (t) {
265 case 'f': 207 case 'f':
@@ -272,17 +214,15 @@ int zcip_main(int argc, char *argv[])
272 if (inet_aton(optarg, &ip) == 0 214 if (inet_aton(optarg, &ip) == 0
273 || (ntohl(ip.s_addr) & IN_CLASSB_NET) 215 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
274 != LINKLOCAL_ADDR) { 216 != LINKLOCAL_ADDR) {
275 zcip_usage("invalid link address"); 217 bb_error_msg_and_die("invalid link address");
276 } 218 }
277 continue; 219 continue;
278 case 'v': 220 case 'v':
279 if (!verbose)
280 printf("%s: version %s\n", prog, ZCIP_VERSION);
281 verbose++; 221 verbose++;
282 foreground = 1; 222 foreground = 1;
283 continue; 223 continue;
284 default: 224 default:
285 zcip_usage("bad option"); 225 bb_error_msg_and_die("bad option");
286 } 226 }
287 } 227 }
288 if (optind < argc - 1) { 228 if (optind < argc - 1) {
@@ -291,8 +231,8 @@ int zcip_main(int argc, char *argv[])
291 script = argv[optind++]; 231 script = argv[optind++];
292 } 232 }
293 if (optind != argc || !intf) 233 if (optind != argc || !intf)
294 zcip_usage("wrong number of arguments"); 234 bb_show_usage();
295 openlog(prog, 0, LOG_DAEMON); 235 openlog(bb_applet_name, 0, LOG_DAEMON);
296 236
297 // initialize the interface (modprobe, ifup, etc) 237 // initialize the interface (modprobe, ifup, etc)
298 if (run(script, "init", intf, NULL) < 0) 238 if (run(script, "init", intf, NULL) < 0)
@@ -447,8 +387,7 @@ fail:
447 if (fds[0].revents & POLLERR) { 387 if (fds[0].revents & POLLERR) {
448 // FIXME: links routinely go down; 388 // FIXME: links routinely go down;
449 // this shouldn't necessarily exit. 389 // this shouldn't necessarily exit.
450 fprintf(stderr, "%s %s: poll error\n", 390 bb_error_msg("%s: poll error\n", intf);
451 prog, intf);
452 if (ready) { 391 if (ready) {
453 run(script, "deconfig", 392 run(script, "deconfig",
454 intf, &ip); 393 intf, &ip);
@@ -536,6 +475,6 @@ bad:
536 perror(why); 475 perror(why);
537 else 476 else
538 syslog(LOG_ERR, "%s %s, %s error: %s", 477 syslog(LOG_ERR, "%s %s, %s error: %s",
539 prog, intf, why, strerror(errno)); 478 bb_applet_name, intf, why, strerror(errno));
540 return EXIT_FAILURE; 479 return EXIT_FAILURE;
541} 480}