/* Add an option (supplied in binary form) to the options.
* Option format: [code][len][data1][data2]..[dataLEN]
*/
-void FAST_FUNC udhcp_add_binary_option(uint8_t *optionptr, uint8_t *addopt)
+void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
{
unsigned len;
+ uint8_t *optionptr = packet->options;
unsigned end = udhcp_end_option(optionptr);
- /* end position + option code/length + addopt length + end option */
len = OPT_DATA + addopt[OPT_LEN];
+ /* end position + (option code/length + addopt length) + end option */
if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) {
+//TODO: learn how to use overflow option if we exhaust packet->options[]
bb_error_msg("option 0x%02x did not fit into the packet",
addopt[OPT_CODE]);
return;
optionptr[end + len] = DHCP_END;
}
-/* Add a one to four byte option to a packet */
-void FAST_FUNC udhcp_add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
+/* Add an one to four byte option to a packet */
+void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data)
{
const struct dhcp_option *dh;
data <<= 8 * (4 - len);
/* Assignment is unaligned! */
move_to_unaligned32(&option[OPT_DATA], data);
- udhcp_add_binary_option(optionptr, option);
+ udhcp_add_binary_option(packet, option);
return;
}
}
}
/* Find option 'code' in opt_list */
-struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
+struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code)
{
while (opt_list && opt_list->data[OPT_CODE] < code)
opt_list = opt_list->next;
char *allocated = NULL;
#endif
- existing = find_option(*opt_list, option->code);
+ existing = udhcp_find_option(*opt_list, option->code);
if (!existing) {
log2("Attaching option %02x to list", option->code);
#if ENABLE_FEATURE_UDHCP_RFC3397
uint8_t *udhcp_get_option(struct dhcp_packet *packet, int code) FAST_FUNC;
int udhcp_end_option(uint8_t *optionptr) FAST_FUNC;
-void udhcp_add_binary_option(uint8_t *optionptr, uint8_t *addopt) FAST_FUNC;
-void udhcp_add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) FAST_FUNC;
+void udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) FAST_FUNC;
+void udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) FAST_FUNC;
#if ENABLE_FEATURE_UDHCP_RFC3397
char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC;
uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) FAST_FUNC;
#endif
-struct option_set *find_option(struct option_set *opt_list, uint8_t code) FAST_FUNC;
+struct option_set *udhcp_find_option(struct option_set *opt_list, uint8_t code) FAST_FUNC;
// RFC 2131 Table 5: Fields and options used by DHCP clients
udhcp_init_header(packet, type);
memcpy(packet->chaddr, client_config.client_mac, 6);
if (client_config.clientid)
- udhcp_add_binary_option(packet->options, client_config.clientid);
+ udhcp_add_binary_option(packet, client_config.clientid);
if (client_config.hostname)
- udhcp_add_binary_option(packet->options, client_config.hostname);
+ udhcp_add_binary_option(packet, client_config.hostname);
if (client_config.fqdn)
- udhcp_add_binary_option(packet->options, client_config.fqdn);
+ udhcp_add_binary_option(packet, client_config.fqdn);
if (type != DHCPDECLINE
&& type != DHCPRELEASE
&& client_config.vendorclass
) {
- udhcp_add_binary_option(packet->options, client_config.vendorclass);
+ udhcp_add_binary_option(packet, client_config.vendorclass);
}
}
static void add_client_options(struct dhcp_packet *packet)
{
- /* Add am "param req" option with the list of options we'd like to have
+ /* Add a "param req" option with the list of options we'd like to have
* from stubborn DHCP servers. Pull the data from the struct in common.c.
* No bounds checking because it goes towards the head of the packet. */
uint8_t c;
{
struct option_set *curr = client_config.options;
while (curr) {
- udhcp_add_binary_option(packet->options, curr->data);
+ udhcp_add_binary_option(packet, curr->data);
curr = curr->next;
}
// if (client_config.sname)
init_packet(&packet, DHCPDISCOVER);
packet.xid = xid;
if (requested)
- udhcp_add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
+ udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
/* Explicitly saying that we want RFC-compliant packets helps
* some buggy DHCP servers to NOT send bigger packets */
- udhcp_add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
+ udhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
add_client_options(&packet);
bb_info_msg("Sending discover...");
init_packet(&packet, DHCPREQUEST);
packet.xid = xid;
- udhcp_add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
- udhcp_add_simple_option(packet.options, DHCP_SERVER_ID, server);
+ udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
+ udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
add_client_options(&packet);
addr.s_addr = requested;
init_packet(&packet, DHCPDECLINE);
packet.xid = xid;
- udhcp_add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
- udhcp_add_simple_option(packet.options, DHCP_SERVER_ID, server);
+ udhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
+ udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
bb_info_msg("Sending decline...");
return raw_bcast_from_client_config_ifindex(&packet);
packet.xid = random_xid();
packet.ciaddr = ciaddr;
- udhcp_add_simple_option(packet.options, DHCP_SERVER_ID, server);
+ udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
bb_info_msg("Sending release...");
return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
packet->flags = oldpacket->flags;
packet->gateway_nip = oldpacket->gateway_nip;
packet->ciaddr = oldpacket->ciaddr;
- udhcp_add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
+ udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
}
/* Fill options field, siaddr_nip, and sname and boot_file fields.
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
- udhcp_add_binary_option(packet->options, curr->data);
+ udhcp_add_binary_option(packet, curr->data);
curr = curr->next;
}
}
lease_time_sec = select_lease_time(oldpacket);
- udhcp_add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
+ udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
add_server_options(&packet);
addr.s_addr = packet.yiaddr;
packet.yiaddr = yiaddr;
lease_time_sec = select_lease_time(oldpacket);
- udhcp_add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
+ udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
add_server_options(&packet);
bb_info_msg("%s (v"BB_VER") started", applet_name);
- option = find_option(server_config.options, DHCP_LEASE_TIME);
+ option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
server_config.max_lease_sec = DEFAULT_LEASE_TIME;
if (option) {
move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
close(fd);
if (server_config.notify_file) {
-// TODO: vfork-based child creation
- char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
- system(cmd);
- free(cmd);
+ char *argv[3];
+ argv[0] = server_config.notify_file;
+ argv[1] = server_config.lease_file;
+ argv[2] = NULL;
+ spawn_and_wait(argv);
}
}
lease = find_lease_by_nip(nip);
if (!lease) {
+//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
if (nobody_responds_to_arp(nip, safe_mac))
return nip;
} else {
packet->cookie = htonl(DHCP_MAGIC);
if (DHCP_END != 0)
packet->options[0] = DHCP_END;
- udhcp_add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
+ udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
}
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
*/
#include <net/if.h>
-//#include <features.h>
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
#include <netpacket/packet.h>
#include <net/ethernet.h>