Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getsockopt: incorrect return values #3489

Closed
vicuna opened this issue Jul 31, 2002 · 2 comments
Closed

getsockopt: incorrect return values #3489

vicuna opened this issue Jul 31, 2002 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jul 31, 2002

Original bug ID: 1282
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hi,

The following program terminates producing an incorrect result of !SO_KEEPALIVE
but will terminate correctly if the 'if ...' is modified to 'if (getsockopt sd
SO_KEEPALIVE)':


open Unix;;

exception Fatal of string;;

let sd =
try socket PF_INET SOCK_STREAM 6
with Unix_error(e,s1,s2) ->
raise(Fatal("Error:"^(error_message(e))^s1^s2))
in
let opt = setsockopt sd SO_KEEPALIVE true
in
if (getsockopt sd SO_KEEPALIVE) = true
then print_endline("SO_KEEPALIVE")
else print_endline("!SO_KEEPALIVE");;

Here is an strace of the running program:


socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
setsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
getsockopt(3, SOL_SOCKET, SO_KEEPALIVE, [8], [4]) = 0
write(1, "!SO_KEEPALIVE\n", 14!SO_KEEPALIVE
) = 14

I believe the problem is due to tha fact that the Unix getsockopt can return
any non-zero integer for true (hence the [8] in the strace output). The
getsockopt implementation calls getsockopt_int and returns a Val_int(..)
though I believe it should return a Val_bool(..) to explictely map numbers > 1
to true.

This probably means implementing getsockopt as:


CAMLprim value unix_getsockopt_bool(value socket, value option) {
return Val_bool(Int_val(getsockopt_int(sockopt_bool, socket, SOL_SOCKET,
option)));
}

Regards,
Steve

@vicuna
Copy link
Author

vicuna commented Jul 31, 2002

Comment author: administrator

The following program terminates producing an incorrect result of
!SO_KEEPALIVE but will terminate correctly if the 'if ...' is
modified to 'if (getsockopt sd SO_KEEPALIVE)':
I believe the problem is due to tha fact that the Unix getsockopt
can return any non-zero integer for true (hence the [8] in the
strace output). The getsockopt implementation calls getsockopt_int
and returns a Val_int(..) though I believe it should return a
Val_bool(..) to explictely map numbers > 1 to true.

You're entirely right. This will be fixed in the next release.

Thanks for the bug report.

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Jul 31, 2002

Comment author: administrator

Fixed as suggested 2002-07-30 by XL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant