On starting the client, use the supplied interface mac address and create
a transaction id. Puzzle together an IP/UDP/DHCP Discover message, compute
checksums and send it out as a raw packet.
Create an additional function that constructs default options common to
all DHCP messages.
Set the DHCP Client ID option as noticed by Grant Erickson in ConnMan
commit b18d9798b3a0ae46ed87d6d2be8d5a474bf3ab1e:
"Some Internet gateways and Wi-Fi access points are unhappy when the
DHCPv4 client-id option (61) is missing and will refuse to issue a
DHCP lease."
---
src/dhcp/client.c | 163 +++
src/dhcp/client.h |3 +
src/dhcp/protocol.h |4 ++
3 files changed, 170 insertions(+)
diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index 172ddd9..1d4d957 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -21,19 +21,25 @@
#include
#include
#include
+#include
#include "util.h"
#include "list.h"
#include "protocol.h"
+#include "internal.h"
#include "client.h"
+#define DHCP_CLIENT_MIN_OPTIONS_SIZE312
+
struct DHCPClient {
DHCPState state;
int index;
uint8_t *req_opts;
int req_opts_size;
struct in_addr *last_addr;
+struct ether_addr mac_addr;
+uint32_t xid;
};
static uint8_t default_req_opts[] = {
@@ -114,6 +120,163 @@ int dhcp_client_set_index(DHCPClient *client, int
interface_index)
return 0;
}
+int dhcp_client_set_mac(DHCPClient *client, struct ether_addr *addr)
+{
+if (!client)
+return -EINVAL;
+
+if (client->state != DHCP_STATE_INIT)
+return -EBUSY;
+
+memcpy(&client->mac_addr, addr, ETH_ALEN);
+
+return 0;
+}
+
+static int client_packet_init(DHCPClient *client, uint8_t type,
+ DHCPMessage *message, uint8_t **opt,
+ int *optlen)
+{
+int err;
+
+*opt = (uint8_t *)(message + 1);
+
+*optlen -= 4;
+if (*optlen < 0)
+return -ENOBUFS;
+
+message->op = BOOTREQUEST;
+message->htype = 1;
+message->hlen = ETHER_ADDR_LEN;
+message->xid = htonl(client->xid);
+
+memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
+(*opt)[0] = 0x63;
+(*opt)[1] = 0x82;
+(*opt)[2] = 0x53;
+(*opt)[3] = 0x63;
+
+*opt += 4;
+
+err = __dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE,
+ 1, &type);
+if (err < 0)
+return err;
+
+err = __dhcp_option_append(opt, optlen,
+ DHCP_OPTION_CLIENT_IDENTIFIER,
+ ETH_ALEN, &client->mac_addr);
+if (err < 0)
+return err;
+
+ if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
+err = __dhcp_option_append(opt, optlen,
+ DHCP_OPTION_PARAMETER_REQUEST_LIST,
+ client->req_opts_size,
+ client->req_opts);
+if (err < 0)
+return err;
+}
+
+return 0;
+}
+
+static uint16_t client_checksum(void *buf, int len)
+{
+uint32_t sum;
+uint16_t *check;
+int i;
+uint8_t *odd;
+
+sum = 0;
+check = buf;
+
+for (i = 0; i < len / 2 ; i++)
+sum += check[i];
+
+if (len & 0x01) {
+odd = buf;
+sum += odd[len];
+}
+
+return ~((sum & 0x) + (sum >> 16));
+}
+
+static int client_send_discover(DHCPClient *client)
+{
+int err = 0;
+DHCPPacket *discover;
+int optlen, len;
+uint8_t *opt;
+
+optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
+len = sizeof(DHCPPacket) + optlen;
+
+discover = malloc0(len);
+
+if (!discover)
+return -ENOBUFS;
+
+err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
+ &opt, &optlen);
+if (err < 0)
+goto error;
+
+if (client->last_addr) {
+err = __dhcp_option_append(&opt, &optlen,
+ DHCP_OPTION_REQUESTED_IP_ADDRESS,
+ 4, &client->last_addr->s_addr);
+if (err < 0)
+goto error;
+}
+
+err = __dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
+if (err < 0)
+goto error;
+
+discover->ip.version = IPVERSION;
+discover->ip.ihl = sizeof(discover->ip) >> 2;
+discover->ip.tot_len = htons(len);
+
+discover->ip.protocol = IPPROTO_UDP;
+discover->ip.saddr = INADDR_ANY;
+discover->ip.daddr = INADDR_BROADCAST;
+
+discover-