-
Notifications
You must be signed in to change notification settings - Fork 71
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
Expose getaddrinfo errors. Was: Return empty list on EAI_ errors for getaddrinfo@luv (#351) #352
Open
haesbaert
wants to merge
20
commits into
ocaml-multicore:main
Choose a base branch
from
haesbaert:getaddrinfo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2e09d49
Return empty list on EAI_ errors for getaddrinfo@luv (#351)
haesbaert 97f093d
works
haesbaert a26ddac
EINVAL on bad EAI
haesbaert b419150
Move things to net and implement libuv
haesbaert b81b9a8
Move getaddrinfo stubs into its own file due to copyrights
haesbaert 864a742
zap gai_strerror bindings, copy strings from libc
haesbaert e93f944
Add Net.getaddrinfo_to_string
haesbaert 6029e39
Add EAI_PROTOCOL and don't be sloppy with defines
haesbaert 2ada9c7
restore eio_stubs properly now that we moved getaddrinfo
haesbaert 4505148
whitespace
haesbaert 17bc361
EAI_PROTOCOL WHOOPS
haesbaert 82812a1
Add EAI_NONAME tests
haesbaert 5a2a401
EAI_OVERFLOW
haesbaert 4e08267
Document semantics in net.mli
haesbaert 8b13591
Better paranoia
haesbaert bc2a62c
We can't really guarantee the error code since it depends on the envi…
haesbaert 928617d
fix tests
haesbaert 61b737d
zap duplicate
haesbaert 3cb0647
Just extern the declaration so we don't need to copy cst_to_constr.
haesbaert 83bda02
Address dra27 point #2, many thanks again !
haesbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/**************************************************************************/ | ||
/* */ | ||
/* OCaml */ | ||
/* */ | ||
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt, */ | ||
/* Christiano Haesbaert, Tarides */ | ||
/* Copyright 2004 Institut National de Recherche en Informatique et */ | ||
/* en Automatique. */ | ||
/* Copyright 2022 Tarides */ | ||
/* */ | ||
/* All rights reserved. This file is distributed under the terms of */ | ||
/* the GNU Lesser General Public License version 2.1, with the */ | ||
/* special exception on linking described in the file LICENSE. */ | ||
/* */ | ||
/**************************************************************************/ | ||
|
||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
|
||
#include <errno.h> | ||
#include <netdb.h> | ||
|
||
#include <caml/mlvalues.h> | ||
#include <caml/memory.h> | ||
#include <caml/alloc.h> | ||
#include <caml/unixsupport.h> | ||
#include <caml/socketaddr.h> | ||
|
||
extern value caml_unix_cst_to_constr(int, int *, int, int); | ||
extern int caml_unix_socket_domain_table[]; /* from socket.c */ | ||
extern int caml_unix_socket_type_table[]; /* from socket.c */ | ||
|
||
static value convert_addrinfo(struct addrinfo * a) | ||
{ | ||
CAMLparam0(); | ||
CAMLlocal3(vres,vaddr,vcanonname); | ||
union sock_addr_union sa; | ||
socklen_param_type len; | ||
|
||
len = a->ai_addrlen; | ||
if (len > sizeof(sa)) len = sizeof(sa); | ||
memcpy(&sa.s_gen, a->ai_addr, len); | ||
vaddr = caml_unix_alloc_sockaddr(&sa, len, -1); | ||
vcanonname = caml_copy_string(a->ai_canonname == NULL ? "" : a->ai_canonname); | ||
vres = caml_alloc_small(5, 0); | ||
Field(vres, 0) = | ||
caml_unix_cst_to_constr(a->ai_family, caml_unix_socket_domain_table, 3, 0); | ||
Field(vres, 1) = | ||
caml_unix_cst_to_constr(a->ai_socktype, caml_unix_socket_type_table, 4, 0); | ||
Field(vres, 2) = Val_int(a->ai_protocol); | ||
Field(vres, 3) = vaddr; | ||
Field(vres, 4) = vcanonname; | ||
CAMLreturn(vres); | ||
} | ||
|
||
/* glibc doesn't define a bunch of EAI_, so fake one since code gets copied around */ | ||
|
||
#ifndef EAI_ADDRFAMILY | ||
#define EAI_ADDRFAMILY (-3000) | ||
#endif /* EAI_ADDRFAMILY */ | ||
|
||
#ifndef EAI_BADHINTS | ||
#define EAI_BADHINTS (-3013) | ||
#endif /* EAI_BADHINTS */ | ||
|
||
#ifndef EAI_NODATA | ||
#define EAI_NODATA (-3007) | ||
#endif /* EAI_NODATA */ | ||
|
||
#ifndef EAI_OVERFLOW | ||
#define EAI_OVERFLOW (-3009) | ||
#endif /* EAI_OVERFLOW */ | ||
|
||
#ifndef EAI_PROTOCOL | ||
#define EAI_PROTOCOL (-3014) | ||
#endif /* EAI_PROTOCOL */ | ||
|
||
static int gai_errors[] = { | ||
EAI_ADDRFAMILY, | ||
EAI_AGAIN, | ||
EAI_BADFLAGS, | ||
EAI_BADHINTS, | ||
EAI_FAIL, | ||
EAI_FAMILY, | ||
EAI_MEMORY, | ||
EAI_NODATA, | ||
EAI_NONAME, | ||
EAI_OVERFLOW, | ||
EAI_PROTOCOL, | ||
EAI_SERVICE, | ||
EAI_SOCKTYPE, | ||
EAI_SYSTEM /* NOTE: must be last */ | ||
}; | ||
|
||
#define nmemb_gai_errors (sizeof(gai_errors) / sizeof(int)) | ||
|
||
CAMLprim value caml_eio_getaddrinfo(value vnode, value vserv, value vopts) | ||
{ | ||
CAMLparam3(vnode, vserv, vopts); | ||
CAMLlocal4(vres, v, e, vret); | ||
char * node, * serv; | ||
struct addrinfo hints; | ||
struct addrinfo * res, * r; | ||
int retcode, i; | ||
|
||
if (! (caml_string_is_c_safe(vnode) && caml_string_is_c_safe(vserv))) | ||
CAMLreturn (Val_emptylist); | ||
|
||
/* Extract "node" parameter */ | ||
if (caml_string_length(vnode) == 0) { | ||
node = NULL; | ||
} else { | ||
node = caml_stat_strdup(String_val(vnode)); | ||
} | ||
/* Extract "service" parameter */ | ||
if (caml_string_length(vserv) == 0) { | ||
serv = NULL; | ||
} else { | ||
serv = caml_stat_strdup(String_val(vserv)); | ||
} | ||
/* Parse options, set hints */ | ||
memset(&hints, 0, sizeof(hints)); | ||
hints.ai_family = PF_UNSPEC; | ||
for (/*nothing*/; vopts != Val_emptylist; vopts = Field(vopts, 1)) { | ||
v = Field(vopts, 0); | ||
if (Is_block(v)) | ||
switch (Tag_val(v)) { | ||
case 0: /* AI_FAMILY of socket_domain */ | ||
hints.ai_family = caml_unix_socket_domain_table[Int_val(Field(v, 0))]; | ||
break; | ||
case 1: /* AI_SOCKTYPE of socket_type */ | ||
hints.ai_socktype = caml_unix_socket_type_table[Int_val(Field(v, 0))]; | ||
break; | ||
case 2: /* AI_PROTOCOL of int */ | ||
hints.ai_protocol = Int_val(Field(v, 0)); | ||
break; | ||
} | ||
else | ||
switch (Int_val(v)) { | ||
case 0: /* AI_NUMERICHOST */ | ||
hints.ai_flags |= AI_NUMERICHOST; break; | ||
case 1: /* AI_CANONNAME */ | ||
hints.ai_flags |= AI_CANONNAME; break; | ||
case 2: /* AI_PASSIVE */ | ||
hints.ai_flags |= AI_PASSIVE; break; | ||
} | ||
} | ||
/* Do the call */ | ||
caml_enter_blocking_section(); | ||
retcode = getaddrinfo(node, serv, &hints, &res); | ||
caml_leave_blocking_section(); | ||
if (node != NULL) caml_stat_free(node); | ||
if (serv != NULL) caml_stat_free(serv); | ||
/* Convert result */ | ||
vres = Val_emptylist; | ||
if (retcode == 0) { | ||
for (r = res; r != NULL; r = r->ai_next) { | ||
e = convert_addrinfo(r); | ||
v = caml_alloc_small(2, Tag_cons); | ||
Field(v, 0) = e; | ||
Field(v, 1) = vres; | ||
vres = v; | ||
} | ||
vret = caml_alloc_small(1, 0); /* 0 = Ok */ | ||
Field(vret, 0) = vres; | ||
freeaddrinfo(res); | ||
} else { | ||
for (i = 0; i < nmemb_gai_errors; i++) | ||
if (gai_errors[i] == retcode) | ||
break; | ||
/* Paranoia keeps the world spinning */ | ||
if (i == nmemb_gai_errors) { | ||
errno = EINVAL; | ||
i = gai_errors[nmemb_gai_errors - 1]; /* EAI_SYSTEM */ | ||
} | ||
vret = caml_alloc_small(1, 1); /* 1 = Error */ | ||
Field(vret, 0) = Val_int(i); | ||
} | ||
|
||
CAMLreturn(vret); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to avoid LGPL code in Eio (it's currently BSD and ISC only). Though they might relicense this bit if asked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gasche @xavierleroy (sorry for the ping).
I've copied significant part of
otherlibs/unix/getaddrinfo.c
into EIO (https://github.com/ocaml-multicore/eio/blob/61b737d5be683b9be611844b16c7e73b5bece09a/lib_eio_linux/getaddrinfo_stubs.c). The original file is LGPLed and we would like to relicense our new file to ISC.Do you permit us to relicense the bits we copied out from LGPL to ISC in EIO ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I only use copyleft licenses for my free software. Please consider using the LGPL with OCaml linking exception for your library, or do not reuse any of my code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, will do one of the two as suggested.