Go Down

Topic: How to obtain the remote client IP address when using the Ethernet Shield (Read 24114 times) previous topic - next topic

pert

Why is this code not incorporated into the Ethernet library?  It's very short but quite helpful!
See: https://github.com/arduino/Arduino/pull/1700
As for why was this function not accepted in the pull request, I've read the associated discussion but still don't understand. I get that compatibility with the Client API is essential but why can't additional functions be added? If you want an easy way to add the remoteIP() function to the stock Ethernet library then you might find this library useful: https://github.com/Chris--A/EthernetClientEx
Note that remoteIP() in the pull request and the EthernetClientEx library as well as EthernetUDP all use an IPAddress return type instead of the "pass by reference" method.

fxfever

Hi! :)

So is it to be understood,that it is still not possible to use the remoteIP() in official releases on incoming TCP connections, and somewhere someone is still pondering if it is I good idea to implement it, but no one can tell where it lands?

And that there are ways to implement it yourself, but it was loosely discussed which way was best and that didn't got settled either ?

I'm puzzled !

Best regards

pert

So is it to be understood,that it is still not possible to use the remoteIP() in official releases on incoming TCP connections
Correct, you need to either modify your Ethernet library(as explained in this thread), install+use EthernetClientEx, or install a library that's already modified for you.

, and somewhere someone is still pondering if it is I good idea to implement it, but no one can tell where it lands?
From the discussion on the attempt to add this function to the stock Ethernet library it seems it was definitely decided against, though I don't understand why. Of course it's been a while so it might be reconsidered if someone brought it up with good reasoning why the previous decision is wrong.

there are ways to implement it yourself, but it was loosely discussed which way was best and that didn't got settled either ?
I use the IPAddress return type because that's how it was done in the official EthernetUDP remoteIP() and is more user friendly but the "pass by reference" method will work fine also.

fxfever

Thanx for the sum up !
Must do the work around then - since I have no idea how to get anyone to consider anything around here ! :)

krasimir

Add this to your sketch (do not change Ethernet library files):


extern "C" {
 #include "utility/w5100.h"
}

void getRemoteIP(EthernetClient *client, uint8_t *ptRemoteIP)
{
W5100.readSnDIPR(client->getSocketNumber(), ptRemoteIP);
}

..
uint8_t remoteIP[4];
getRemoteIP(client,remoteIP);
..

Banjo5

Thanks for this, it seems like a lot more civilized way of reaching in to see the remote IP address.
The get the result out I had to use:

getRemoteIP(&client,remoteIP);

rather than

getRemoteIP(client_1,remoteIP);


Add this to your sketch (do not change Ethernet library files):


extern "C" {
 #include "utility/w5100.h"
}

void getRemoteIP(EthernetClient *client, uint8_t *ptRemoteIP)
{
W5100.readSnDIPR(client->getSocketNumber(), ptRemoteIP);
}

..
uint8_t remoteIP[4];
getRemoteIP(client,remoteIP);
..

Stephen_Kurth

Hello all, this is my first post. I've been working to add this remote IP functionality to my project. All the suggestions here and elsewhere have not worked. My project uses an UNO with an Ethernet2 shield. The original Ethernet libray worked fine except for the remote IP functionality. So, I dowmloaded the Ethernet2 library from somewhere and it did not have any code pertaining to remote IP. So I started poking at it with a stick. I got it to work today, finally. Here's what I did:


Ethernet2 Library Modifications

Modifications made to two files:

...\Arduino\libraries\Ethernet2-1.0.4\src\ EthernetClient.cpp

I added the follow bit of code to the very end of the file:

   // Added by Stephen Kurth Feb 8, 2019
   // returns the remote IP address:
   IPAddress EthernetClient::getRemoteIP()
   {
      if (_sock >= MAX_SOCK_NUM) return IPAddress((uint32_t)0);
      uint8_t remoteIParray[4];
      SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
      w5500.readSnDIPR(_sock, remoteIParray);
      SPI.endTransaction();
      return IPAddress(remoteIParray);
   }

I originallly pasted a copy of this code from the same .cpp file in the original Ethernet Library. Had to change a bunch of stuff in it though to get it compile. The shown code compiled and worked.

...\Arduino\libraries\Ethernet2-1.0.4\src\ EthernetClient.h

I added this code to this file in this location:

   
  virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
  // added by Stephen Kurth Feb 8, 2019
  IPAddress getRemoteIP();

  friend class EthernetServer;

Most of the other lines start with virtual but I don't really know what it does. By that point I was just poking it with a stick and seeing if it would compile. It did, finally.

