1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
6 /*                                                                        */
7 /*   Copyright 2004 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 #include <string.h>
17 #include <caml/mlvalues.h>
18 #include <caml/alloc.h>
19 #include <caml/fail.h>
20 #include <caml/memory.h>
21 #include <caml/signals.h>
22 #include "unixsupport.h"
23 
24 #if defined(HAS_SOCKETS) && defined(HAS_IPV6)
25 
26 #include "socketaddr.h"
27 #ifndef _WIN32
28 #include <sys/types.h>
29 #include <netdb.h>
30 #endif
31 
32 static int getnameinfo_flag_table[] = {
33   NI_NOFQDN, NI_NUMERICHOST, NI_NAMEREQD, NI_NUMERICSERV, NI_DGRAM
34 };
35 
unix_getnameinfo(value vaddr,value vopts)36 CAMLprim value unix_getnameinfo(value vaddr, value vopts)
37 {
38   CAMLparam0();
39   CAMLlocal3(vhost, vserv, vres);
40   union sock_addr_union addr;
41   socklen_param_type addr_len;
42   char host[4096];
43   char serv[1024];
44   int opts, retcode;
45 
46   get_sockaddr(vaddr, &addr, &addr_len);
47   opts = caml_convert_flag_list(vopts, getnameinfo_flag_table);
48   caml_enter_blocking_section();
49   retcode =
50     getnameinfo((const struct sockaddr *) &addr.s_gen, addr_len,
51                 host, sizeof(host), serv, sizeof(serv), opts);
52   caml_leave_blocking_section();
53   if (retcode != 0) caml_raise_not_found(); /* TODO: detailed error reporting? */
54   vhost = caml_copy_string(host);
55   vserv = caml_copy_string(serv);
56   vres = caml_alloc_small(2, 0);
57   Field(vres, 0) = vhost;
58   Field(vres, 1) = vserv;
59   CAMLreturn(vres);
60 }
61 
62 #else
63 
unix_getnameinfo(value vaddr,value vopts)64 CAMLprim value unix_getnameinfo(value vaddr, value vopts)
65 { caml_invalid_argument("getnameinfo not implemented"); }
66 
67 #endif
68