io6Library
WIZnet Dual Stack TCP/IP Ethernet Controller Driver
Data Structures | Macros | Enumerations | Functions | Variables
dhcpv4.c File Reference

DHCP APIs implement file. More...

#include "socket.h"
#include "dhcpv4.h"

Go to the source code of this file.

Data Structures

struct  RIP_MSG
 

Macros

#define STATE_DHCPV4_INIT   0
 Initialize. More...
 
#define STATE_DHCPV4_DISCOVER   1
 send DISCOVER and wait OFFER More...
 
#define STATE_DHCPV4_REQUEST   2
 send REQEUST and wait ACK or NACK More...
 
#define STATE_DHCPV4_LEASED   3
 ReceiveD ACK and IP leased. More...
 
#define STATE_DHCPV4_REREQUEST   4
 send REQUEST for maintaining leased IP More...
 
#define STATE_DHCPV4_RELEASE   5
 No use. More...
 
#define STATE_DHCPV4_STOP   6
 Stop processing DHCP. More...
 
#define DHCPV4_FLAGSBROADCAST   0x8000
 The broadcast value of flags in RIP_MSG. More...
 
#define DHCPV4_FLAGSUNICAST   0x0000
 The unicast value of flags in RIP_MSG. More...
 
#define DHCPV4_BOOTREQUEST   1
 Request Message used in op of RIP_MSG. More...
 
#define DHCPV4_BOOTREPLY   2
 Reply Message used i op of RIP_MSG. More...
 
#define DHCPV4_DISCOVER   1
 DISCOVER message in OPT of RIP_MSG. More...
 
#define DHCPV4_OFFER   2
 OFFER message in OPT of RIP_MSG. More...
 
#define DHCPV4_REQUEST   3
 REQUEST message in OPT of RIP_MSG. More...
 
#define DHCPV4_DECLINE   4
 DECLINE message in OPT of RIP_MSG. More...
 
#define DHCPV4_ACK   5
 ACK message in OPT of RIP_MSG. More...
 
#define DHCPV4_NAK   6
 NACK message in OPT of RIP_MSG. More...
 
#define DHCPV4_RELEASE   7
 RELEASE message in OPT of RIP_MSG. No use. More...
 
#define DHCPV4_INFORM   8
 INFORM message in OPT of RIP_MSG. No use. More...
 
#define DHCPV4_HTYPE10MB   1
 Used in type of RIP_MSG. More...
 
#define DHCPV4_HTYPE100MB   2
 Used in type of RIP_MSG. More...
 
#define DHCPV4_HLENETHERNET   6
 Used in hlen of RIP_MSG. More...
 
#define DHCPV4_HOPS   0
 Used in hops of RIP_MSG. More...
 
#define DHCPV4_SECS   0
 Used in secs of RIP_MSG. More...
 
#define INFINITE_LEASETIME   0xffffffff
 Infinite lease time. More...
 
#define OPT_SIZE   312
 
#define RIP_MSG_SIZE   (236+OPT_SIZE)
 

Enumerations

enum  {
  padOption = 0, subnetMask = 1, timerOffset = 2, routersOnSubnet = 3,
  timeServer = 4, nameServer = 5, dns = 6, logServer = 7,
  cookieServer = 8, lprServer = 9, impressServer = 10, resourceLocationServer = 11,
  hostName = 12, bootFileSize = 13, meritDumpFile = 14, domainName = 15,
  swapServer = 16, rootPath = 17, extentionsPath = 18, IPforwarding = 19,
  nonLocalSourceRouting = 20, policyFilter = 21, maxDgramReasmSize = 22, defaultIPTTL = 23,
  pathMTUagingTimeout = 24, pathMTUplateauTable = 25, ifMTU = 26, allSubnetsLocal = 27,
  broadcastAddr = 28, performMaskDiscovery = 29, maskSupplier = 30, performRouterDiscovery = 31,
  routerSolicitationAddr = 32, staticRoute = 33, trailerEncapsulation = 34, arpCacheTimeout = 35,
  ethernetEncapsulation = 36, tcpDefaultTTL = 37, tcpKeepaliveInterval = 38, tcpKeepaliveGarbage = 39,
  nisDomainName = 40, nisServers = 41, ntpServers = 42, vendorSpecificInfo = 43,
  netBIOSnameServer = 44, netBIOSdgramDistServer = 45, netBIOSnodeType = 46, netBIOSscope = 47,
  xFontServer = 48, xDisplayManager = 49, dhcpRequestedIPaddr = 50, dhcpIPaddrLeaseTime = 51,
  dhcpOptionOverload = 52, dhcpMessageType = 53, dhcpServerIdentifier = 54, dhcpParamRequest = 55,
  dhcpMsg = 56, dhcpMaxMsgSize = 57, dhcpT1value = 58, dhcpT2value = 59,
  dhcpClassIdentifier = 60, dhcpClientIdentifier = 61, endOption = 255
}
 

