#pragma once

#include "Sockets.h"    // j1699 project Socket,UDP,TCP declarations


// defines
#define MAX_DHCP_RSP       400

#define DHCP_RMT_PORT  68
#define DHCP_LCL_PORT  67


// DHCP hardware type (htype)
#define HTYPE_ETHER       1    /* Ethernet 10Mbps              */
#define HTYPE_IEEE802     6    /* IEEE 802.2 Token Ring... */
#define HTYPE_FDDI        8    /* FDDI...          */
#define HTYPE_INFINIBAND  32   /* IP over Infiniband       */
#define HTYPE_IPMP        255  /* IPMP - random hw address */


// DHCP Message Types
#define DHCPDISCOVER          1
#define DHCPOFFER             2
#define DHCPREQUEST           3
#define DHCPDECLINE           4
#define DHCPACK               5
#define DHCPNAK               6
#define DHCPRELEASE           7
#define DHCPINFORM            8
#define DHCPFORCERENEW        9
#define DHCPLEASEQUERY        10
#define DHCPLEASEUNASSIGNED   11
#define DHCPLEASEUNKNOWN      12
#define DHCPLEASEACTIVE       13
#define DHCPBULKLEASEQUERY    14
#define DHCPLEASEQUERYDONE    15
#define DHCPACTIVELEASEQUERY  16
#define DHCPLEASEQUERYSTATUS  17
#define DHCPTLS               18


// DHCP Option codes
#define DHO_PAD                           0
#define DHO_SUBNET_MASK                   1
#define DHO_TIME_OFFSET                   2
#define DHO_ROUTERS                       3
#define DHO_TIME_SERVERS                  4
#define DHO_NAME_SERVERS                  5
#define DHO_DOMAIN_NAME_SERVERS           6
#define DHO_LOG_SERVERS                   7
#define DHO_COOKIE_SERVERS                8
#define DHO_LPR_SERVERS                   9
#define DHO_IMPRESS_SERVERS               10
#define DHO_RESOURCE_LOCATION_SERVERS     11
#define DHO_HOST_NAME                     12
#define DHO_BOOT_SIZE                     13
#define DHO_MERIT_DUMP                    14
#define DHO_DOMAIN_NAME                   15
#define DHO_SWAP_SERVER                   16
#define DHO_ROOT_PATH                     17
#define DHO_EXTENSIONS_PATH               18
#define DHO_IP_FORWARDING                 19
#define DHO_NON_LOCAL_SOURCE_ROUTING      20
#define DHO_POLICY_FILTER                 21
#define DHO_MAX_DGRAM_REASSEMBLY          22
#define DHO_DEFAULT_IP_TTL                23
#define DHO_PATH_MTU_AGING_TIMEOUT        24
#define DHO_PATH_MTU_PLATEAU_TABLE        25
#define DHO_INTERFACE_MTU                 26
#define DHO_ALL_SUBNETS_LOCAL             27
#define DHO_BROADCAST_ADDRESS             28
#define DHO_PERFORM_MASK_DISCOVERY        29
#define DHO_MASK_SUPPLIER                 30
#define DHO_ROUTER_DISCOVERY              31
#define DHO_ROUTER_SOLICITATION_ADDRESS   32
#define DHO_STATIC_ROUTES                 33
#define DHO_TRAILER_ENCAPSULATION         34
#define DHO_ARP_CACHE_TIMEOUT             35
#define DHO_IEEE802_3_ENCAPSULATION       36
#define DHO_DEFAULT_TCP_TTL               37
#define DHO_TCP_KEEPALIVE_INTERVAL        38
#define DHO_TCP_KEEPALIVE_GARBAGE         39
#define DHO_NIS_DOMAIN                    40
#define DHO_NIS_SERVERS                   41
#define DHO_NTP_SERVERS                   42
#define DHO_VENDOR_ENCAPSULATED_OPTIONS   43
#define DHO_NETBIOS_NAME_SERVERS          44
#define DHO_NETBIOS_DD_SERVER             45
#define DHO_NETBIOS_NODE_TYPE             46
#define DHO_NETBIOS_SCOPE                 47
#define DHO_FONT_SERVERS                  48
#define DHO_X_DISPLAY_MANAGER             49
#define DHO_DHCP_REQUESTED_ADDRESS        50
#define DHO_DHCP_LEASE_TIME               51
#define DHO_DHCP_OPTION_OVERLOAD          52
#define DHO_DHCP_MESSAGE_TYPE             53
#define DHO_DHCP_SERVER_IDENTIFIER        54
#define DHO_DHCP_PARAMETER_REQUEST_LIST   55
#define DHO_DHCP_MESSAGE                  56
#define DHO_DHCP_MAX_MESSAGE_SIZE         57
#define DHO_DHCP_RENEWAL_TIME             58
#define DHO_DHCP_REBINDING_TIME           59
#define DHO_VENDOR_CLASS_IDENTIFIER       60
#define DHO_DHCP_CLIENT_IDENTIFIER        61
#define DHO_NWIP_DOMAIN_NAME              62
#define DHO_NWIP_SUBOPTIONS               63
#define DHO_USER_CLASS                    77
#define DHO_FQDN                          81
#define DHO_DHCP_AGENT_OPTIONS            82
#define DHO_AUTHENTICATE                  90   // RFC3118, was 210
#define DHO_CLIENT_LAST_TRANSACTION_TIME  91
#define DHO_ASSOCIATED_IP                 92
#define DHO_V6_ONLY_PREFERRED             108  // RFC8925
#define DHO_SUBNET_SELECTION              118  // RFC3011!
#define DHO_DOMAIN_SEARCH                 119  // RFC3397
#define DHO_VIVCO_SUBOPTIONS              124
#define DHO_VIVSO_SUBOPTIONS              125

