0
votes

I have an Arduino Uno with Ethernet Shield as server, and I make requests on the Arduino through the Internet. I use two libraries to do it (Ethernet.h and SPI.h).

I want to check the client IP address, so I accept only HTTP requests from a known IP address (for example, 50.50.50.50) which is the static IP address in my office. How can I get the the client IP address on the Arduino?

3
What do you mean by the client IP address is "known"? Being able to be looked up in DNS?Peter Mortensen
What hardware do you use to connect to the Internet (name, vendor)? An Arduino Uno can not communicate with Internet without additional software.Peter Mortensen
What software/software libraries are you using (e.g. for the extra hardware and for the HTTP server)?Peter Mortensen

3 Answers

3
votes

have a look at the following, this works for TCP:

http://forum.arduino.cc/index.php?PHPSESSID=jh6t8omt7vrb8nget5c9j5dbk4&/topic,82416.0.html

The following is a quote from the author's post, I am just copying the excellent work:

To make it work, I did the following:

I added the following lines to the end of the EthernetClient.cpp file:

uint8_t *EthernetClient::getRemoteIP(uint8_t remoteIP[])
{
  W5100.readSnDIPR(_sock, remoteIP);
  return remoteIP;
}

I then added the following line (under the virtual void stop(); line)to the EthernetClient.h file:

uint8_t *getRemoteIP(uint8_t RemoteIP[]);//adds remote ip address

Finally I used the following code in my sketch to access the remote IP:

client.getRemoteIP(rip); // where rip is defined as byte rip[] = {0,0,0,0 };

to display the IP in the serial monitor, I used:

for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }
1
votes

I've done this using UDP, hopefully this will help you.

Get UDP.h from Google here: UDP.h

Code:

#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h> 

// *****  ETHERNET VARS *****
// MAC address and IP for arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,98};
unsigned int localPort = 8888;      // local port to listen on

// SenderIP and SenderPort are set when message is received
byte SenderIP[IP_LENGTH];        // holds received packet's originating IP
unsigned int SenderPort;        // holds received packet's originating port

// buffer for receiving data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
int packetSize = 0;

void setup() 
{
  Ethernet.begin(mac,ip);  //start Ethernet
  Udp.begin(localPort);    //start UDP
}

void loop()
{
  if(NewPortMessage())
  {
      // Do stuff, SenderIP is the IP where the UDP message was received from
  }
}

boolean NewPortMessage()
{
  packetSize = Udp.available();
  if(packetSize > 0)
  {
    packetSize -= 8; //subtract UDP 8-byte header
    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, SenderIP, SenderPort);
    return true;
  }
  clearPacketBuffer();
  return false;
}

void clearPacketBuffer()
{
  for(int i=0; i < packetSize; i++)
    packetBuffer[i] = 0;
}
0
votes

what about changing the approach? you could use SOA and you could make your arduino a web client instead of a web server.......then you could handle all of this restrictions in the web server that host your web service, this web service will be the core of your app, and this way you could call it from any mobile device you want :D

just an idea arduino web servers are not very useful, with this approach you could use the internet instead of using LAN only

good luck with your project