net_db.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* "net_db.c" network database support
  2. * Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2006, 2009 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
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but 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 02110-1301 USA
  17. */
  18. /* Written in 1994 by Aubrey Jaffer.
  19. * Thanks to Hallvard.Tretteberg@si.sintef.no for inspiration and discussion.
  20. * Rewritten by Gary Houston to be a closer interface to the C socket library.
  21. * Split into net_db.c and socket.c.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. # include <config.h>
  25. #endif
  26. #include <errno.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/feature.h"
  29. #include "libguile/strings.h"
  30. #include "libguile/vectors.h"
  31. #include "libguile/dynwind.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/net_db.h"
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include <sys/types.h>
  38. #ifdef HAVE_WINSOCK2_H
  39. #include <winsock2.h>
  40. #else
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #endif
  46. #ifdef __MINGW32__
  47. #include "win32-socket.h"
  48. #endif
  49. #if !defined (HAVE_H_ERRNO) && !defined (__MINGW32__) && !defined (__CYGWIN__)
  50. /* h_errno not found in netdb.h, maybe this will help. */
  51. extern int h_errno;
  52. #endif
  53. #if defined HAVE_HSTRERROR && !HAVE_DECL_HSTRERROR \
  54. && !defined __MINGW32__ && !defined __CYGWIN__
  55. /* Some OSes, such as Tru64 5.1b, lack a declaration for hstrerror(3). */
  56. extern const char *hstrerror (int);
  57. #endif
  58. SCM_SYMBOL (scm_host_not_found_key, "host-not-found");
  59. SCM_SYMBOL (scm_try_again_key, "try-again");
  60. SCM_SYMBOL (scm_no_recovery_key, "no-recovery");
  61. SCM_SYMBOL (scm_no_data_key, "no-data");
  62. static void scm_resolv_error (const char *subr, SCM bad_value)
  63. {
  64. #ifdef NETDB_INTERNAL
  65. if (h_errno == NETDB_INTERNAL)
  66. {
  67. /* errno supposedly contains a useful value. */
  68. scm_syserror (subr);
  69. }
  70. else
  71. #endif
  72. {
  73. SCM key;
  74. const char *errmsg;
  75. switch (h_errno)
  76. {
  77. case HOST_NOT_FOUND:
  78. key = scm_host_not_found_key;
  79. errmsg = "Unknown host";
  80. break;
  81. case TRY_AGAIN:
  82. key = scm_try_again_key;
  83. errmsg = "Host name lookup failure";
  84. break;
  85. case NO_RECOVERY:
  86. key = scm_no_recovery_key;
  87. errmsg = "Unknown server error";
  88. break;
  89. case NO_DATA:
  90. key = scm_no_data_key;
  91. errmsg = "No address associated with name";
  92. break;
  93. default:
  94. scm_misc_error (subr, "Unknown resolver error", SCM_EOL);
  95. errmsg = NULL;
  96. }
  97. #ifdef HAVE_HSTRERROR
  98. errmsg = (const char *) hstrerror (h_errno);
  99. #endif
  100. scm_error (key, subr, errmsg, SCM_BOOL_F, SCM_EOL);
  101. }
  102. }
  103. /* Should take an extra arg for address format (will be needed for IPv6).
  104. Should use reentrant facilities if available.
  105. */
  106. SCM_DEFINE (scm_gethost, "gethost", 0, 1, 0,
  107. (SCM host),
  108. "@deffnx {Scheme Procedure} gethostbyname hostname\n"
  109. "@deffnx {Scheme Procedure} gethostbyaddr address\n"
  110. "Look up a host by name or address, returning a host object. The\n"
  111. "@code{gethost} procedure will accept either a string name or an integer\n"
  112. "address; if given no arguments, it behaves like @code{gethostent} (see\n"
  113. "below). If a name or address is supplied but the address can not be\n"
  114. "found, an error will be thrown to one of the keys:\n"
  115. "@code{host-not-found}, @code{try-again}, @code{no-recovery} or\n"
  116. "@code{no-data}, corresponding to the equivalent @code{h_error} values.\n"
  117. "Unusual conditions may result in errors thrown to the\n"
  118. "@code{system-error} or @code{misc_error} keys.")
  119. #define FUNC_NAME s_scm_gethost
  120. {
  121. SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  122. SCM lst = SCM_EOL;
  123. struct hostent *entry;
  124. struct in_addr inad;
  125. char **argv;
  126. int i = 0;
  127. if (SCM_UNBNDP (host))
  128. {
  129. #ifdef HAVE_GETHOSTENT
  130. entry = gethostent ();
  131. #else
  132. entry = NULL;
  133. #endif
  134. if (! entry)
  135. {
  136. /* As far as I can tell, there's no good way to tell whether
  137. zero means an error or end-of-file. The trick of
  138. clearing errno before calling gethostent and checking it
  139. afterwards doesn't cut it, because, on Linux, it seems to
  140. try to contact some other server (YP?) and fails, which
  141. is a benign failure. */
  142. return SCM_BOOL_F;
  143. }
  144. }
  145. else if (scm_is_string (host))
  146. {
  147. char *str = scm_to_locale_string (host);
  148. entry = gethostbyname (str);
  149. free (str);
  150. }
  151. else
  152. {
  153. inad.s_addr = htonl (scm_to_ulong (host));
  154. entry = gethostbyaddr ((char *) &inad, sizeof (inad), AF_INET);
  155. }
  156. if (!entry)
  157. scm_resolv_error (FUNC_NAME, host);
  158. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->h_name));
  159. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->h_aliases));
  160. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->h_addrtype));
  161. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_int (entry->h_length));
  162. if (sizeof (struct in_addr) != entry->h_length)
  163. {
  164. SCM_SIMPLE_VECTOR_SET(result, 4, SCM_BOOL_F);
  165. return result;
  166. }
  167. for (argv = entry->h_addr_list; argv[i]; i++);
  168. while (i--)
  169. {
  170. inad = *(struct in_addr *) argv[i];
  171. lst = scm_cons (scm_from_ulong (ntohl (inad.s_addr)), lst);
  172. }
  173. SCM_SIMPLE_VECTOR_SET(result, 4, lst);
  174. return result;
  175. }
  176. #undef FUNC_NAME
  177. /* In all subsequent getMUMBLE functions, when we're called with no
  178. arguments, we're supposed to traverse the tables entry by entry.
  179. However, there doesn't seem to be any documented way to distinguish
  180. between end-of-table and an error; in both cases the functions
  181. return zero. Gotta love Unix. For the time being, we clear errno,
  182. and if we get a zero and errno is set, we signal an error. This
  183. doesn't seem quite right (what if errno gets set as part of healthy
  184. operation?), but it seems to work okay. We'll see. */
  185. #if defined(HAVE_GETNETENT) && defined(HAVE_GETNETBYNAME) && defined(HAVE_GETNETBYADDR)
  186. SCM_DEFINE (scm_getnet, "getnet", 0, 1, 0,
  187. (SCM net),
  188. "@deffnx {Scheme Procedure} getnetbyname net-name\n"
  189. "@deffnx {Scheme Procedure} getnetbyaddr net-number\n"
  190. "Look up a network by name or net number in the network database. The\n"
  191. "@var{net-name} argument must be a string, and the @var{net-number}\n"
  192. "argument must be an integer. @code{getnet} will accept either type of\n"
  193. "argument, behaving like @code{getnetent} (see below) if no arguments are\n"
  194. "given.")
  195. #define FUNC_NAME s_scm_getnet
  196. {
  197. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  198. struct netent *entry;
  199. int eno;
  200. if (SCM_UNBNDP (net))
  201. {
  202. entry = getnetent ();
  203. if (! entry)
  204. {
  205. /* There's no good way to tell whether zero means an error
  206. or end-of-file, so we always return #f. See `gethost'
  207. for details. */
  208. return SCM_BOOL_F;
  209. }
  210. }
  211. else if (scm_is_string (net))
  212. {
  213. char *str = scm_to_locale_string (net);
  214. entry = getnetbyname (str);
  215. eno = errno;
  216. free (str);
  217. }
  218. else
  219. {
  220. unsigned long netnum = scm_to_ulong (net);
  221. entry = getnetbyaddr (netnum, AF_INET);
  222. eno = errno;
  223. }
  224. if (!entry)
  225. SCM_SYSERROR_MSG ("no such network ~A", scm_list_1 (net), eno);
  226. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->n_name));
  227. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->n_aliases));
  228. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->n_addrtype));
  229. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->n_net));
  230. return result;
  231. }
  232. #undef FUNC_NAME
  233. #endif
  234. #if defined (HAVE_GETPROTOENT) || defined (__MINGW32__)
  235. SCM_DEFINE (scm_getproto, "getproto", 0, 1, 0,
  236. (SCM protocol),
  237. "@deffnx {Scheme Procedure} getprotobyname name\n"
  238. "@deffnx {Scheme Procedure} getprotobynumber number\n"
  239. "Look up a network protocol by name or by number. @code{getprotobyname}\n"
  240. "takes a string argument, and @code{getprotobynumber} takes an integer\n"
  241. "argument. @code{getproto} will accept either type, behaving like\n"
  242. "@code{getprotoent} (see below) if no arguments are supplied.")
  243. #define FUNC_NAME s_scm_getproto
  244. {
  245. SCM result = scm_c_make_vector (3, SCM_UNSPECIFIED);
  246. struct protoent *entry;
  247. int eno;
  248. if (SCM_UNBNDP (protocol))
  249. {
  250. entry = getprotoent ();
  251. if (! entry)
  252. {
  253. /* There's no good way to tell whether zero means an error
  254. or end-of-file, so we always return #f. See `gethost'
  255. for details. */
  256. return SCM_BOOL_F;
  257. }
  258. }
  259. else if (scm_is_string (protocol))
  260. {
  261. char *str = scm_to_locale_string (protocol);
  262. entry = getprotobyname (str);
  263. eno = errno;
  264. free (str);
  265. }
  266. else
  267. {
  268. unsigned long protonum = scm_to_ulong (protocol);
  269. entry = getprotobynumber (protonum);
  270. eno = errno;
  271. }
  272. if (!entry)
  273. SCM_SYSERROR_MSG ("no such protocol ~A", scm_list_1 (protocol), eno);
  274. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->p_name));
  275. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->p_aliases));
  276. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_int (entry->p_proto));
  277. return result;
  278. }
  279. #undef FUNC_NAME
  280. #endif
  281. #if defined (HAVE_GETSERVENT) || defined (__MINGW32__)
  282. static SCM
  283. scm_return_entry (struct servent *entry)
  284. {
  285. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  286. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->s_name));
  287. SCM_SIMPLE_VECTOR_SET(result, 1, scm_makfromstrs (-1, entry->s_aliases));
  288. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_uint16 (ntohs (entry->s_port)));
  289. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (entry->s_proto));
  290. return result;
  291. }
  292. SCM_DEFINE (scm_getserv, "getserv", 0, 2, 0,
  293. (SCM name, SCM protocol),
  294. "@deffnx {Scheme Procedure} getservbyname name protocol\n"
  295. "@deffnx {Scheme Procedure} getservbyport port protocol\n"
  296. "Look up a network service by name or by service number, and return a\n"
  297. "network service object. The @var{protocol} argument specifies the name\n"
  298. "of the desired protocol; if the protocol found in the network service\n"
  299. "database does not match this name, a system error is signalled.\n\n"
  300. "The @code{getserv} procedure will take either a service name or number\n"
  301. "as its first argument; if given no arguments, it behaves like\n"
  302. "@code{getservent} (see below).")
  303. #define FUNC_NAME s_scm_getserv
  304. {
  305. struct servent *entry;
  306. char *protoname;
  307. int eno;
  308. if (SCM_UNBNDP (name))
  309. {
  310. entry = getservent ();
  311. if (!entry)
  312. {
  313. /* There's no good way to tell whether zero means an error
  314. or end-of-file, so we always return #f. See `gethost'
  315. for details. */
  316. return SCM_BOOL_F;
  317. }
  318. return scm_return_entry (entry);
  319. }
  320. scm_dynwind_begin (0);
  321. protoname = scm_to_locale_string (protocol);
  322. scm_dynwind_free (protoname);
  323. if (scm_is_string (name))
  324. {
  325. char *str = scm_to_locale_string (name);
  326. entry = getservbyname (str, protoname);
  327. eno = errno;
  328. free (str);
  329. }
  330. else
  331. {
  332. entry = getservbyport (htons (scm_to_int (name)), protoname);
  333. eno = errno;
  334. }
  335. if (!entry)
  336. SCM_SYSERROR_MSG("no such service ~A", scm_list_1 (name), eno);
  337. scm_dynwind_end ();
  338. return scm_return_entry (entry);
  339. }
  340. #undef FUNC_NAME
  341. #endif
  342. #if defined(HAVE_SETHOSTENT) && defined(HAVE_ENDHOSTENT)
  343. SCM_DEFINE (scm_sethost, "sethost", 0, 1, 0,
  344. (SCM stayopen),
  345. "If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.\n"
  346. "Otherwise it is equivalent to @code{sethostent stayopen}.")
  347. #define FUNC_NAME s_scm_sethost
  348. {
  349. if (SCM_UNBNDP (stayopen))
  350. endhostent ();
  351. else
  352. sethostent (scm_is_true (stayopen));
  353. return SCM_UNSPECIFIED;
  354. }
  355. #undef FUNC_NAME
  356. #endif
  357. #if defined(HAVE_SETNETENT) && defined(HAVE_ENDNETENT)
  358. SCM_DEFINE (scm_setnet, "setnet", 0, 1, 0,
  359. (SCM stayopen),
  360. "If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.\n"
  361. "Otherwise it is equivalent to @code{setnetent stayopen}.")
  362. #define FUNC_NAME s_scm_setnet
  363. {
  364. if (SCM_UNBNDP (stayopen))
  365. endnetent ();
  366. else
  367. setnetent (scm_is_true (stayopen));
  368. return SCM_UNSPECIFIED;
  369. }
  370. #undef FUNC_NAME
  371. #endif
  372. #if defined (HAVE_SETPROTOENT) && defined (HAVE_ENDPROTOENT) || defined (__MINGW32__)
  373. SCM_DEFINE (scm_setproto, "setproto", 0, 1, 0,
  374. (SCM stayopen),
  375. "If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.\n"
  376. "Otherwise it is equivalent to @code{setprotoent stayopen}.")
  377. #define FUNC_NAME s_scm_setproto
  378. {
  379. if (SCM_UNBNDP (stayopen))
  380. endprotoent ();
  381. else
  382. setprotoent (scm_is_true (stayopen));
  383. return SCM_UNSPECIFIED;
  384. }
  385. #undef FUNC_NAME
  386. #endif
  387. #if defined (HAVE_SETSERVENT) && defined (HAVE_ENDSERVENT) || defined (__MINGW32__)
  388. SCM_DEFINE (scm_setserv, "setserv", 0, 1, 0,
  389. (SCM stayopen),
  390. "If @var{stayopen} is omitted, this is equivalent to @code{endservent}.\n"
  391. "Otherwise it is equivalent to @code{setservent stayopen}.")
  392. #define FUNC_NAME s_scm_setserv
  393. {
  394. if (SCM_UNBNDP (stayopen))
  395. endservent ();
  396. else
  397. setservent (scm_is_true (stayopen));
  398. return SCM_UNSPECIFIED;
  399. }
  400. #undef FUNC_NAME
  401. #endif
  402. void
  403. scm_init_net_db ()
  404. {
  405. scm_add_feature ("net-db");
  406. #include "libguile/net_db.x"
  407. }
  408. /*
  409. Local Variables:
  410. c-file-style: "gnu"
  411. End:
  412. */