106 lines
2.1 KiB
C++
106 lines
2.1 KiB
C++
#ifndef _SOFT_DHCP_H_
|
|
#define _SOFT_DHCP_H_
|
|
|
|
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
#include "SoftDhcpConsts.h"
|
|
|
|
|
|
typedef union {
|
|
uint8_t octets[4];
|
|
uint32_t addr;
|
|
} ipaddr;
|
|
|
|
|
|
typedef struct __attribute__ ((packed)) DhcpPacket {
|
|
uint8_t op;
|
|
uint8_t htype;
|
|
uint8_t hlen;
|
|
uint8_t hops;
|
|
uint32_t xid;
|
|
uint16_t secs;
|
|
uint16_t flags;
|
|
ipaddr ciaddr;
|
|
ipaddr yiaddr;
|
|
ipaddr siaddr;
|
|
ipaddr giaddr;
|
|
uint8_t chaddr[16];
|
|
uint8_t sname[64];
|
|
uint8_t file[128];
|
|
uint32_t magic;
|
|
uint8_t options[308];
|
|
} DhcpPacket;
|
|
|
|
|
|
class SoftDhcp {
|
|
|
|
private:
|
|
DhcpPacket packet;
|
|
uint8_t *optptr;
|
|
uint8_t prl[64];
|
|
uint8_t prl_count;
|
|
uint8_t reqopt[308];
|
|
uint8_t *reqoptptr;
|
|
IPAddress clientIP;
|
|
IPAddress serverIP;
|
|
IPAddress routerIP;
|
|
IPAddress netmask;
|
|
IPAddress dnsIP1;
|
|
IPAddress dnsIP2;
|
|
|
|
WiFiUDP udp;
|
|
bool request_sent;
|
|
uint32_t last_xid;
|
|
uint32_t last_update;
|
|
ipaddr sid;
|
|
ipaddr yiaddr;
|
|
int32_t renew_countdown;
|
|
int32_t rebind_countdown;
|
|
int32_t expire_countdown;
|
|
|
|
void (*leaseHandler)();
|
|
void (*offerHandler)();
|
|
void (*errorHandler)();
|
|
void (*expireHandler)();
|
|
|
|
void initializePacket();
|
|
void request();
|
|
void respond();
|
|
void clearOptions();
|
|
bool addOption(uint8_t type, uint8_t len, uint8_t *value);
|
|
bool addOption(uint8_t type, uint8_t value);
|
|
bool appendRequestOptions();
|
|
uint8_t *getOption(uint8_t type);
|
|
int32_t getTimeoutOption(uint8_t option, int32_t def);
|
|
|
|
void dump();
|
|
|
|
public:
|
|
|
|
SoftDhcp();
|
|
|
|
void onLease(void (*handler)());
|
|
void onOffer(void (*handler)());
|
|
void onError(void (*handler)());
|
|
void onExpire(void (*handler)());
|
|
|
|
void begin();
|
|
void update();
|
|
|
|
IPAddress localIP();
|
|
IPAddress subnetMask();
|
|
IPAddress gatewayIP();
|
|
IPAddress dnsIP(uint8_t dns_no = 0);
|
|
|
|
ssize_t getOption(uint8_t type, void *buf, size_t len);
|
|
ssize_t getOption(uint8_t type, void *buf, size_t len, ssize_t item);
|
|
void requestOption(uint8_t type);
|
|
bool addRequestOption(uint8_t type, uint8_t len, uint8_t *value);
|
|
void hostname(const char *name);
|
|
void hostname(String& name);
|
|
};
|
|
|
|
|
|
#endif // _SOFT_DHCP_H_
|