Saturday, June 24th 2017, 12:29pm UTC+2

You are not logged in.

  • Login
  • Register

Date of registration: Nov 21st 2011

Posts: 4

1

Monday, November 21st 2011, 3:43pm

Socket descriptor 0 is invalid?

Porting a simple application for BSD sockets we faced the following problem:

the original code was an evergreen of socket programming:

C/C++ Source code

1
sh = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

this call returns a socket handle sh=0, which, according to embOS/IP documentation, is a valid handle, but subsequent calls to getsockopt() and setsockopt() return -1;

We then discovered that the same example works replacing the socket creation code with:

C/C++ Source code

1
sh = socket(AF_INET, SOCK_STREAM, 0);


We suggest to treat internally any non-zero value passed as third parameter like a passed "0" to ease portings, otherwise we'll need to modify all socket() calls.
Could you also clarify if sh=0 is a valid socket or not?

Thanks
Francesco

SEGGER - Sven

Super Moderator

Date of registration: Apr 13th 2010

Posts: 3

2

Tuesday, November 29th 2011, 12:15pm

Quoted

the original code was an evergreen of socket programming:

C/C++ Source code

1
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);


this call returns a socket handle sh=0, which, according to embOS/IP documentation, is a valid handle, but subsequent calls to getsockopt() and setsockopt() return -1;

We then discovered that the same example works replacing the socket creation code with:

C/C++ Source code

1
socket(AF_INET, SOCK_STREAM, 0);


We suggest to treat internally any non-zero value passed as third parameter like a passed "0" to ease portings, otherwise we'll need to modify all socket() calls.
Could you also clarify if sh=0 is a valid socket or not?

The returned socket handle is not valid. IPPROTO_TCP is an invalid value for the parameter Proto.
The embOS/IP manual says clearly that the parameter Proto must be set to zero (embOS/IP User Guide, page 105, parameter description for the function socket()).

I have changed the code of socket() to return SOCKET_ERROR if the parameter Proto is not zero.
Replacing invalid values internally is not a good solution, since an internal correction of the parameter can possibly cause further problems in the application.

The updated version will be ready by the end of this week.

Best regards,
Sven