-
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
base: main
Are you sure you want to change the base?
Changes from 18 commits
2e09d49
97f093d
a26ddac
b419150
b81b9a8
864a742
e93f944
6029e39
2ada9c7
4505148
17bc361
82812a1
5a2a401
4e08267
8b13591
bc2a62c
928617d
61b737d
3cb0647
83bda02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/**************************************************************************/ | ||
/* */ | ||
/* 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> | ||
|
||
static value caml_unix_cst_to_constr(int n, int *tbl, int size, int deflt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could avoid this duplication by borrowing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed, somehow my brain remembered the function being static in first place. |
||
{ | ||
int i; | ||
for (i = 0; i < size; i++) | ||
if (n == tbl[i]) return Val_int(i); | ||
return Val_int(deflt); | ||
} | ||
|
||
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); | ||
CAMLlocal3(vres, v, 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) { | ||
v = caml_alloc_small(2, Tag_cons); | ||
Field(v, 0) = convert_addrinfo(r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates GC rule 5 - the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for spotting this, fixed it now |
||
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); | ||
} |
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.