mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Dhcp.h Source File

Dhcp.h

00001 // DHCP Library v0.3 - April 25, 2009
00002 // Author: Jordan Terrell - blog.jordanterrell.com
00003 #ifndef Dhcp_h
00004 #define Dhcp_h
00005 
00006 #include "UIPUdp.h"
00007 
00008 /* DHCP state machine. */
00009 
00010 #define STATE_DHCP_START        0
00011 #define STATE_DHCP_DISCOVER     1
00012 #define STATE_DHCP_REQUEST      2
00013 #define STATE_DHCP_LEASED       3
00014 #define STATE_DHCP_REREQUEST    4
00015 #define STATE_DHCP_RELEASE      5
00016 
00017 #define DHCP_FLAGSBROADCAST     0x8000
00018 
00019 /* UDP port numbers for DHCP */
00020 
00021 #define DHCP_SERVER_PORT    67          /* from server to client */
00022 
00023 #define DHCP_CLIENT_PORT    68          /* from client to server */
00024 
00025 /* DHCP message OP code */
00026 
00027 #define DHCP_BOOTREQUEST    1
00028 #define DHCP_BOOTREPLY      2
00029 
00030 /* DHCP message type */
00031 
00032 #define DHCP_DISCOVER           1
00033 #define DHCP_OFFER              2
00034 #define DHCP_REQUEST            3
00035 #define DHCP_DECLINE            4
00036 #define DHCP_ACK                5
00037 #define DHCP_NAK                6
00038 #define DHCP_RELEASE            7
00039 #define DHCP_INFORM             8
00040 
00041 #define DHCP_HTYPE10MB          1
00042 #define DHCP_HTYPE100MB         2
00043 
00044 #define DHCP_HLENETHERNET       6
00045 #define DHCP_HOPS               0
00046 #define DHCP_SECS               0
00047 
00048 #define MAGIC_COOKIE            0x63825363
00049 #define MAX_DHCP_OPT            16
00050 
00051 #define HOST_NAME               "ENC28J"
00052 #define DEFAULT_LEASE           (900)   //default lease time in seconds
00053 
00054 #define DHCP_CHECK_NONE         (0)
00055 #define DHCP_CHECK_RENEW_FAIL   (1)
00056 #define DHCP_CHECK_RENEW_OK     (2)
00057 #define DHCP_CHECK_REBIND_FAIL  (3)
00058 #define DHCP_CHECK_REBIND_OK    (4)
00059 
00060 enum
00061 {
00062     padOption           = 0,
00063     subnetMask          = 1,
00064     timerOffset         = 2,
00065     routersOnSubnet     = 3,
00066 
00067     /* timeServer       =   4,
00068     nameServer      =   5,*/
00069     dns                 = 6,
00070 
00071     /*logServer     =   7,
00072     cookieServer        =   8,
00073     lprServer       =   9,
00074     impressServer       =   10,
00075     resourceLocationServer  =   11,*/
00076     hostName            = 12,
00077 
00078     /*bootFileSize      =   13,
00079     meritDumpFile       =   14,*/
00080     domainName          = 15,
00081 
00082     /*swapServer        =   16,
00083     rootPath        =   17,
00084     extentionsPath      =   18,
00085     IPforwarding        =   19,
00086     nonLocalSourceRouting   =   20,
00087     policyFilter        =   21,
00088     maxDgramReasmSize   =   22,
00089     defaultIPTTL        =   23,
00090     pathMTUagingTimeout =   24,
00091     pathMTUplateauTable =   25,
00092     ifMTU           =   26,
00093     allSubnetsLocal     =   27,
00094     broadcastAddr       =   28,
00095     performMaskDiscovery    =   29,
00096     maskSupplier        =   30,
00097     performRouterDiscovery  =   31,
00098     routerSolicitationAddr  =   32,
00099     staticRoute     =   33,
00100     trailerEncapsulation    =   34,
00101     arpCacheTimeout     =   35,
00102     ethernetEncapsulation   =   36,
00103     tcpDefaultTTL       =   37,
00104     tcpKeepaliveInterval    =   38,
00105     tcpKeepaliveGarbage =   39,
00106     nisDomainName       =   40,
00107     nisServers      =   41,
00108     ntpServers      =   42,
00109     vendorSpecificInfo  =   43,
00110     netBIOSnameServer   =   44,
00111     netBIOSdgramDistServer  =   45,
00112     netBIOSnodeType     =   46,
00113     netBIOSscope        =   47,
00114     xFontServer     =   48,
00115     xDisplayManager     =   49,*/
00116     dhcpRequestedIPaddr = 50,
00117     dhcpIPaddrLeaseTime = 51,
00118 
00119     /*dhcpOptionOverload    =   52,*/
00120     dhcpMessageType     = 53,
00121     dhcpServerIdentifier= 54,
00122     dhcpParamRequest    = 55,
00123 
00124     /*dhcpMsg           =   56,
00125     dhcpMaxMsgSize      =   57,*/
00126     dhcpT1value         = 58,
00127     dhcpT2value         = 59,
00128 
00129     /*dhcpClassIdentifier   =   60,*/
00130     dhcpClientIdentifier= 61,
00131     endOption           = 255
00132 };
00133 
00134 typedef struct _RIP_MSG_FIXED
00135 {
00136     uint8_t     op;
00137     uint8_t     htype;
00138     uint8_t     hlen;
00139     uint8_t     hops;
00140     uint32_t    xid;
00141     uint16_t    secs;
00142     uint16_t    flags;
00143     uint8_t     ciaddr[4];
00144     uint8_t     yiaddr[4];
00145     uint8_t     siaddr[4];
00146     uint8_t     giaddr[4];
00147     uint8_t     chaddr[6];
00148 } RIP_MSG_FIXED;
00149 
00150 class   DhcpClass
00151 {
00152 private:
00153     uint32_t        _dhcpInitialTransactionId;
00154     uint32_t        _dhcpTransactionId;
00155     uint8_t         _dhcpMacAddr[6];
00156     uint8_t         _dhcpLocalIp[4];
00157     uint8_t         _dhcpSubnetMask[4];
00158     uint8_t         _dhcpGatewayIp[4];
00159     uint8_t         _dhcpDhcpServerIp[4];
00160     uint8_t         _dhcpDnsServerIp[4];
00161     uint32_t        _dhcpLeaseTime;
00162     uint32_t        _dhcpT1, _dhcpT2;
00163     signed long     _renewInSec;
00164     signed long     _rebindInSec;
00165     signed long     _lastCheck;
00166     unsigned long   _timeout;
00167     unsigned long   _responseTimeout;
00168     unsigned long   _secTimeout;
00169     uint8_t         _dhcp_state;
00170     UIPUDP          _dhcpUdpSocket;
00171 
00172     int             request_DHCP_lease(void);
00173     void            reset_DHCP_lease(void);
00174     void            presend_DHCP(void);
00175     void            send_DHCP_MESSAGE(uint8_t, uint16_t);
00176     void            printByte(char* , uint8_t);
00177 
00178     uint8_t         parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
00179 public:
00180     IPAddress   getLocalIp(void);
00181     IPAddress   getSubnetMask(void);
00182     IPAddress   getGatewayIp(void);
00183     IPAddress   getDhcpServerIp(void);
00184     IPAddress   getDnsServerIp(void);
00185 
00186     int         beginWithDHCP(uint8_t* , unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
00187     int         checkLease(void);
00188 };
00189 #endif