#define DHO_END                           255


typedef struct DHCPOption
{
	BYTE  ucType;    // Information type
	BYTE  ucLength;  // Length of Value field
	BYTE  ucValue;   // first byte of Value field
} t_stDHCPOption;


#define _0_BYTES         0
#define _1_BYTE          1
#define MIN_1_BYTE       2
#define _2_BYTES         3
#define MIN_2_BYTES      4
#define MULT_OF_2_BYTES  5
#define _4_BYTES         6
#define MULT_OF_4_BYTES  7
#define MULT_OF_8_BYTES  8

typedef struct DHCPOptionLookup
{
   char * szLabel;
   short  Length;
} t_stDHCPOptionLookup;


#define DHCPMAGICCOOKIE 0x63825363

typedef struct DHCPMessage
{
	BYTE   ucOp;            // Operation Code
	BYTE   ucHType;         // Hardware Type ( 1 = ethernet 10M)
	BYTE   ucHLen;          // Hardware Address Length (ethernet = 6)
	BYTE   ucHops;          // The number of DHCP relay agents through which a DHCP message passes
	DWORD  ulXId;           // Transaction Identifier
	USHORT usSecs;          // the number of seconds elapsed since a client obtained or renewed an IP address
	USHORT usFlags;         // the mode a DHCP server uses to transmit a DHCP Offer message:
	                        //     0x0000: The DHCP server unicasts a DHCP Offer message.
	                        //     0x8000: The DHCP server broadcasts a DHCP Offer message
	DWORD  ulCiaddr;        // Client IP Address (0.0.0.0 intially)
	DWORD  ulYiaddr;        // Your IP Address (the IP address that a DHCP server allocates to a client)
	DWORD  ulSiaddr;        // Server IP Address
	DWORD  ulGiaddr;        // Gateway Address (the IP address of the first DHCP relay agent)
	BYTE   aucChaddr[6];    // Client Hardware (MAC) Address
	BYTE   aucPadding[10];  // the remaining 10 bytes of chaddr filled with 0s
	BYTE   acSname[64];     // Server Hostname (optional, 0 terminated)
	BYTE   acFile[128];     // Boot File: the name of the startup configuration file to be obtained by a client (optional, 0 terminated)
	DWORD  ulMagicCookie;   // 0x63 82 53 63
	t_stDHCPOption  stOption;
} t_stDHCPMessage;


typedef struct DHCPEntity
{
	BYTE   TransID[4];  // Transactio ID
	BYTE   MacAddr[6];  // Hardware (MAC) Address
	BYTE   IPAddr[4];   // Assigned IP Address
	BYTE   Status;      // Status of Address Assignment
} t_stDHCPEntity;

#define MAX_DHCP_ENTITIES  10
extern t_stDHCPEntity        DHCPList[MAX_DHCP_ENTITIES];
extern BYTE                  DHCP_Entity_Count;

extern t_stDHCPOptionLookup  OptionType[];;
extern unsigned char         aucDHCPMessage[MAX_DHCP_RSP];
extern SADAPTER_INFO         AdapterInfo;


// function prototypes
int     BuildDHCPResponse  ( unsigned long ulSiaddr );
STATUS  DHCPServer         ( void );
void    DisplayUDPMessage  ( BYTE *pMsg, WORD MsgLen, boolean bRx );
void    DisplayDHCPMessage ( t_stDHCPMessage* pstDHCPMsg );
