views:

81

answers:

2

Hey everyone,

This question sort of covers both ServerFault.com and here, but this seems more programming than server. I set up my home computer to run Apache server. It answers to port 5900, which has been port forwarded from my wireless router. I then set up a Dynamic DNS server to continually update what the IP address of my home network is. I know that part works, because I used a different computer on a different wireless network, and was able to access my server's index page using MYURL.com:5900.

My goal is to now send a message to my home server. I've written a script on my home server, where if I send it a POST message, it will save that message to a file. In other words, the series of events should go like this:

  1. I log on to my web page, write text in a Input, and hit a send button
  2. The message gets passed to my web site's server.
  3. My server runs a script that uses CURL to send the message as a POST to my home server's DDNS
  4. The server at home takes the post, and runs a script which writes it to a file.

I know how to do 1,2 and 4. I've been trying to get 3 to work and can't. I can't even get CURL to read my home server's index.html. Here's the code I've used with CURL (Using PHP):

    $string = 'http://MYURL.com';

    echo "sending to " . $string . '<br/>';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PORT, 5900);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $response = curl_exec($ch);

    if(curl_error($ch))
    {
        echo curl_error($ch);
    }
    echo $response;

    curl_close($ch);

I tested this with google.com, and the default port, and it worked fine. It echoed the html given to me by google. When I run it on my home server's DNS, however, it always times out. When I don't add a time out, it said it couldn't connect. This is using the exact same URL that, when put into a browser, correctly sends me my home server's index.html.

Does anyone know what's going wrong? Also, if there was a better way to do this, what is it?

I know this was a long question, so thank you so much.

Ethan

+4  A: 

From where are you testing this? Most networks these days are heavily firewalled and disallow outgoing connections to strange ports. Your port 5900 certainly qualifies as strange.

