summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-06-10 17:22:49 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-06-10 17:22:49 +0000
commit24833430bc2dbea733c3b0b9ea6c6b976f95474a (patch)
tree805a4197b8a0d36eaa6880dfc23d8c2539359fe9
parent6c43f743a3f607f91ee22a53db880fce2df645f0 (diff)
downloadbusybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.tar.gz
busybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.tar.bz2
busybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.zip
Vodz, last_patch_88
-rw-r--r--include/applets.h12
-rw-r--r--libbb/find_root_device.c11
-rw-r--r--networking/httpd.c1
-rw-r--r--networking/udhcp/AUTHORS1
-rw-r--r--networking/udhcp/ChangeLog4
-rw-r--r--networking/udhcp/Makefile.in15
-rw-r--r--networking/udhcp/arpping.c9
-rw-r--r--networking/udhcp/clientpacket.c10
-rw-r--r--networking/udhcp/dhcpc.c120
-rw-r--r--networking/udhcp/dhcpc.h2
-rw-r--r--networking/udhcp/dhcpd.c77
-rw-r--r--networking/udhcp/dhcpd.h2
-rw-r--r--networking/udhcp/dumpleases.c44
-rw-r--r--networking/udhcp/files.c123
-rw-r--r--networking/udhcp/files.h10
-rw-r--r--networking/udhcp/leases.c34
-rw-r--r--networking/udhcp/leases.h3
-rw-r--r--networking/udhcp/options.c4
-rw-r--r--networking/udhcp/packet.c10
-rw-r--r--networking/udhcp/script.c59
-rw-r--r--networking/udhcp/serverpacket.c7
-rw-r--r--networking/udhcp/socket.c18
22 files changed, 211 insertions, 365 deletions
diff --git a/include/applets.h b/include/applets.h
index 6697be5ee..01cfb8629 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -283,12 +283,12 @@
283#ifdef CONFIG_IP 283#ifdef CONFIG_IP
284 APPLET(ip, ip_main, _BB_DIR_BIN, _BB_SUID_NEVER) 284 APPLET(ip, ip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
285#endif 285#endif
286#ifdef CONFIG_IPCALC
287 APPLET(ipcalc, ipcalc_main, _BB_DIR_BIN, _BB_SUID_NEVER)
288#endif
289#ifdef CONFIG_IPADDRESS 286#ifdef CONFIG_IPADDRESS
290 APPLET(ipaddr, ipaddr_main, _BB_DIR_BIN, _BB_SUID_NEVER) 287 APPLET(ipaddr, ipaddr_main, _BB_DIR_BIN, _BB_SUID_NEVER)
291#endif 288#endif
289#ifdef CONFIG_IPCALC
290 APPLET(ipcalc, ipcalc_main, _BB_DIR_BIN, _BB_SUID_NEVER)
291#endif
292#ifdef CONFIG_IPLINK 292#ifdef CONFIG_IPLINK
293 APPLET(iplink, iplink_main, _BB_DIR_BIN, _BB_SUID_NEVER) 293 APPLET(iplink, iplink_main, _BB_DIR_BIN, _BB_SUID_NEVER)
294#endif 294#endif
@@ -386,7 +386,7 @@
386 APPLET(more, more_main, _BB_DIR_BIN, _BB_SUID_NEVER) 386 APPLET(more, more_main, _BB_DIR_BIN, _BB_SUID_NEVER)
387#endif 387#endif
388#ifdef CONFIG_MOUNT 388#ifdef CONFIG_MOUNT
389 APPLET(mount, mount_main, _BB_DIR_BIN, _BB_SUID_NEVER) 389 APPLET(mount, mount_main, _BB_DIR_BIN, _BB_SUID_MAYBE)
390#endif 390#endif
391#ifdef CONFIG_MSH 391#ifdef CONFIG_MSH
392 APPLET_NOUSAGE("msh", msh_main, _BB_DIR_BIN, _BB_SUID_NEVER) 392 APPLET_NOUSAGE("msh", msh_main, _BB_DIR_BIN, _BB_SUID_NEVER)
@@ -428,7 +428,7 @@
428 APPLET(pidof, pidof_main, _BB_DIR_BIN, _BB_SUID_NEVER) 428 APPLET(pidof, pidof_main, _BB_DIR_BIN, _BB_SUID_NEVER)
429#endif 429#endif
430#ifdef CONFIG_PING 430#ifdef CONFIG_PING
431 APPLET(ping, ping_main, _BB_DIR_BIN, _BB_SUID_NEVER) 431 APPLET(ping, ping_main, _BB_DIR_BIN, _BB_SUID_MAYBE)
432#endif 432#endif
433#ifdef CONFIG_PING6 433#ifdef CONFIG_PING6
434 APPLET(ping6, ping6_main, _BB_DIR_BIN, _BB_SUID_NEVER) 434 APPLET(ping6, ping6_main, _BB_DIR_BIN, _BB_SUID_NEVER)
@@ -572,7 +572,7 @@
572 APPLET(tr, tr_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) 572 APPLET(tr, tr_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
573#endif 573#endif
574#ifdef CONFIG_TRACEROUTE 574#ifdef CONFIG_TRACEROUTE
575 APPLET(traceroute, traceroute_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER) 575 APPLET(traceroute, traceroute_main, _BB_DIR_USR_BIN, _BB_SUID_MAYBE)
576#endif 576#endif
577#ifdef CONFIG_TRUE 577#ifdef CONFIG_TRUE
578 APPLET(true, true_main, _BB_DIR_BIN, _BB_SUID_NEVER) 578 APPLET(true, true_main, _BB_DIR_BIN, _BB_SUID_NEVER)
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
index b12d392a2..654f17145 100644
--- a/libbb/find_root_device.c
+++ b/libbb/find_root_device.c
@@ -48,11 +48,14 @@ extern char *find_real_root_device_name(const char* name)
48 bb_perror_msg("could not open '/dev'"); 48 bb_perror_msg("could not open '/dev'");
49 else { 49 else {
50 while((entry = readdir(dir)) != NULL) { 50 while((entry = readdir(dir)) != NULL) {
51 51 const char *name = entry->d_name;
52 fileName = concat_subpath_file("/dev", entry->d_name); 52 /* Must skip ".." since that is "/", and so we
53 if(fileName == NULL) 53 * would get a false positive on ".." */
54 if (name[0] == '.' && name[1] == '.' && !name[2])
54 continue; 55 continue;
55 56
57 fileName = concat_path_file("/dev", name);
58
56 /* Some char devices have the same dev_t as block 59 /* Some char devices have the same dev_t as block
57 * devices, so make sure this is a block device */ 60 * devices, so make sure this is a block device */
58 if (stat(fileName, &statBuf) == 0 && 61 if (stat(fileName, &statBuf) == 0 &&
@@ -66,7 +69,7 @@ extern char *find_real_root_device_name(const char* name)
66 } 69 }
67 } 70 }
68 if(fileName==NULL) 71 if(fileName==NULL)
69 fileName=bb_xstrdup("/dev/root"); 72 fileName = bb_xstrdup("/dev/root");
70 return fileName; 73 return fileName;
71} 74}
72 75
diff --git a/networking/httpd.c b/networking/httpd.c
index ef8263b99..43b489fac 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -402,6 +402,7 @@ static void parse_conf(const char *path, int flag)
402 402
403 /* free previous ip setup if present */ 403 /* free previous ip setup if present */
404 free_config_lines(&config->ip_a_d); 404 free_config_lines(&config->ip_a_d);
405 config->flg_deny_all = 0;
405 /* retain previous auth and mime config only for subdir parse */ 406 /* retain previous auth and mime config only for subdir parse */
406 if(flag != SUBDIR_PARSE) { 407 if(flag != SUBDIR_PARSE) {
407#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH 408#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
diff --git a/networking/udhcp/AUTHORS b/networking/udhcp/AUTHORS
index 89a6de41d..3772aedb3 100644
--- a/networking/udhcp/AUTHORS
+++ b/networking/udhcp/AUTHORS
@@ -9,5 +9,6 @@ Other Credits:
9-------------- 9--------------
10Moreton Bay (http://www.moretonbay.com/) 10Moreton Bay (http://www.moretonbay.com/)
11Lineo (http://opensource.lineo.com) 11Lineo (http://opensource.lineo.com)
12Vladimir Oleynik <dzo@simtrea.ru>, optimize and more integrate for busybox
12 13
13 14
diff --git a/networking/udhcp/ChangeLog b/networking/udhcp/ChangeLog
index 7409fd0ad..57e89a018 100644
--- a/networking/udhcp/ChangeLog
+++ b/networking/udhcp/ChangeLog
@@ -1,3 +1,7 @@
10.9.10
2 Size optimization (over 3k), more busybox integration
3 (Vladimir Oleynik <dzo@simtreas.ru>
4
10.9.9 (pending) 50.9.9 (pending)
2+ Fixed a little endian problem in mton (Bastian Blank <waldi@debian.org>) 6+ Fixed a little endian problem in mton (Bastian Blank <waldi@debian.org>)
3+ Fixed a arpping alignment problem (Rui He <rhe@3eti.com>) 7+ Fixed a arpping alignment problem (Rui He <rhe@3eti.com>)
diff --git a/networking/udhcp/Makefile.in b/networking/udhcp/Makefile.in
index 369308e8d..5d6176bab 100644
--- a/networking/udhcp/Makefile.in
+++ b/networking/udhcp/Makefile.in
@@ -33,11 +33,22 @@ CONFIG_UDHCP_SHARED=n
33endif 33endif
34endif 34endif
35 35
36ifeq ($(CONFIG_UDHCPD), y)
37CONFIG_UDHCP_LEASES_FILE=y
38else
39ifeq ($(CONFIG_UDHCPD), y)
40CONFIG_UDHCP_LEASES_FILE=y
41else
42CONFIG_UDHCP_LEASES_FILE=n
43endif
44endif
45
36UDHCP-y:= 46UDHCP-y:=
37UDHCP-$(CONFIG_UDHCP_SHARED) += options.c socket.c packet.c pidfile.c 47UDHCP-$(CONFIG_UDHCP_SHARED) += options.c socket.c packet.c common.c
38UDHCP-$(CONFIG_UDHCPC) += dhcpc.c clientpacket.c script.c 48UDHCP-$(CONFIG_UDHCPC) += dhcpc.c clientpacket.c script.c
39UDHCP-$(CONFIG_UDHCPD) += dhcpd.c arpping.c files.c leases.c serverpacket.c 49UDHCP-$(CONFIG_UDHCPD) += dhcpd.c arpping.c files.c leases.c serverpacket.c
40UDHCP-$(CONFIG_DUMPLEASES) += dumpleases.c 50UDHCP-$(CONFIG_DUMPLEASES) += dumpleases.c
51UDHCP-$(CONFIG_UDHCP_LEASES_FILE) += leases_file.c
41UDHCP_OBJS=$(patsubst %.c,$(UDHCP_DIR)%.o, $(UDHCP-y)) 52UDHCP_OBJS=$(patsubst %.c,$(UDHCP_DIR)%.o, $(UDHCP-y))
42 53
43libraries-y+=$(UDHCP_DIR)$(UDHCP_AR) 54libraries-y+=$(UDHCP_DIR)$(UDHCP_AR)
@@ -46,5 +57,5 @@ $(UDHCP_DIR)$(UDHCP_AR): $(UDHCP_OBJS)
46 $(AR) -ro $@ $(UDHCP_OBJS) 57 $(AR) -ro $@ $(UDHCP_OBJS)
47 58
48$(UDHCP_OBJS): %.o : %.c 59$(UDHCP_OBJS): %.o : %.c
49 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DIN_BUSYBOX -c $< -o $@ 60 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c $< -o $@
50 61
diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c
index 4c0b3d83b..e20395a9e 100644
--- a/networking/udhcp/arpping.c
+++ b/networking/udhcp/arpping.c
@@ -5,22 +5,19 @@
5 * by Yoichi Hariguchi <yoichi@fore.com> 5 * by Yoichi Hariguchi <yoichi@fore.com>
6 */ 6 */
7 7
8#include <sys/types.h>
9#include <sys/time.h> 8#include <sys/time.h>
10#include <time.h> 9#include <time.h>
11#include <sys/types.h>
12#include <sys/socket.h> 10#include <sys/socket.h>
13#include <netinet/if_ether.h> 11#include <netinet/if_ether.h>
14#include <net/if_arp.h> 12#include <net/if_arp.h>
15#include <netinet/in.h> 13#include <netinet/in.h>
16#include <stdio.h>
17#include <string.h> 14#include <string.h>
18#include <unistd.h> 15#include <unistd.h>
19#include <errno.h> 16#include <errno.h>
20 17
21#include "dhcpd.h" 18#include "dhcpd.h"
22#include "debug.h"
23#include "arpping.h" 19#include "arpping.h"
20#include "common.h"
24 21
25/* args: yiaddr - what IP to ping 22/* args: yiaddr - what IP to ping
26 * ip - our ip 23 * ip - our ip
@@ -47,7 +44,7 @@ int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface)
47 44
48 45
49 if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) { 46 if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
50 LOG(LOG_ERR, "Could not open raw socket"); 47 LOG(LOG_ERR, bb_msg_can_not_create_raw_socket);
51 return -1; 48 return -1;
52 } 49 }
53 50
@@ -84,7 +81,7 @@ int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface)
84 FD_SET(s, &fdset); 81 FD_SET(s, &fdset);
85 tm.tv_sec = timeout; 82 tm.tv_sec = timeout;
86 if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) { 83 if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
87 DEBUG(LOG_ERR, "Error on ARPING request: %s", strerror(errno)); 84 DEBUG(LOG_ERR, "Error on ARPING request: %m");
88 if (errno != EINTR) rv = 0; 85 if (errno != EINTR) rv = 0;
89 } else if (FD_ISSET(s, &fdset)) { 86 } else if (FD_ISSET(s, &fdset)) {
90 if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0; 87 if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c
index 86faec925..7debac2ef 100644
--- a/networking/udhcp/clientpacket.c
+++ b/networking/udhcp/clientpacket.c
@@ -35,17 +35,13 @@
35#include <unistd.h> 35#include <unistd.h>
36#include <netinet/in.h> 36#include <netinet/in.h>
37#include <arpa/inet.h> 37#include <arpa/inet.h>
38#include <errno.h>
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <fcntl.h> 38#include <fcntl.h>
42 39
43 40
44#include "dhcpd.h" 41#include "dhcpd.h"
45#include "packet.h"
46#include "options.h" 42#include "options.h"
47#include "dhcpc.h" 43#include "dhcpc.h"
48#include "debug.h" 44#include "common.h"
49 45
50 46
51/* Create a random xid */ 47/* Create a random xid */
@@ -58,8 +54,7 @@ unsigned long random_xid(void)
58 54
59 fd = open("/dev/urandom", 0); 55 fd = open("/dev/urandom", 0);
60 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { 56 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
61 LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %s", 57 LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
62 strerror(errno));
63 seed = time(0); 58 seed = time(0);
64 } 59 }
65 if (fd >= 0) close(fd); 60 if (fd >= 0) close(fd);
@@ -250,4 +245,3 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
250 return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); 245 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
251 246
252} 247}
253
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index c97e422e2..4612c4100 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -19,9 +19,7 @@
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */ 20 */
21 21
22#include <stdio.h>
23#include <sys/time.h> 22#include <sys/time.h>
24#include <sys/types.h>
25#include <sys/file.h> 23#include <sys/file.h>
26#include <unistd.h> 24#include <unistd.h>
27#include <getopt.h> 25#include <getopt.h>
@@ -40,11 +38,9 @@
40#include "dhcpc.h" 38#include "dhcpc.h"
41#include "options.h" 39#include "options.h"
42#include "clientpacket.h" 40#include "clientpacket.h"
43#include "packet.h"
44#include "script.h" 41#include "script.h"
45#include "socket.h" 42#include "socket.h"
46#include "debug.h" 43#include "common.h"
47#include "pidfile.h"
48 44
49static int state; 45static int state;
50static unsigned long requested_ip; /* = 0 */ 46static unsigned long requested_ip; /* = 0 */
@@ -52,7 +48,6 @@ static unsigned long server_addr;
52static unsigned long timeout; 48static unsigned long timeout;
53static int packet_num; /* = 0 */ 49static int packet_num; /* = 0 */
54static int fd = -1; 50static int fd = -1;
55static int signal_pipe[2];
56 51
57#define LISTEN_NONE 0 52#define LISTEN_NONE 0
58#define LISTEN_KERNEL 1 53#define LISTEN_KERNEL 1
@@ -80,34 +75,6 @@ struct client_config_t client_config = {
80 arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */ 75 arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */
81}; 76};
82 77
83#ifndef IN_BUSYBOX
84static void __attribute__ ((noreturn)) bb_show_usage(void)
85{
86 printf(
87"Usage: udhcpc [OPTIONS]\n\n"
88" -c, --clientid=CLIENTID Client identifier\n"
89" -H, --hostname=HOSTNAME Client hostname\n"
90" -h Alias for -H\n"
91" -f, --foreground Do not fork after getting lease\n"
92" -b, --background Fork to background if lease cannot be\n"
93" immediately negotiated.\n"
94" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
95" -n, --now Exit with failure if lease cannot be\n"
96" immediately negotiated.\n"
97" -p, --pidfile=file Store process ID of daemon in file\n"
98" -q, --quit Quit after obtaining lease\n"
99" -r, --request=IP IP address to request (default: none)\n"
100" -s, --script=file Run file at dhcp events (default:\n"
101" " DEFAULT_SCRIPT ")\n"
102" -v, --version Display version\n"
103 );
104 exit(0);
105}
106#else
107extern void bb_show_usage(void) __attribute__ ((noreturn));
108#endif
109
110
111/* just a little helper */ 78/* just a little helper */
112static void change_mode(int new_mode) 79static void change_mode(int new_mode)
113{ 80{
@@ -172,46 +139,15 @@ static void perform_release(void)
172} 139}
173 140
174 141
175/* Exit and cleanup */ 142static void client_background(void)
176static void exit_client(int retval)
177{
178 pidfile_delete(client_config.pidfile);
179 CLOSE_LOG();
180 exit(retval);
181}
182
183
184/* Signal handler */
185static void signal_handler(int sig)
186{
187 if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
188 LOG(LOG_ERR, "Could not send signal: %s",
189 strerror(errno));
190 }
191}
192
193
194static void background(void)
195{ 143{
196 int pid_fd; 144 background(client_config.pidfile);
197
198 pid_fd = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */
199 while (pid_fd >= 0 && pid_fd < 3) pid_fd = dup(pid_fd); /* don't let daemon close it */
200 if (daemon(0, 0) == -1) {
201 perror("fork");
202 exit_client(1);
203 }
204 client_config.foreground = 1; /* Do not fork again. */ 145 client_config.foreground = 1; /* Do not fork again. */
205 client_config.background_if_no_lease = 0; 146 client_config.background_if_no_lease = 0;
206 pidfile_write_release(pid_fd);
207} 147}
208 148
209 149
210#ifdef COMBINED_BINARY
211int udhcpc_main(int argc, char *argv[]) 150int udhcpc_main(int argc, char *argv[])
212#else
213int main(int argc, char *argv[])
214#endif
215{ 151{
216 unsigned char *temp, *message; 152 unsigned char *temp, *message;
217 unsigned long t1 = 0, t2 = 0, xid = 0; 153 unsigned long t1 = 0, t2 = 0, xid = 0;
@@ -222,12 +158,11 @@ int main(int argc, char *argv[])
222 int c, len; 158 int c, len;
223 struct dhcpMessage packet; 159 struct dhcpMessage packet;
224 struct in_addr temp_addr; 160 struct in_addr temp_addr;
225 int pid_fd;
226 time_t now; 161 time_t now;
227 int max_fd; 162 int max_fd;
228 int sig; 163 int sig;
229 164
230 static struct option arg_options[] = { 165 static const struct option arg_options[] = {
231 {"clientid", required_argument, 0, 'c'}, 166 {"clientid", required_argument, 0, 'c'},
232 {"foreground", no_argument, 0, 'f'}, 167 {"foreground", no_argument, 0, 'f'},
233 {"background", no_argument, 0, 'b'}, 168 {"background", no_argument, 0, 'b'},
@@ -240,7 +175,6 @@ int main(int argc, char *argv[])
240 {"request", required_argument, 0, 'r'}, 175 {"request", required_argument, 0, 'r'},
241 {"script", required_argument, 0, 's'}, 176 {"script", required_argument, 0, 's'},
242 {"version", no_argument, 0, 'v'}, 177 {"version", no_argument, 0, 'v'},
243 {"help", no_argument, 0, '?'},
244 {0, 0, 0, 0} 178 {0, 0, 0, 0}
245 }; 179 };
246 180
@@ -294,23 +228,18 @@ int main(int argc, char *argv[])
294 client_config.script = optarg; 228 client_config.script = optarg;
295 break; 229 break;
296 case 'v': 230 case 'v':
297 printf("udhcpcd, version %s\n\n", VERSION); 231 bb_error_msg("version %s\n", VERSION);
298 exit_client(0); 232 return(0);
299 break; 233 break;
300 default: 234 default:
301 bb_show_usage(); 235 bb_show_usage();
302 } 236 }
303 } 237 }
304 238
305 OPEN_LOG("udhcpc"); 239 start_log("client");
306 LOG(LOG_INFO, "udhcp client (v%s) started", VERSION);
307
308 pid_fd = pidfile_acquire(client_config.pidfile);
309 pidfile_write_release(pid_fd);
310
311 if (read_interface(client_config.interface, &client_config.ifindex, 240 if (read_interface(client_config.interface, &client_config.ifindex,
312 NULL, client_config.arp) < 0) 241 NULL, client_config.arp) < 0)
313 exit_client(1); 242 return(1);
314 243
315 if (!client_config.clientid) { 244 if (!client_config.clientid) {
316 client_config.clientid = xmalloc(6 + 3); 245 client_config.clientid = xmalloc(6 + 3);
@@ -321,10 +250,7 @@ int main(int argc, char *argv[])
321 } 250 }
322 251
323 /* setup signal handlers */ 252 /* setup signal handlers */
324 socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe); 253 udhcp_set_signal_pipe(SIGUSR2);
325 signal(SIGUSR1, signal_handler);
326 signal(SIGUSR2, signal_handler);
327 signal(SIGTERM, signal_handler);
328 254
329 state = INIT_SELECTING; 255 state = INIT_SELECTING;
330 run_script(NULL, "deconfig"); 256 run_script(NULL, "deconfig");
@@ -342,16 +268,16 @@ int main(int argc, char *argv[])
342 else 268 else
343 fd = raw_socket(client_config.ifindex); 269 fd = raw_socket(client_config.ifindex);
344 if (fd < 0) { 270 if (fd < 0) {
345 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s", strerror(errno)); 271 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
346 exit_client(0); 272 return(0);
347 } 273 }
348 } 274 }
349 if (fd >= 0) FD_SET(fd, &rfds); 275 if (fd >= 0) FD_SET(fd, &rfds);
350 FD_SET(signal_pipe[0], &rfds); 276 FD_SET(udhcp_signal_pipe[0], &rfds);
351 277
352 if (tv.tv_sec > 0) { 278 if (tv.tv_sec > 0) {
353 DEBUG(LOG_INFO, "Waiting on select...\n"); 279 DEBUG(LOG_INFO, "Waiting on select...\n");
354 max_fd = signal_pipe[0] > fd ? signal_pipe[0] : fd; 280 max_fd = udhcp_signal_pipe[0] > fd ? udhcp_signal_pipe[0] : fd;
355 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); 281 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
356 } else retval = 0; /* If we already timed out, fall through */ 282 } else retval = 0; /* If we already timed out, fall through */
357 283
@@ -372,10 +298,10 @@ int main(int argc, char *argv[])
372 } else { 298 } else {
373 if (client_config.background_if_no_lease) { 299 if (client_config.background_if_no_lease) {
374 LOG(LOG_INFO, "No lease, forking to background."); 300 LOG(LOG_INFO, "No lease, forking to background.");
375 background(); 301 client_background();
376 } else if (client_config.abort_if_no_lease) { 302 } else if (client_config.abort_if_no_lease) {
377 LOG(LOG_INFO, "No lease, failing."); 303 LOG(LOG_INFO, "No lease, failing.");
378 exit_client(1); 304 return(1);
379 } 305 }
380 /* wait to try again */ 306 /* wait to try again */
381 packet_num = 0; 307 packet_num = 0;
@@ -453,7 +379,7 @@ int main(int argc, char *argv[])
453 else len = get_raw_packet(&packet, fd); 379 else len = get_raw_packet(&packet, fd);
454 380
455 if (len == -1 && errno != EINTR) { 381 if (len == -1 && errno != EINTR) {
456 DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno)); 382 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
457 change_mode(listen_mode); /* just close and reopen */ 383 change_mode(listen_mode); /* just close and reopen */
458 } 384 }
459 if (len < 0) continue; 385 if (len < 0) continue;
@@ -517,9 +443,9 @@ int main(int argc, char *argv[])
517 state = BOUND; 443 state = BOUND;
518 change_mode(LISTEN_NONE); 444 change_mode(LISTEN_NONE);
519 if (client_config.quit_after_lease) 445 if (client_config.quit_after_lease)
520 exit_client(0); 446 return(0);
521 if (!client_config.foreground) 447 if (!client_config.foreground)
522 background(); 448 client_background();
523 449
524 } else if (*message == DHCPNAK) { 450 } else if (*message == DHCPNAK) {
525 /* return to init state */ 451 /* return to init state */
@@ -537,10 +463,9 @@ int main(int argc, char *argv[])
537 break; 463 break;
538 /* case BOUND, RELEASED: - ignore all packets */ 464 /* case BOUND, RELEASED: - ignore all packets */
539 } 465 }
540 } else if (retval > 0 && FD_ISSET(signal_pipe[0], &rfds)) { 466 } else if (retval > 0 && FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
541 if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) { 467 if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) {
542 DEBUG(LOG_ERR, "Could not read signal: %s", 468 DEBUG(LOG_ERR, "Could not read signal: %m");
543 strerror(errno));
544 continue; /* probably just EINTR */ 469 continue; /* probably just EINTR */
545 } 470 }
546 switch (sig) { 471 switch (sig) {
@@ -552,7 +477,7 @@ int main(int argc, char *argv[])
552 break; 477 break;
553 case SIGTERM: 478 case SIGTERM:
554 LOG(LOG_INFO, "Received SIGTERM"); 479 LOG(LOG_INFO, "Received SIGTERM");
555 exit_client(0); 480 return(0);
556 } 481 }
557 } else if (retval == -1 && errno == EINTR) { 482 } else if (retval == -1 && errno == EINTR) {
558 /* a signal was caught */ 483 /* a signal was caught */
@@ -564,4 +489,3 @@ int main(int argc, char *argv[])
564 } 489 }
565 return 0; 490 return 0;
566} 491}
567
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h
index 52157f5c8..e23d9d4fe 100644
--- a/networking/udhcp/dhcpc.h
+++ b/networking/udhcp/dhcpc.h
@@ -2,8 +2,6 @@
2#ifndef _DHCPC_H 2#ifndef _DHCPC_H
3#define _DHCPC_H 3#define _DHCPC_H
4 4
5#include "libbb_udhcp.h"
6
7#define INIT_SELECTING 0 5#define INIT_SELECTING 0
8#define REQUESTING 1 6#define REQUESTING 1
9#define BOUND 2 7#define BOUND 2
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index 56ddaa942..c21cb72a2 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -25,12 +25,9 @@
25#include <string.h> 25#include <string.h>
26#include <stdlib.h> 26#include <stdlib.h>
27#include <sys/wait.h> 27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <arpa/inet.h> 28#include <arpa/inet.h>
30#include <netdb.h> 29#include <netdb.h>
31#include <netinet/in.h> 30#include <netinet/in.h>
32#include <stdio.h>
33#include <sys/types.h>
34#include <sys/socket.h> 31#include <sys/socket.h>
35#include <unistd.h> 32#include <unistd.h>
36#include <signal.h> 33#include <signal.h>
@@ -39,47 +36,21 @@
39#include <time.h> 36#include <time.h>
40#include <sys/time.h> 37#include <sys/time.h>
41 38
42#include "debug.h"
43#include "dhcpd.h" 39#include "dhcpd.h"
44#include "arpping.h" 40#include "arpping.h"
45#include "socket.h" 41#include "socket.h"
46#include "options.h" 42#include "options.h"
47#include "files.h" 43#include "files.h"
48#include "leases.h"
49#include "packet.h"
50#include "serverpacket.h" 44#include "serverpacket.h"
51#include "pidfile.h" 45#include "common.h"
52 46
53 47
54/* globals */ 48/* globals */
55struct dhcpOfferedAddr *leases; 49struct dhcpOfferedAddr *leases;
56struct server_config_t server_config; 50struct server_config_t server_config;
57static int signal_pipe[2];
58
59/* Exit and cleanup */
60static void exit_server(int retval)
61{
62 pidfile_delete(server_config.pidfile);
63 CLOSE_LOG();
64 exit(retval);
65}
66
67
68/* Signal handler */
69static void signal_handler(int sig)
70{
71 if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
72 LOG(LOG_ERR, "Could not send signal: %s",
73 strerror(errno));
74 }
75}
76 51
77 52
78#ifdef COMBINED_BINARY
79int udhcpd_main(int argc, char *argv[]) 53int udhcpd_main(int argc, char *argv[])
80#else
81int main(int argc, char *argv[])
82#endif
83{ 54{
84 fd_set rfds; 55 fd_set rfds;
85 struct timeval tv; 56 struct timeval tv;
@@ -92,13 +63,11 @@ int main(int argc, char *argv[])
92 unsigned long timeout_end; 63 unsigned long timeout_end;
93 struct option_set *option; 64 struct option_set *option;
94 struct dhcpOfferedAddr *lease; 65 struct dhcpOfferedAddr *lease;
95 int pid_fd;
96 int max_sock; 66 int max_sock;
97 int sig; 67 int sig;
98 unsigned long num_ips; 68 unsigned long num_ips;
99 69
100 OPEN_LOG("udhcpd"); 70 start_log("server");
101 LOG(LOG_INFO, "udhcp server (v%s) started", VERSION);
102 71
103 memset(&server_config, 0, sizeof(struct server_config_t)); 72 memset(&server_config, 0, sizeof(struct server_config_t));
104 73
@@ -106,9 +75,6 @@ int main(int argc, char *argv[])
106 read_config(DHCPD_CONF_FILE); 75 read_config(DHCPD_CONF_FILE);
107 else read_config(argv[1]); 76 else read_config(argv[1]);
108 77
109 pid_fd = pidfile_acquire(server_config.pidfile);
110 pidfile_write_release(pid_fd);
111
112 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { 78 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
113 memcpy(&server_config.lease, option->data + 2, 4); 79 memcpy(&server_config.lease, option->data + 2, 4);
114 server_config.lease = ntohl(server_config.lease); 80 server_config.lease = ntohl(server_config.lease);
@@ -118,51 +84,43 @@ int main(int argc, char *argv[])
118 /* Sanity check */ 84 /* Sanity check */
119 num_ips = ntohl(server_config.end) - ntohl(server_config.start); 85 num_ips = ntohl(server_config.end) - ntohl(server_config.start);
120 if (server_config.max_leases > num_ips) { 86 if (server_config.max_leases > num_ips) {
121 LOG(LOG_ERR, "max_leases value (%lu) not sane, setting to %lu instead", 87 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
88 "setting to %lu instead",
122 server_config.max_leases, num_ips); 89 server_config.max_leases, num_ips);
123 server_config.max_leases = num_ips; 90 server_config.max_leases = num_ips;
124 } 91 }
125 92
126 leases = xmalloc(sizeof(struct dhcpOfferedAddr) * server_config.max_leases); 93 leases = xcalloc(sizeof(struct dhcpOfferedAddr), server_config.max_leases);
127 memset(leases, 0, sizeof(struct dhcpOfferedAddr) * server_config.max_leases);
128 read_leases(server_config.lease_file); 94 read_leases(server_config.lease_file);
129 95
130 if (read_interface(server_config.interface, &server_config.ifindex, 96 if (read_interface(server_config.interface, &server_config.ifindex,
131 &server_config.server, server_config.arp) < 0) 97 &server_config.server, server_config.arp) < 0)
132 exit_server(1); 98 return(1);
133 99
134#ifndef DEBUGGING 100#ifndef CONFIG_FEATURE_UDHCP_DEBUG
135 pid_fd = pidfile_acquire(server_config.pidfile); /* hold lock during fork. */ 101 background(server_config.pidfile);
136 if (daemon(0, 0) == -1) {
137 perror("fork");
138 exit_server(1);
139 }
140 pidfile_write_release(pid_fd);
141#endif 102#endif
142 103
143 104 udhcp_set_signal_pipe(0);
144 socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe);
145 signal(SIGUSR1, signal_handler);
146 signal(SIGTERM, signal_handler);
147 105
148 timeout_end = time(0) + server_config.auto_time; 106 timeout_end = time(0) + server_config.auto_time;
149 while(1) { /* loop until universe collapses */ 107 while(1) { /* loop until universe collapses */
150 108
151 if (server_socket < 0) 109 if (server_socket < 0)
152 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { 110 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
153 LOG(LOG_ERR, "FATAL: couldn't create server socket, %s", strerror(errno)); 111 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
154 exit_server(0); 112 return(2);
155 } 113 }
156 114
157 FD_ZERO(&rfds); 115 FD_ZERO(&rfds);
158 FD_SET(server_socket, &rfds); 116 FD_SET(server_socket, &rfds);
159 FD_SET(signal_pipe[0], &rfds); 117 FD_SET(udhcp_signal_pipe[0], &rfds);
160 if (server_config.auto_time) { 118 if (server_config.auto_time) {
161 tv.tv_sec = timeout_end - time(0); 119 tv.tv_sec = timeout_end - time(0);
162 tv.tv_usec = 0; 120 tv.tv_usec = 0;
163 } 121 }
164 if (!server_config.auto_time || tv.tv_sec > 0) { 122 if (!server_config.auto_time || tv.tv_sec > 0) {
165 max_sock = server_socket > signal_pipe[0] ? server_socket : signal_pipe[0]; 123 max_sock = server_socket > udhcp_signal_pipe[0] ? server_socket : udhcp_signal_pipe[0];
166 retval = select(max_sock + 1, &rfds, NULL, NULL, 124 retval = select(max_sock + 1, &rfds, NULL, NULL,
167 server_config.auto_time ? &tv : NULL); 125 server_config.auto_time ? &tv : NULL);
168 } else retval = 0; /* If we already timed out, fall through */ 126 } else retval = 0; /* If we already timed out, fall through */
@@ -176,8 +134,8 @@ int main(int argc, char *argv[])
176 continue; 134 continue;
177 } 135 }
178 136
179 if (FD_ISSET(signal_pipe[0], &rfds)) { 137 if (FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
180 if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) 138 if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0)
181 continue; /* probably just EINTR */ 139 continue; /* probably just EINTR */
182 switch (sig) { 140 switch (sig) {
183 case SIGUSR1: 141 case SIGUSR1:
@@ -188,13 +146,13 @@ int main(int argc, char *argv[])
188 continue; 146 continue;
189 case SIGTERM: 147 case SIGTERM:
190 LOG(LOG_INFO, "Received a SIGTERM"); 148 LOG(LOG_INFO, "Received a SIGTERM");
191 exit_server(0); 149 return(0);
192 } 150 }
193 } 151 }
194 152
195 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ 153 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
196 if (bytes == -1 && errno != EINTR) { 154 if (bytes == -1 && errno != EINTR) {
197 DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno)); 155 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
198 close(server_socket); 156 close(server_socket);
199 server_socket = -1; 157 server_socket = -1;
200 } 158 }
@@ -293,4 +251,3 @@ int main(int argc, char *argv[])
293 251
294 return 0; 252 return 0;
295} 253}
296
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h
index 5debc8388..e219143cc 100644
--- a/networking/udhcp/dhcpd.h
+++ b/networking/udhcp/dhcpd.h
@@ -5,9 +5,7 @@
5#include <netinet/ip.h> 5#include <netinet/ip.h>
6#include <netinet/udp.h> 6#include <netinet/udp.h>
7 7
8#include "libbb_udhcp.h"
9#include "leases.h" 8#include "leases.h"
10#include "version.h"
11 9
12/************************************/ 10/************************************/
13/* Defaults _you_ may want to tweak */ 11/* Defaults _you_ may want to tweak */
diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c
index 014f73f9d..4c6107cf7 100644
--- a/networking/udhcp/dumpleases.c
+++ b/networking/udhcp/dumpleases.c
@@ -1,79 +1,55 @@
1#include <fcntl.h> 1#include <fcntl.h>
2#include <string.h> 2#include <string.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <stdio.h>
5#include <sys/wait.h> 4#include <sys/wait.h>
6#include <sys/stat.h>
7#include <arpa/inet.h> 5#include <arpa/inet.h>
8#include <netdb.h> 6#include <netdb.h>
9#include <netinet/in.h> 7#include <netinet/in.h>
10#include <stdio.h> 8#include <stdio.h>
11#include <sys/types.h>
12#include <sys/socket.h> 9#include <sys/socket.h>
13#include <unistd.h> 10#include <unistd.h>
14#include <syslog.h>
15#include <signal.h>
16#include <errno.h>
17#include <getopt.h> 11#include <getopt.h>
18#include <time.h> 12#include <time.h>
19 13
20#include "libbb_udhcp.h" 14#include "leases.h"
15#include "busybox.h"
21 16
22#define REMAINING 0 17#define REMAINING 0
23#define ABSOLUTE 1 18#define ABSOLUTE 1
24 19
25struct lease_t {
26 unsigned char chaddr[16];
27 u_int32_t yiaddr;
28 u_int32_t expires;
29};
30
31#ifdef IN_BUSYBOX
32int dumpleases_main(int argc, char *argv[]) 20int dumpleases_main(int argc, char *argv[])
33#else
34int main(int argc, char *argv[])
35#endif
36{ 21{
37 FILE *fp; 22 FILE *fp;
38 int i, c, mode = REMAINING; 23 int i, c, mode = REMAINING;
39 long expires; 24 long expires;
40 char file[255] = "/var/lib/misc/udhcpd.leases"; 25 const char *file = leases_file;
41 struct lease_t lease; 26 struct dhcpOfferedAddr lease;
42 struct in_addr addr; 27 struct in_addr addr;
43 28
44 static struct option options[] = { 29 static const struct option options[] = {
45 {"absolute", 0, 0, 'a'}, 30 {"absolute", 0, 0, 'a'},
46 {"remaining", 0, 0, 'r'}, 31 {"remaining", 0, 0, 'r'},
47 {"file", 1, 0, 'f'}, 32 {"file", 1, 0, 'f'},
48 {"help", 0, 0, 'h'},
49 {0, 0, 0, 0} 33 {0, 0, 0, 0}
50 }; 34 };
51 35
52 while (1) { 36 while (1) {
53 int option_index = 0; 37 int option_index = 0;
54 c = getopt_long(argc, argv, "arf:h", options, &option_index); 38 c = getopt_long(argc, argv, "arf:", options, &option_index);
55 if (c == -1) break; 39 if (c == -1) break;
56 40
57 switch (c) { 41 switch (c) {
58 case 'a': mode = ABSOLUTE; break; 42 case 'a': mode = ABSOLUTE; break;
59 case 'r': mode = REMAINING; break; 43 case 'r': mode = REMAINING; break;
60 case 'f': 44 case 'f':
61 strncpy(file, optarg, 255); 45 file = optarg;
62 file[254] = '\0';
63 break;
64 case 'h':
65 printf("Usage: dumpleases -f <file> -[r|a]\n\n");
66 printf(" -f, --file=FILENAME Leases file to load\n");
67 printf(" -r, --remaining Interepret lease times as time remaing\n");
68 printf(" -a, --absolute Interepret lease times as expire time\n");
69 break; 46 break;
47 default:
48 bb_show_usage();
70 } 49 }
71 } 50 }
72 51
73 if (!(fp = fopen(file, "r"))) { 52 fp = bb_xfopen(file, "r");
74 perror("could not open input file");
75 return 0;
76 }
77 53
78 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at"); 54 printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
79 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */ 55 /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 842e0f2db..3d0344b09 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -3,23 +3,20 @@
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */ 4 */
5 5
6#include <stdio.h>
7#include <sys/socket.h> 6#include <sys/socket.h>
8#include <arpa/inet.h> 7#include <arpa/inet.h>
9#include <string.h> 8#include <string.h>
10#include <stdlib.h> 9#include <stdlib.h>
11#include <time.h> 10#include <time.h>
12#include <ctype.h> 11#include <ctype.h>
13#include <netdb.h>
14 12
15#include "debug.h"
16#include "dhcpd.h" 13#include "dhcpd.h"
17#include "files.h" 14#include "files.h"
18#include "options.h" 15#include "options.h"
19#include "leases.h" 16#include "common.h"
20 17
21/* on these functions, make sure you datatype matches */ 18/* on these functions, make sure you datatype matches */
22static int read_ip(char *line, void *arg) 19static int read_ip(const char *line, void *arg)
23{ 20{
24 struct in_addr *addr = arg; 21 struct in_addr *addr = arg;
25 struct hostent *host; 22 struct hostent *host;
@@ -34,7 +31,7 @@ static int read_ip(char *line, void *arg)
34} 31}
35 32
36 33
37static int read_str(char *line, void *arg) 34static int read_str(const char *line, void *arg)
38{ 35{
39 char **dest = arg; 36 char **dest = arg;
40 37
@@ -45,7 +42,7 @@ static int read_str(char *line, void *arg)
45} 42}
46 43
47 44
48static int read_u32(char *line, void *arg) 45static int read_u32(const char *line, void *arg)
49{ 46{
50 u_int32_t *dest = arg; 47 u_int32_t *dest = arg;
51 char *endptr; 48 char *endptr;
@@ -54,7 +51,7 @@ static int read_u32(char *line, void *arg)
54} 51}
55 52
56 53
57static int read_yn(char *line, void *arg) 54static int read_yn(const char *line, void *arg)
58{ 55{
59 char *dest = arg; 56 char *dest = arg;
60 int retval = 1; 57 int retval = 1;
@@ -68,32 +65,33 @@ static int read_yn(char *line, void *arg)
68 return retval; 65 return retval;
69} 66}
70 67
68#define READ_CONFIG_BUF_SIZE 512 /* domainname may have 254 chars */
71 69
72/* read a dhcp option and add it to opt_list */ 70/* read a dhcp option and add it to opt_list */
73static int read_opt(char *line, void *arg) 71static int read_opt(const char *const_line, void *arg)
74{ 72{
73 char line[READ_CONFIG_BUF_SIZE];
75 struct option_set **opt_list = arg; 74 struct option_set **opt_list = arg;
76 char *opt, *val, *endptr; 75 char *opt, *val, *endptr;
77 struct dhcp_option *option = NULL; 76 struct dhcp_option *option;
78 int retval = 0, length = 0; 77 int retval = 0, length;
79 char buffer[255]; 78 char buffer[256]; /* max opt length */
80 u_int16_t result_u16; 79 u_int16_t result_u16;
81 u_int32_t result_u32; 80 u_int32_t result_u32;
82 int i; 81 void *valptr;
83
84 if (!(opt = strtok(line, " \t="))) return 0;
85 82
86 for (i = 0; options[i].code; i++) 83 if ((opt = strtok(strcpy(line, const_line), " \t="))) {
87 if (!strcmp(options[i].name, opt))
88 option = &(options[i]);
89 84
90 if (!option) return 0; 85 for (option = options; option->code; option++)
86 if (!strcasecmp(option->name, opt))
87 break;
91 88
92 do { 89 if (option->code) do {
93 val = strtok(NULL, ", \t"); 90 val = strtok(NULL, ", \t");
94 if (val) { 91 if(!val)
92 break;
95 length = option_lengths[option->flags & TYPE_MASK]; 93 length = option_lengths[option->flags & TYPE_MASK];
96 retval = 0; 94 valptr = NULL;
97 switch (option->flags & TYPE_MASK) { 95 switch (option->flags & TYPE_MASK) {
98 case OPTION_IP: 96 case OPTION_IP:
99 retval = read_ip(val, buffer); 97 retval = read_ip(val, buffer);
@@ -107,8 +105,9 @@ static int read_opt(char *line, void *arg)
107 length = strlen(val); 105 length = strlen(val);
108 if (length > 0) { 106 if (length > 0) {
109 if (length > 254) length = 254; 107 if (length > 254) length = 254;
110 memcpy(buffer, val, length); 108 endptr = buffer + length;
111 retval = 1; 109 endptr[0] = 0;
110 valptr = val;
112 } 111 }
113 break; 112 break;
114 case OPTION_BOOLEAN: 113 case OPTION_BOOLEAN:
@@ -116,41 +115,44 @@ static int read_opt(char *line, void *arg)
116 break; 115 break;
117 case OPTION_U8: 116 case OPTION_U8:
118 buffer[0] = strtoul(val, &endptr, 0); 117 buffer[0] = strtoul(val, &endptr, 0);
119 retval = (endptr[0] == '\0'); 118 valptr = buffer;
120 break; 119 break;
121 case OPTION_U16: 120 case OPTION_U16:
122 result_u16 = htons(strtoul(val, &endptr, 0)); 121 result_u16 = htons(strtoul(val, &endptr, 0));
123 memcpy(buffer, &result_u16, 2); 122 valptr = &result_u16;
124 retval = (endptr[0] == '\0');
125 break; 123 break;
126 case OPTION_S16: 124 case OPTION_S16:
127 result_u16 = htons(strtol(val, &endptr, 0)); 125 result_u16 = htons(strtol(val, &endptr, 0));
128 memcpy(buffer, &result_u16, 2); 126 valptr = &result_u16;
129 retval = (endptr[0] == '\0');
130 break; 127 break;
131 case OPTION_U32: 128 case OPTION_U32:
132 result_u32 = htonl(strtoul(val, &endptr, 0)); 129 result_u32 = htonl(strtoul(val, &endptr, 0));
133 memcpy(buffer, &result_u32, 4); 130 valptr = &result_u32;
134 retval = (endptr[0] == '\0');
135 break; 131 break;
136 case OPTION_S32: 132 case OPTION_S32:
137 result_u32 = htonl(strtol(val, &endptr, 0)); 133 result_u32 = htonl(strtol(val, &endptr, 0));
138 memcpy(buffer, &result_u32, 4); 134 valptr = &result_u32;
139 retval = (endptr[0] == '\0');
140 break; 135 break;
141 default: 136 default:
137 retval = 0;
142 break; 138 break;
143 } 139 }
140 if(valptr) {
141 memcpy(buffer, valptr, length);
142 retval = (endptr[0] == '\0');
143 }
144 if (retval) 144 if (retval)
145 attach_option(opt_list, option, buffer, length); 145 attach_option(opt_list, option, buffer, length);
146 }; 146 else
147 } while (val && retval && option->flags & OPTION_LIST); 147 break;
148 } while (option->flags & OPTION_LIST);
149 }
148 return retval; 150 return retval;
149} 151}
150 152
151 153
152static struct config_keyword keywords[] = { 154static const struct config_keyword keywords[] = {
153 /* keyword[14] handler variable address default[20] */ 155 /* keyword handler variable address default */
154 {"start", read_ip, &(server_config.start), "192.168.0.20"}, 156 {"start", read_ip, &(server_config.start), "192.168.0.20"},
155 {"end", read_ip, &(server_config.end), "192.168.0.254"}, 157 {"end", read_ip, &(server_config.end), "192.168.0.254"},
156 {"interface", read_str, &(server_config.interface), "eth0"}, 158 {"interface", read_str, &(server_config.interface), "eth0"},
@@ -163,7 +165,7 @@ static struct config_keyword keywords[] = {
163 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"}, 165 {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
164 {"offer_time", read_u32, &(server_config.offer_time), "60"}, 166 {"offer_time", read_u32, &(server_config.offer_time), "60"},
165 {"min_lease", read_u32, &(server_config.min_lease), "60"}, 167 {"min_lease", read_u32, &(server_config.min_lease), "60"},
166 {"lease_file", read_str, &(server_config.lease_file), "/var/lib/misc/udhcpd.leases"}, 168 {"lease_file", read_str, &(server_config.lease_file), leases_file},
167 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"}, 169 {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
168 {"notify_file", read_str, &(server_config.notify_file), ""}, 170 {"notify_file", read_str, &(server_config.notify_file), ""},
169 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"}, 171 {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
@@ -174,14 +176,15 @@ static struct config_keyword keywords[] = {
174}; 176};
175 177
176 178
177int read_config(char *file) 179int read_config(const char *file)
178{ 180{
179 FILE *in; 181 FILE *in;
180 char buffer[80], orig[80], *token, *line; 182 char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
183 char *token, *line;
181 int i; 184 int i;
182 185
183 for (i = 0; strlen(keywords[i].keyword); i++) 186 for (i = 0; keywords[i].keyword[0]; i++)
184 if (strlen(keywords[i].def)) 187 if (keywords[i].def[0])
185 keywords[i].handler(keywords[i].def, keywords[i].var); 188 keywords[i].handler(keywords[i].def, keywords[i].var);
186 189
187 if (!(in = fopen(file, "r"))) { 190 if (!(in = fopen(file, "r"))) {
@@ -189,24 +192,25 @@ int read_config(char *file)
189 return 0; 192 return 0;
190 } 193 }
191 194
192 while (fgets(buffer, 80, in)) { 195 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
193 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0'; 196 if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
194 strncpy(orig, buffer, 80); 197 strcpy(orig, buffer);
195 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0'; 198 if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
196 token = buffer + strspn(buffer, " \t"); 199 token = strtok(buffer, " \t");
197 if (*token == '\0') continue; 200 if(!token)
198 line = token + strcspn(token, " \t="); 201 continue;
199 if (*line == '\0') continue; 202 line = strtok(NULL, "");
200 *line = '\0'; 203 if(!line)
204 continue;
205 while(*line == '=' || isspace(*line))
201 line++; 206 line++;
202
203 /* eat leading whitespace */
204 line = line + strspn(line, " \t=");
205 /* eat trailing whitespace */ 207 /* eat trailing whitespace */
206 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--); 208 for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
207 line[i] = '\0'; 209 line[i] = '\0';
210 if (*line == '\0')
211 continue;
208 212
209 for (i = 0; strlen(keywords[i].keyword); i++) 213 for (i = 0; keywords[i].keyword[0]; i++)
210 if (!strcasecmp(token, keywords[i].keyword)) 214 if (!strcasecmp(token, keywords[i].keyword))
211 if (!keywords[i].handler(line, keywords[i].var)) { 215 if (!keywords[i].handler(line, keywords[i].var)) {
212 LOG(LOG_ERR, "unable to parse '%s'", orig); 216 LOG(LOG_ERR, "unable to parse '%s'", orig);
@@ -233,16 +237,17 @@ void write_leases(void)
233 } 237 }
234 238
235 for (i = 0; i < server_config.max_leases; i++) { 239 for (i = 0; i < server_config.max_leases; i++) {
240 struct dhcpOfferedAddr lease;
236 if (leases[i].yiaddr != 0) { 241 if (leases[i].yiaddr != 0) {
237 if (server_config.remaining) { 242 if (server_config.remaining) {
238 if (lease_expired(&(leases[i]))) 243 if (lease_expired(&(leases[i])))
239 lease_time = 0; 244 lease_time = 0;
240 else lease_time = leases[i].expires - curr; 245 else lease_time = leases[i].expires - curr;
241 } else lease_time = leases[i].expires; 246 } else lease_time = leases[i].expires;
242 lease_time = htonl(lease_time); 247 lease.expires = htonl(lease_time);
243 fwrite(leases[i].chaddr, 16, 1, fp); 248 memcpy(lease.chaddr, leases[i].chaddr, 16);
244 fwrite(&(leases[i].yiaddr), 4, 1, fp); 249 lease.yiaddr = leases[i].yiaddr;
245 fwrite(&lease_time, 4, 1, fp); 250 fwrite(leases, sizeof(lease), 1, fp);
246 } 251 }
247 } 252 }
248 fclose(fp); 253 fclose(fp);
@@ -254,7 +259,7 @@ void write_leases(void)
254} 259}
255 260
256 261
257void read_leases(char *file) 262void read_leases(const char *file)
258{ 263{
259 FILE *fp; 264 FILE *fp;
260 unsigned int i = 0; 265 unsigned int i = 0;
@@ -280,5 +285,3 @@ void read_leases(char *file)
280 DEBUG(LOG_INFO, "Read %d leases", i); 285 DEBUG(LOG_INFO, "Read %d leases", i);
281 fclose(fp); 286 fclose(fp);
282} 287}
283
284
diff --git a/networking/udhcp/files.h b/networking/udhcp/files.h
index 1f2638fe9..279738adf 100644
--- a/networking/udhcp/files.h
+++ b/networking/udhcp/files.h
@@ -3,15 +3,15 @@
3#define _FILES_H 3#define _FILES_H
4 4
5struct config_keyword { 5struct config_keyword {
6 char keyword[14]; 6 const char *keyword;
7 int (*handler)(char *line, void *var); 7 int (* const handler)(const char *line, void *var);
8 void *var; 8 void *var;
9 char def[30]; 9 const char *def;
10}; 10};
11 11
12 12
13int read_config(char *file); 13int read_config(const char *file);
14void write_leases(void); 14void write_leases(void);
15void read_leases(char *file); 15void read_leases(const char *file);
16 16
17#endif 17#endif
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c
index 7ade91d93..0b6b409cd 100644
--- a/networking/udhcp/leases.c
+++ b/networking/udhcp/leases.c
@@ -9,12 +9,12 @@
9#include <netinet/in.h> 9#include <netinet/in.h>
10#include <arpa/inet.h> 10#include <arpa/inet.h>
11 11
12#include "debug.h"
13#include "dhcpd.h" 12#include "dhcpd.h"
14#include "files.h" 13#include "files.h"
15#include "options.h" 14#include "options.h"
16#include "leases.h"
17#include "arpping.h" 15#include "arpping.h"
16#include "common.h"
17
18 18
19unsigned char blank_chaddr[] = {[0 ... 15] = 0}; 19unsigned char blank_chaddr[] = {[0 ... 15] = 0};
20 20
@@ -102,6 +102,20 @@ struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr)
102} 102}
103 103
104 104
105/* check is an IP is taken, if it is, add it to the lease table */
106static int check_ip(u_int32_t addr)
107{
108 struct in_addr temp;
109
110 if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
111 temp.s_addr = addr;
112 LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
113 inet_ntoa(temp), server_config.conflict_time);
114 add_lease(blank_chaddr, addr, server_config.conflict_time);
115 return 1;
116 } else return 0;
117}
118
105/* find an assignable address, it check_expired is true, we check all the expired leases as well. 119/* find an assignable address, it check_expired is true, we check all the expired leases as well.
106 * Maybe this should try expired leases by age... */ 120 * Maybe this should try expired leases by age... */
107u_int32_t find_address(int check_expired) 121u_int32_t find_address(int check_expired)
@@ -133,19 +147,3 @@ u_int32_t find_address(int check_expired)
133 } 147 }
134 return 0; 148 return 0;
135} 149}
136
137
138/* check is an IP is taken, if it is, add it to the lease table */
139int check_ip(u_int32_t addr)
140{
141 struct in_addr temp;
142
143 if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
144 temp.s_addr = addr;
145 LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
146 inet_ntoa(temp), server_config.conflict_time);
147 add_lease(blank_chaddr, addr, server_config.conflict_time);
148 return 1;
149 } else return 0;
150}
151
diff --git a/networking/udhcp/leases.h b/networking/udhcp/leases.h
index bc50f68de..d54cd11cc 100644
--- a/networking/udhcp/leases.h
+++ b/networking/udhcp/leases.h
@@ -9,6 +9,8 @@ struct dhcpOfferedAddr {
9 u_int32_t expires; /* host order */ 9 u_int32_t expires; /* host order */
10}; 10};
11 11
12extern const char leases_file[];
13
12extern unsigned char blank_chaddr[]; 14extern unsigned char blank_chaddr[];
13 15
14void clear_lease(u_int8_t *chaddr, u_int32_t yiaddr); 16void clear_lease(u_int8_t *chaddr, u_int32_t yiaddr);
@@ -18,7 +20,6 @@ struct dhcpOfferedAddr *oldest_expired_lease(void);
18struct dhcpOfferedAddr *find_lease_by_chaddr(u_int8_t *chaddr); 20struct dhcpOfferedAddr *find_lease_by_chaddr(u_int8_t *chaddr);
19struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr); 21struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr);
20u_int32_t find_address(int check_expired); 22u_int32_t find_address(int check_expired);
21int check_ip(u_int32_t addr);
22 23
23 24
24#endif 25#endif
diff --git a/networking/udhcp/options.c b/networking/udhcp/options.c
index 9f95c1558..a3edd0b8b 100644
--- a/networking/udhcp/options.c
+++ b/networking/udhcp/options.c
@@ -3,15 +3,13 @@
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
4 */ 4 */
5 5
6#include <stdio.h>
7#include <stdlib.h> 6#include <stdlib.h>
8#include <string.h> 7#include <string.h>
9 8
10#include "debug.h"
11#include "dhcpd.h" 9#include "dhcpd.h"
12#include "files.h" 10#include "files.h"
13#include "options.h" 11#include "options.h"
14#include "leases.h" 12#include "common.h"
15 13
16 14
17/* supported options are easily added here */ 15/* supported options are easily added here */
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c
index d9f715971..0f2a3bc6f 100644
--- a/networking/udhcp/packet.c
+++ b/networking/udhcp/packet.c
@@ -14,10 +14,9 @@
14#endif 14#endif
15#include <errno.h> 15#include <errno.h>
16 16
17#include "packet.h"
18#include "debug.h"
19#include "dhcpd.h" 17#include "dhcpd.h"
20#include "options.h" 18#include "options.h"
19#include "common.h"
21 20
22 21
23void init_header(struct dhcpMessage *packet, char type) 22void init_header(struct dhcpMessage *packet, char type)
@@ -123,7 +122,7 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port
123 struct udp_dhcp_packet packet; 122 struct udp_dhcp_packet packet;
124 123
125 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { 124 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
126 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno)); 125 DEBUG(LOG_ERR, "socket call failed: %m");
127 return -1; 126 return -1;
128 } 127 }
129 128
@@ -136,7 +135,7 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port
136 dest.sll_halen = 6; 135 dest.sll_halen = 6;
137 memcpy(dest.sll_addr, dest_arp, 6); 136 memcpy(dest.sll_addr, dest_arp, 6);
138 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) { 137 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
139 DEBUG(LOG_ERR, "bind call failed: %s", strerror(errno)); 138 DEBUG(LOG_ERR, "bind call failed: %m");
140 close(fd); 139 close(fd);
141 return -1; 140 return -1;
142 } 141 }
@@ -159,7 +158,7 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port
159 158
160 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest)); 159 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
161 if (result <= 0) { 160 if (result <= 0) {
162 DEBUG(LOG_ERR, "write on socket failed: %s", strerror(errno)); 161 DEBUG(LOG_ERR, "write on socket failed: %m");
163 } 162 }
164 close(fd); 163 close(fd);
165 return result; 164 return result;
@@ -200,4 +199,3 @@ int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_p
200 close(fd); 199 close(fd);
201 return result; 200 return result;
202} 201}
203
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c
index 48ff8e07e..41b680d6b 100644
--- a/networking/udhcp/script.c
+++ b/networking/udhcp/script.c
@@ -28,17 +28,15 @@
28#include <arpa/inet.h> 28#include <arpa/inet.h>
29#include <sys/types.h> 29#include <sys/types.h>
30#include <sys/wait.h> 30#include <sys/wait.h>
31#include <errno.h>
32 31
33#include "options.h" 32#include "options.h"
34#include "dhcpd.h" 33#include "dhcpd.h"
35#include "dhcpc.h" 34#include "dhcpc.h"
36#include "packet.h"
37#include "options.h" 35#include "options.h"
38#include "debug.h" 36#include "common.h"
39 37
40/* get a rough idea of how long an option will be (rounding up...) */ 38/* get a rough idea of how long an option will be (rounding up...) */
41static int max_option_length[] = { 39static const int max_option_length[] = {
42 [OPTION_IP] = sizeof("255.255.255.255 "), 40 [OPTION_IP] = sizeof("255.255.255.255 "),
43 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, 41 [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
44 [OPTION_STRING] = 1, 42 [OPTION_STRING] = 1,
@@ -51,16 +49,21 @@ static int max_option_length[] = {
51}; 49};
52 50
53 51
54static int upper_length(int length, struct dhcp_option *option) 52static inline int upper_length(int length, int opt_index)
55{ 53{
56 return max_option_length[option->flags & TYPE_MASK] * 54 return max_option_length[opt_index] *
57 (length / option_lengths[option->flags & TYPE_MASK]); 55 (length / option_lengths[opt_index]);
58} 56}
59 57
60 58
61static int sprintip(char *dest, char *pre, unsigned char *ip) 59static int sprintip(char *dest, unsigned char *ip)
62{ 60{
63 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]); 61 return sprintf(dest, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
62}
63
64static void asprintip(char **dest, char *pre, unsigned char *ip)
65{
66 asprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
64} 67}
65 68
66 69
@@ -93,12 +96,12 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
93 for(;;) { 96 for(;;) {
94 switch (type) { 97 switch (type) {
95 case OPTION_IP_PAIR: 98 case OPTION_IP_PAIR:
96 dest += sprintip(dest, "", option); 99 dest += sprintip(dest, option);
97 *(dest++) = '/'; 100 *(dest++) = '/';
98 option += 4; 101 option += 4;
99 optlen = 4; 102 optlen = 4;
100 case OPTION_IP: /* Works regardless of host byte order. */ 103 case OPTION_IP: /* Works regardless of host byte order. */
101 dest += sprintip(dest, "", option); 104 dest += sprintip(dest, option);
102 break; 105 break;
103 case OPTION_BOOLEAN: 106 case OPTION_BOOLEAN:
104 dest += sprintf(dest, *option ? "yes" : "no"); 107 dest += sprintf(dest, *option ? "yes" : "no");
@@ -137,15 +140,10 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
137 140
138static char *find_env(const char *prefix, char *defaultstr) 141static char *find_env(const char *prefix, char *defaultstr)
139{ 142{
140 extern char **environ; 143 char *ptr;
141 char **ptr;
142 const int len = strlen(prefix);
143 144
144 for (ptr = environ; *ptr != NULL; ptr++) { 145 ptr = getenv(prefix);
145 if (strncmp(prefix, *ptr, len) == 0) 146 return ptr ? ptr : defaultstr;
146 return *ptr;
147 }
148 return defaultstr;
149} 147}
150 148
151 149
@@ -174,8 +172,7 @@ static char **fill_envp(struct dhcpMessage *packet)
174 172
175 envp = xmalloc((num_options + 5) * sizeof(char *)); 173 envp = xmalloc((num_options + 5) * sizeof(char *));
176 j = 0; 174 j = 0;
177 envp[j++] = xmalloc(sizeof("interface=") + strlen(client_config.interface)); 175 asprintf(&envp[j++], "interface=%s", client_config.interface);
178 sprintf(envp[0], "interface=%s", client_config.interface);
179 envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin"); 176 envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin");
180 envp[j++] = find_env("HOME", "HOME=/"); 177 envp[j++] = find_env("HOME", "HOME=/");
181 178
@@ -184,38 +181,33 @@ static char **fill_envp(struct dhcpMessage *packet)
184 return envp; 181 return envp;
185 } 182 }
186 183
187 envp[j] = xmalloc(sizeof("ip=255.255.255.255")); 184 asprintip(&envp[j++], "ip=", (unsigned char *) &packet->yiaddr);
188 sprintip(envp[j++], "ip=", (unsigned char *) &packet->yiaddr);
189 185
190 186
191 for (i = 0; options[i].code; i++) { 187 for (i = 0; options[i].code; i++) {
192 if (!(temp = get_option(packet, options[i].code))) 188 if (!(temp = get_option(packet, options[i].code)))
193 continue; 189 continue;
194 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2); 190 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], options[i].flags & TYPE_MASK) + strlen(options[i].name) + 2);
195 fill_options(envp[j++], temp, &options[i]); 191 fill_options(envp[j++], temp, &options[i]);
196 192
197 /* Fill in a subnet bits option for things like /24 */ 193 /* Fill in a subnet bits option for things like /24 */
198 if (options[i].code == DHCP_SUBNET) { 194 if (options[i].code == DHCP_SUBNET) {
199 envp[j] = xmalloc(sizeof("mask=32"));
200 memcpy(&subnet, temp, 4); 195 memcpy(&subnet, temp, 4);
201 sprintf(envp[j++], "mask=%d", mton(&subnet)); 196 asprintf(&envp[j++], "mask=%d", mton(&subnet));
202 } 197 }
203 } 198 }
204 if (packet->siaddr) { 199 if (packet->siaddr) {
205 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); 200 asprintip(&envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
206 sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
207 } 201 }
208 if (!(over & FILE_FIELD) && packet->file[0]) { 202 if (!(over & FILE_FIELD) && packet->file[0]) {
209 /* watch out for invalid packets */ 203 /* watch out for invalid packets */
210 packet->file[sizeof(packet->file) - 1] = '\0'; 204 packet->file[sizeof(packet->file) - 1] = '\0';
211 envp[j] = xmalloc(sizeof("boot_file=") + strlen(packet->file)); 205 asprintf(&envp[j++], "boot_file=%s", packet->file);
212 sprintf(envp[j++], "boot_file=%s", packet->file);
213 } 206 }
214 if (!(over & SNAME_FIELD) && packet->sname[0]) { 207 if (!(over & SNAME_FIELD) && packet->sname[0]) {
215 /* watch out for invalid packets */ 208 /* watch out for invalid packets */
216 packet->sname[sizeof(packet->sname) - 1] = '\0'; 209 packet->sname[sizeof(packet->sname) - 1] = '\0';
217 envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname)); 210 asprintf(&envp[j++], "sname=%s", packet->sname);
218 sprintf(envp[j++], "sname=%s", packet->sname);
219 } 211 }
220 envp[j] = NULL; 212 envp[j] = NULL;
221 return envp; 213 return envp;
@@ -245,8 +237,7 @@ void run_script(struct dhcpMessage *packet, const char *name)
245 DEBUG(LOG_INFO, "execle'ing %s", client_config.script); 237 DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
246 execle(client_config.script, client_config.script, 238 execle(client_config.script, client_config.script,
247 name, NULL, envp); 239 name, NULL, envp);
248 LOG(LOG_ERR, "script %s failed: %s", 240 LOG(LOG_ERR, "script %s failed: %m", client_config.script);
249 client_config.script, strerror(errno));
250 exit(1); 241 exit(1);
251 } 242 }
252} 243}
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c
index e969c7af8..09682444e 100644
--- a/networking/udhcp/serverpacket.c
+++ b/networking/udhcp/serverpacket.c
@@ -25,11 +25,9 @@
25#include <string.h> 25#include <string.h>
26#include <time.h> 26#include <time.h>
27 27
28#include "packet.h"
29#include "debug.h"
30#include "dhcpd.h" 28#include "dhcpd.h"
31#include "options.h" 29#include "options.h"
32#include "leases.h" 30#include "common.h"
33 31
34/* send a packet to giaddr using the kernel ip stack */ 32/* send a packet to giaddr using the kernel ip stack */
35static int send_packet_to_relay(struct dhcpMessage *payload) 33static int send_packet_to_relay(struct dhcpMessage *payload)
@@ -258,6 +256,3 @@ int send_inform(struct dhcpMessage *oldpacket)
258 256
259 return send_packet(&packet, 0); 257 return send_packet(&packet, 0);
260} 258}
261
262
263
diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c
index 3a2261561..a51a74369 100644
--- a/networking/udhcp/socket.c
+++ b/networking/udhcp/socket.c
@@ -41,7 +41,7 @@
41#include <linux/if_ether.h> 41#include <linux/if_ether.h>
42#endif 42#endif
43 43
44#include "debug.h" 44#include "common.h"
45 45
46int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp) 46int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)
47{ 47{
@@ -60,8 +60,7 @@ int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char
60 *addr = our_ip->sin_addr.s_addr; 60 *addr = our_ip->sin_addr.s_addr;
61 DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); 61 DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
62 } else { 62 } else {
63 LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %s", 63 LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m");
64 strerror(errno));
65 return -1; 64 return -1;
66 } 65 }
67 } 66 }
@@ -70,7 +69,7 @@ int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char
70 DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex); 69 DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
71 *ifindex = ifr.ifr_ifindex; 70 *ifindex = ifr.ifr_ifindex;
72 } else { 71 } else {
73 LOG(LOG_ERR, "SIOCGIFINDEX failed!: %s", strerror(errno)); 72 LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m");
74 return -1; 73 return -1;
75 } 74 }
76 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { 75 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
@@ -78,11 +77,11 @@ int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char
78 DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", 77 DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
79 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); 78 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
80 } else { 79 } else {
81 LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %s", strerror(errno)); 80 LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m");
82 return -1; 81 return -1;
83 } 82 }
84 } else { 83 } else {
85 LOG(LOG_ERR, "socket failed!: %s", strerror(errno)); 84 LOG(LOG_ERR, "socket failed!: %m");
86 return -1; 85 return -1;
87 } 86 }
88 close(fd); 87 close(fd);
@@ -99,7 +98,7 @@ int listen_socket(unsigned int ip, int port, char *inf)
99 98
100 DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf); 99 DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
101 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 100 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
102 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno)); 101 DEBUG(LOG_ERR, "socket call failed: %m");
103 return -1; 102 return -1;
104 } 103 }
105 104
@@ -139,7 +138,7 @@ int raw_socket(int ifindex)
139 138
140 DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex); 139 DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
141 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { 140 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
142 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno)); 141 DEBUG(LOG_ERR, "socket call failed: %m");
143 return -1; 142 return -1;
144 } 143 }
145 144
@@ -147,11 +146,10 @@ int raw_socket(int ifindex)
147 sock.sll_protocol = htons(ETH_P_IP); 146 sock.sll_protocol = htons(ETH_P_IP);
148 sock.sll_ifindex = ifindex; 147 sock.sll_ifindex = ifindex;
149 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) { 148 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
150 DEBUG(LOG_ERR, "bind call failed: %s", strerror(errno)); 149 DEBUG(LOG_ERR, "bind call failed: %m");
151 close(fd); 150 close(fd);
152 return -1; 151 return -1;
153 } 152 }
154 153
155 return fd; 154 return fd;
156} 155}
157