tags:

views:

99

answers:

3

Hello. I have seen applications send a packet through UDP to an IP on some port and receiving a response. Which port does the response go to? (Btw, how can the router know that the response is for my PC, if there are no ports forwarded to my PC?)

Greetings, Neo_b

A: 

What port a response is assigned is up to the application. UDP is completely stateless, so after firing off a packet the only way an application can expect a response is if it knows the other end is going to send one. Depending on the UDP application, I'd expect that the response would come on the same port for simplicity -- this is not the case for protocols like TCP, which have an intentionally random (and high) source port.

To answer your second question, many routers, even inexpensive home routers, do stateful packet inspection (SPI). Something like this likely happens, but I'm up for being corrected if I'm off:

[Set stage with client, router, Internet, server.]

  1. Client emits UDP packet.
  2. Router passes UDP packet to the Internet.
  3. Router remembers that client sent a UDP packet to server, and establishes a mapping in its memory.
  4. Server sends a UDP packet, probably on the same port.
  5. Router receives packet, and checks mapping to find client talked to server recently.
  6. Router passes packet to client.

How this is implemented is specific to the router, I'd imagine, but that's my understanding of how it works.

Jed Smith
A: 

When you create the UDP socket, you must bind it to a port number. If you dont, the operating system will assign an ephemeral port.

The application on the other side must know of this port. When replies are sent back, your router might not know how to route. There are 2 ways to resolve this problem

  1. You can explicitly configure a route to your computer on a particular port.
  2. You can configure your router to track the UDP connection by automatically opening a route to your computer when a particular packet is sent. UPNP protocol is based on this concept.
Andrew Keith
Thanks to you both for quick answers. I have compiled slightly modified versions of a UDP Client and Server. They work on my PC when I try to send a message to localhost on practically any ports I've tried. Although when I send a query to a server that is running on another PC in my LAN and listen for a response on the same port I sent the query on, I do not get a response (while when I'm using my own UnrealScript code, I somehow receive response).
Neo_b
A: 

If I send a message to a UDP port on another machine, whichever port I send the message from, no matter how it is selected, will appear in the UDP datagram. I would have thought that the remote end would send any response to that datagram to that source port.

I suppose the same applies even if ports are changed by firewall or NAT device, the remote end sees a datagram from a particular port and sends the reply back, the firewall/NAT device then translates that port to the original source port.

Tony van der Peet