Re: accept(2) call doesn't populate sockaddr struct with connecting address
Re: accept(2) call doesn't populate sockaddr struct with connecting address
- Subject: Re: accept(2) call doesn't populate sockaddr struct with connecting address
- From: Josh Graessley <email@hidden>
- Date: Fri, 08 Aug 2014 13:17:17 -0700
> On Aug 8, 2014, at 1:10 PM, Carl Hoefs <email@hidden> wrote:
>
> OS X 10.8.5
>
> I have a simple TCP socket app that a client app connects to. It works, data is transmitted & received, but on successful completion of the accept(2) call the connecting user's IP address does not get written into the supplied sockaddr struct, only zeros. (The connecting IP address is non-local to the machine running the code below.) Thus I have no idea who is connecting.
>
> The accept(2) man page says: "The argument 'struct sockaddr *restrict address' is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the address parameter is determined by the domain in which the communication is occurring."
>
> What's going wrong here?
>
> int listen_socket = -1;
> struct sockaddr_in client_address;
> short listen_port = 0;
> int on = 1;
>
> listen_socket = socket( AF_INET,SOCK_STREAM,IPPROTO_TCP );
> setsockopt( listen_socket,SOL_SOCKET,SO_REUSEADDR,(const char *)&on,sizeof(on) );
>
> client_address.sin_family = AF_INET;
> client_address.sin_addr.s_addr = INADDR_ANY;
> client_address.sin_port = htons(listen_port);
> bind( listen_socket,(struct sockaddr *)&client_address,sizeof(struct sockaddr_in) );
> listen( listen_socket,5 );
>
> user_socket = accept( listen_socket,&user_address,&user_addr_len );
>
> < break into the debugger after accept() completes >
>
>> p listen_socket
> listen_socket = 4
>> p user_socket
> user_socket = 5
>> p *user_address
> user_address = {
> sa_len = 0 '\0',
> sa_family = 0 '\0',
> sa_data = '\0' <repeats 13 times>
> }
>> p user_addr_len
> user_addr_len = 16
Before the call to accept, what is user_addr_len set to? How big (and what) is user_address? Have you considered the changes required to support IPv6?
-josh
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden