net_db.c 23 KB

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