0

I have a PHP application which is attempting to open a socket connection to a printer through my router.

A couple things to note - my networking skills are lacking so I may be missing something obvious or this may not even be possible in the way I want it to be. That being said, I have done a bunch of searching on the issue and can't seem to find anyone with this exact issue (just lots of people trying to SSH remotely). I also know that the device I am trying to connect to is working as intended because I have a small nodejs application which successfully opens a connection to it on the local network. And finally I am using Docker to run the PHP application locally, which may be relevant to my problem (though I know the connection is reaching the router due to the logs).

The code is simple:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_TCP, SO_DEBUG, 1);

$server_connect = socket_connect($socket, "router_public_ip", 9100);

Within my router I setup that port to be forwarded: enter image description here

When I attempt to connect PHP throws socket_connect(): unable to connect [111]: Connection refused and the router logs show that a connection failed.

I noticed within the router logs that the start port is random and tried changing my port forwarding configuration to: enter image description here

This gives me the same result and my router does not give me much info to go on (as to why the connection failed, was it blocked?). This is all that is shown in the logs:

enter image description here

Edit: The printer config utility menu does not seem to mention anything about a firewall or remote connections. enter image description here

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11
EvilZebra
  • 103
  • 4
  • Are you trying to connect to 192.168.1.150:9100? You should use the IP address of the router. – Gerard H. Pille Jan 10 '22 at 14:37
  • @GerardH.Pille - no, I am trying to connect to the router's public IP address. I believe the router automatically directs me to that local address due to the port forward having an internal IP address set? – EvilZebra Jan 10 '22 at 14:40
  • OK. The start port being random (ephemere IIRC) is the way it works. Now, is the printer listening on 9100 and accepting external connections? The "start port" should be 9100 external and internal. – Gerard H. Pille Jan 10 '22 at 14:42
  • The printer is listening (as I mention in my post I can successfully connect, and print, to the device using a local nodejs script) and I don't think it differentiates between internal/external connections. – EvilZebra Jan 10 '22 at 14:50
  • Can you make sure? Currently I don't see what else it could be. I'll check your PHP code. – Gerard H. Pille Jan 10 '22 at 15:08
  • I am 100% certain. My original PHP code sends details to nodejs, nodejs connects to printer locally through socket and prints details. New PHP code is trying to connect to socket directly (and remotely). If I use old code - printer works, use new code - errors. – EvilZebra Jan 10 '22 at 15:17
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/133066/discussion-between-evilzebra-and-gerard-h-pille). – EvilZebra Jan 10 '22 at 15:40
  • That doesn't prove the printer accepts external connections. The PHP you show is OK, I can connect to my printer using the external address, but my printer hasn't got any "firewall" settings. The HP direct is active (listening on port 9100) or not. – Gerard H. Pille Jan 10 '22 at 15:42
  • @GerardH.Pille - as per our discussion in chat I tested out your latest ideas and it turned out to be the Default Gateway! According to a Google search the device must have the default gateway set to be able to communicate remotely. So I set that to `192.168.1.1` and all is well. Feel free to throw up an answer to this question so I can accept it and get you your karma. – EvilZebra Jan 11 '22 at 16:27

1 Answers1

1

Then I wonder about the 0.0.0.0 gateway in the printer's network config.

It would mean that your printer can connect to any IP address directly. Under normal circumstances, a system will only be able to communicate directly with other systems in its subnet (see the mask). For wider reach it'll need to go through a gateway.

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11