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!