* llist_t *llist_add_to(llist_t *old_head, void *data)
* etc does not result in smaller code... */
-/* start_stop_daemon and (udhcpc with ifupdown) are special - they want to
- * create pidfiles regardless of FEATURE_PIDFILE. */
+/* start_stop_daemon and udhcpc are special - they want
+ * to create pidfiles regardless of FEATURE_PIDFILE */
#if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE)
int write_pidfile(const char *path);
#define remove_pidfile(f) ((void)unlink(f))
char *end;
char buf[sizeof(int)*3 + 2];
+ if (!path)
+ return 1;
/* we will overwrite stale pidfile */
pid_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (pid_fd < 0)
return 0;
/* few bytes larger, but doesn't use stdio */
end = utoa_to_buf(getpid(), buf, sizeof(buf));
- end[0] = '\n';
+ *end = '\n';
full_write(pid_fd, buf, end - buf + 1);
close(pid_fd);
return 1;
/* vi: set sw=4 ts=4: */
/* common.c
- *
- * Functions for debugging and logging as well as some other
- * simple helper functions.
- *
- * Russ Dill <Russ.Dill@asu.edu> 2001-2003
- * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include "common.h"
const uint8_t MAC_BCAST_ADDR[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-
-void udhcp_make_pidfile(const char *pidfile)
-{
- /* Make sure fd 0,1,2 are open */
- bb_sanitize_stdio();
-
- /* Equivalent of doing a fflush after every \n */
- setlinebuf(stdout);
-
- /* Create pidfile */
- if (pidfile && !write_pidfile(pidfile))
- bb_perror_msg("cannot create pidfile %s", pidfile);
-
- bb_info_msg("%s (v%s) started", applet_name, BB_VER);
-}
/**/
-void udhcp_make_pidfile(const char *pidfile);
-
void udhcp_run_script(struct dhcpMessage *packet, const char *name);
// Still need to clean these up...
static void client_background(void)
{
-#ifdef __uClinux__
+#if !BB_MMU
bb_error_msg("cannot background in uclinux (yet)");
/* ... mainly because udhcpc calls client_background()
* in _the _middle _of _udhcpc _run_, not at the start!
bb_daemonize(0);
logmode &= ~LOGMODE_STDIO;
/* rewrite pidfile, as our pid is different now */
- if (client_config.pidfile)
- write_pidfile(client_config.pidfile);
+ write_pidfile(client_config.pidfile);
#endif
/* Do not fork again. */
client_config.foreground = 1;
char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
uint32_t xid = 0;
uint32_t lease = 0; /* can be given as 32-bit quantity */
- unsigned t1 = 0, t2 = 0;
+ unsigned t1 = 0, t2 = 0; /* what a wonderful names */
unsigned start = 0;
unsigned now;
unsigned opt;
int max_fd;
- int sig;
int retval;
int len;
- int no_clientid = 0;
- fd_set rfds;
struct timeval tv;
- struct dhcpMessage packet;
struct in_addr temp_addr;
+ struct dhcpMessage packet;
+ fd_set rfds;
enum {
OPT_c = 1 << 0,
if (opt & OPT_c)
client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
- if (opt & OPT_C)
- no_clientid = 1;
+ //if (opt & OPT_C)
if (opt & OPT_V)
client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
if (opt & OPT_f)
if (opt & OPT_t)
client_config.retries = xatoi_u(str_t);
if (opt & OPT_v) {
- printf("version %s\n\n", BB_VER);
+ printf("version %s\n", BB_VER);
return 0;
}
}
if (read_interface(client_config.interface, &client_config.ifindex,
- NULL, client_config.arp) < 0)
+ NULL, client_config.arp))
return 1;
- /* Sanitize fd's and write pidfile */
- udhcp_make_pidfile(client_config.pidfile);
+ /* Make sure fd 0,1,2 are open */
+ bb_sanitize_stdio();
+ /* Equivalent of doing a fflush after every \n */
+ setlinebuf(stdout);
+
+ /* Create pidfile */
+ write_pidfile(client_config.pidfile);
+ /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
+
+ /* Goes to stdout and possibly syslog */
+ bb_info_msg("%s (v%s) started", applet_name, BB_VER);
/* if not set, and not suppressed, setup the default client ID */
- if (!client_config.clientid && !no_clientid) {
+ if (!client_config.clientid && !(opt & OPT_C)) {
client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
client_config.clientid[OPT_DATA] = 1;
memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
state = INIT_SELECTING;
udhcp_run_script(NULL, "deconfig");
change_mode(LISTEN_RAW);
+ tv.tv_sec = 0;
+ goto jump_in;
for (;;) {
tv.tv_sec = timeout - monotonic_sec();
+ jump_in:
tv.tv_usec = 0;
if (listen_mode != LISTEN_NONE && sockfd < 0) {
}
max_fd = udhcp_sp_fd_set(&rfds, sockfd);
+ retval = 0; /* If we already timed out, fall through, else... */
if (tv.tv_sec > 0) {
DEBUG("Waiting on select...");
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
- } else retval = 0; /* If we already timed out, fall through */
+ }
now = monotonic_sec();
- if (retval == 0) {
+ if (retval < 0) {
+ /* EINTR? signal was caught, don't panic */
+ if (errno != EINTR) {
+ /* Else: an error occured, panic! */
+ bb_perror_msg_and_die("select");
+ }
+ } else if (retval == 0) {
/* timeout dropped to zero */
switch (state) {
case INIT_SELECTING:
} else {
/* send a request packet */
send_renew(xid, server_addr, requested_ip); /* unicast */
-
t1 = (t2 - t1) / 2 + t1;
- timeout = t1 + start;
+ timeout = start + t1;
}
break;
case REBINDING:
} else {
/* send a request packet */
send_renew(xid, 0, requested_ip); /* broadcast */
-
t2 = (lease - t2) / 2 + t2;
- timeout = t2 + start;
+ timeout = start + t2;
}
break;
case RELEASED:
timeout = INT_MAX;
break;
}
- } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
+ } else if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
/* a packet is ready, read it */
if (listen_mode == LISTEN_KERNEL)
bb_info_msg("Lease of %s obtained, lease time %u",
inet_ntoa(temp_addr), (unsigned)lease);
start = now;
- timeout = t1 + start;
+ timeout = start + t1;
requested_ip = packet.yiaddr;
udhcp_run_script(&packet,
((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
break;
/* case BOUND, RELEASED: - ignore all packets */
}
- } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
- switch (sig) {
+ } else {
+ int signo = udhcp_sp_read(&rfds);
+ switch (signo) {
case SIGUSR1:
perform_renew();
break;
perform_release();
goto ret0;
}
- } else if (retval == -1 && errno == EINTR) {
- /* a signal was caught */
- } else {
- /* An error occured */
- bb_perror_msg("select");
}
} /* for (;;) */
ret0:
* otherwise NOMMU machines will parse config twice */
read_config(argv[1] ? argv[1] : DHCPD_CONF_FILE);
- udhcp_make_pidfile(server_config.pidfile);
+ /* Make sure fd 0,1,2 are open */
+ bb_sanitize_stdio();
+ /* Equivalent of doing a fflush after every \n */
+ setlinebuf(stdout);
+
+ /* Create pidfile */
+ write_pidfile(server_config.pidfile);
+ /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
+
+ bb_info_msg("%s (v%s) started", applet_name, BB_VER);
option = find_option(server_config.options, DHCP_LEASE_TIME);
server_config.lease = LEASE_TIME;
read_leases(server_config.lease_file);
if (read_interface(server_config.interface, &server_config.ifindex,
- &server_config.server, server_config.arp) < 0) {
+ &server_config.server, server_config.arp)) {
retval = 1;
goto ret;
}
} dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
-static struct xid_item * xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
+static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
{
struct xid_item *item;
return item;
}
-
static void xid_expire(void)
{
struct xid_item *item = dhcprelay_xid_list.next;
time_t current_time = time(NULL);
while (item != NULL) {
- if ((current_time-item->timestamp) > MAX_LIFETIME) {
+ if ((current_time - item->timestamp) > MAX_LIFETIME) {
last->next = item->next;
free(item);
item = last->next;
}
}
-static struct xid_item * xid_find(uint32_t xid)
+static struct xid_item *xid_find(uint32_t xid)
{
struct xid_item *item = dhcprelay_xid_list.next;
while (item != NULL) {
}
}
-
/**
* get_dhcp_packet_type - gets the message type of a dhcp packet
* p - pointer to the dhcp packet
* signal_handler - handles signals ;-)
* sig - sent signal
*/
-static int dhcprelay_stopflag;
+static smallint dhcprelay_stopflag;
+
static void dhcprelay_signal_handler(int sig)
{
dhcprelay_stopflag = 1;
* dev_list - comma separated list of devices
* returns array
*/
-static char ** get_client_devices(char *dev_list, int *client_number)
+static char **get_client_devices(char *dev_list, int *client_number)
{
char *s, *list, **client_dev;
int i, cn;
(struct sockaddr *)(&client_addr), &addr_size);
if (packlen <= 0)
continue;
- if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL) < 0)
+ if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
dhcp_msg.giaddr = gw_ip;
pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
}
num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
- if (read_interface(argv[2], NULL, &gw_ip, NULL) == -1)
+ if (read_interface(argv[2], NULL, &gw_ip, NULL))
return 1;
dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
struct sockaddr_in *our_ip;
memset(&ifr, 0, sizeof(ifr));
- fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
- if (fd < 0) {
- bb_perror_msg("socket failed");
- return -1;
- }
+ fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+// if (fd < 0) {
+// bb_perror_msg("socket failed");
+// return -1;
+// }
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
}
+ close(fd);
return 0;
}