Thanks to Sandeep Mistry, who addressed the issue of being unable to obtain the remote IP of a client connected to the WiFi101 shield, acting as a server.
The issue is detailed at:
IPAddress rip; // remote IP;
long rport; // remote port
// ************* Remote IP & port callback ******************************************
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;
rip = pstrAccept->strAddr.sin_addr.s_addr;
rport = pstrAccept->strAddr.sin_port;
Serial.print(F("\nRemote connection from: "));
Serial.print(rip); Serial.print(":"); Serial.println(rport);
}
}
// ************* End Remote IP callback ******************************************
void setup(){
// ...
// after connecting to the wifi network
delay(1000);
// overide the original callbacks
registerSocketCallback(mySocketBufferCb, myResolve_cb);
// start the server:
server.begin();
}
void loop() {
// wait for a new client:
WiFiClient client = server.available();
if (client){
// remote ip is now available in global variable rip
// remote port is available in rport
// Log the client if required
// ...
}
Each time a client connects the callback routine intercepts
u8Msg == SOCKET_MSG_ACCEPT
(There are a number of other messages- see socket.h in library)
The Remote IP and remote port are sent to the serial monitor and the global variables updated.
I have used the code to update a client log on my server.
It is also possible to reject the client by comparing rip against a file of bad client IP's.
David