The code in my Arduino sketch is:

  client = server.available();  // try to get client

  if (client) // got client?
  {
    IPAddress remoteIp = client.getRemoteIP();
    Serial.print(F("Remote IP: "));
    Serial.println(remoteIp);
      boolean currentLineIsBlank = true;
      while (client.connected())
      {
          if (client.available()) // client data available to read
          {

I made all the library edits in Visual Studio 2017.

I just thought I'd share in case anyone else is trying to do this with the same hardware.

pert

The original Ethernet libray worked fine except for the remote IP functionality.
What problem did you encounter with the remote IP functionality?

Stephen_Kurth

Hello pert,

To be honest, I can't really say why none of the suggestions were working for me... It is entirely probable that I was overlooking something, or made a mistake somewhere... In hindsight, I should have paid more attention to the compiler errors. Having edited the Ethernet 2 library to add the remote IP function, I suppose I could've done the same for the original library...

However, I am using the Ethernet 2 shield in my project so I thought it best to concentrate on a library written specifically for the w5500 chip. I had run into this issue with another sketch (did not involve remote IP) where the original Ethernet library did not work but the Ehternet 2 library did ...

As I am rethinking this, I wonder if I could've added a w5500.h utility file to the original library?..

One thing, among many, concerned me with the original Ethernet library. The EthernetClient.h file has nothing in it except an include line: #include "Ethernet.h". That's it. I don't know if that's an issue or not but the same file in the Ethernet 2 library has a ton more stuff in it.

BTW, I downloaded the Ethernet 2 library from https://www.arduinolibraries.info/libraries/ethernet2

Sorry I can't give you a better answer, I'm just a guy poking at this with a stick. I'm amazed that I actually got this to work.

Stephen_Kurth

hello again, pert

I just remembered... I believe the compiler error I was getting with the original Ethernet library was something like: EthernetClicent:: no function remoteIP() to call ... or something to that effect..


Stephen

Stephen_Kurth

So I just swapped out the Ethernet2 library with the original Ethernet library and tried to compile my sketch. The error I get is:

'class EthernetClient' has no member named 'getRemoteIP'

Looking at the EthernetClient.cpp file I found that I was calling the wrong function. The function name is "remoteIP()" not "getRemoteIP()". There is no function named getRemoteIP(), which is what the compiler error message says! I wonder if i'd gotten mixed up somewhere along the line with the function name and the name of the instance of the EthernetClient class used to call that function?...

So I changed the sketch code to call remoteIP() and it compiled!

A classic beginner mistake I guess... but I did learn a lot about library files!

Stephen


Stephen_Kurth

So, there's a silver lining here to my blunder...

Since I got my sketch to work with the modified Ethernet2 library which is in my original post, my sketch has grown larger.

So having discovered this morning the that original Ethernet library remoteIP() does indeed work, I further discovered that my sketch is then too large for an UNO! So I swapped the EThernet2 library, with my mods, back in and it fit into the UNO at 93% of program storage space.

Same sketch, different Ethernet libraries.  I compiled my sketch with the Ethernet2 library for a Mega and it compiled just fine.

So, the silver lining is that all the bumbling about modifying the EThernet2 library was useful in the end because it all fits into my UNO...

Stephen

pert

However, I am using the Ethernet 2 shield in my project so I thought it best to concentrate on a library written specifically for the w5500 chip. I had run into this issue with another sketch (did not involve remote IP) where the original Ethernet library did not work but the Ehternet 2 library did ...

As I am rethinking this, I wonder if I could've added a w5500.h utility file to the original library?..
The latest 2.0.0 release of the official Arduino Ethernet library has been updated to automatically support the W5100, W5200, and W5500. Previous versions of the library did only work with the W5100 and that was why the Ethernet2 library was created.

One thing, among many, concerned me with the original Ethernet library. The EthernetClient.h file has nothing in it except an include line: #include "Ethernet.h". That's it. I don't know if that's an issue or not but the same file in the Ethernet 2 library has a ton more stuff in it.
That was an intentional decision by the person who updated the library. There was some discussion of it at the time which explained the reasoning, but I didn't find it after a quick search.

So I changed the sketch code to call remoteIP() and it compiled!

A classic beginner mistake I guess... but I did learn a lot about library files!
Good job finding the issue, and learning along the way!

So having discovered this morning the that original Ethernet library remoteIP() does indeed work, I further discovered that my sketch is then too large for an UNO! So I swapped the EThernet2 library, with my mods, back in and it fit into the UNO at 93% of program storage space.

Same sketch, different Ethernet libraries.  I compiled my sketch with the Ethernet2 library for a Mega and it compiled just fine.
Yes, this is the unfortunate thing about the updated Ethernet library. The increase in memory usage is pretty significant. I think some of that extra memory is used for the automatic detection of which Wiznet chip is being used. This change was really needed because it was creating a lot of problems by people assuming that the Ethernet library provided by Arduino would work with the W5500-based official products they were selling, but there are still a lot of people using the W5100 who expect the Ethernet library to continue to work for them. The person who updated the library definitely knows there stuff and I'm confident that the new code was written to the highest of standards but if it means the sketch will no longer fit in your Uno, none of that really matters. I do recommend that people use the Ethernet library if possible, as there is a lot of improvements in it beyond just the W5x00 support when compared with the Ethernet2 library. However, the Ethernet2 library should also work when you need it.

Go Up