views:

84

answers:

1

Hello, I am browsing some source code which connects to a remote host on port P(using TCP). After the call to connect,(assuming successful), I would like to discover the client port of the connection (i.e the client port). In truth, I'm browsing the openssh source, in sshconnect.c, there is a function ssh_connect which calls timeout_connect. I have the remote host ip,port, local ip but would like to know the local (client) port after a successful connect.

I hope I have been clear and thank you for your answers Regards Sapsi

+3  A: 

Try feeding the client socket file descriptor to getsockname, something like so

struct sockaddr_in local_address;
int addr_size = sizeof(local_address);
getsockname(fd, &local_address, &addr_size);

Then you can pick apart the address structure for the IP and port.

JustJeff
Yes, I just found that out. Would have been nice to answer my own question.
Fishy