Move udhcp to new NOMMU helpers.
Fix server part to compile under NOMMU. Client is not compilable yet. On MMU everything compiles (and maybe even works :)
This commit is contained in:
parent
5a142025d3
commit
af1c84360f
@ -6,9 +6,9 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
lib-y:=
|
lib-y:=
|
||||||
lib-$(CONFIG_APP_UDHCPC) += common.o options.o packet.o pidfile.o \
|
lib-$(CONFIG_APP_UDHCPC) += common.o options.o packet.o \
|
||||||
signalpipe.o socket.o
|
signalpipe.o socket.o
|
||||||
lib-$(CONFIG_APP_UDHCPD) += common.o options.o packet.o pidfile.o \
|
lib-$(CONFIG_APP_UDHCPD) += common.o options.o packet.o \
|
||||||
signalpipe.o socket.o
|
signalpipe.o socket.o
|
||||||
lib-$(CONFIG_APP_UDHCPC) += dhcpc.o clientpacket.o clientsocket.o \
|
lib-$(CONFIG_APP_UDHCPC) += dhcpc.o clientpacket.o clientsocket.o \
|
||||||
script.o
|
script.o
|
||||||
|
@ -22,40 +22,56 @@ long uptime(void)
|
|||||||
return info.uptime;
|
return info.uptime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void udhcp_background(const char *pidfile)
|
|
||||||
{
|
|
||||||
#ifdef __uClinux__
|
|
||||||
bb_error_msg("cannot background in uclinux (yet)");
|
|
||||||
#else /* __uClinux__ */
|
|
||||||
int pid_fd;
|
|
||||||
|
|
||||||
/* hold lock during fork. */
|
static const char *saved_pidfile;
|
||||||
pid_fd = pidfile_acquire(pidfile);
|
|
||||||
setsid();
|
static void pidfile_delete(void)
|
||||||
xdaemon(0, 0);
|
{
|
||||||
logmode &= ~LOGMODE_STDIO;
|
if (saved_pidfile)
|
||||||
pidfile_write_release(pid_fd);
|
unlink(saved_pidfile);
|
||||||
#endif /* __uClinux__ */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void udhcp_start_log_and_pid(const char *pidfile)
|
static int pidfile_acquire(const char *pidfile)
|
||||||
|
{
|
||||||
|
int pid_fd;
|
||||||
|
if (!pidfile) return -1;
|
||||||
|
|
||||||
|
pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644);
|
||||||
|
if (pid_fd < 0) {
|
||||||
|
bb_perror_msg("cannot open pidfile %s", pidfile);
|
||||||
|
} else {
|
||||||
|
/* lockf(pid_fd, F_LOCK, 0); */
|
||||||
|
if (!saved_pidfile)
|
||||||
|
atexit(pidfile_delete);
|
||||||
|
saved_pidfile = pidfile;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pid_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pidfile_write_release(int pid_fd)
|
||||||
|
{
|
||||||
|
if (pid_fd < 0) return;
|
||||||
|
|
||||||
|
fdprintf(pid_fd, "%d\n", getpid());
|
||||||
|
/* lockf(pid_fd, F_UNLCK, 0); */
|
||||||
|
close(pid_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void udhcp_make_pidfile(const char *pidfile)
|
||||||
{
|
{
|
||||||
int pid_fd;
|
int pid_fd;
|
||||||
|
|
||||||
/* Make sure our syslog fd isn't overwritten */
|
/* Make sure fd 0,1,2 are open */
|
||||||
bb_sanitize_stdio();
|
bb_sanitize_stdio();
|
||||||
|
|
||||||
/* do some other misc startup stuff while we are here to save bytes */
|
/* Equivalent of doing a fflush after every \n */
|
||||||
pid_fd = pidfile_acquire(pidfile);
|
|
||||||
pidfile_write_release(pid_fd);
|
|
||||||
|
|
||||||
/* equivelent of doing a fflush after every \n */
|
|
||||||
setlinebuf(stdout);
|
setlinebuf(stdout);
|
||||||
|
|
||||||
if (ENABLE_FEATURE_UDHCP_SYSLOG) {
|
/* Create pidfile */
|
||||||
openlog(applet_name, LOG_PID, LOG_LOCAL0);
|
pid_fd = pidfile_acquire(pidfile);
|
||||||
logmode |= LOGMODE_SYSLOG;
|
pidfile_write_release(pid_fd);
|
||||||
}
|
|
||||||
|
|
||||||
bb_info_msg("%s (v%s) started", applet_name, BB_VER);
|
bb_info_msg("%s (v%s) started", applet_name, BB_VER);
|
||||||
}
|
}
|
||||||
|
@ -60,16 +60,12 @@ int udhcp_kernel_packet(struct dhcpMessage *payload,
|
|||||||
|
|
||||||
/**/
|
/**/
|
||||||
|
|
||||||
void udhcp_background(const char *pidfile);
|
void udhcp_make_pidfile(const char *pidfile);
|
||||||
void udhcp_start_log_and_pid(const char *pidfile);
|
|
||||||
|
|
||||||
void udhcp_run_script(struct dhcpMessage *packet, const char *name);
|
void udhcp_run_script(struct dhcpMessage *packet, const char *name);
|
||||||
|
|
||||||
// Still need to clean these up...
|
// Still need to clean these up...
|
||||||
|
|
||||||
/* from pidfile.h */
|
|
||||||
#define pidfile_acquire udhcp_pidfile_acquire
|
|
||||||
#define pidfile_write_release udhcp_pidfile_write_release
|
|
||||||
/* from options.h */
|
/* from options.h */
|
||||||
#define get_option udhcp_get_option
|
#define get_option udhcp_get_option
|
||||||
#define end_option udhcp_end_option
|
#define end_option udhcp_end_option
|
||||||
@ -91,8 +87,6 @@ int udhcp_sp_read(fd_set *rfds);
|
|||||||
int raw_socket(int ifindex);
|
int raw_socket(int ifindex);
|
||||||
int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp);
|
int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t *arp);
|
||||||
int listen_socket(uint32_t ip, int port, const char *inf);
|
int listen_socket(uint32_t ip, int port, const char *inf);
|
||||||
int pidfile_acquire(const char *pidfile);
|
|
||||||
void pidfile_write_release(int pid_fd);
|
|
||||||
int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *arp, char *interface);
|
int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *arp, char *interface);
|
||||||
|
|
||||||
#if ENABLE_FEATURE_UDHCP_DEBUG
|
#if ENABLE_FEATURE_UDHCP_DEBUG
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "dhcpd.h"
|
#include "dhcpd.h"
|
||||||
@ -103,7 +104,16 @@ static void perform_release(void)
|
|||||||
|
|
||||||
static void client_background(void)
|
static void client_background(void)
|
||||||
{
|
{
|
||||||
udhcp_background(client_config.pidfile);
|
#ifdef __uClinux__
|
||||||
|
bb_error_msg("cannot background in uclinux (yet)");
|
||||||
|
/* ... mainly because udhcpc calls client_background()
|
||||||
|
* in _the _middle _of _udhcpc _run_, not at the start!
|
||||||
|
* If that will be properly disabled for NOMMU, client_background()
|
||||||
|
* will work on NOMMU too */
|
||||||
|
#else
|
||||||
|
bb_daemonize(DAEMON_CHDIR_ROOT);
|
||||||
|
logmode &= ~LOGMODE_STDIO;
|
||||||
|
#endif
|
||||||
client_config.foreground = 1; /* Do not fork again. */
|
client_config.foreground = 1; /* Do not fork again. */
|
||||||
client_config.background_if_no_lease = 0;
|
client_config.background_if_no_lease = 0;
|
||||||
}
|
}
|
||||||
@ -246,13 +256,18 @@ int udhcpc_main(int argc, char *argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start the log, sanitize fd's, and write a pid file */
|
if (ENABLE_FEATURE_UDHCP_SYSLOG) {
|
||||||
udhcp_start_log_and_pid(client_config.pidfile);
|
openlog(applet_name, LOG_PID, LOG_LOCAL0);
|
||||||
|
logmode |= LOGMODE_SYSLOG;
|
||||||
|
}
|
||||||
|
|
||||||
if (read_interface(client_config.interface, &client_config.ifindex,
|
if (read_interface(client_config.interface, &client_config.ifindex,
|
||||||
NULL, client_config.arp) < 0)
|
NULL, client_config.arp) < 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
/* Sanitize fd's and write pidfile */
|
||||||
|
udhcp_make_pidfile(client_config.pidfile);
|
||||||
|
|
||||||
/* if not set, and not suppressed, setup the default client ID */
|
/* if not set, and not suppressed, setup the default client ID */
|
||||||
if (!client_config.clientid && !no_clientid) {
|
if (!client_config.clientid && !no_clientid) {
|
||||||
client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
|
client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <syslog.h>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "dhcpd.h"
|
#include "dhcpd.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
@ -33,16 +34,30 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
struct option_set *option;
|
struct option_set *option;
|
||||||
struct dhcpOfferedAddr *lease, static_lease;
|
struct dhcpOfferedAddr *lease, static_lease;
|
||||||
|
|
||||||
|
//Huh, dhcpd don't have --foreground, --syslog options?? TODO
|
||||||
|
|
||||||
|
if (!ENABLE_FEATURE_UDHCP_DEBUG) {
|
||||||
|
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
||||||
|
logmode &= ~LOGMODE_STDIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ENABLE_FEATURE_UDHCP_SYSLOG) {
|
||||||
|
openlog(applet_name, LOG_PID, LOG_LOCAL0);
|
||||||
|
logmode |= LOGMODE_SYSLOG;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Would rather not do read_config before daemonization -
|
||||||
|
* otherwise NOMMU machines will parse config twice */
|
||||||
read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
|
read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
|
||||||
|
|
||||||
/* Start the log, sanitize fd's, and write a pid file */
|
udhcp_make_pidfile(server_config.pidfile);
|
||||||
udhcp_start_log_and_pid(server_config.pidfile);
|
|
||||||
|
|
||||||
if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
|
option = find_option(server_config.options, DHCP_LEASE_TIME);
|
||||||
|
if (option) {
|
||||||
memcpy(&server_config.lease, option->data + 2, 4);
|
memcpy(&server_config.lease, option->data + 2, 4);
|
||||||
server_config.lease = ntohl(server_config.lease);
|
server_config.lease = ntohl(server_config.lease);
|
||||||
}
|
} else
|
||||||
else server_config.lease = LEASE_TIME;
|
server_config.lease = LEASE_TIME;
|
||||||
|
|
||||||
/* Sanity check */
|
/* Sanity check */
|
||||||
num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
|
num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
|
||||||
@ -60,9 +75,6 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
&server_config.server, server_config.arp) < 0)
|
&server_config.server, server_config.arp) < 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (!ENABLE_FEATURE_UDHCP_DEBUG)
|
|
||||||
udhcp_background(server_config.pidfile); /* hold lock during fork. */
|
|
||||||
|
|
||||||
/* Setup the signal pipe */
|
/* Setup the signal pipe */
|
||||||
udhcp_sp_setup();
|
udhcp_sp_setup();
|
||||||
|
|
||||||
@ -106,7 +118,8 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
default: continue; /* signal or error (probably EINTR) */
|
default: continue; /* signal or error (probably EINTR) */
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
|
bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
|
||||||
|
if (bytes < 0) {
|
||||||
if (bytes == -1 && errno != EINTR) {
|
if (bytes == -1 && errno != EINTR) {
|
||||||
DEBUG("error on read, %s, reopening socket", strerror(errno));
|
DEBUG("error on read, %s, reopening socket", strerror(errno));
|
||||||
close(server_socket);
|
close(server_socket);
|
||||||
@ -115,7 +128,8 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
|
state = get_option(&packet, DHCP_MESSAGE_TYPE);
|
||||||
|
if (state == NULL) {
|
||||||
bb_error_msg("cannot get option from packet, ignoring");
|
bb_error_msg("cannot get option from packet, ignoring");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -131,7 +145,6 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
static_lease.expires = 0;
|
static_lease.expires = 0;
|
||||||
|
|
||||||
lease = &static_lease;
|
lease = &static_lease;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
lease = find_lease_by_chaddr(packet.chaddr);
|
lease = find_lease_by_chaddr(packet.chaddr);
|
||||||
}
|
}
|
||||||
@ -157,25 +170,23 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
if (server_id) {
|
if (server_id) {
|
||||||
/* SELECTING State */
|
/* SELECTING State */
|
||||||
DEBUG("server_id = %08x", ntohl(server_id_align));
|
DEBUG("server_id = %08x", ntohl(server_id_align));
|
||||||
if (server_id_align == server_config.server && requested &&
|
if (server_id_align == server_config.server && requested
|
||||||
requested_align == lease->yiaddr) {
|
&& requested_align == lease->yiaddr
|
||||||
|
) {
|
||||||
sendACK(&packet, lease->yiaddr);
|
sendACK(&packet, lease->yiaddr);
|
||||||
}
|
}
|
||||||
|
} else if (requested) {
|
||||||
|
/* INIT-REBOOT State */
|
||||||
|
if (lease->yiaddr == requested_align)
|
||||||
|
sendACK(&packet, lease->yiaddr);
|
||||||
|
else
|
||||||
|
sendNAK(&packet);
|
||||||
|
} else if (lease->yiaddr == packet.ciaddr) {
|
||||||
|
/* RENEWING or REBINDING State */
|
||||||
|
sendACK(&packet, lease->yiaddr);
|
||||||
} else {
|
} else {
|
||||||
if (requested) {
|
/* don't know what to do!!!! */
|
||||||
/* INIT-REBOOT State */
|
sendNAK(&packet);
|
||||||
if (lease->yiaddr == requested_align)
|
|
||||||
sendACK(&packet, lease->yiaddr);
|
|
||||||
else sendNAK(&packet);
|
|
||||||
} else {
|
|
||||||
/* RENEWING or REBINDING State */
|
|
||||||
if (lease->yiaddr == packet.ciaddr)
|
|
||||||
sendACK(&packet, lease->yiaddr);
|
|
||||||
else {
|
|
||||||
/* don't know what to do!!!! */
|
|
||||||
sendNAK(&packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* what to do if we have no record of the client */
|
/* what to do if we have no record of the client */
|
||||||
@ -184,19 +195,22 @@ int udhcpd_main(int argc, char *argv[])
|
|||||||
|
|
||||||
} else if (requested) {
|
} else if (requested) {
|
||||||
/* INIT-REBOOT State */
|
/* INIT-REBOOT State */
|
||||||
if ((lease = find_lease_by_yiaddr(requested_align))) {
|
lease = find_lease_by_yiaddr(requested_align);
|
||||||
|
if (lease) {
|
||||||
if (lease_expired(lease)) {
|
if (lease_expired(lease)) {
|
||||||
/* probably best if we drop this lease */
|
/* probably best if we drop this lease */
|
||||||
memset(lease->chaddr, 0, 16);
|
memset(lease->chaddr, 0, 16);
|
||||||
/* make some contention for this address */
|
/* make some contention for this address */
|
||||||
} else sendNAK(&packet);
|
} else
|
||||||
} else if (requested_align < server_config.start ||
|
sendNAK(&packet);
|
||||||
requested_align > server_config.end) {
|
} else if (requested_align < server_config.start
|
||||||
|
|| requested_align > server_config.end
|
||||||
|
) {
|
||||||
sendNAK(&packet);
|
sendNAK(&packet);
|
||||||
} /* else remain silent */
|
} /* else remain silent */
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
/* RENEWING or REBINDING State */
|
/* RENEWING or REBINDING State */
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DHCPDECLINE:
|
case DHCPDECLINE:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user