Functions

void default_ipv4_assign (void)
 
void default_ipv4_update (void)
 
void default_ipv4_conflict (void)
 
void reg_dhcpv4_cbfunc (void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void))
 
void makeDHCPV4MSG (void)
 
void send_DHCPv4_DISCOVER (void)
 
void send_DHCPv4_REQUEST (void)
 
void send_DHCPv4_DECLINE (void)
 
int8_t parseDHCPv4MSG (void)
 
uint8_t DHCPv4_run (void)
 
void DHCPv4_stop (void)
 
uint8_t check_DHCPv4_timeout (void)
 
int8_t check_DHCPv4_leasedIP (void)
 
void DHCPv4_init (uint8_t s, uint8_t *buf)
 
void reset_DHCPv4_timeout (void)
 
void DHCPv4_time_handler (void)
 
void getIPfromDHCPv4 (uint8_t *ip)
 
void getGWfromDHCPv4 (uint8_t *ip)
 
void getSNfromDHCPv4 (uint8_t *ip)
 
void getDNSfromDHCPv4 (uint8_t *ip)
 
uint32_t getDHCPv4Leasetime (void)
 
char NibbleToHex (uint8_t nibble)
 

Variables

uint8_t DHCPV4_SOCKET
 
uint8_t DHCPV4_SIP [4]
 
uint8_t DHCPV4_REAL_SIP [4]
 
uint8_t OLD_allocated_ip [4] = {0, }
 
uint8_t DHCPv4_allocated_ip [4] = {0, }
 
uint8_t DHCPv4_allocated_gw [4] = {0, }
 
uint8_t DHCPv4_allocated_sn [4] = {0, }
 
uint8_t DHCPv4_allocated_dns [4] = {0, }
 
int8_t dhcpv4_state = STATE_DHCPV4_INIT
 
int8_t dhcpv4_retry_count = 0
 
uint32_t dhcpv4_lease_time = INFINITE_LEASETIME
 
volatile uint32_t dhcpv4_tick_1s = 0
 
uint32_t dhcpv4_tick_next = DHCPV4_WAIT_TIME
 
uint32_t DHCPV4_XID
 
RIP_MSGpDHCPv4MSG
 
uint8_t HOST_NAMEv4 [] = DCHPV4_HOST_NAME
 
uint8_t DHCPv4_CHADDR [6]
 
void(* dhcp_ipv4_assign )(void)
 

Detailed Description

DHCP APIs implement file.

Processing DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE.

Version
1.1.1
Date
2019/10/08
Revision history
<2019/10/08> compare DHCP server ip address <2013/11/18> 1st Release <2012/12/20> V1.1.0 1. Optimize code 2. Add reg_dhcpv4_cbfunc() 3. Add DHCPv4_stop() 4. Integrate check_DHCPv4_state() & DHCPv4_run() to DHCPv4_run() 5. Don't care system endian 6. Add comments <2012/12/26> V1.1.1 1. Modify variable declaration: dhcpv4_tick_1s is declared volatile for code optimization
Author
Eric Jung & MidnightCow

Copyright (c) 2013, WIZnet Co., LTD. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file dhcpv4.c.

Macro Definition Documentation

◆ STATE_DHCPV4_INIT

#define STATE_DHCPV4_INIT   0

Initialize.

Definition at line 63 of file dhcpv4.c.

◆ STATE_DHCPV4_DISCOVER

#define STATE_DHCPV4_DISCOVER   1

send DISCOVER and wait OFFER

Definition at line 64 of file dhcpv4.c.

◆ STATE_DHCPV4_REQUEST

#define STATE_DHCPV4_REQUEST   2

