net_db.c 24 KB

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