Socket #0

Hello,

I’ve been doing some work with the conn API for a TCP stack that I’ve been developing. I noticed that on line 238 of posix_sockets.c (https://github.com/RIOT-OS/RIOT/blame/master/sys/posix/sockets/posix_sockets.c#L238), in the socket_close() function, the comparison to test whether a socket number can be closed is:

if ((unsigned)(socket - 1) > (SOCKET_POOL_SIZE - 1)) {

return -1; |

  • |

}

I noticed that if the socket number is 0, then socket_close() will always fail. Is socket 0 a special socket that can’t be closed? The problem I’m having is that a call to socket() sometimes returns 0, and I’m unable to close that socket due to this issue.

Sam Kumar

Hi Sam, can you try with PR #6004 which ports POSIX sockets from conn to sock? There are still some kinks in this PR, but I plan to fix them until the release. On another note: the conn implementation of sockets was never tested with TCP (since there was no TCP implementation then), but feel free to test (and hopefully fix) any issues in that PR.

Cheers, Martine

[1] https://github.com/RIOT-OS/RIOT/pull/6004

Hi Martine,

I will work on testing it with sock and PR #6004, but it may take some time since I might need to port the interface from conn to sock.

It seems to me that conn is deprecated and that sock is its replacement moving forward. Am I correct? And what is the motivation for moving away from conn?

Sam

Hi Sam,

Hello,

I finally got around to porting my implementation to use sock instead of conn. I am running into a similar issue now. It seems this check is still in sock (see https://github.com/RIOT-OS/RIOT/blame/master/sys/posix/sockets/posix_sockets.c#L237).

I think that the check should be:

assert(0 <= socket && socket < _ACTUAL_SOCKET_POOL_SIZE);

but perhaps there is some reasoning behind the original check that I am not aware of. Could someone more familiar with the sock code please take a look?

Thanks,

Sam

Hi Sam, you are right. `assert(((unsigned)socket) < (_ACTUAL_SOCKET_POOL_SIZE));` should be the right one... with `0 <= socket` as you gave it only 0 and negative socket ids would be allowed which is definetly allowed ;-). I provided a PR to fix that [1]. If you spot any more errors don't be afraid to provide your own PR to fix them.

Cheers, Martine

[1] https://github.com/RIOT-OS/RIOT/pull/6761