Remote Client IP

10 Aug 2009 . Edited: 31 Aug 2009

In the middle of my mbed ethernet server code I have this line to print out the local IP on my LCD:

 

  net->waitUntilReady();
  /* Wait for a NetLink */
  lcd.printf("IP: %hhu.%hhu.%hhu.%hhu\n", (net->getIPAddr().addr)&0xFF, (net->getIPAddr().addr>>8)&0xFF, (net->getIPAddr().addr>>16)&0xFF, (net->getIPAddr().addr>>24)&0xFF);
  

Can anyone tell me how to get the remote connected client IP so I can print that on my LCD also?  I'd like to log each connection to the server.

 

I've been searching the libraries for two hours so far with no answer.

Scott

10 Aug 2009

What kind of connection does you try to etablish?

There is no method to get a client IP adress implemented in the HTTPServer. Because a server object is able to handle multible client connections at the same time. Each connection is stored in its own class which is not accessable from outside of the httpserver object.

Rolf

There is no method to get a client IP adress implemented in the HTTPServer

I guess I'll have to make one then.

Scott

10 Aug 2009

Hi again,

I've thought about it over the day and think the right way to implement a log function would be as HTTPHandler like:

Core/TCPItem.h:

      ...
      void open();
      struct tcp_pcb *pcb() { return _pcb; }
    protected:
      ...

HTTPServer/HTTPLog.h

#ifndef HTTPLOG_H
#define HTTPLOG_H

#include "HTTPServer.h"

class HTTPLog : public HTTPHandler {
  public:
    HTTPLog(const char *prefix) : HTTPHandler(prefix) {
    }

  private:
    virtual HTTPHandle action(HTTPConnection *con) const {
      struct ip_addr ip = con->pcb()->remote_ip;
      printf("%hhu.%hhu.%hhu.%hhu %s %s", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF, (con->getType() == POST? "POST" : "GET"), con->getURL());
      return HTTP_AddFields;
    }
};

#endif

And main.cpp:

#include "HTTPLog.h"
...
  HTTPServer *http = new HTTPServer();
  http->addHandler(new HTTPLog("/"));
... // Other Handler

What do you think?

Rolf

The remote IP won't be available until after some packets are received right?

Sometime during this code execution:

NetServer::get()->poll();

10 Aug 2009

Yes.

Therefore I've registered a Handler which is able to write every request out to serial.

A handler is basically what answers your request. An rpc call (HTTPRPC), a static page in flash/ram (HTTPStaticPage) or from the local file system (HTTPFileSystem).

Because I've found it useful I've added a special type of Handler to modify request behaviours (HTTPFields). It enables you to modify HTTPHeaders from the answer. But they will not answer the request by them self.

All Handlers will be checked if the Path (first constructor argument) fits until one handler will return HTTP_Diliver. All checked Handler can modify the answer or check the request.

I've use this to get the remote IP. Unfortunately the remote IP is only reachable in the lwip tcp_pcb therefore it is not ideal to access it directly. but possible with the changes in TCPItem.h

It works well Rolf,

Thanks.

Scott