send REQEUST and wait ACK or NACK

Definition at line 65 of file dhcpv4.c.

◆ STATE_DHCPV4_LEASED

#define STATE_DHCPV4_LEASED   3

ReceiveD ACK and IP leased.

Definition at line 66 of file dhcpv4.c.

◆ STATE_DHCPV4_REREQUEST

#define STATE_DHCPV4_REREQUEST   4

send REQUEST for maintaining leased IP

Definition at line 67 of file dhcpv4.c.

◆ STATE_DHCPV4_RELEASE

#define STATE_DHCPV4_RELEASE   5

No use.

Definition at line 68 of file dhcpv4.c.

◆ STATE_DHCPV4_STOP

#define STATE_DHCPV4_STOP   6

Stop processing DHCP.

Definition at line 69 of file dhcpv4.c.

◆ DHCPV4_FLAGSBROADCAST

#define DHCPV4_FLAGSBROADCAST   0x8000

The broadcast value of flags in RIP_MSG.

Definition at line 71 of file dhcpv4.c.

◆ DHCPV4_FLAGSUNICAST

#define DHCPV4_FLAGSUNICAST   0x0000

The unicast value of flags in RIP_MSG.

Definition at line 72 of file dhcpv4.c.

◆ DHCPV4_BOOTREQUEST

#define DHCPV4_BOOTREQUEST   1

Request Message used in op of RIP_MSG.

Definition at line 75 of file dhcpv4.c.

◆ DHCPV4_BOOTREPLY

#define DHCPV4_BOOTREPLY   2

Reply Message used i op of RIP_MSG.

Definition at line 76 of file dhcpv4.c.

◆ DHCPV4_DISCOVER

#define DHCPV4_DISCOVER   1

DISCOVER message in OPT of RIP_MSG.

Definition at line 79 of file dhcpv4.c.

◆ DHCPV4_OFFER

#define DHCPV4_OFFER   2

OFFER message in OPT of RIP_MSG.

Definition at line 80 of file dhcpv4.c.

◆ DHCPV4_REQUEST

#define DHCPV4_REQUEST   3

REQUEST message in OPT of RIP_MSG.

Definition at line 81 of file dhcpv4.c.

◆ DHCPV4_DECLINE

#define DHCPV4_DECLINE   4

DECLINE message in OPT of RIP_MSG.

Definition at line 82 of file dhcpv4.c.

◆ DHCPV4_ACK

#define DHCPV4_ACK   5

ACK message in OPT of RIP_MSG.

Definition at line 83 of file dhcpv4.c.

◆ DHCPV4_NAK

#define DHCPV4_NAK   6

NACK message in OPT of RIP_MSG.

Definition at line 84 of file dhcpv4.c.

◆ DHCPV4_RELEASE

#define DHCPV4_RELEASE   7

RELEASE message in OPT of RIP_MSG. No use.

Definition at line 85 of file dhcpv4.c.

◆ DHCPV4_INFORM

#define DHCPV4_INFORM   8

INFORM message in OPT of RIP_MSG. No use.

Definition at line 86 of file dhcpv4.c.

◆ DHCPV4_HTYPE10MB

#define DHCPV4_HTYPE10MB   1

Used in type of RIP_MSG.

Definition at line 88 of file dhcpv4.c.

◆ DHCPV4_HTYPE100MB

#define DHCPV4_HTYPE100MB   2

Used in type of RIP_MSG.

Definition at line 89 of file dhcpv4.c.

◆ DHCPV4_HLENETHERNET

#define DHCPV4_HLENETHERNET   6

Used in hlen of RIP_MSG.

Definition at line 91 of file dhcpv4.c.

◆ DHCPV4_HOPS

#define DHCPV4_HOPS   0

Used in hops of RIP_MSG.

Definition at line 92 of file dhcpv4.c.

◆ DHCPV4_SECS

#define DHCPV4_SECS   0

Used in secs of RIP_MSG.

Definition at line 93 of file dhcpv4.c.

◆ INFINITE_LEASETIME

#define INFINITE_LEASETIME   0xffffffff

Infinite lease time.

Definition at line 95 of file dhcpv4.c.

◆ OPT_SIZE

#define OPT_SIZE   312

Definition at line 97 of file dhcpv4.c.

