Tab
Tab

Reputation: 155

How to determine outgoing port in Python through NAT

I'm working on something that sends data from one program over UDP to another program at a known IP and port. The program at the known IP and port receives the message from the originating IP but thanks to the NAT the port is obscured (to something like 30129). The program at the known IP and port wants to send an acknowledgement and/or info to the querying program. It can send it back to the original IP and the obscured port #. But how will the querying program know what port to monitor to get it back on? Or, is there a way (this is Python) to say "send this out over port 3200 to known IP (1.2.3.4) on port 7000? That way the known IP/port can respond to port 30129, but it'll get redirected to 3200, which the querying program knows to monitor. Any help appreciated. And no, TCP is not an option.

Upvotes: 1

Views: 406

Answers (2)

Tab
Tab

Reputation: 155

Okay, I figured it out - the trick is to use the same sock object to receive that you used to send. At least in initial experiments, that seems to do the trick. Thanks for your help.

Upvotes: 1

user590028
user590028

Reputation: 11728

The simple answer is you don't care what the "real" (ie: pre-natted) port is. Just reply to the nat query and allow the nat to handling delivering the result. If you ABSOLUTELY have to know the source UDP port, include the information in your UDP packet -- but I strongly recommend against this.

Upvotes: 0

Related Questions