Go Down

Topic: how to find out DHCP-server's IP-address? [SOLVED ...BUT] (Read 1 time) previous topic - next topic

JO3RI

Nov 22, 2012, 10:31 am Last Edit: Jan 10, 2013, 12:14 pm by JO3RI Reason: 1
When getting an IP-address from an dhcp-server, you'll get a localIP, subnetMask, gatewayIP, dnsServerIP.

But is there a way to know the dhcp-server's IP address? something like dhcpServerIP ?

johnwasser

The DHCP class has a public method for fetching the DHCP server IP address:
Code: [Select]

IPAddress DhcpClass::getDhcpServerIp();


Unfortunately for you the Ethernet class keeps it's pointer to the DHCP object (_dhcp) private.  If it didn't you could call:

Code: [Select]

IPAddress dhcpAddress = Ethernet._dhcp->getDhcpServerIp();


You can edit Ethernet.h to make that pointer public but your code would not be portable.

You could do your own DHCP request and pass the results to Ethernet.begin().  That would be the supported way.  Just copy the code from EthernetClass::begin(uint8_t *mac_address) and when you have the DHCP results, call Ethernet.begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

JO3RI

Thanks,

I do understand what you're saying, but let's say I'm happy using Ethernet.h and I only want to do a DHCP request to fetch the server's IP, not more

How would one do that?
obviously I would only need a small part?

(I'm going to try to find out myself too)

johnwasser

Something like:
Code: [Select]

#include "Dhcp.h"
  IPAddress DhcpServerIp;
  DhcpClass *dhcp = new DhcpClass();

  // Now try to get our config info from a DHCP server
  int ret = dhcp->beginWithDHCP(mac_address);
  if(ret == 1)
  {
    DhcpIPServerAddress = dhcp-> getDhcpServerIp()
  }


Because this is a separate DHCP object from the one Ethernet is using the DHCP server you find may be different from the one that Ethernet found.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

JO3RI

Quote
Because this is a separate DHCP object from the one Ethernet is using the DHCP server you find may be different from the one that Ethernet found.


Yes, I came to that conclusion myself too. And it is not the kind of behaviour I want, if I get an IP, I most now for sure what dhcp client it came from. So your first post, is the way to solve the problem.

I'll just have to include the changed ethernet.h within the sketch and that should make it portable, doesn't it? 

Go Up