Cannot port forward to Spark listening with TCPServer [SOLVED]

Hello,

I am very new to Spark, and I am having some trouble I could use help with.

I have flashed the following example sketch from documentation:

#include "application.h"

// Do not connect to the Cloud
SYSTEM_MODE(MANUAL);

const int LED = D0;

#define LISTEN_PORT 6666

TCPServer server(LISTEN_PORT);
TCPClient client;

void init_serial_over_usb() {
	digitalWrite(LED, HIGH);

	Serial.begin(9600);

	while (!Serial.available()) SPARK_WLAN_Loop(); 

	digitalWrite(LED, LOW);
}

void connect_to_wifi() {
	Serial.println("Connecting to WiFi...");
	WiFi.on();
	WiFi.connect();
	Serial.println("Connected.");

	Serial.println("Acquiring DHCP info:");
	while (!WiFi.ready()) SPARK_WLAN_Loop();
	Serial.print("SSID: ");	Serial.println(WiFi.SSID());
	Serial.print("IP: "); Serial.println(WiFi.localIP());
	Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
}

void setup() {
	pinMode(LED, OUTPUT);

	init_serial_over_usb();
	connect_to_wifi();

	// start listening for clients
	server.begin();
	Serial.print("Listening on "); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.println(LISTEN_PORT);
}

void loop() {
	if (client.connected()) {
		Serial.println("Client connected!");
		// echo all available bytes back to the client
		while (client.available()) {
			server.write(client.read());
			Serial.println("ECHO");
		}
	} else {
		// if no client is yet connected, check for a new connection
		client = server.available();
		Serial.println("Waiting for connection...");
	}
	delay(2000);
}

Here’s the network info printed to the serial terminal when Spark starts:

Connecting to WiFi...
Connected.
Acquiring DHCP info:
SSID: vulcan
IP: 192.168.7.121
Gateway: 192.168.7.1
Listening on 192.168.7.121:6666

The test program work fine, if I connect to it from a device on the same LAN. So something like this works well:

nc 192.168.7.121 6666

The connection succeeds and my input is echoed back to me. So far so good.

Next, I try to perform exactly the same test, but from a device on another LAN. In order to achieve this I configured the WiFi router to forward port 6666 to 192.168.7.121, which is reserved for the Spark’s MAC address. This does not work - the following simply times out:

nc 192.168.6.1 6666

In order to make sure that my port forward is correct, I changed the forward IP address to another Linux box on the same network as the Spark, and made sure that port forwarding is working properly with:

nc -l 6666

Switching the IP back to the Spark, once again leads to time outs.

So, I can successfully port forward to a Linux box, but not to Spark running TCPServer to listen for connections. Any idea what might be going wrong here?

I wonder if this might be related to this issue: https://community.spark.io/t/cannot-port-forward-to-spark-outside-local-network/5364

Any advice would be appreciated.

Val

If you port forward… it should be reached via the same IP address that the core is on am I right?

How is the network setup like?

Hi Kenneth,

Sorry, I am not sure what you are asking in the first sentence.

My network setup is as follows:

There are 2 networks in the house. First is a Gigabit switch wired network, with a Linux box (gatekeeper) acting as the router at 192.168.0.10. Gatekeeper has a lot of network cards in it, but the 2 we care about is the one for the LAN (192.168.0.10), and one for the WiFi router (192.168.6.2).

Second network is formed with a WiFi switch - that’s where the Spark lives.
WAN IP: 192.168.6.1
Gateway: 192.168.6.2 (this points to a network interface on gatekeeper)

DHCP is configured to assign addressed in 192.168.7.100+ range.

Gatekeeper has some iptables rules which take the traffic from the WiFi router (192.168.6.2) and forward it in such a way that wireless devices can connect to anything on the wired LAN or Internet.

B/c the WiFi router exposes only its own WAN IP address (192.168.6.1), accessing any of the wireless devices requires a port forward to get past the NAT.

So if the Spark has IP address of 192.168.7.121, I can create a mapping for port 6666 and access it at 192.168.6.1:6666. At least that’s how it worked with all other wireless devices I have used with this setup for about a decade. Spark is the first device I ever had trouble with.

This setup is documented in detail and with some pictures here: http://vace.homelinux.com/wiki/index.php/WIFI_Setup

Access credentials: wiki/wiki

I don’t know where you problem is but it is most likely in the NAT in the WiFi router. You even say in your wiki that this is the big problem with this setup.

Are you sure port 6666 is open on your wireless router? That port (IRC) is used by lots of old virus/trojan programs and is often blocked by default. You have to open it inbound and outbound on the D-Link router. The wireless router will not typically block outgoing ports but inbound traffic is a different story.

Can you try a different port number temporarily? Maybe one that is already in use for another device to test? Do your other devices on the wireless side that use port forwarding initiate an outbound connection or do they also wait for an inbound one like you want your Spark to?

You don’t say what the serial output of the Spark in the failed case–is it no output? That would mean that the forwarding isn’t working.

Can you sniff the wire with Wireshark or tcpdump on the wireless network? I would think that the packets never got there, but that will tell you for sure.

2 Likes

@bko You were totally right! It was a wireless router security configuration problem.

This is kind of embarrassing now, but the problem was that the last time I added a new wireless device was so long ago, that I forgot that I had MAC address filtering turned on! This is why my test worked when I directed the port forward to an old wifi connected laptop.

After adding Spark’s MAC address to the list, everything worked as expected.

Thanks!

2 Likes