◆ RIP_MSG_SIZE

#define RIP_MSG_SIZE   (236+OPT_SIZE)

Definition at line 98 of file dhcpv4.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
padOption 
subnetMask 
timerOffset 
routersOnSubnet 
timeServer 
nameServer 
dns 
logServer 
cookieServer 
lprServer 
impressServer 
resourceLocationServer 
hostName 
bootFileSize 
meritDumpFile 
domainName 
swapServer 
rootPath 
extentionsPath 
IPforwarding 
nonLocalSourceRouting 
policyFilter 
maxDgramReasmSize 
defaultIPTTL 
pathMTUagingTimeout 
pathMTUplateauTable 
ifMTU 
allSubnetsLocal 
broadcastAddr 
performMaskDiscovery 
maskSupplier 
performRouterDiscovery 
routerSolicitationAddr 
staticRoute 
trailerEncapsulation 
arpCacheTimeout 
ethernetEncapsulation 
tcpDefaultTTL 
tcpKeepaliveInterval 
tcpKeepaliveGarbage 
nisDomainName 
nisServers 
ntpServers 
vendorSpecificInfo 
netBIOSnameServer 
netBIOSdgramDistServer 
netBIOSnodeType 
netBIOSscope 
xFontServer 
xDisplayManager 
dhcpRequestedIPaddr 
dhcpIPaddrLeaseTime 
dhcpOptionOverload 
dhcpMessageType 
dhcpServerIdentifier 
dhcpParamRequest 
dhcpMsg 
dhcpMaxMsgSize 
dhcpT1value 
dhcpT2value 
dhcpClassIdentifier 
dhcpClientIdentifier 
endOption 

Definition at line 103 of file dhcpv4.c.

Function Documentation

◆ default_ipv4_assign()

void default_ipv4_assign ( void  )

◆ default_ipv4_update()

void default_ipv4_update ( void  )

Definition at line 265 of file dhcpv4.c.

References default_ipv4_assign(), DHCPv4_CHADDR, getSYCR0, setSHAR, setSYCR0, and SYCR0_RST.

Referenced by reg_dhcpv4_cbfunc().

◆ default_ipv4_conflict()

void default_ipv4_conflict ( void  )

Definition at line 275 of file dhcpv4.c.

References DHCPv4_CHADDR, getSYCR0, setSHAR, setSYCR0, and SYCR0_RST.

Referenced by reg_dhcpv4_cbfunc().

◆ reg_dhcpv4_cbfunc()

void reg_dhcpv4_cbfunc ( void(*)(void)  ip_assign,
void(*)(void)  ip_update,
void(*)(void)  ip_conflict 
)

◆ makeDHCPV4MSG()

void makeDHCPV4MSG ( void  )

◆ send_DHCPv4_DISCOVER()

void send_DHCPv4_DISCOVER ( void  )

◆ send_DHCPv4_REQUEST()

void send_DHCPv4_REQUEST ( void  )

◆ send_DHCPv4_DECLINE()

void send_DHCPv4_DECLINE ( void  )

◆ parseDHCPv4MSG()

int8_t parseDHCPv4MSG ( void  )

◆ DHCPv4_run()

uint8_t DHCPv4_run ( void  )

◆ DHCPv4_stop()

void DHCPv4_stop ( void  )

Definition at line 838 of file dhcpv4.c.

References close(), DHCPV4_SOCKET, dhcpv4_state, and STATE_DHCPV4_STOP.

◆ check_DHCPv4_timeout()

uint8_t check_DHCPv4_timeout ( void  )

◆ check_DHCPv4_leasedIP()

int8_t check_DHCPv4_leasedIP ( void  )

◆ DHCPv4_init()

void DHCPv4_init ( uint8_t  s,
uint8_t *  buf 
)

◆ reset_DHCPv4_timeout()

void reset_DHCPv4_timeout ( void  )

◆ DHCPv4_time_handler()

void DHCPv4_time_handler ( void  )

Definition at line 969 of file dhcpv4.c.

References dhcpv4_tick_1s.

◆ getIPfromDHCPv4()

void getIPfromDHCPv4 ( uint8_t *  ip)

Definition at line 974 of file dhcpv4.c.

References DHCPv4_allocated_ip.

◆ getGWfromDHCPv4()

