DHCP basic setup and communication?

#1

I have the WIZ550io that has the W5500 chip. I want to connect to the network to be able to send HTTP requests to a server. The only problem is that the data sheet doesn’t talk about how to setup DHCP to get the Gateway IP Address, Subnet Mask Address, and Source IP Address that I need to write to the common register during setup. I read through the dhcp.h, and dhcp.c files found on the wiki, but I don’t understand exactly what is going on. Can anyone give me the most basic steps to connect to the network as a dhcp client? Thanks so much!!!

#2

Hi,

I will try to describe how to use DHCP application.
If you cannot understand, reply.
I explain based on ioLibrary with CoIDE project at WIZwiki.

Step 1.
Register DHCP timer handler.

[code]void SysTickIntHandler(void)
{
msTicks++; /* increment counter necessary in Delay()*/

////////////////////////////////////////////////////////
// SHOULD BE Added DHCP Timer Handler your 1s tick timer
if(msTicks % 1000 == 0)	DHCP_time_handler();
////////////////////////////////////////////////////////

}[/code]
SysTickIntHandler is MCU timer handler. This handler is dependent on MCU. So you should refer to your MCU vendor datasheet.

msTicks is increased every 1ms and DHCP timer wants to increase every 1sec. So DHCP_time_handler() called every 1sec by if(msTicks % 1000 == 0) DHCP_time_handler();

Step 2.

You can see structure gWIZNETINFO.

wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd}, .ip = {192, 168, 1, 123}, .sn = {255,255,255,0}, .gw = {192, 168, 1, 1}, .dns = {0,0,0,0}, .dhcp = NETINFO_DHCP };
You concern only MAC address, so set MAC address.
And then run setSHAR function.

Then MAC address setting is done.

Step 3.
Run DHCP_init function and register callback function.

(It uses number 0 socket)

Step 4.
Run main loop

