Discussion:
How can you get the remote TX port with a TCP connection?
(too old to reply)
m***@potatofield.co.uk
2021-02-21 16:59:39 UTC
Permalink
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
Kaz Kylheku
2021-02-21 17:05:58 UTC
Permalink
Post by m***@potatofield.co.uk
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.
Post by m***@potatofield.co.uk
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
Barry Margolin
2021-02-20 22:59:47 UTC
Permalink
Post by Kaz Kylheku
Post by m***@potatofield.co.uk
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.
Post by m***@potatofield.co.uk
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, ***@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Kaz Kylheku
2021-02-21 18:29:41 UTC
Permalink
Post by Barry Margolin
Post by Kaz Kylheku
Post by m***@potatofield.co.uk
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.
Post by m***@potatofield.co.uk
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
m***@potatofield.co.uk
2021-02-22 09:08:19 UTC
Permalink
On Sat, 20 Feb 2021 17:59:47 -0500
Post by Barry Margolin
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.
Kaz Kylheku
2021-02-22 15:35:41 UTC
Permalink
Post by m***@potatofield.co.uk
On Sat, 20 Feb 2021 17:59:47 -0500
Post by Barry Margolin
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
m***@potatofield.co.uk
2021-02-22 08:44:27 UTC
Permalink
On Sun, 21 Feb 2021 17:05:58 -0000 (UTC)
Post by Kaz Kylheku
Post by m***@potatofield.co.uk
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.
Post by Kaz Kylheku
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.
Jorgen Grahn
2021-02-22 21:24:28 UTC
Permalink
Post by m***@potatofield.co.uk
On Sun, 21 Feb 2021 17:05:58 -0000 (UTC)
Post by Kaz Kylheku
Post by m***@potatofield.co.uk
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 .
Loading...