void getGWfromDHCPv4 ( uint8_t *  ip)

Definition at line 982 of file dhcpv4.c.

References DHCPv4_allocated_gw.

◆ getSNfromDHCPv4()

void getSNfromDHCPv4 ( uint8_t *  ip)

Definition at line 990 of file dhcpv4.c.

References DHCPv4_allocated_sn.

◆ getDNSfromDHCPv4()

void getDNSfromDHCPv4 ( uint8_t *  ip)

Definition at line 998 of file dhcpv4.c.

References DHCPv4_allocated_dns.

◆ getDHCPv4Leasetime()

uint32_t getDHCPv4Leasetime ( void  )

Definition at line 1006 of file dhcpv4.c.

References dhcpv4_lease_time.

◆ NibbleToHex()

char NibbleToHex ( uint8_t  nibble)

Definition at line 1011 of file dhcpv4.c.

Referenced by send_DHCPv4_DISCOVER(), and send_DHCPv4_REQUEST().

Variable Documentation

◆ DHCPV4_SOCKET

uint8_t DHCPV4_SOCKET

◆ DHCPV4_SIP

uint8_t DHCPV4_SIP[4]

◆ DHCPV4_REAL_SIP

uint8_t DHCPV4_REAL_SIP[4]

Definition at line 196 of file dhcpv4.c.

Referenced by parseDHCPv4MSG(), and send_DHCPv4_DISCOVER().

◆ OLD_allocated_ip

uint8_t OLD_allocated_ip[4] = {0, }

Definition at line 199 of file dhcpv4.c.

Referenced by DHCPv4_run().

◆ DHCPv4_allocated_ip

uint8_t DHCPv4_allocated_ip[4] = {0, }

◆ DHCPv4_allocated_gw

uint8_t DHCPv4_allocated_gw[4] = {0, }

Definition at line 201 of file dhcpv4.c.

Referenced by getGWfromDHCPv4(), and parseDHCPv4MSG().

◆ DHCPv4_allocated_sn

uint8_t DHCPv4_allocated_sn[4] = {0, }

Definition at line 202 of file dhcpv4.c.

Referenced by getSNfromDHCPv4(), and parseDHCPv4MSG().

◆ DHCPv4_allocated_dns

uint8_t DHCPv4_allocated_dns[4] = {0, }

Definition at line 203 of file dhcpv4.c.

Referenced by getDNSfromDHCPv4(), and parseDHCPv4MSG().

◆ dhcpv4_state

int8_t dhcpv4_state = STATE_DHCPV4_INIT

◆ dhcpv4_retry_count

int8_t dhcpv4_retry_count = 0

Definition at line 207 of file dhcpv4.c.

Referenced by check_DHCPv4_timeout(), DHCPv4_run(), and reset_DHCPv4_timeout().

◆ dhcpv4_lease_time

uint32_t dhcpv4_lease_time = INFINITE_LEASETIME

Definition at line 209 of file dhcpv4.c.

Referenced by DHCPv4_run(), getDHCPv4Leasetime(), and parseDHCPv4MSG().

◆ dhcpv4_tick_1s

volatile uint32_t dhcpv4_tick_1s = 0

◆ dhcpv4_tick_next

uint32_t dhcpv4_tick_next = DHCPV4_WAIT_TIME

Definition at line 211 of file dhcpv4.c.

Referenced by check_DHCPv4_timeout(), and reset_DHCPv4_timeout().

◆ DHCPV4_XID

uint32_t DHCPV4_XID

Definition at line 213 of file dhcpv4.c.

Referenced by DHCPv4_init(), DHCPv4_run(), and makeDHCPV4MSG().

◆ pDHCPv4MSG

RIP_MSG* pDHCPv4MSG

◆ HOST_NAMEv4

uint8_t HOST_NAMEv4[] = DCHPV4_HOST_NAME

Definition at line 217 of file dhcpv4.c.

Referenced by send_DHCPv4_DISCOVER(), and send_DHCPv4_REQUEST().

◆ DHCPv4_CHADDR

uint8_t DHCPv4_CHADDR[6]

◆ dhcp_ipv4_assign

void(* dhcp_ipv4_assign) (void)
default

Definition at line 227 of file dhcpv4.c.

Referenced by DHCPv4_run(), and reg_dhcpv4_cbfunc().