/* Main Loop */ while(1) { switch(DHCP_run()) { case DHCP_IP_ASSIGN: case DHCP_IP_CHANGED: /* If this block empty, act with default_ip_assign & default_ip_update */ // // This example calls my_ip_assign in the two case. // // Add to ... // break; case DHCP_IP_LEASED: // // TO DO YOUR NETWORK APPs. // break; case DHCP_FAILED: /* ===== Example pseudo code ===== */ // The below code can be replaced your code or omitted. // if omitted, retry to process DHCP my_dhcp_retry++; if(my_dhcp_retry > MY_MAX_DHCP_RETRY) { #ifdef _MAIN_DEBUG_ printf(">> DHCP %d Failed\r\n", my_dhcp_retry); #endif my_dhcp_retry = 0; DHCP_stop(); // if restart, recall DHCP_init() network_init(); // apply the default static network and print out netinfo to serial } break; default: break; }
The main loop has DHCP_run() function. This function get IP, gateway, subnet. And then you concern only return value.
If main loop is success, return value is DHCP_IP_LEASED.
And IP, gateway, subbet is set already so just design your application in case DHCP_IP_LEASED:

Thanks.

DHCP with ioLibrary not connecting
#3

Thanks!

So I’m trying to understand the lowest level right now. Could you help me? Not sure if the general steps are:

  1. Set MAC address in common register
  2. Setup socket n ???UDP mode???
  3. Send DHCP requests (not sure which ones I need to send, in what order, and what the format of the messages are)
  4. Read DHCP response (???does this response have the IP address, Gateway Address, and Subnet included???)

I’m very new at this, but I don’t want to use the prewritten files from Wiznet. Any help would be greatly appreciated!

#4

It’s no shame to look and understand the prewritten files.
Consider them as an instructions manual.
When you’ll understand them, you’ll be able to write your own code.

#5
  1. Right
  2. Open any socket number with UDP mode.
    And then DHCP phases are run.

Refer to WIKI~ :smiley:

http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol

There is detailed explanation.

Also refer to WIZnet W5100 application note.
https://www.wiznet.co.kr:8011/UpLoad_Files/ReferenceFiles/W5100_Appnote_DHCP.zip

#6

So this is what I have…I send the message, but never receive a response. Maybe my message is in the wrong format…please let me know if you see anything that I may be doing wrong (ps-I know this isn’t the prettiest code, but I’m working on it :smiley:) Thanks!!!

Here’s my DHCP setup:

	uint8_t Port[2] = {0x00, 0x44};
	unsigned int Source_Port[2] = {0x0004, 0x0005};
	ethernet_continuous_send(Port, Source_Port, 2, socket); //Sets 68 as the source port

	ethernet_send(0x02, 0x001F, socket, 1); //sets socket TX buffer to 2kb
	ethernet_send(0x02, 0x001E, socket, 1); //sets socket RX buffer to 2kb

	uint8_t DIPR[4] = {0xFF, 0xFF, 0xFF, 0xFF}; //Destination IP = 255.255.255.255
	unsigned int DIPR_address[4] = {0x000C, 0x000D, 0x000E, 0x000F};
	ethernet_continuous_send(DIPR, DIPR_address, 4, socket);

	uint8_t DPORT[2] = {0x00, 0x43}; //Destination Port = 67
	unsigned int DPORT_address[2] = {0x0010, 0x0011};
	ethernet_continuous_send(DPORT, DPORT_address, 2, socket);

	uint8_t SIPR[4] = {0x00, 0x00, 0x00, 0x00}; //Source IP address = 0.0.0.0
	unsigned int SIPR_address[4] = {0x000F, 0x0010, 0x0011, 0x0012};
	ethernet_continuous_send(SIPR, SIPR_address, 4, COMMON);

	socket_command(socket, OPEN);
	delay(100);
	
	while (ethernet_receive(SOCK_STATUS, socket) != SOCK_UDP);
	Println("SOCKET(UDP) OPENED");

	DHCP_discover();
	socket_send(socket, dhcp_msg, DHCP_MSG_SIZE);

Here’s the DHCP DISCOVER message I make…

uint8_t XID[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t MAC[6] = {0x00, 0x08, 0xDC, 0x1E, 0x00, 0x6F}; //Source Hardware = MAC Address
uint8_t MAGIC_COOKIE[4] = {0x63, 0x82, 0x53, 0x63}; //99.130.83.99

void DHCP_discover()
{
	int i=0;
	while (i < DHCP_MSG_SIZE)
		dhcp_msg[i++] = 0x00;

	i=0;
	dhcp_msg[i++] = 0x01; //Operation Code = 1 (client sending request to server)
	dhcp_msg[i++] = 0x01; //Hardware Type = 1 (Ethernet 10 Mb)
	dhcp_msg[i++] = 0x06; //Hardware Address Length = 6 (MAC address length)
	dhcp_msg[i++] = 0x00; //Hops = 0 (for client)
	dhcp_msg[i++] = XID[0];//Transaction ID
	dhcp_msg[i++] = XID[1];
	dhcp_msg[i++] = XID[2];
	dhcp_msg[i++] = XID[3];

	i=27;
	dhcp_msg[i++] = MAC[0];
	dhcp_msg[i++] = MAC[1];
	dhcp_msg[i++] = MAC[2];
	dhcp_msg[i++] = MAC[3];
	dhcp_msg[i++] = MAC[4];
	dhcp_msg[i++] = MAC[5];

	i=236;
	dhcp_msg[i++] = MAGIC_COOKIE[0];
	dhcp_msg[i++] = MAGIC_COOKIE[1];
	dhcp_msg[i++] = MAGIC_COOKIE[2];
	dhcp_msg[i++] = MAGIC_COOKIE[3];

	dhcp_msg[i++] = 53; //Option code 53 = DHCP message type
	dhcp_msg[i++] = 0x01; //byte length of option
	dhcp_msg[i++] = 0x01; //DHCP Discover message

	dhcp_msg[i++] = 61; //Option code 61 = Client ID
	dhcp_msg[i++] = 0x07;
	dhcp_msg[i++] = 0x01;
	dhcp_msg[i++] = MAC[0];
	dhcp_msg[i++] = MAC[1];
	dhcp_msg[i++] = MAC[2];
	dhcp_msg[i++] = MAC[3];
	dhcp_msg[i++] = MAC[4];
	dhcp_msg[i++] = MAC[5];

	dhcp_msg[i++] = 12; //Option code 12 = Host name
	dhcp_msg[i++] = 0x06;
	dhcp_msg[i++] = 0x57; //'W'
	dhcp_msg[i++] = 0x49; //'I'
	dhcp_msg[i++] = 0x5A; //'Z'
	dhcp_msg[i++] = 0x6E; //'n'
	dhcp_msg[i++] = 0x65; //'e'
	dhcp_msg[i++] = 0x74; //'t'
	dhcp_msg[i++] = 0x5C; //'\'
	dhcp_msg[i++] = 0x30; //'0'

	dhcp_msg[i++] = 55; //Option code 55 = Parameter Request
	dhcp_msg[i++] = 0x06;
	dhcp_msg[i++] = 0x01; //subnet mask
	dhcp_msg[i++] = 0x03; //Routers on Subnet
	dhcp_msg[i++] = 0x06; //DNS
	dhcp_msg[i++] = 0x0F; //15 - Domain Name
	dhcp_msg[i++] = 0x3A; //DHCP values???
	dhcp_msg[i++] = 0x3B;

	dhcp_msg[i++] = 0xFF; //255 - End Option
}
#7

I want to know your discover message by packet capture(like wireshark).

Plz, attach files.

Thanks.