net_db.c 14 KB

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