• How can you get the remote TX port with a TCP connection?

    From Barry Margolin@21:1/5 to Kaz Kylheku on Sat Feb 20 17:59:47 2021
    In article <20210221090013.364@kylheku.com>,
    Kaz Kylheku <563-365-8930@kylheku.com> wrote:

    On 2021-02-21, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote:
    Hello

    Does anyone know how to get the remote TX port of a TCP connection using

    What's a "TX port"? TCP has one port number at each endpoint.

    the standard C sockets API on linux? Google is proving less than helpful.

    The sockets API has an ancient funtion called getpeername for obtaining
    the address of the remote endpoint.

    Using this API can often be avoided. If you're writing a server
    which accepts connections, the accept call gives you each peer's
    address.

    No it doesn't, it just returns the socket FD, and you then call
    getpeername(). You may be thinking of recvfrom(). This is normally used
    with UDP sockets, but I suppose it will also work with TCP.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to mickspud@potatofield.co.uk on Sun Feb 21 17:05:58 2021
    On 2021-02-21, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote:
    Hello

    Does anyone know how to get the remote TX port of a TCP connection using

    What's a "TX port"? TCP has one port number at each endpoint.

    the standard C sockets API on linux? Google is proving less than helpful.

    The sockets API has an ancient funtion called getpeername for obtaining
    the address of the remote endpoint.

    Using this API can often be avoided. If you're writing a server
    which accepts connections, the accept call gives you each peer's
    address. If you're writing a client which calls connect, of course it
    has the address.

    a parent (or via a fd passing mechanism) and needs to obtain that
    information about it. For instance, the inetd daemon accepts a
    connection and passes the open socket to a service. That service needs
    the IP address for filtering, logging, and so on.

    Since that is not passed on the command line, it has to be obtained
    using getpeername.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygna: Cygwin Native Application Library: http://kylheku.com/cygnal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mickspud@potatofield.co.uk@21:1/5 to All on Sun Feb 21 16:59:39 2021
    Hello

    Does anyone know how to get the remote TX port of a TCP connection using
    the standard C sockets API on linux? Google is proving less than helpful.

    Thanks in advance

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Barry Margolin on Sun Feb 21 18:29:41 2021
    On 2021-02-20, Barry Margolin <barmar@alum.mit.edu> wrote:
    In article <20210221090013.364@kylheku.com>,
    Kaz Kylheku <563-365-8930@kylheku.com> wrote:

    On 2021-02-21, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote:
    Hello

    Does anyone know how to get the remote TX port of a TCP connection using >>
    What's a "TX port"? TCP has one port number at each endpoint.

    the standard C sockets API on linux? Google is proving less than helpful. >>
    The sockets API has an ancient funtion called getpeername for obtaining
    the address of the remote endpoint.

    Using this API can often be avoided. If you're writing a server
    which accepts connections, the accept call gives you each peer's
    address.

    No it doesn't, it just returns the socket FD, and you then call getpeername(). You may be thinking of recvfrom(). This is normally used
    with UDP sockets, but I suppose it will also work with TCP.

    You may have forgotten that the prototype of accept (modulo possible
    "restrict" fluff) is:

    int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

    ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^

    New socket is returned directly; peer address via "out" parameters,
    which can be null if you don't care.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygna: Cygwin Native Application Library: http://kylheku.com/cygnal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mickspud@potatofield.co.uk@21:1/5 to Barry Margolin on Mon Feb 22 09:08:19 2021
    On Sat, 20 Feb 2021 17:59:47 -0500
    Barry Margolin <barmar@alum.mit.edu> wrote:
    No it doesn't, it just returns the socket FD, and you then call >getpeername(). You may be thinking of recvfrom(). This is normally used
    with UDP sockets, but I suppose it will also work with TCP.

    Turns out it does and thats what I used in the end.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mickspud@potatofield.co.uk@21:1/5 to Kaz Kylheku on Mon Feb 22 08:44:27 2021
    On Sun, 21 Feb 2021 17:05:58 -0000 (UTC)
    Kaz Kylheku <563-365-8930@kylheku.com> wrote:
    On 2021-02-21, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote: >> Hello

    Does anyone know how to get the remote TX port of a TCP connection using

    What's a "TX port"? TCP has one port number at each endpoint.

    I meant in the sense of the client port, not the server port.

    The sockets API has an ancient funtion called getpeername for obtaining
    the address of the remote endpoint.

    Thanks. Won't need it but useful info.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to mickspud@potatofield.co.uk on Mon Feb 22 15:35:41 2021
    On 2021-02-22, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote:
    On Sat, 20 Feb 2021 17:59:47 -0500
    Barry Margolin <barmar@alum.mit.edu> wrote:
    No it doesn't, it just returns the socket FD, and you then call >>getpeername(). You may be thinking of recvfrom(). This is normally used >>with UDP sockets, but I suppose it will also work with TCP.

    Turns out it does and thats what I used in the end.

    Right. getpeername can be seen as useful for situations where you have
    one place in the program which is doing the recv (like some low level
    I/O routine in a I/O library that is used by everything else). You
    don't want every recv call on a stream to be getting the address, which
    doesn't change over the life of a stream.

    You might want to know the peer's address right away without waiting for
    it to send data first, and for some reason you didn't get that from
    accept (because that happened in another module or even another
    separate program, which handed you a file descriptor).

    Knowing the peer's address before performing input can be useful. Say
    that some malicious IP address range is generating "honeypot" behaviors:
    peers from that range hold the connection open, but don't send any data.
    The user of your program would like to put in an IP-based rule block
    that address range but, oops, your program doesn't determine the remote
    IP until it reads data.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygna: Cygwin Native Application Library: http://kylheku.com/cygnal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jorgen Grahn@21:1/5 to mickspud@potatofield.co.uk on Mon Feb 22 21:24:28 2021
    On Mon, 2021-02-22, mickspud@potatofield.co.uk wrote:
    On Sun, 21 Feb 2021 17:05:58 -0000 (UTC)
    Kaz Kylheku <563-365-8930@kylheku.com> wrote:
    On 2021-02-21, mickspud@potatofield.co.uk <mickspud@potatofield.co.uk> wrote: >>> Hello

    Does anyone know how to get the remote TX port of a TCP connection using

    What's a "TX port"? TCP has one port number at each endpoint.

    I meant in the sense of the client port, not the server port.

    Side note: after the connection is made, the client/server distinction
    doesn't exist, either. Thus the "peer" in "getpeername".

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)