2022-02-07 06:35:29 +05:30
|
|
|
// Copyright 2004-2018 Nicholas J. Kain <njkain at gmail dot com>
|
|
|
|
// SPDX-License-Identifier: MIT
|
2011-07-25 12:00:57 +05:30
|
|
|
#ifndef NDHC_DHCP_H_
|
|
|
|
#define NDHC_DHCP_H_
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2014-03-31 02:32:48 +05:30
|
|
|
#include <stdint.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/ip.h>
|
2014-03-13 02:35:43 +05:30
|
|
|
#include "ndhc.h"
|
2010-12-24 19:25:59 +05:30
|
|
|
|
2011-06-11 20:49:05 +05:30
|
|
|
#define DHCP_SERVER_PORT 67
|
|
|
|
#define DHCP_CLIENT_PORT 68
|
|
|
|
#define DHCP_MAGIC 0x63825363
|
|
|
|
|
|
|
|
enum {
|
|
|
|
DHCPDISCOVER = 1,
|
|
|
|
DHCPOFFER = 2,
|
|
|
|
DHCPREQUEST = 3,
|
|
|
|
DHCPDECLINE = 4,
|
|
|
|
DHCPACK = 5,
|
|
|
|
DHCPNAK = 6,
|
|
|
|
DHCPRELEASE = 7,
|
|
|
|
DHCPINFORM = 8
|
|
|
|
};
|
|
|
|
|
2011-06-27 02:55:00 +05:30
|
|
|
struct dhcpmsg {
|
|
|
|
uint8_t op; // Message type: 1 = BOOTREQUEST for clients.
|
2011-07-27 17:33:42 +05:30
|
|
|
uint8_t htype; // ARP HW address type: always '1' for ethernet.
|
|
|
|
uint8_t hlen; // Hardware address length: always '6' for ethernet.
|
2011-06-27 02:55:00 +05:30
|
|
|
uint8_t hops; // Client sets to zero.
|
|
|
|
uint32_t xid; // Transaction ID: random number identifying session
|
|
|
|
uint16_t secs; // Filled by client: seconds since client began address
|
|
|
|
// aquisition or renewal process.
|
|
|
|
uint16_t flags; // DHCP flags
|
2011-07-06 21:02:22 +05:30
|
|
|
uint32_t ciaddr; // Client IP: only filled in if client is in BOUND, RENEW,
|
2011-03-31 04:28:09 +05:30
|
|
|
// or REBINDING and can reply to ARP requests
|
|
|
|
uint32_t yiaddr; // 'your' (client) IP address
|
2011-07-27 17:33:42 +05:30
|
|
|
uint32_t siaddr; // Always zero -- unused.
|
|
|
|
uint32_t giaddr; // Always zero -- unused.
|
2011-06-27 02:55:00 +05:30
|
|
|
uint8_t chaddr[16]; // Client MAC address
|
2011-07-27 17:33:42 +05:30
|
|
|
uint8_t sname[64]; // More DHCP options (#3)
|
|
|
|
uint8_t file[128]; // More DHCP options (#2)
|
2011-07-02 14:15:11 +05:30
|
|
|
uint32_t cookie; // Magic number cookie that starts DHCP options
|
2011-07-27 17:33:42 +05:30
|
|
|
uint8_t options[308]; // DHCP options field (#1)
|
2010-11-12 14:32:18 +05:30
|
|
|
};
|
|
|
|
|
2010-11-14 12:23:23 +05:30
|
|
|
struct ip_udp_dhcp_packet {
|
2010-11-14 11:24:05 +05:30
|
|
|
struct iphdr ip;
|
|
|
|
struct udphdr udp;
|
2011-06-27 02:55:00 +05:30
|
|
|
struct dhcpmsg data;
|
2010-11-12 14:32:18 +05:30
|
|
|
};
|
|
|
|
|
2010-11-14 12:23:23 +05:30
|
|
|
struct udp_dhcp_packet {
|
|
|
|
struct udphdr udp;
|
2011-06-27 02:55:00 +05:30
|
|
|
struct dhcpmsg data;
|
2010-11-14 12:23:23 +05:30
|
|
|
};
|
|
|
|
|
2022-01-12 09:05:19 +05:30
|
|
|
void start_dhcp_listen(struct client_state_t *cs);
|
|
|
|
void stop_dhcp_listen(struct client_state_t *cs);
|
|
|
|
bool dhcp_packet_get(struct client_state_t *cs, struct dhcpmsg *packet,
|
|
|
|
uint8_t *msgtype, uint32_t *srcaddr);
|
|
|
|
ssize_t send_discover(struct client_state_t *cs);
|
|
|
|
ssize_t send_selecting(struct client_state_t *cs);
|
|
|
|
ssize_t send_renew_or_rebind(struct client_state_t *cs, bool is_renew);
|
|
|
|
static inline ssize_t send_renew(struct client_state_t *cs)
|
2020-10-19 14:56:47 +05:30
|
|
|
{
|
|
|
|
return send_renew_or_rebind(cs, true);
|
|
|
|
}
|
2022-01-12 09:05:19 +05:30
|
|
|
static inline ssize_t send_rebind(struct client_state_t *cs)
|
2020-10-19 14:56:47 +05:30
|
|
|
{
|
|
|
|
return send_renew_or_rebind(cs, false);
|
|
|
|
}
|
2022-01-12 09:05:19 +05:30
|
|
|
ssize_t send_decline(struct client_state_t *cs, uint32_t server);
|
|
|
|
ssize_t send_release(struct client_state_t *cs);
|
2011-06-11 20:49:05 +05:30
|
|
|
|
2010-11-12 14:32:18 +05:30
|
|
|
#endif
|