[network] `setSocketOption' fails with `Linger' option

Valery V. Vorotyntsev valery.vv at gmail.com
Mon Nov 30 09:49:13 EST 2009


See http://is.gd/57Ptu (Network.Socket.setSocketOption).

`setSocketOption' uses FFI binding to setsockopt(2), named
`c_setsockopt'.

> foreign import CALLCONV unsafe "setsockopt"
>   c_setsockopt :: CInt -> CInt -> CInt -> Ptr CInt -> CInt -> IO CInt

The binding treats OPTVAL (4th argument) as a pointer to `int'. This
is mostly fine but not when `SO_LINGER' option is being set. In the
latter case OPTVAL must be a pointer to `struct linger'. [1]

  [1] http://www.gnu.org/s/libc/manual/html_node/Socket_002dLevel-Options.html

Haskell programs trying to set SO_LINGER socket option will
successfully compile but fail in runtime.

    $ runhaskell socket-linger.hs || echo X
    $ runhaskell -DLINGER socket-linger.hs || echo X
    socket-linger.hs: setSocketOption: invalid argument (Invalid argument)
    X

    $ gcc -g -Wall -W -o /tmp/1 socket-linger.c && /tmp/1 || echo X
    $ gcc -g -DLINGER_AS_INT -Wall -W -o /tmp/1 socket-linger.c &&
/tmp/1 || echo X
    setsockopt: Invalid argument
    X

(See attached files `socket-linger.c', `socket-linger.hs'.)

The necessity of `SO_LINGER' option is controversial (see [2]).
I think it is better to comment the `Linger' constructor out of
Network/Socket.hsc.

  [2] http://www.developerweb.net/forum/archive/index.php/t-2982.html

Cheers.

-- 
vvv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: socket-linger.hs
Type: text/x-haskell
Size: 565 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/libraries/attachments/20091130/6221433c/socket-linger.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: socket-linger.c
Type: application/octet-stream
Size: 1307 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/libraries/attachments/20091130/6221433c/socket-linger.obj


More information about the Libraries mailing list