Pages: [1]   Go Down
Author Topic: Ethernet server with client´s IP address filter  (Read 1250 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello evereyone,

I´m using arduino with the ethernet shield and arduino webserver example. In this example, arduino reads client´s get requests and respond with some html page. My problem is, that I want to respond only for one defined client. In header of every packet of client´s get request is his source IP address. So, i would like to read this IP, compare with pre the predefined and if there is a match, then send the response...but i can´t find some function in the ethernet library to access packet header or specifically its source IP address. Is there someone, who knows, how to do this stuff ? Is there any additional library to work with packets ?

Every suggestion is welcomed. 
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I modified the IDE v1.0.2 ethernet library to retrieve the client ip. I attached the modified files to this post.
Ethernet.h
Ethernet.cpp
EthernetClient.h
EthernetClient.cpp

Save your original library files first! Then replace with these.

To get the client ip in your sketch:
Code:
 EthernetClient client = server.available();
  if(client) {
    Serial.print("Client IP: ");
    Serial.println(client.remoteIP());
  // rest of your loop code

edit: I put the comment "// SurferTim added" at each change in the files if you want to know what was changed.

If you want to restrict your remote IP to the 192.168.1.x subnet, this works:
Code:
    IPAddress ipBuf;   
    Serial.print("Client IP: ");
    ipBuf = client.remoteIP();
    Serial.println(ipBuf);

    if((ipBuf[0] == 192) && (ipBuf[1] == 168) && (ipBuf[2] == 1)) Serial.println("IP OK");
    else Serial.println("IP fail");
   

* Ethernet.h (1.23 KB - downloaded 52 times.)
* Ethernet.cpp (3.33 KB - downloaded 51 times.)
* EthernetClient.h (0.85 KB - downloaded 52 times.)
* EthernetClient.cpp (3.4 KB - downloaded 56 times.)
« Last Edit: December 01, 2012, 12:20:58 pm by SurferTim » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably a similar way to get the remote IP in the discussion below.

http://arduino.cc/forum/index.php/topic,82416.msg619420.html#msg619420

Code:
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte rip[4];
//byte rip[] = {0,0,0,0};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
client.getRemoteIP(rip);
for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }

Serial.println();
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
         
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat: It probably does if the ethernet library has been modified to include this function. This is not a function in the standard ethernet library.
Code:
client.getRemoteIP(rip);
There is really one way to get the remote IP in the W5100 code. All roads lead to Rome, so to speak.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much, it works as I wanted ... smiley
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat: It probably does if the ethernet library has been modified to include this function. This is not a function in the standard ethernet library.
Code:
client.getRemoteIP(rip);
There is really one way to get the remote IP in the W5100 code. All roads lead to Rome, so to speak.


it is interesting that the "rip" way I used only required two file mods and your way used four. Maybe the "ethernet" files actually call the "ethernetclient" files during the compile process. My code calls ethernet.h but I modified the ethernetclient files (and things seem to work).  I'm sure it all goes back to the programming of the w5100 chips and what is available there for use.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat: That was the first challenge. The IPAddress stuff is in Ethernet.h and Ethernet.cpp, and the socket number is in EthernetClient.h and EthernetClient.cpp. I make an intermediate call to the EthernetClient module to get the current socket number. I use the Ethernet module to be able to return an IPAddress type. Just a matter of convenience, I guess. It stays in convention with the other ip address functions.

For those who haven't figured out what this does, it is a miniature firewall.
« Last Edit: December 01, 2012, 01:57:43 pm by SurferTim » Logged

Pages: [1]   Go Up
Jump to: