1 /* "net_db.c" network database support
2 * Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
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.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
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.
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. */
43 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
44 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
47 /* Written in 1994 by Aubrey Jaffer.
48 * Thanks to Hallvard.Tretteberg@si.sintef.no for inspiration and discussion.
49 * Rewritten by Gary Houston to be a closer interface to the C socket library.
50 * Split into net_db.c and socket.c.
56 #include "libguile/_scm.h"
57 #include "libguile/feature.h"
58 #include "libguile/strings.h"
59 #include "libguile/vectors.h"
61 #include "libguile/validate.h"
62 #include "libguile/net_db.h"
68 #include <sys/types.h>
69 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
78 #endif /* STDC_HEADERS */
80 #ifndef HAVE_INET_ATON
81 extern int inet_aton ();
84 SCM_DEFINE (scm_inet_aton
, "inet-aton", 1, 0, 0,
86 "Converts a string containing an Internet host address in the traditional\n"
87 "dotted decimal notation into an integer.\n\n"
89 "(inet-aton \"127.0.0.1\") @result{} 2130706433\n\n"
91 #define FUNC_NAME s_scm_inet_aton
95 SCM_VALIDATE_STRING (1, address
);
96 SCM_STRING_COERCE_0TERMINATION_X (address
);
97 if (inet_aton (SCM_STRING_CHARS (address
), &soka
) == 0)
98 SCM_MISC_ERROR ("bad address", SCM_EOL
);
99 return scm_ulong2num (ntohl (soka
.s_addr
));
104 SCM_DEFINE (scm_inet_ntoa
, "inet-ntoa", 1, 0, 0,
106 "Converts an integer Internet host address into a string with the\n"
107 "traditional dotted decimal representation.\n\n"
109 "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
111 #define FUNC_NAME s_scm_inet_ntoa
116 addr
.s_addr
= htonl (SCM_NUM2ULONG (1,inetid
));
117 s
= inet_ntoa (addr
);
118 answer
= scm_makfromstr (s
, strlen (s
), 0);
123 #ifdef HAVE_INET_NETOF
124 SCM_DEFINE (scm_inet_netof
, "inet-netof", 1, 0, 0,
126 "Returns the network number part of the given integer Internet address.\n\n"
128 "(inet-netof 2130706433) @result{} 127\n"
130 #define FUNC_NAME s_scm_inet_netof
133 addr
.s_addr
= htonl (SCM_NUM2ULONG (1,address
));
134 return scm_ulong2num ((unsigned long) inet_netof (addr
));
139 #ifdef HAVE_INET_LNAOF
140 SCM_DEFINE (scm_lnaof
, "inet-lnaof", 1, 0, 0,
142 "Returns the local-address-with-network part of the given Internet\n"
145 "(inet-lnaof 2130706433) @result{} 1\n"
147 #define FUNC_NAME s_scm_lnaof
150 addr
.s_addr
= htonl (SCM_NUM2ULONG (1,address
));
151 return scm_ulong2num ((unsigned long) inet_lnaof (addr
));
156 #ifdef HAVE_INET_MAKEADDR
157 SCM_DEFINE (scm_inet_makeaddr
, "inet-makeaddr", 2, 0, 0,
159 "Makes an Internet host address by combining the network number @var{net}\n"
160 "with the local-address-within-network number @var{lna}.\n\n"
162 "(inet-makeaddr 127 1) @result{} 2130706433\n"
164 #define FUNC_NAME s_scm_inet_makeaddr
167 unsigned long netnum
;
168 unsigned long lnanum
;
170 #if 0 /* GJB:FIXME:: */
171 SCM_VALIDATE_INUM_COPY (1,net
,netnum
);
172 SCM_VALIDATE_INUM_COPY (2,lna
,lnanum
);
174 netnum
= SCM_NUM2ULONG (1, net
);
175 lnanum
= SCM_NUM2ULONG (2, lna
);
177 addr
= inet_makeaddr (netnum
, lnanum
);
178 return scm_ulong2num (ntohl (addr
.s_addr
));
183 SCM_SYMBOL (scm_host_not_found_key
, "host-not-found");
184 SCM_SYMBOL (scm_try_again_key
, "try-again");
185 SCM_SYMBOL (scm_no_recovery_key
, "no-recovery");
186 SCM_SYMBOL (scm_no_data_key
, "no-data");
188 static void scm_resolv_error (const char *subr
, SCM bad_value
)
190 #ifdef NETDB_INTERNAL
191 if (h_errno
== NETDB_INTERNAL
)
193 /* errno supposedly contains a useful value. */
205 key
= scm_host_not_found_key
;
206 errmsg
= "Unknown host";
209 key
= scm_try_again_key
;
210 errmsg
= "Host name lookup failure";
213 key
= scm_no_recovery_key
;
214 errmsg
= "Unknown server error";
217 key
= scm_no_data_key
;
218 errmsg
= "No address associated with name";
221 scm_misc_error (subr
, "Unknown resolver error", SCM_EOL
);
225 #ifdef HAVE_HSTRERROR
226 errmsg
= (const char *) hstrerror (h_errno
);
228 scm_error (key
, subr
, errmsg
, scm_cons (bad_value
, SCM_EOL
), SCM_EOL
);
232 /* Should take an extra arg for address format (will be needed for IPv6).
233 Should use reentrant facilities if available.
236 SCM_DEFINE (scm_gethost
, "gethost", 0, 1, 0,
238 "@deffnx procedure gethostbyname hostname\n"
239 "@deffnx procedure gethostbyaddr address\n"
240 "Look up a host by name or address, returning a host object. The\n"
241 "@code{gethost} procedure will accept either a string name or an integer\n"
242 "address; if given no arguments, it behaves like @code{gethostent} (see\n"
243 "below). If a name or address is supplied but the address can not be\n"
244 "found, an error will be thrown to one of the keys:\n"
245 "@code{host-not-found}, @code{try-again}, @code{no-recovery} or\n"
246 "@code{no-data}, corresponding to the equivalent @code{h_error} values.\n"
247 "Unusual conditions may result in errors thrown to the\n"
248 "@code{system-error} or @code{misc_error} keys.")
249 #define FUNC_NAME s_scm_gethost
251 SCM ans
= scm_c_make_vector (5, SCM_UNSPECIFIED
);
252 SCM
*ve
= SCM_VELTS (ans
);
254 struct hostent
*entry
;
258 if (SCM_UNBNDP (host
))
260 #ifdef HAVE_GETHOSTENT
261 entry
= gethostent ();
267 /* As far as I can tell, there's no good way to tell whether
268 zero means an error or end-of-file. The trick of
269 clearing errno before calling gethostent and checking it
270 afterwards doesn't cut it, because, on Linux, it seems to
271 try to contact some other server (YP?) and fails, which
272 is a benign failure. */
276 else if (SCM_STRINGP (host
))
278 SCM_STRING_COERCE_0TERMINATION_X (host
);
279 entry
= gethostbyname (SCM_STRING_CHARS (host
));
283 inad
.s_addr
= htonl (SCM_NUM2ULONG (1,host
));
284 entry
= gethostbyaddr ((char *) &inad
, sizeof (inad
), AF_INET
);
287 scm_resolv_error (FUNC_NAME
, host
);
289 ve
[0] = scm_makfromstr (entry
->h_name
,
290 (scm_sizet
) strlen (entry
->h_name
), 0);
291 ve
[1] = scm_makfromstrs (-1, entry
->h_aliases
);
292 ve
[2] = SCM_MAKINUM (entry
->h_addrtype
+ 0L);
293 ve
[3] = SCM_MAKINUM (entry
->h_length
+ 0L);
294 if (sizeof (struct in_addr
) != entry
->h_length
)
299 for (argv
= entry
->h_addr_list
; argv
[i
]; i
++);
302 inad
= *(struct in_addr
*) argv
[i
];
303 lst
= scm_cons (scm_ulong2num (ntohl (inad
.s_addr
)), lst
);
311 /* In all subsequent getMUMBLE functions, when we're called with no
312 arguments, we're supposed to traverse the tables entry by entry.
313 However, there doesn't seem to be any documented way to distinguish
314 between end-of-table and an error; in both cases the functions
315 return zero. Gotta love Unix. For the time being, we clear errno,
316 and if we get a zero and errno is set, we signal an error. This
317 doesn't seem quite right (what if errno gets set as part of healthy
318 operation?), but it seems to work okay. We'll see. */
320 #if defined(HAVE_GETNETENT) && defined(HAVE_GETNETBYNAME) && defined(HAVE_GETNETBYADDR)
321 SCM_DEFINE (scm_getnet
, "getnet", 0, 1, 0,
323 "@deffnx procedure getnetbyname net-name\n"
324 "@deffnx procedure getnetbyaddr net-number\n"
325 "Look up a network by name or net number in the network database. The\n"
326 "@var{net-name} argument must be a string, and the @var{net-number}\n"
327 "argument must be an integer. @code{getnet} will accept either type of\n"
328 "argument, behaving like @code{getnetent} (see below) if no arguments are\n"
330 #define FUNC_NAME s_scm_getnet
334 struct netent
*entry
;
336 ans
= scm_c_make_vector (4, SCM_UNSPECIFIED
);
337 ve
= SCM_VELTS (ans
);
338 if (SCM_UNBNDP (net
))
340 entry
= getnetent ();
343 /* There's no good way to tell whether zero means an error
344 or end-of-file, so we always return #f. See `gethost'
349 else if (SCM_STRINGP (net
))
351 SCM_STRING_COERCE_0TERMINATION_X (net
);
352 entry
= getnetbyname (SCM_STRING_CHARS (net
));
356 unsigned long netnum
;
357 netnum
= SCM_NUM2ULONG (1, net
);
358 entry
= getnetbyaddr (netnum
, AF_INET
);
361 SCM_SYSERROR_MSG ("no such network ~A",
362 scm_listify (net
, SCM_UNDEFINED
), errno
);
363 ve
[0] = scm_makfromstr (entry
->n_name
, (scm_sizet
) strlen (entry
->n_name
), 0);
364 ve
[1] = scm_makfromstrs (-1, entry
->n_aliases
);
365 ve
[2] = SCM_MAKINUM (entry
->n_addrtype
+ 0L);
366 ve
[3] = scm_ulong2num (entry
->n_net
+ 0L);
372 #ifdef HAVE_GETPROTOENT
373 SCM_DEFINE (scm_getproto
, "getproto", 0, 1, 0,
375 "@deffnx procedure getprotobyname name\n"
376 "@deffnx procedure getprotobynumber number\n"
377 "Look up a network protocol by name or by number. @code{getprotobyname}\n"
378 "takes a string argument, and @code{getprotobynumber} takes an integer\n"
379 "argument. @code{getproto} will accept either type, behaving like\n"
380 "@code{getprotoent} (see below) if no arguments are supplied.")
381 #define FUNC_NAME s_scm_getproto
385 struct protoent
*entry
;
387 ans
= scm_c_make_vector (3, SCM_UNSPECIFIED
);
388 ve
= SCM_VELTS (ans
);
389 if (SCM_UNBNDP (protocol
))
391 entry
= getprotoent ();
394 /* There's no good way to tell whether zero means an error
395 or end-of-file, so we always return #f. See `gethost'
400 else if (SCM_STRINGP (protocol
))
402 SCM_STRING_COERCE_0TERMINATION_X (protocol
);
403 entry
= getprotobyname (SCM_STRING_CHARS (protocol
));
407 unsigned long protonum
;
408 protonum
= SCM_NUM2ULONG (1,protocol
);
409 entry
= getprotobynumber (protonum
);
412 SCM_SYSERROR_MSG ("no such protocol ~A",
413 scm_listify (protocol
, SCM_UNDEFINED
), errno
);
414 ve
[0] = scm_makfromstr (entry
->p_name
, (scm_sizet
) strlen (entry
->p_name
), 0);
415 ve
[1] = scm_makfromstrs (-1, entry
->p_aliases
);
416 ve
[2] = SCM_MAKINUM (entry
->p_proto
+ 0L);
423 scm_return_entry (struct servent
*entry
)
428 ans
= scm_c_make_vector (4, SCM_UNSPECIFIED
);
429 ve
= SCM_VELTS (ans
);
430 ve
[0] = scm_makfromstr (entry
->s_name
, (scm_sizet
) strlen (entry
->s_name
), 0);
431 ve
[1] = scm_makfromstrs (-1, entry
->s_aliases
);
432 ve
[2] = SCM_MAKINUM (ntohs (entry
->s_port
) + 0L);
433 ve
[3] = scm_makfromstr (entry
->s_proto
, (scm_sizet
) strlen (entry
->s_proto
), 0);
437 #ifdef HAVE_GETSERVENT
438 SCM_DEFINE (scm_getserv
, "getserv", 0, 2, 0,
439 (SCM name
, SCM protocol
),
440 "@deffnx procedure getservbyname name protocol\n"
441 "@deffnx procedure getservbyport port protocol\n"
442 "Look up a network service by name or by service number, and return a\n"
443 "network service object. The @var{protocol} argument specifies the name\n"
444 "of the desired protocol; if the protocol found in the network service\n"
445 "database does not match this name, a system error is signalled.\n\n"
446 "The @code{getserv} procedure will take either a service name or number\n"
447 "as its first argument; if given no arguments, it behaves like\n"
448 "@code{getservent} (see below).")
449 #define FUNC_NAME s_scm_getserv
451 struct servent
*entry
;
452 if (SCM_UNBNDP (name
))
454 entry
= getservent ();
457 /* There's no good way to tell whether zero means an error
458 or end-of-file, so we always return #f. See `gethost'
462 return scm_return_entry (entry
);
464 SCM_VALIDATE_STRING (2, protocol
);
465 SCM_STRING_COERCE_0TERMINATION_X (protocol
);
466 if (SCM_STRINGP (name
))
468 SCM_STRING_COERCE_0TERMINATION_X (name
);
469 entry
= getservbyname (SCM_STRING_CHARS (name
), SCM_STRING_CHARS (protocol
));
473 SCM_VALIDATE_INUM (1,name
);
474 entry
= getservbyport (htons (SCM_INUM (name
)), SCM_STRING_CHARS (protocol
));
477 SCM_SYSERROR_MSG("no such service ~A",
478 scm_listify (name
, SCM_UNDEFINED
), errno
);
479 return scm_return_entry (entry
);
484 #if defined(HAVE_SETHOSTENT) && defined(HAVE_ENDHOSTENT)
485 SCM_DEFINE (scm_sethost
, "sethost", 0, 1, 0,
487 "If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.\n"
488 "Otherwise it is equivalent to @code{sethostent stayopen}.")
489 #define FUNC_NAME s_scm_sethost
491 if (SCM_UNBNDP (stayopen
))
494 sethostent (SCM_NFALSEP (stayopen
));
495 return SCM_UNSPECIFIED
;
500 #if defined(HAVE_SETNETENT) && defined(HAVE_ENDNETENT)
501 SCM_DEFINE (scm_setnet
, "setnet", 0, 1, 0,
503 "If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.\n"
504 "Otherwise it is equivalent to @code{setnetent stayopen}.")
505 #define FUNC_NAME s_scm_setnet
507 if (SCM_UNBNDP (stayopen
))
510 setnetent (SCM_NFALSEP (stayopen
));
511 return SCM_UNSPECIFIED
;
516 #if defined(HAVE_SETPROTOENT) && defined(HAVE_ENDPROTOENT)
517 SCM_DEFINE (scm_setproto
, "setproto", 0, 1, 0,
519 "If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.\n"
520 "Otherwise it is equivalent to @code{setprotoent stayopen}.")
521 #define FUNC_NAME s_scm_setproto
523 if (SCM_UNBNDP (stayopen
))
526 setprotoent (SCM_NFALSEP (stayopen
));
527 return SCM_UNSPECIFIED
;
532 #if defined(HAVE_SETSERVENT) && defined(HAVE_ENDSERVENT)
533 SCM_DEFINE (scm_setserv
, "setserv", 0, 1, 0,
535 "If @var{stayopen} is omitted, this is equivalent to @code{endservent}.\n"
536 "Otherwise it is equivalent to @code{setservent stayopen}.")
537 #define FUNC_NAME s_scm_setserv
539 if (SCM_UNBNDP (stayopen
))
542 setservent (SCM_NFALSEP (stayopen
));
543 return SCM_UNSPECIFIED
;
553 scm_sysintern ("INADDR_ANY", scm_ulong2num (INADDR_ANY
));
555 #ifdef INADDR_BROADCAST
556 scm_sysintern ("INADDR_BROADCAST", scm_ulong2num (INADDR_BROADCAST
));
559 scm_sysintern ("INADDR_NONE", scm_ulong2num (INADDR_NONE
));
561 #ifdef INADDR_LOOPBACK
562 scm_sysintern ("INADDR_LOOPBACK", scm_ulong2num (INADDR_LOOPBACK
));
565 scm_add_feature ("net-db");
566 #ifndef SCM_MAGIC_SNARFER
567 #include "libguile/net_db.x"