Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A call to get the remote IP in Server or AP mode would be very useful ! #40

Closed
drp0 opened this issue Mar 2, 2016 · 7 comments

Comments

Projects
None yet
2 participants
@drp0
Copy link

commented Mar 2, 2016

This would help considerably with server security.
David

@drp0

This comment has been minimized.

Copy link
Author

commented Jul 18, 2016

I raised this way back in March?
It would be very surprising if Amtel had omitted to include a call to obtain the remote IP of the attached client in server/AP mode.
The lack of a library call to access the remote IP is a significant shortfall!

@sandeepmistry

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2016

Hi @drp0,

There is no API in the Atmel SDK to get the remote IP for a socket. However, when a server socket accepts a new connection the IP address and port of the client is provided via an event. So, it is possible to store this value for each socket. However, this would require 4 extra bytes per socket for each of the 7 TCP sockets available. If we also wanted to include the remote socket port, this would require another 2 bytes of storage per socket.

This would also introduce fragmentation to API's since the Arduino Client interface does not include support for retrieving the remote IP address of a connection. So, code that uses the new MKR1000 WiFiClient capabilities could not be used with other Client based network libraries.

@drp0

This comment has been minimized.

Copy link
Author

commented Jul 19, 2016

Hello Sandeep,
I understand the byte requirement.
I am using a mega so can absorb the increased code size.
Which library routine could be used to intercept the event? I would appreciate the opportunity of investigating the code potential.
David

@sandeepmistry

This comment has been minimized.

Copy link
Contributor

commented Sep 6, 2016

Hi @drp0,

Here's an example sketch of how to intercept the event:

#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)


int status = WL_IDLE_STATUS;

WiFiServer server(23);

// new callbacks /////////////////////////////////////////////////////////
static void myResolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
{
  WiFi._resolve = hostIp;
}

void mySocketBufferCb(SOCKET sock, uint8 u8Msg, void *pvMsg)
{
  socketBufferCb(sock, u8Msg, pvMsg); // call the original callback

  if( u8Msg == SOCKET_MSG_ACCEPT) {
      tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg *)pvMsg;

      Serial.println("Accepted connection from: ");
      IPAddress ip = pstrAccept->strAddr.sin_addr.s_addr;
      Serial.print(ip);
      Serial.print(":");
      Serial.print(pstrAccept->strAddr.sin_port);
      Serial.println();
  }
}
///////////////////////////////////////////////////////////////////////////

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // override the original callbacks
  registerSocketCallback(mySocketBufferCb, myResolve_cb);

  // start the server:
  server.begin();
}


void loop() {
  // wait for a new client:
  WiFiClient client = server.available();

  // ...


}

I'm going to close this issue for now.

@drp0

This comment has been minimized.

Copy link
Author

commented Sep 7, 2016

Hello Sandeep,

This works perfectly.

I can access the remote IP (and port) by storing them in global variables within the callback function:

then monitor the IP following an if client call……..

WiFiClient client = server.available();

if (client) {

……..

The client can be logged (and rejected if the ip is on a bad caller list)

Exactly as required.

It would be worth releasing this on the WIFI101 forum.

Thank you,

David

From: Sandeep Mistry [mailto:notifications@github.com]
Sent: 06 September 2016 22:07
To: arduino-libraries/WiFi101
Cc: drp0; Mention
Subject: Re: [arduino-libraries/WiFi101] A call to get the remote IP in Server or AP mode would be very useful ! (#40)

Hi @drp0 https://github.com/drp0 ,

Here's an example sketch of how to intercept the event:

#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(23);

// new callbacks /////////////////////////////////////////////////////////
static void myResolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
{
WiFi._resolve = hostIp;
}

void mySocketBufferCb(SOCKET sock, uint8 u8Msg, void *pvMsg)
{
socketBufferCb(sock, u8Msg, pvMsg); // call the original callback

if( u8Msg == SOCKET_MSG_ACCEPT) {
tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg *)pvMsg;

  Serial.println("Accepted connection from: ");
  IPAddress ip = pstrAccept->strAddr.sin_addr.s_addr;
  Serial.print(ip);
  Serial.print(":");
  Serial.print(pstrAccept->strAddr.sin_port);
  Serial.println();

}
}
///////////////////////////////////////////////////////////////////////////

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);

}

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// override the original callbacks
registerSocketCallback(mySocketBufferCb, myResolve_cb);

// start the server:
server.begin();
}

void loop() {
// wait for a new client:
WiFiClient client = server.available();

// ...

}

I'm going to close this issue for now.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #40 (comment) , or mute the thread https://github.com/notifications/unsubscribe-auth/ANHAGrXoqGNrGAiuak1jZcFie_ABRmaaks5qndYOgaJpZM4HnPWM . https://github.com/notifications/beacon/ANHAGkVsQFkIbv91pUcID0sdFCNxb0jqks5qndYOgaJpZM4HnPWM.gif

@sandeepmistry

This comment has been minimized.

Copy link
Contributor

commented Sep 7, 2016

Hi @drp0,

Thanks for the feedback. Feel free to post a link to this Github issue on the forum.

@sandeepmistry

This comment has been minimized.

Copy link
Contributor

commented Jan 2, 2018

New API's for this have been added in pull request #207, the changes have been merged and will be released shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.