Pages: [1]   Go Down
Author Topic: how to find out DHCP-server's IP-address? [SOLVED ...BUT]  (Read 869 times)
0 Members and 1 Guest are viewing this topic.
Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
« Last Edit: January 10, 2013, 06:14:26 am by JO3RI » Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The DHCP class has a public method for fetching the DHCP server IP address:
Code:
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:
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).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like:
Code:
#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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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? 
Logged


Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I figured out what files I had to change:

Ethernet.h
Ethernet.cpp

But this means, every one wanting to use this sketch, will have to change there Ethernet Library (I attached my files). Can't I just include these files into my sketch? I tried todo so, but the Arduino IDE complains about a conflict between the original files (in the library folder) and the ones in my sketch. Why?

OR maybe even better, Can't I submit my change? So that everyone can use this: finding out your dhcp-servers IP-address.

* Ethernet.h (1.2 KB - downloaded 12 times.)
* Ethernet.cpp (2.69 KB - downloaded 9 times.)
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46149
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
OR maybe even better, Can't I submit my change? So that everyone can use this: finding out your dhcp-servers IP-address.
Yes, you can. Do a search for SurferTim's name. He's posted details about the change requests he's submitted, including how and where.
Logged

Pages: [1]   Go Up
Jump to: