uv_tcp_getsockname in uv_connection_cb

329 views
Skip to first unread message

Mike Mangelsdorf

unread,
Feb 10, 2014, 7:04:59 AM2/10/14
to li...@googlegroups.com
I'd like to retrieve address/port information for new connections using the following snippet:

(In my uv_connection_cb, after successful calls to uv_accept and uv_read_start)

int len;
char str[INET_ADDRSTRLEN];

struct sockaddr_in addr;
memset( &addr, 0, sizeof( struct sockaddr_in));

uv_tcp_getsockname( (uv_tcp_t*) server, (struct sockaddr*) &addr, &len);

inet_ntop( AF_INET, &addr.sin_addr.s_addr, str, INET_ADDRSTRLEN);  

if (addr.sin_family == AF_INET) printf( "AF_INET ");
printf("%s  %d\n", str, ntohs( addr.sin_port));

The server variable is the uv_tcp_t* that gets passed to uv_connection_cb and is the same pointer (I checked) that is initialized during uv_listen. The output of printf is all zeros. If I place the same statements just after the successful uv_listen call (in my main), the output of the printf is correct: AF_INET 127.0.0.1  3000.

What stumps me is that if I replace the uv_tcp_getsockname() call by a call to uv_tcp_getpeername(), that works and I get: AF_INET 127.0.0.1  56719.

I'm not sure what I'm doing wrong. If I hadn't looked at the source, I would assume uv_tcp_getsockname() was a stub...

Does anyone have an idea about this? Thanks!


Saúl Ibarra Corretgé

unread,
Feb 10, 2014, 8:05:47 AM2/10/14
to li...@googlegroups.com
Hi,
If you want to know the remote address of the connection you just
accepted you need to call uv_tcp_getpeername if the handle you passed to
uv_accept, not the server handle.

Can you share you full snipet?


Cheers,

--
Saúl Ibarra Corretgé
bettercallsaghul.com

Mike Mangelsdorf

unread,
Feb 10, 2014, 8:38:45 AM2/10/14
to li...@googlegroups.com
Thanks for your answer. I should have said more on this -- I did pass the client handle to the getpeername call. I only mentioned it to show that getpeername works fine with the client handle. As the server handle works fine with getsockname after uv_listen, this seems to only leave my way of dealing with "addr" as the culprit...

"server" is a global that gets passed into uv_listen and into uv_accept. The socket works - I'm running a successfull websocket protocol over it, it's just getsockname seems to tell me IP and Port are 0 on connect should be localhost:3000...

Mike Mangelsdorf

unread,
Feb 10, 2014, 8:50:22 AM2/10/14
to li...@googlegroups.com
 >>gets passed into uv_listen and into uv_accept.
Oops: uv_accept should read uv_connection_cb...

Mike Mangelsdorf

unread,
Feb 10, 2014, 11:18:43 AM2/10/14
to li...@googlegroups.com
I got it, the "int len" passed into uv_tcp_getsockname must be initialized. I thought that getsockname would do that based on that the socket is AF_INET.

int len = sizeof (struct sockaddr_in);
was required.

Saúl Ibarra Corretgé

unread,
Feb 10, 2014, 11:31:35 AM2/10/14
to li...@googlegroups.com
THat's correct. Sorry I didn't catch it in your first mail, but I'm glad
you sorted it out!

Mike Mangelsdorf

unread,
Feb 10, 2014, 11:34:38 AM2/10/14
to li...@googlegroups.com
Many thanks Saúl, I appreciate it!
Reply all
Reply to author
Forward
0 new messages