The reason why your test using the wireless (I'm assuming 3G?) network works is because that PC/laptop happens to be connected directly to the internet and is therefore not blocked by a firewall.

The easiest way to test the firewall theory is to set up a test page on your server on port 80 and try accessing it using CURL. If that works then you know the problem is someone along the chain of routers and proxies is blocking your port 5900.


A little bit on why port 80 isn't considered "strange":

Port 80 is the port conventionally used for HTTP (web server) as assigned by IANA. As such it is usually not blocked considering that the primary reason people connect to the internet is to access the World Wide Web (which runs on HTTP and thus require port 80 to be open). Blocking port 80 makes the internet effectively useless (well, not quite, you still have email). If a sysadmin ever blocks port 80 he might as well disconnect the internet. Which is why by default port 80 is not blocked.

Now, what does this mean to you, the home-server-admin. Of course, your IP address only has one port 80. This means there can only really be one port-forwarding rule attached to port 80. This means that there can only be one computer on your local network serving web pages to the outside world*. If you little brother or sister wants to run another web server then he/she's going to have to use another port.

Which is why web servers are designed to server multiple web pages (they detect the URL so the single port 80 can server different sites). It's to allow a single IP address to server different web sites from a single machine. Google: http virtual host for more info.

*note: Not quite true, you can use a load balancing or re-routing proxy to redirect the HTTP request to other machines on the network. But the principle is still true. Only one machine's port 80 can be directly exposed to the internet. The others are just proxied.

slebetman
5900 is not strange - it's the standard port of VNC
Maerlyn
To a sysadmin VNC is strange (not normal). Non-strange ports are the usual suspects: 80, 443, 53, 21 etc. Windows sysadmins even consider port 22 strange.
slebetman
I've never really done this stuff before. I just picked 5900 randomly because it was unused. This is my wireless router at home. From what I understand 80 is the default TCP/IP port. Wouldn't port forwarding 80 to my laptop mean no one else could use port 80, and the internet, on that wireless router?
Ethan
No, port 80 is NOT the default TCP/IP port. Port 80 is the conventional HTTP (web server) port just like port 110 is the conventional POP3 (email) port. Everyone on the internet expects web servers to be on port 80. Almost all websites use port 80 (or 443 if they use HTTPS). I'll elaborate on this in my answer.
slebetman
I got it working! Apparently the server my site is hosting on blocks port 5900 as you said. I really appreciate it! The one thing I don't understand is this: Suppose I port forward 80 to my computer. Then my roommate tries to use Firefox. He sends his request out port 80 to google.com's port 80. Google then sends the response back to our port 80. Wouldn't the response then get forwarded to me? Wouldn't that make it so he couldn't use the WWW anymore?
Ethan
The request to the server is sent to port 80 (which the server is listening to). The server sends the response to the port on the requesting machine from which the request was sent. This could be any (non-well-known, currently not in use) port. In other words, the requesting machine picks a port, and in the tcp section of the request "tells" the server to respond to that port. Port 80 is used for the request, not the response.
GZipp
As mentioned by GZipp, that's not how TCP/IP works. Firefox, and in fact all client software, don't, in fact shouldn't, use IANA assigned ports. They use a randomly selected port (to get a random port open port zero) from the range of private ports (49152 through 65535). Once the randomly selected port is open it is then connected to port 80 on the server side. A TCP/IP connection is identified by 4 things: source port (clients use random port), source address (client IP address), destination port (servers use IANA assigned port depending on protocol) and destination address (server IP).
slebetman
@GZipp: It's not that port 80 is not used for response. It is (but on a different socket from the listening socket). It's the fact that the port on the client side doesn't need to be port 80.
slebetman
And another thing. Since a TCP/IP connection is identified by the four: source port/address, destination port/address, A request made by you on port 80 to Google can be differentiated from a request made by you to Stackoverflow. But if you are sharing a connection with your roommate, Google will see both your requests coming from the same machine: your router. It is your router (which can tell the requests apart because you and your roomate have different private IP) that splits up the connection on your side. See: http://en.wikipedia.org/wiki/Network_address_translation
slebetman
I stand by my comment. When I said port 80 is not used for the response, I meant for receipt of the packets coming from the server (which is the issue here). The receiving machine dictates the port to which all packets from the remote server will be sent, whether through a NAT device or not. A router may present a different quasi-random port to the internet, but will re-translate to the port chosen by the requesting machine upon packet receipt.
GZipp
@GZipp: You're still getting it wrong. Each IP packet involves not only two IP addresses but also two ports. Using terms like receiving and sending will confuse you because both mahchines receive and send packets. Better to talk about source/destination or client/server. If you use ethereal or wireshark and look at the actual IP packet you will see that both the random port number *AND* port 80 are there. For packets going from client to server the destination port field in the packet will have the number 80, for the other direction you will see 80 in the source port.
slebetman
@GZipp continued: You're thinking in terms of some port xxx or port 80 so in your view port 80 is not used since you think port xxx is used. In reality **both** port xxx and port 80 are used so in reality port 80 is used.
slebetman
@slebetman - Well, thanks for paraphrasing my comments. I've been talking about request/response explicitly (which you call source/destination or client/server, although I don't see how they can be used interchangeably. Request and response are the terms used in the RFCs). I'll boil it down again for you: the HTTP request goes to port 80, the response to port xxx.
GZipp
@GZipp: OK, so we're talking at different levels here. You're thinking Application Layer while I was thinking Transport Layer. Anyway, yes the response goes to port xxx but it comes from port 80. Like I said, the packet contains both port xxx **and** port 80. It's how an IP connection is defined and how the router knows how to differentiate one connection from another. Leaving out this detail and saying that the router does it by **magic** does not aid in understanding how TCP/IP work.
slebetman
@slebetman - Sorry, I guess I lost track of the shifting topic. I was talking about the questioner's concern that an HTTP response wouldn't find its way to the correct machine because of port 80 forwarding. And thanks for reminding me that Wireshark, which I use almost daily, was years ago called Ethereal. But where the hell do you get off saying that I said the router does it by magic? Read my first comment again, then come back and talk to the wind.
GZipp
A: 

Sometime the router prevent you to access your external IP adress from the internal network.

Try to telnet your own external IP and access the 5900 port from your internal network, you'll see if it's the case.

Colin Hebert
Wouldn't my test where I used a different computer, IP address, and wireless router cover this test? I was able to access my home network using the browser. Isn't the browser sending essentially the same GET request?
Ethan
You were, but from another network, just try to access your external IP from your internal network and see if it works.
Colin Hebert