net_db.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /* "net_db.c" network database support
  2. Copyright 1995-2001,2006,2009-2013,2018
  3. Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. /* Written in 1994 by Aubrey Jaffer.
  17. * Thanks to Hallvard.Tretteberg@si.sintef.no for inspiration and discussion.
  18. * Rewritten by Gary Houston to be a closer interface to the C socket library.
  19. * Split into net_db.c and socket.c.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include <verify.h>
  25. #include <errno.h>
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netdb.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. #include "boolean.h"
  35. #include "dynwind.h"
  36. #include "feature.h"
  37. #include "gsubr.h"
  38. #include "list.h"
  39. #include "modules.h"
  40. #include "numbers.h"
  41. #include "pairs.h"
  42. #include "socket.h"
  43. #include "strings.h"
  44. #include "symbols.h"
  45. #include "throw.h"
  46. #include "vectors.h"
  47. #include "net_db.h"
  48. #if defined (HAVE_H_ERRNO)
  49. /* Only wrap gethostbyname / gethostbyaddr if h_errno is available. */
  50. #if defined HAVE_HSTRERROR && !HAVE_DECL_HSTRERROR
  51. /* Some OSes, such as Tru64 5.1b, lack a declaration for hstrerror(3). */
  52. extern const char *hstrerror (int);
  53. #endif
  54. SCM_SYMBOL (scm_host_not_found_key, "host-not-found");
  55. SCM_SYMBOL (scm_try_again_key, "try-again");
  56. SCM_SYMBOL (scm_no_recovery_key, "no-recovery");
  57. SCM_SYMBOL (scm_no_data_key, "no-data");
  58. static void scm_resolv_error (const char *subr, SCM bad_value)
  59. {
  60. #ifdef NETDB_INTERNAL
  61. if (h_errno == NETDB_INTERNAL)
  62. {
  63. /* errno supposedly contains a useful value. */
  64. scm_syserror (subr);
  65. }
  66. else
  67. #endif
  68. {
  69. SCM key;
  70. const char *errmsg;
  71. switch (h_errno)
  72. {
  73. case HOST_NOT_FOUND:
  74. key = scm_host_not_found_key;
  75. errmsg = "Unknown host";
  76. break;
  77. case TRY_AGAIN:
  78. key = scm_try_again_key;
  79. errmsg = "Host name lookup failure";
  80. break;
  81. case NO_RECOVERY:
  82. key = scm_no_recovery_key;
  83. errmsg = "Unknown server error";
  84. break;
  85. case NO_DATA:
  86. key = scm_no_data_key;
  87. errmsg = "No address associated with name";
  88. break;
  89. default:
  90. scm_misc_error (subr, "Unknown resolver error", SCM_EOL);
  91. errmsg = NULL;
  92. }
  93. #ifdef HAVE_HSTRERROR
  94. errmsg = (const char *) hstrerror (h_errno);
  95. #endif
  96. scm_error (key, subr, errmsg, SCM_BOOL_F, SCM_EOL);
  97. }
  98. }
  99. /* Should take an extra arg for address format (will be needed for IPv6).
  100. Should use reentrant facilities if available.
  101. */
  102. SCM_DEFINE (scm_gethost, "gethost", 0, 1, 0,
  103. (SCM host),
  104. "@deffnx {Scheme Procedure} gethostbyname hostname\n"
  105. "@deffnx {Scheme Procedure} gethostbyaddr address\n"
  106. "Look up a host by name or address, returning a host object. The\n"
  107. "@code{gethost} procedure will accept either a string name or an integer\n"
  108. "address; if given no arguments, it behaves like @code{gethostent} (see\n"
  109. "below). If a name or address is supplied but the address can not be\n"
  110. "found, an error will be thrown to one of the keys:\n"
  111. "@code{host-not-found}, @code{try-again}, @code{no-recovery} or\n"
  112. "@code{no-data}, corresponding to the equivalent @code{h_error} values.\n"
  113. "Unusual conditions may result in errors thrown to the\n"
  114. "@code{system-error} or @code{misc_error} keys.")
  115. #define FUNC_NAME s_scm_gethost
  116. {
  117. SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  118. SCM lst = SCM_EOL;
  119. struct hostent *entry;
  120. struct in_addr inad;
  121. char **argv;
  122. int i = 0;
  123. if (SCM_UNBNDP (host))
  124. {
  125. #ifdef HAVE_GETHOSTENT
  126. entry = gethostent ();
  127. #else
  128. entry = NULL;
  129. #endif
  130. if (! entry)
  131. {
  132. /* As far as I can tell, there's no good way to tell whether
  133. zero means an error or end-of-file. The trick of
  134. clearing errno before calling gethostent and checking it
  135. afterwards doesn't cut it, because, on Linux, it seems to
  136. try to contact some other server (YP?) and fails, which
  137. is a benign failure. */
  138. return SCM_BOOL_F;
  139. }
  140. }
  141. else if (scm_is_string (host))
  142. {
  143. char *str = scm_to_locale_string (host);
  144. entry = gethostbyname (str);
  145. free (str);
  146. }
  147. else
  148. {
  149. inad.s_addr = htonl (scm_to_ulong (host));
  150. entry = gethostbyaddr ((char *) &inad, sizeof (inad), AF_INET);
  151. }
  152. if (!entry)
  153. scm_resolv_error (FUNC_NAME, host);
  154. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->h_name));
  155. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->h_aliases));
  156. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->h_addrtype));
  157. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_int (entry->h_length));
  158. if (sizeof (struct in_addr) != entry->h_length)
  159. {
  160. SCM_SIMPLE_VECTOR_SET(result, 4, SCM_BOOL_F);
  161. return result;
  162. }
  163. for (argv = entry->h_addr_list; argv[i]; i++);
  164. while (i--)
  165. {
  166. inad = *(struct in_addr *) argv[i];
  167. lst = scm_cons (scm_from_ulong (ntohl (inad.s_addr)), lst);
  168. }
  169. SCM_SIMPLE_VECTOR_SET(result, 4, lst);
  170. return result;
  171. }
  172. #undef FUNC_NAME
  173. #endif /* HAVE_H_ERRNO */
  174. /* In all subsequent getMUMBLE functions, when we're called with no
  175. arguments, we're supposed to traverse the tables entry by entry.
  176. However, there doesn't seem to be any documented way to distinguish
  177. between end-of-table and an error; in both cases the functions
  178. return zero. Gotta love Unix. For the time being, we clear errno,
  179. and if we get a zero and errno is set, we signal an error. This
  180. doesn't seem quite right (what if errno gets set as part of healthy
  181. operation?), but it seems to work okay. We'll see. */
  182. #if defined(HAVE_GETNETENT) && defined(HAVE_GETNETBYNAME) && defined(HAVE_GETNETBYADDR)
  183. SCM_DEFINE (scm_getnet, "getnet", 0, 1, 0,
  184. (SCM net),
  185. "@deffnx {Scheme Procedure} getnetbyname net-name\n"
  186. "@deffnx {Scheme Procedure} getnetbyaddr net-number\n"
  187. "Look up a network by name or net number in the network database. The\n"
  188. "@var{net-name} argument must be a string, and the @var{net-number}\n"
  189. "argument must be an integer. @code{getnet} will accept either type of\n"
  190. "argument, behaving like @code{getnetent} (see below) if no arguments are\n"
  191. "given.")
  192. #define FUNC_NAME s_scm_getnet
  193. {
  194. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  195. struct netent *entry;
  196. int eno;
  197. if (SCM_UNBNDP (net))
  198. {
  199. entry = getnetent ();
  200. if (! entry)
  201. {
  202. /* There's no good way to tell whether zero means an error
  203. or end-of-file, so we always return #f. See `gethost'
  204. for details. */
  205. return SCM_BOOL_F;
  206. }
  207. }
  208. else if (scm_is_string (net))
  209. {
  210. char *str = scm_to_locale_string (net);
  211. entry = getnetbyname (str);
  212. eno = errno;
  213. free (str);
  214. }
  215. else
  216. {
  217. unsigned long netnum = scm_to_ulong (net);
  218. entry = getnetbyaddr (netnum, AF_INET);
  219. eno = errno;
  220. }
  221. if (!entry)
  222. SCM_SYSERROR_MSG ("no such network ~A", scm_list_1 (net), eno);
  223. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->n_name));
  224. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->n_aliases));
  225. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->n_addrtype));
  226. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->n_net));
  227. return result;
  228. }
  229. #undef FUNC_NAME
  230. #endif
  231. #if defined (HAVE_GETPROTOENT)
  232. SCM_DEFINE (scm_getproto, "getproto", 0, 1, 0,
  233. (SCM protocol),
  234. "@deffnx {Scheme Procedure} getprotobyname name\n"
  235. "@deffnx {Scheme Procedure} getprotobynumber number\n"
  236. "Look up a network protocol by name or by number. @code{getprotobyname}\n"
  237. "takes a string argument, and @code{getprotobynumber} takes an integer\n"
  238. "argument. @code{getproto} will accept either type, behaving like\n"
  239. "@code{getprotoent} (see below) if no arguments are supplied.")
  240. #define FUNC_NAME s_scm_getproto
  241. {
  242. SCM result = scm_c_make_vector (3, SCM_UNSPECIFIED);
  243. struct protoent *entry;
  244. int eno;
  245. if (SCM_UNBNDP (protocol))
  246. {
  247. entry = getprotoent ();
  248. if (! entry)
  249. {
  250. /* There's no good way to tell whether zero means an error
  251. or end-of-file, so we always return #f. See `gethost'
  252. for details. */
  253. return SCM_BOOL_F;
  254. }
  255. }
  256. else if (scm_is_string (protocol))
  257. {
  258. char *str = scm_to_locale_string (protocol);
  259. entry = getprotobyname (str);
  260. eno = errno;
  261. free (str);
  262. }
  263. else
  264. {
  265. unsigned long protonum = scm_to_ulong (protocol);
  266. entry = getprotobynumber (protonum);
  267. eno = errno;
  268. }
  269. if (!entry)
  270. SCM_SYSERROR_MSG ("no such protocol ~A", scm_list_1 (protocol), eno);
  271. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->p_name));
  272. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->p_aliases));
  273. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->p_proto));
  274. return result;
  275. }
  276. #undef FUNC_NAME
  277. #endif
  278. #if defined (HAVE_GETSERVENT)
  279. static SCM
  280. scm_return_entry (struct servent *entry)
  281. {
  282. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  283. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->s_name));
  284. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->s_aliases));
  285. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_uint16 (ntohs (entry->s_port)));
  286. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (entry->s_proto));
  287. return result;
  288. }
  289. SCM_DEFINE (scm_getserv, "getserv", 0, 2, 0,
  290. (SCM name, SCM protocol),
  291. "@deffnx {Scheme Procedure} getservbyname name protocol\n"
  292. "@deffnx {Scheme Procedure} getservbyport port protocol\n"
  293. "Look up a network service by name or by service number, and return a\n"
  294. "network service object. The @var{protocol} argument specifies the name\n"
  295. "of the desired protocol; if the protocol found in the network service\n"
  296. "database does not match this name, a system error is signalled.\n\n"
  297. "The @code{getserv} procedure will take either a service name or number\n"
  298. "as its first argument; if given no arguments, it behaves like\n"
  299. "@code{getservent} (see below).")
  300. #define FUNC_NAME s_scm_getserv
  301. {
  302. struct servent *entry;
  303. char *protoname;
  304. int eno;
  305. if (SCM_UNBNDP (name))
  306. {
  307. entry = getservent ();
  308. if (!entry)
  309. {
  310. /* There's no good way to tell whether zero means an error
  311. or end-of-file, so we always return #f. See `gethost'
  312. for details. */
  313. return SCM_BOOL_F;
  314. }
  315. return scm_return_entry (entry);
  316. }
  317. scm_dynwind_begin (0);
  318. protoname = scm_to_locale_string (protocol);
  319. scm_dynwind_free (protoname);
  320. if (scm_is_string (name))
  321. {
  322. char *str = scm_to_locale_string (name);
  323. entry = getservbyname (str, protoname);
  324. eno = errno;
  325. free (str);
  326. }
  327. else
  328. {
  329. entry = getservbyport (htons (scm_to_int (name)), protoname);
  330. eno = errno;
  331. }
  332. if (!entry)
  333. SCM_SYSERROR_MSG("no such service ~A", scm_list_1 (name), eno);
  334. scm_dynwind_end ();
  335. return scm_return_entry (entry);
  336. }
  337. #undef FUNC_NAME
  338. #endif
  339. #if defined(HAVE_SETHOSTENT) && defined(HAVE_ENDHOSTENT)
  340. SCM_DEFINE (scm_sethost, "sethost", 0, 1, 0,
  341. (SCM stayopen),
  342. "If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.\n"
  343. "Otherwise it is equivalent to @code{sethostent stayopen}.")
  344. #define FUNC_NAME s_scm_sethost
  345. {
  346. if (SCM_UNBNDP (stayopen))
  347. endhostent ();
  348. else
  349. sethostent (scm_is_true (stayopen));
  350. return SCM_UNSPECIFIED;
  351. }
  352. #undef FUNC_NAME
  353. #endif
  354. #if defined(HAVE_SETNETENT) && defined(HAVE_ENDNETENT)
  355. SCM_DEFINE (scm_setnet, "setnet", 0, 1, 0,
  356. (SCM stayopen),
  357. "If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.\n"
  358. "Otherwise it is equivalent to @code{setnetent stayopen}.")
  359. #define FUNC_NAME s_scm_setnet
  360. {
  361. if (SCM_UNBNDP (stayopen))
  362. endnetent ();
  363. else
  364. setnetent (scm_is_true (stayopen));
  365. return SCM_UNSPECIFIED;
  366. }
  367. #undef FUNC_NAME
  368. #endif
  369. #if defined (HAVE_SETPROTOENT) && defined (HAVE_ENDPROTOENT)
  370. SCM_DEFINE (scm_setproto, "setproto", 0, 1, 0,
  371. (SCM stayopen),
  372. "If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.\n"
  373. "Otherwise it is equivalent to @code{setprotoent stayopen}.")
  374. #define FUNC_NAME s_scm_setproto
  375. {
  376. if (SCM_UNBNDP (stayopen))
  377. endprotoent ();
  378. else
  379. setprotoent (scm_is_true (stayopen));
  380. return SCM_UNSPECIFIED;
  381. }
  382. #undef FUNC_NAME
  383. #endif
  384. #if defined (HAVE_SETSERVENT) && defined (HAVE_ENDSERVENT)
  385. SCM_DEFINE (scm_setserv, "setserv", 0, 1, 0,
  386. (SCM stayopen),
  387. "If @var{stayopen} is omitted, this is equivalent to @code{endservent}.\n"
  388. "Otherwise it is equivalent to @code{setservent stayopen}.")
  389. #define FUNC_NAME s_scm_setserv
  390. {
  391. if (SCM_UNBNDP (stayopen))
  392. endservent ();
  393. else
  394. setservent (scm_is_true (stayopen));
  395. return SCM_UNSPECIFIED;
  396. }
  397. #undef FUNC_NAME
  398. #endif
  399. /* Protocol-independent name resolution with getaddrinfo(3) & co. */
  400. SCM_SYMBOL (sym_getaddrinfo_error, "getaddrinfo-error");
  401. #define SCM_DEFINE_CONSTANT(constant) \
  402. SCM_SNARF_HERE(verify (constant < SCM_MOST_POSITIVE_FIXNUM)) \
  403. SCM_SNARF_INIT(scm_c_define (#constant, SCM_I_MAKINUM (constant));)
  404. /* Valid values for the `ai_flags' to `struct addrinfo'. */
  405. SCM_DEFINE_CONSTANT (AI_PASSIVE);
  406. SCM_DEFINE_CONSTANT (AI_CANONNAME);
  407. SCM_DEFINE_CONSTANT (AI_NUMERICHOST);
  408. SCM_DEFINE_CONSTANT (AI_NUMERICSERV);
  409. SCM_DEFINE_CONSTANT (AI_V4MAPPED);
  410. SCM_DEFINE_CONSTANT (AI_ALL);
  411. SCM_DEFINE_CONSTANT (AI_ADDRCONFIG);
  412. /* Return a Scheme vector whose elements correspond to the fields of C_AI,
  413. ignoring the `ai_next' field. This function is not exported because the
  414. definition of `struct addrinfo' is provided by Gnulib. */
  415. static SCM
  416. scm_from_addrinfo (const struct addrinfo *c_ai)
  417. {
  418. SCM ai;
  419. /* Note: The indices here must be kept synchronized with those used by the
  420. `addrinfo:' procedures in `networking.scm'. */
  421. ai = scm_c_make_vector (6, SCM_UNDEFINED);
  422. SCM_SIMPLE_VECTOR_SET (ai, 0, scm_from_int (c_ai->ai_flags));
  423. SCM_SIMPLE_VECTOR_SET (ai, 1, scm_from_int (c_ai->ai_family));
  424. SCM_SIMPLE_VECTOR_SET (ai, 2, scm_from_int (c_ai->ai_socktype));
  425. SCM_SIMPLE_VECTOR_SET (ai, 3, scm_from_int (c_ai->ai_protocol));
  426. SCM_SIMPLE_VECTOR_SET (ai, 4,
  427. scm_from_sockaddr (c_ai->ai_addr, c_ai->ai_addrlen));
  428. SCM_SIMPLE_VECTOR_SET (ai, 5,
  429. c_ai->ai_canonname != NULL
  430. ? scm_from_locale_string (c_ai->ai_canonname)
  431. : SCM_BOOL_F);
  432. return ai;
  433. }
  434. SCM_DEFINE (scm_getaddrinfo, "getaddrinfo", 1, 5, 0,
  435. (SCM name, SCM service, SCM hint_flags, SCM hint_family,
  436. SCM hint_socktype, SCM hint_protocol),
  437. "Return a list of @code{addrinfo} structures containing "
  438. "a socket address and associated information for host @var{name} "
  439. "and/or @var{service} to be used in creating a socket with "
  440. "which to address the specified service.\n\n"
  441. "@example\n"
  442. "(let* ((ai (car (getaddrinfo \"www.gnu.org\" \"http\")))\n"
  443. " (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)\n"
  444. " (addrinfo:protocol ai))))\n"
  445. " (connect s (addrinfo:addr ai))\n"
  446. " s)\n"
  447. "@end example\n\n"
  448. "When @var{service} is omitted or is @code{#f}, return "
  449. "network-level addresses for @var{name}. When @var{name} "
  450. "is @code{#f} @var{service} must be provided and service "
  451. "locations local to the caller are returned.\n"
  452. "\n"
  453. "Additional hints can be provided. When specified, "
  454. "@var{hint_flags} should be a bitwise-or of zero or more "
  455. "constants among the following:\n\n"
  456. "@table @code\n"
  457. "@item AI_PASSIVE\n"
  458. "Socket address is intended for @code{bind}.\n\n"
  459. "@item AI_CANONNAME\n"
  460. "Request for canonical host name, available via "
  461. "@code{addrinfo:canonname}. This makes sense mainly when "
  462. "DNS lookups are involved.\n\n"
  463. "@item AI_NUMERICHOST\n"
  464. "Specifies that @var{name} is a numeric host address string "
  465. "(e.g., @code{\"127.0.0.1\"}), meaning that name resolution "
  466. "will not be used.\n\n"
  467. "@item AI_NUMERICSERV\n"
  468. "Likewise, specifies that @var{service} is a numeric port "
  469. "string (e.g., @code{\"80\"}).\n\n"
  470. "@item AI_ADDRCONFIG\n"
  471. "Return only addresses configured on the local system. It is "
  472. "highly recommended to provide this flag when the returned "
  473. "socket addresses are to be used to make connections; "
  474. "otherwise, some of the returned addresses could be unreachable "
  475. "or use a protocol that is not supported.\n\n"
  476. "@item AI_V4MAPPED\n"
  477. "When looking up IPv6 addresses, return mapped "
  478. "IPv4 addresses if there is no IPv6 address available at all.\n\n"
  479. "@item AI_ALL\n"
  480. "If this flag is set along with @code{AI_V4MAPPED} when looking "
  481. "up IPv6 addresses, return all IPv6 addresses "
  482. "as well as all IPv4 addresses, the latter mapped to IPv6 "
  483. "format.\n"
  484. "@end table\n\n"
  485. "When given, @var{hint_family} should specify the requested "
  486. "address family, e.g., @code{AF_INET6}. Similarly, "
  487. "@var{hint_socktype} should specify the requested socket type "
  488. "(e.g., @code{SOCK_DGRAM}), and @var{hint_protocol} should "
  489. "specify the requested protocol (its value is interpretered "
  490. "as in calls to @code{socket}).\n"
  491. "\n"
  492. "On error, an exception with key @code{getaddrinfo-error} is "
  493. "thrown, with an error code (an integer) as its argument:\n\n"
  494. "@example\n"
  495. "(catch 'getaddrinfo-error\n"
  496. " (lambda ()\n"
  497. " (getaddrinfo \"www.gnu.org\" \"gopher\"))\n"
  498. " (lambda (key errcode)\n"
  499. " (cond ((= errcode EAI_SERVICE)\n"
  500. " (display \"doesn't know about Gopher!\\n\"))\n"
  501. " ((= errcode EAI_NONAME)\n"
  502. " (display \"www.gnu.org not found\\n\"))\n"
  503. " (else\n"
  504. " (format #t \"something wrong: ~a\\n\"\n"
  505. " (gai-strerror errcode))))))\n"
  506. "@end example\n"
  507. "\n"
  508. "Error codes are:\n\n"
  509. "@table @code\n"
  510. "@item EAI_AGAIN\n"
  511. "The name or service could not be resolved at this time. Future "
  512. "attempts may succeed.\n\n"
  513. "@item EAI_BADFLAGS\n"
  514. "@var{hint_flags} contains an invalid value.\n\n"
  515. "@item EAI_FAIL\n"
  516. "A non-recoverable error occurred when attempting to "
  517. "resolve the name.\n\n"
  518. "@item EAI_FAMILY\n"
  519. "@var{hint_family} was not recognized.\n\n"
  520. "@item EAI_NONAME\n"
  521. "Either @var{name} does not resolve for the supplied parameters, "
  522. "or neither @var{name} nor @var{service} were supplied.\n\n"
  523. /* See `sysdeps/posix/getaddrinfo.c' in the GNU libc, and
  524. <http://www.opensource.apple.com/source/Libinfo/Libinfo-324.1/lookup.subproj/netdb.h>,
  525. for details on EAI_NODATA. */
  526. "@item EAI_NODATA\n"
  527. "This non-POSIX error code can be returned on some systems (GNU "
  528. "and Darwin, at least), for example when @var{name} is known "
  529. "but requests that were made turned out no data. Error handling\n"
  530. "code should be prepared to handle it when it is defined.\n\n"
  531. "@item EAI_SERVICE\n"
  532. "@var{service} was not recognized for the specified socket type.\n\n"
  533. "@item EAI_SOCKTYPE\n"
  534. "@var{hint_socktype} was not recognized.\n\n"
  535. "@item EAI_SYSTEM\n"
  536. "A system error occurred. In C, the error code can be found in "
  537. "@code{errno}; this value is not accessible from Scheme, but in\n"
  538. "practice it provides little information about the actual error "
  539. "cause.\n\n" /* see <http://bugs.gnu.org/13958>. */
  540. "@end table\n"
  541. "\n"
  542. "Users are encouraged to read the "
  543. "@url{http://www.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html,"
  544. "POSIX specification} for more details.\n")
  545. #define FUNC_NAME s_scm_getaddrinfo
  546. {
  547. int err;
  548. char *c_name, *c_service;
  549. struct addrinfo c_hints, *c_result;
  550. SCM result = SCM_EOL;
  551. if (scm_is_true (name))
  552. SCM_VALIDATE_STRING (SCM_ARG1, name);
  553. if (!SCM_UNBNDP (service) && scm_is_true (service))
  554. SCM_VALIDATE_STRING (SCM_ARG2, service);
  555. scm_dynwind_begin (0);
  556. if (scm_is_string (name))
  557. {
  558. c_name = scm_to_locale_string (name);
  559. scm_dynwind_free (c_name);
  560. }
  561. else
  562. c_name = NULL;
  563. if (scm_is_string (service))
  564. {
  565. c_service = scm_to_locale_string (service);
  566. scm_dynwind_free (c_service);
  567. }
  568. else
  569. c_service = NULL;
  570. memset (&c_hints, 0, sizeof (c_hints));
  571. if (!SCM_UNBNDP (hint_flags))
  572. {
  573. c_hints.ai_flags = scm_to_int (hint_flags);
  574. if (!SCM_UNBNDP (hint_family))
  575. {
  576. c_hints.ai_family = scm_to_int (hint_family);
  577. if (!SCM_UNBNDP (hint_socktype))
  578. {
  579. c_hints.ai_socktype = scm_to_int (hint_socktype);
  580. if (!SCM_UNBNDP (hint_family))
  581. c_hints.ai_family = scm_to_int (hint_family);
  582. }
  583. }
  584. }
  585. err = getaddrinfo (c_name, c_service, &c_hints, &c_result);
  586. if (err == 0)
  587. {
  588. SCM *prev_addr;
  589. struct addrinfo *a;
  590. for (prev_addr = &result, a = c_result;
  591. a != NULL;
  592. a = a->ai_next, prev_addr = SCM_CDRLOC (*prev_addr))
  593. *prev_addr = scm_list_1 (scm_from_addrinfo (a));
  594. freeaddrinfo (c_result);
  595. }
  596. else
  597. scm_throw (sym_getaddrinfo_error, scm_list_1 (scm_from_int (err)));
  598. scm_dynwind_end ();
  599. return result;
  600. }
  601. #undef FUNC_NAME
  602. /* Error codes returned by `getaddrinfo'. */
  603. SCM_DEFINE_CONSTANT (EAI_BADFLAGS);
  604. SCM_DEFINE_CONSTANT (EAI_NONAME);
  605. SCM_DEFINE_CONSTANT (EAI_AGAIN);
  606. SCM_DEFINE_CONSTANT (EAI_FAIL);
  607. SCM_DEFINE_CONSTANT (EAI_FAMILY);
  608. SCM_DEFINE_CONSTANT (EAI_SOCKTYPE);
  609. SCM_DEFINE_CONSTANT (EAI_SERVICE);
  610. SCM_DEFINE_CONSTANT (EAI_MEMORY);
  611. SCM_DEFINE_CONSTANT (EAI_SYSTEM);
  612. SCM_DEFINE_CONSTANT (EAI_OVERFLOW);
  613. /* The following values are GNU extensions. */
  614. #ifdef EAI_NODATA
  615. SCM_DEFINE_CONSTANT (EAI_NODATA);
  616. #endif
  617. #ifdef EAI_ADDRFAMILY
  618. SCM_DEFINE_CONSTANT (EAI_ADDRFAMILY);
  619. #endif
  620. #ifdef EAI_INPROGRESS
  621. SCM_DEFINE_CONSTANT (EAI_INPROGRESS);
  622. #endif
  623. #ifdef EAI_CANCELED
  624. SCM_DEFINE_CONSTANT (EAI_CANCELED);
  625. #endif
  626. #ifdef EAI_NOTCANCELED
  627. SCM_DEFINE_CONSTANT (EAI_NOTCANCELED);
  628. #endif
  629. #ifdef EAI_ALLDONE
  630. SCM_DEFINE_CONSTANT (EAI_ALLDONE);
  631. #endif
  632. #ifdef EAI_INTR
  633. SCM_DEFINE_CONSTANT (EAI_INTR);
  634. #endif
  635. #ifdef EAI_IDN_ENCODE
  636. SCM_DEFINE_CONSTANT (EAI_IDN_ENCODE);
  637. #endif
  638. SCM_DEFINE (scm_gai_strerror, "gai-strerror", 1, 0, 0,
  639. (SCM error),
  640. "Return a string describing @var{error}, an integer error code "
  641. "returned by @code{getaddrinfo}.")
  642. #define FUNC_NAME s_scm_gai_strerror
  643. {
  644. return scm_from_locale_string (gai_strerror (scm_to_int (error)));
  645. }
  646. #undef FUNC_NAME
  647. /* TODO: Add a getnameinfo(3) wrapper. */
  648. void
  649. scm_init_net_db ()
  650. {
  651. scm_add_feature ("net-db");
  652. #include "net_db.x"
  653. }