net_db.c 14 KB

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