Search code examples
pythonnat

how to know public ip and port behind NAT


I have a client behind NAT and a server who is accessible by this computer

something like this :

  local ip    external ip IPe
              external port Pe
C --------- | ----------------- S
           NAT

I want S to know C IPe and Pe used to transmit between those two

how can i do it ?

I'm using python


Solution

  • If S is the server then just do

     conn, addr = s.accept()
    

    Else if C doesn't know anything about S Here is a solution for you problem: You should use an intermediate server that will resolve the communication problem for you.

    1. C connect to the server and send his external port number (the IPe is automatically known by the server).
    2. The server store IPe and Pe.
    3. S ask the server for C and gets the IPe and the Pe.
    4. Then S can contact C by its own

    Requirement:

    • TCP connection
    • S and C are client
    • An intermediate server

    NOTE: the secret is that both C and S knows the server.