socket.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /* Copyright (C) 1996,1997,1998, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/unif.h"
  44. #include "libguile/feature.h"
  45. #include "libguile/fports.h"
  46. #include "libguile/strings.h"
  47. #include "libguile/vectors.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/socket.h"
  50. #ifdef HAVE_STRING_H
  51. #include <string.h>
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h>
  55. #endif
  56. #include <sys/types.h>
  57. #include <sys/socket.h>
  58. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  59. #include <sys/un.h>
  60. #endif
  61. #include <netinet/in.h>
  62. #include <netdb.h>
  63. #include <arpa/inet.h>
  64. SCM_DEFINE (scm_htons, "htons", 1, 0, 0,
  65. (SCM in),
  66. "Returns a new integer from @var{value} by converting from host to\n"
  67. "network order. @var{value} must be within the range of a C unsigned\n"
  68. "short integer.")
  69. #define FUNC_NAME s_scm_htons
  70. {
  71. unsigned short c_in;
  72. SCM_VALIDATE_INUM_COPY (1,in,c_in);
  73. if (c_in != SCM_INUM (in))
  74. SCM_OUT_OF_RANGE (1,in);
  75. return SCM_MAKINUM (htons (c_in));
  76. }
  77. #undef FUNC_NAME
  78. SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0,
  79. (SCM in),
  80. "Returns a new integer from @var{value} by converting from network to\n"
  81. "host order. @var{value} must be within the range of a C unsigned short\n"
  82. "integer.")
  83. #define FUNC_NAME s_scm_ntohs
  84. {
  85. unsigned short c_in;
  86. SCM_VALIDATE_INUM_COPY (1,in,c_in);
  87. if (c_in != SCM_INUM (in))
  88. SCM_OUT_OF_RANGE (1,in);
  89. return SCM_MAKINUM (ntohs (c_in));
  90. }
  91. #undef FUNC_NAME
  92. SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0,
  93. (SCM in),
  94. "Returns a new integer from @var{value} by converting from host to\n"
  95. "network order. @var{value} must be within the range of a C unsigned\n"
  96. "long integer.")
  97. #define FUNC_NAME s_scm_htonl
  98. {
  99. unsigned long c_in = SCM_NUM2ULONG (1,in);
  100. return scm_ulong2num (htonl (c_in));
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0,
  104. (SCM in),
  105. "Returns a new integer from @var{value} by converting from network to\n"
  106. "host order. @var{value} must be within the range of a C unsigned\n"
  107. "long integer.")
  108. #define FUNC_NAME s_scm_ntohl
  109. {
  110. unsigned long c_in = SCM_NUM2ULONG (1,in);
  111. return scm_ulong2num (ntohl (c_in));
  112. }
  113. #undef FUNC_NAME
  114. SCM_SYMBOL (sym_socket, "socket");
  115. static SCM
  116. scm_sock_fd_to_port (int fd, const char *proc)
  117. {
  118. SCM result;
  119. if (fd == -1)
  120. scm_syserror (proc);
  121. result = scm_fdes_to_port (fd, "r+0", sym_socket);
  122. return result;
  123. }
  124. #define SCM_SOCK_FD_TO_PORT(fd) (scm_sock_fd_to_port((fd),FUNC_NAME))
  125. SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
  126. (SCM family, SCM style, SCM proto),
  127. "Returns a new socket port of the type specified by @var{family}, @var{style}\n"
  128. "and @var{protocol}. All three parameters are integers. Typical values\n"
  129. "for @var{family} are the values of @code{AF_UNIX}\n"
  130. "and @code{AF_INET}. Typical values for @var{style} are\n"
  131. "the values of @code{SOCK_STREAM}, @code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
  132. "@var{protocol} can be obtained from a protocol name using\n"
  133. "@code{getprotobyname}. A value of\n"
  134. "zero specifies the default protocol, which is usually right.\n\n"
  135. "A single socket port cannot by used for communication until\n"
  136. "it has been connected to another socket.")
  137. #define FUNC_NAME s_scm_socket
  138. {
  139. int fd;
  140. SCM result;
  141. SCM_VALIDATE_INUM (1,family);
  142. SCM_VALIDATE_INUM (2,style);
  143. SCM_VALIDATE_INUM (3,proto);
  144. fd = socket (SCM_INUM (family), SCM_INUM (style), SCM_INUM (proto));
  145. result = SCM_SOCK_FD_TO_PORT (fd);
  146. return result;
  147. }
  148. #undef FUNC_NAME
  149. #ifdef HAVE_SOCKETPAIR
  150. SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
  151. (SCM family, SCM style, SCM proto),
  152. "Returns a pair of connected (but unnamed) socket ports of the type specified\n"
  153. "by @var{family}, @var{style} and @var{protocol}.\n"
  154. "Many systems support only\n"
  155. "socket pairs of the @code{AF_UNIX} family. Zero is likely to be\n"
  156. "the only meaningful value for @var{protocol}.")
  157. #define FUNC_NAME s_scm_socketpair
  158. {
  159. int fam;
  160. int fd[2];
  161. SCM a;
  162. SCM b;
  163. SCM_VALIDATE_INUM (1,family);
  164. SCM_VALIDATE_INUM (2,style);
  165. SCM_VALIDATE_INUM (3,proto);
  166. fam = SCM_INUM (family);
  167. if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
  168. SCM_SYSERROR;
  169. a = SCM_SOCK_FD_TO_PORT(fd[0]);
  170. b = SCM_SOCK_FD_TO_PORT(fd[1]);
  171. return scm_cons (a, b);
  172. }
  173. #undef FUNC_NAME
  174. #endif
  175. SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
  176. (SCM sock, SCM level, SCM optname),
  177. "Returns the value of a particular socket option for the socket\n"
  178. "port @var{socket}. @var{level} is an integer code for type of option\n"
  179. "being requested, e.g., @code{SOL_SOCKET} for socket-level options.\n"
  180. "@var{optname} is an\n"
  181. "integer code for the option required and should be specified using one of\n"
  182. "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
  183. "The returned value is typically an integer but @code{SO_LINGER} returns a\n"
  184. "pair of integers.")
  185. #define FUNC_NAME s_scm_getsockopt
  186. {
  187. int fd;
  188. size_t optlen;
  189. #ifdef HAVE_STRUCT_LINGER
  190. char optval[sizeof (struct linger)];
  191. #else
  192. char optval[sizeof (scm_sizet)];
  193. #endif
  194. int ilevel;
  195. int ioptname;
  196. #ifdef HAVE_STRUCT_LINGER
  197. optlen = sizeof (struct linger);
  198. #else
  199. optlen = sizeof (size_t);
  200. #endif
  201. sock = SCM_COERCE_OUTPORT (sock);
  202. SCM_VALIDATE_OPFPORT (1, sock);
  203. SCM_VALIDATE_INUM_COPY (2, level, ilevel);
  204. SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
  205. fd = SCM_FPORT_FDES (sock);
  206. if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
  207. SCM_SYSERROR;
  208. #ifdef SO_LINGER
  209. if (ilevel == SOL_SOCKET && ioptname == SO_LINGER)
  210. {
  211. #ifdef HAVE_STRUCT_LINGER
  212. struct linger *ling = (struct linger *) optval;
  213. return scm_cons (SCM_MAKINUM (ling->l_onoff),
  214. SCM_MAKINUM (ling->l_linger));
  215. #else
  216. scm_sizet *ling = (scm_sizet *) optval;
  217. return scm_cons (SCM_MAKINUM (*ling),
  218. SCM_MAKINUM (0));
  219. #endif
  220. }
  221. #endif
  222. #ifdef SO_SNDBUF
  223. if (ilevel == SOL_SOCKET && ioptname == SO_SNDBUF)
  224. {
  225. scm_sizet *bufsize = (scm_sizet *) optval;
  226. return SCM_MAKINUM (*bufsize);
  227. }
  228. #endif
  229. #ifdef SO_RCVBUF
  230. if (ilevel == SOL_SOCKET && ioptname == SO_RCVBUF)
  231. {
  232. scm_sizet *bufsize = (scm_sizet *) optval;
  233. return SCM_MAKINUM (*bufsize);
  234. }
  235. #endif
  236. return SCM_MAKINUM (*(int *) optval);
  237. }
  238. #undef FUNC_NAME
  239. SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
  240. (SCM sock, SCM level, SCM optname, SCM value),
  241. "Sets the value of a particular socket option for the socket\n"
  242. "port @var{socket}. @var{level} is an integer code for type of option\n"
  243. "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n"
  244. "@var{optname} is an\n"
  245. "integer code for the option to set and should be specified using one of\n"
  246. "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n"
  247. "@var{value} is the value to which the option should be set. For\n"
  248. "most options this must be an integer, but for @code{SO_LINGER} it must\n"
  249. "be a pair.\n\n"
  250. "The return value is unspecified.")
  251. #define FUNC_NAME s_scm_setsockopt
  252. {
  253. int fd;
  254. int optlen;
  255. #ifdef HAVE_STRUCT_LINGER
  256. char optval[sizeof (struct linger)]; /* Biggest option :-( */
  257. #else
  258. char optval[sizeof (scm_sizet)];
  259. #endif
  260. int ilevel, ioptname;
  261. sock = SCM_COERCE_OUTPORT (sock);
  262. SCM_VALIDATE_OPFPORT (1,sock);
  263. SCM_VALIDATE_INUM_COPY (2,level,ilevel);
  264. SCM_VALIDATE_INUM_COPY (3,optname,ioptname);
  265. fd = SCM_FPORT_FDES (sock);
  266. if (0);
  267. #ifdef SO_LINGER
  268. else if (ilevel == SOL_SOCKET && ioptname == SO_LINGER)
  269. {
  270. #ifdef HAVE_STRUCT_LINGER
  271. struct linger ling;
  272. SCM_ASSERT (SCM_CONSP (value)
  273. && SCM_INUMP (SCM_CAR (value))
  274. && SCM_INUMP (SCM_CDR (value)),
  275. value, SCM_ARG4, FUNC_NAME);
  276. ling.l_onoff = SCM_INUM (SCM_CAR (value));
  277. ling.l_linger = SCM_INUM (SCM_CDR (value));
  278. optlen = (int) sizeof (struct linger);
  279. memcpy (optval, (void *) &ling, optlen);
  280. #else
  281. scm_sizet ling;
  282. SCM_ASSERT (SCM_CONSP (value)
  283. && SCM_INUMP (SCM_CAR (value))
  284. && SCM_INUMP (SCM_CDR (value)),
  285. value, SCM_ARG4, FUNC_NAME);
  286. ling = SCM_INUM (SCM_CAR (value));
  287. optlen = (int) sizeof (scm_sizet);
  288. (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
  289. #endif
  290. }
  291. #endif
  292. #ifdef SO_SNDBUF
  293. else if (ilevel == SOL_SOCKET && ioptname == SO_SNDBUF)
  294. {
  295. SCM_VALIDATE_INUM (4,value);
  296. optlen = (int) sizeof (scm_sizet);
  297. (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
  298. }
  299. #endif
  300. #ifdef SO_RCVBUF
  301. else if (ilevel == SOL_SOCKET && ioptname == SO_RCVBUF)
  302. {
  303. SCM_VALIDATE_INUM (4,value);
  304. optlen = (int) sizeof (scm_sizet);
  305. (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
  306. }
  307. #endif
  308. else
  309. {
  310. /* Most options just take an int. */
  311. SCM_VALIDATE_INUM (4,value);
  312. optlen = (int) sizeof (int);
  313. (*(int *) optval) = (int) SCM_INUM (value);
  314. }
  315. if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
  316. SCM_SYSERROR;
  317. return SCM_UNSPECIFIED;
  318. }
  319. #undef FUNC_NAME
  320. SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
  321. (SCM sock, SCM how),
  322. "Sockets can be closed simply by using @code{close-port}. The\n"
  323. "@code{shutdown} procedure allows reception or tranmission on a\n"
  324. "connection to be shut down individually, according to the parameter\n"
  325. "@var{how}:\n\n"
  326. "@table @asis\n"
  327. "@item 0\n"
  328. "Stop receiving data for this socket. If further data arrives, reject it.\n"
  329. "@item 1\n"
  330. "Stop trying to transmit data from this socket. Discard any\n"
  331. "data waiting to be sent. Stop looking for acknowledgement of\n"
  332. "data already sent; don't retransmit it if it is lost.\n"
  333. "@item 2\n"
  334. "Stop both reception and transmission.\n"
  335. "@end table\n\n"
  336. "The return value is unspecified.")
  337. #define FUNC_NAME s_scm_shutdown
  338. {
  339. int fd;
  340. sock = SCM_COERCE_OUTPORT (sock);
  341. SCM_VALIDATE_OPFPORT (1,sock);
  342. SCM_VALIDATE_INUM (2,how);
  343. SCM_ASSERT_RANGE(2,how,0 <= SCM_INUM (how) && 2 >= SCM_INUM (how));
  344. fd = SCM_FPORT_FDES (sock);
  345. if (shutdown (fd, SCM_INUM (how)) == -1)
  346. SCM_SYSERROR;
  347. return SCM_UNSPECIFIED;
  348. }
  349. #undef FUNC_NAME
  350. /* convert fam/address/args into a sockaddr of the appropriate type.
  351. args is modified by removing the arguments actually used.
  352. which_arg and proc are used when reporting errors:
  353. which_arg is the position of address in the original argument list.
  354. proc is the name of the original procedure.
  355. size returns the size of the structure allocated. */
  356. static struct sockaddr *
  357. scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,scm_sizet *size)
  358. {
  359. switch (fam)
  360. {
  361. case AF_INET:
  362. {
  363. SCM isport;
  364. struct sockaddr_in *soka;
  365. soka = (struct sockaddr_in *)
  366. scm_must_malloc (sizeof (struct sockaddr_in), proc);
  367. /* e.g., for BSDs which don't like invalid sin_len. */
  368. memset (soka, 0, sizeof (struct sockaddr_in));
  369. soka->sin_family = AF_INET;
  370. soka->sin_addr.s_addr =
  371. htonl (scm_num2ulong (address, (char *) which_arg, proc));
  372. SCM_ASSERT (SCM_CONSP (*args), *args,
  373. which_arg + 1, proc);
  374. isport = SCM_CAR (*args);
  375. *args = SCM_CDR (*args);
  376. SCM_ASSERT (SCM_INUMP (isport), isport, which_arg + 1, proc);
  377. soka->sin_port = htons (SCM_INUM (isport));
  378. *size = sizeof (struct sockaddr_in);
  379. return (struct sockaddr *) soka;
  380. }
  381. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  382. case AF_UNIX:
  383. {
  384. struct sockaddr_un *soka;
  385. soka = (struct sockaddr_un *)
  386. scm_must_malloc (sizeof (struct sockaddr_un), proc);
  387. memset (soka, 0, sizeof (struct sockaddr_un));
  388. soka->sun_family = AF_UNIX;
  389. SCM_ASSERT (SCM_ROSTRINGP (address), address,
  390. which_arg, proc);
  391. memcpy (soka->sun_path, SCM_ROCHARS (address),
  392. 1 + SCM_ROLENGTH (address));
  393. *size = sizeof (struct sockaddr_un);
  394. return (struct sockaddr *) soka;
  395. }
  396. #endif
  397. default:
  398. scm_out_of_range (proc, SCM_MAKINUM (fam));
  399. }
  400. }
  401. SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
  402. (SCM sock, SCM fam, SCM address, SCM args),
  403. "Initiates a connection from @var{socket} to the address\n"
  404. "specified by @var{address} and possibly @var{arg @dots{}}. The format\n"
  405. "required for @var{address}\n"
  406. "and @var{arg} @dots{} depends on the family of the socket.\n\n"
  407. "For a socket of family @code{AF_UNIX},\n"
  408. "only @code{address} is specified and must be a string with the\n"
  409. "filename where the socket is to be created.\n\n"
  410. "For a socket of family @code{AF_INET},\n"
  411. "@code{address} must be an integer Internet host address and @var{arg} @dots{}\n"
  412. "must be a single integer port number.\n\n"
  413. "The return value is unspecified.")
  414. #define FUNC_NAME s_scm_connect
  415. {
  416. int fd;
  417. struct sockaddr *soka;
  418. scm_sizet size;
  419. sock = SCM_COERCE_OUTPORT (sock);
  420. SCM_VALIDATE_OPFPORT (1,sock);
  421. SCM_VALIDATE_INUM (2,fam);
  422. fd = SCM_FPORT_FDES (sock);
  423. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME, &size);
  424. if (connect (fd, soka, size) == -1)
  425. SCM_SYSERROR;
  426. scm_must_free ((char *) soka);
  427. return SCM_UNSPECIFIED;
  428. }
  429. #undef FUNC_NAME
  430. SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
  431. (SCM sock, SCM fam, SCM address, SCM args),
  432. "Assigns an address to the socket port @var{socket}.\n"
  433. "Generally this only needs to be done for server sockets,\n"
  434. "so they know where to look for incoming connections. A socket\n"
  435. "without an address will be assigned one automatically when it\n"
  436. "starts communicating.\n\n"
  437. "The format of @var{address} and @var{ARG} @dots{} depends on the family\n"
  438. "of the socket.\n\n"
  439. "For a socket of family @code{AF_UNIX}, only @var{address}\n"
  440. "is specified and must \n"
  441. "be a string with the filename where the socket is to be created.\n\n"
  442. "For a socket of family @code{AF_INET}, @var{address} must be an integer\n"
  443. "Internet host address and @var{arg} @dots{} must be a single integer\n"
  444. "port number.\n\n"
  445. "The values of the following variables can also be used for @var{address}:\n\n"
  446. "@defvar INADDR_ANY\n"
  447. "Allow connections from any address.\n"
  448. "@end defvar\n\n"
  449. "@defvar INADDR_LOOPBACK\n"
  450. "The address of the local host using the loopback device.\n"
  451. "@end defvar\n\n"
  452. "@defvar INADDR_BROADCAST\n"
  453. "The broadcast address on the local network.\n"
  454. "@end defvar\n\n"
  455. "@defvar INADDR_NONE\n"
  456. "No address.\n"
  457. "@end defvar\n\n"
  458. "The return value is unspecified.")
  459. #define FUNC_NAME s_scm_bind
  460. {
  461. int rv;
  462. struct sockaddr *soka;
  463. scm_sizet size;
  464. int fd;
  465. sock = SCM_COERCE_OUTPORT (sock);
  466. SCM_VALIDATE_OPFPORT (1,sock);
  467. SCM_VALIDATE_INUM (2,fam);
  468. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME, &size);
  469. fd = SCM_FPORT_FDES (sock);
  470. rv = bind (fd, soka, size);
  471. if (rv == -1)
  472. SCM_SYSERROR;
  473. scm_must_free ((char *) soka);
  474. return SCM_UNSPECIFIED;
  475. }
  476. #undef FUNC_NAME
  477. SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
  478. (SCM sock, SCM backlog),
  479. "This procedure enables @var{socket} to accept connection\n"
  480. "requests. @var{backlog} is an integer specifying\n"
  481. "the maximum length of the queue for pending connections.\n"
  482. "If the queue fills, new clients will fail to connect until the\n"
  483. "server calls @code{accept} to accept a connection from the queue.\n\n"
  484. "The return value is unspecified.")
  485. #define FUNC_NAME s_scm_listen
  486. {
  487. int fd;
  488. sock = SCM_COERCE_OUTPORT (sock);
  489. SCM_VALIDATE_OPFPORT (1,sock);
  490. SCM_VALIDATE_INUM (2,backlog);
  491. fd = SCM_FPORT_FDES (sock);
  492. if (listen (fd, SCM_INUM (backlog)) == -1)
  493. SCM_SYSERROR;
  494. return SCM_UNSPECIFIED;
  495. }
  496. #undef FUNC_NAME
  497. /* Put the components of a sockaddr into a new SCM vector. */
  498. static SCM
  499. scm_addr_vector (struct sockaddr *address,const char *proc)
  500. {
  501. short int fam = address->sa_family;
  502. SCM result;
  503. SCM *ve;
  504. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  505. if (fam == AF_UNIX)
  506. {
  507. struct sockaddr_un *nad = (struct sockaddr_un *) address;
  508. result = scm_make_vector (SCM_MAKINUM (2), SCM_UNSPECIFIED);
  509. ve = SCM_VELTS (result);
  510. ve[0] = scm_ulong2num ((unsigned long) fam);
  511. ve[1] = scm_makfromstr (nad->sun_path,
  512. (scm_sizet) strlen (nad->sun_path), 0);
  513. }
  514. else
  515. #endif
  516. if (fam == AF_INET)
  517. {
  518. struct sockaddr_in *nad = (struct sockaddr_in *) address;
  519. result = scm_make_vector (SCM_MAKINUM (3), SCM_UNSPECIFIED);
  520. ve = SCM_VELTS (result);
  521. ve[0] = scm_ulong2num ((unsigned long) fam);
  522. ve[1] = scm_ulong2num (ntohl (nad->sin_addr.s_addr));
  523. ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin_port));
  524. }
  525. else
  526. scm_misc_error (proc, "Unrecognised address family: ~A",
  527. scm_listify (SCM_MAKINUM (fam), SCM_UNDEFINED));
  528. return result;
  529. }
  530. /* Allocate a buffer large enough to hold any sockaddr type. */
  531. static char *scm_addr_buffer;
  532. static size_t scm_addr_buffer_size;
  533. static void
  534. scm_init_addr_buffer (void)
  535. {
  536. scm_addr_buffer_size =
  537. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  538. sizeof (struct sockaddr_un)
  539. #else
  540. 0
  541. #endif
  542. ;
  543. if (sizeof (struct sockaddr_in) > scm_addr_buffer_size)
  544. scm_addr_buffer_size = sizeof (struct sockaddr_in);
  545. scm_addr_buffer = scm_must_malloc (scm_addr_buffer_size, "address buffer");
  546. }
  547. SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
  548. (SCM sock),
  549. "Accepts a connection on a bound, listening socket @var{socket}. If there\n"
  550. "are no pending connections in the queue, it waits until\n"
  551. "one is available unless the non-blocking option has been set on the\n"
  552. "socket.\n\n"
  553. "The return value is a\n"
  554. "pair in which the CAR is a new socket port for the connection and\n"
  555. "the CDR is an object with address information about the client which\n"
  556. "initiated the connection.\n\n"
  557. "If the address is not available then the CDR will be an empty vector.\n\n"
  558. "@var{socket} does not become part of the\n"
  559. "connection and will continue to accept new requests.")
  560. #define FUNC_NAME s_scm_accept
  561. {
  562. int fd;
  563. int newfd;
  564. SCM address;
  565. SCM newsock;
  566. size_t tmp_size;
  567. sock = SCM_COERCE_OUTPORT (sock);
  568. SCM_VALIDATE_OPFPORT (1,sock);
  569. fd = SCM_FPORT_FDES (sock);
  570. tmp_size = scm_addr_buffer_size;
  571. newfd = accept (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size);
  572. newsock = scm_sock_fd_to_port (newfd, FUNC_NAME);
  573. if (tmp_size > 0)
  574. address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
  575. else
  576. address = SCM_BOOL_F;
  577. return scm_cons (newsock, address);
  578. }
  579. #undef FUNC_NAME
  580. SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
  581. (SCM sock),
  582. "Returns the address of @var{socket}, in the same form as the object\n"
  583. "returned by @code{accept}. On many systems the address of a socket\n"
  584. "in the @code{AF_FILE} namespace cannot be read.")
  585. #define FUNC_NAME s_scm_getsockname
  586. {
  587. size_t tmp_size;
  588. int fd;
  589. SCM result;
  590. sock = SCM_COERCE_OUTPORT (sock);
  591. SCM_VALIDATE_OPFPORT (1,sock);
  592. fd = SCM_FPORT_FDES (sock);
  593. tmp_size = scm_addr_buffer_size;
  594. if (getsockname (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size) == -1)
  595. SCM_SYSERROR;
  596. if (tmp_size > 0)
  597. result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
  598. else
  599. result = SCM_BOOL_F;
  600. return result;
  601. }
  602. #undef FUNC_NAME
  603. SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
  604. (SCM sock),
  605. "Returns the address of the socket that the socket @var{socket} is connected to,\n"
  606. "in the same form as the object\n"
  607. "returned by @code{accept}. On many systems the address of a socket\n"
  608. "in the @code{AF_FILE} namespace cannot be read.")
  609. #define FUNC_NAME s_scm_getpeername
  610. {
  611. size_t tmp_size;
  612. int fd;
  613. SCM result;
  614. sock = SCM_COERCE_OUTPORT (sock);
  615. SCM_VALIDATE_OPFPORT (1,sock);
  616. fd = SCM_FPORT_FDES (sock);
  617. tmp_size = scm_addr_buffer_size;
  618. if (getpeername (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size) == -1)
  619. SCM_SYSERROR;
  620. if (tmp_size > 0)
  621. result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
  622. else
  623. result = SCM_BOOL_F;
  624. return result;
  625. }
  626. #undef FUNC_NAME
  627. SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
  628. (SCM sock, SCM buf, SCM flags),
  629. "Receives data from the socket port @var{socket}. @var{socket} must already\n"
  630. "be bound to the address from which data is to be received.\n"
  631. "@var{buf} is a string into which\n"
  632. "the data will be written. The size of @var{buf} limits the amount of\n"
  633. "data which can be received: in the case of packet\n"
  634. "protocols, if a packet larger than this limit is encountered then some data\n"
  635. "will be irrevocably lost.\n\n"
  636. "The optional @var{flags} argument is a value or\n"
  637. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  638. "The value returned is the number of bytes read from the socket.\n\n"
  639. "Note that the data is read directly from the socket file descriptor:any unread buffered port data is ignored.")
  640. #define FUNC_NAME s_scm_recv
  641. {
  642. int rv;
  643. int fd;
  644. int flg;
  645. SCM_VALIDATE_OPFPORT (1,sock);
  646. SCM_VALIDATE_STRING (2,buf);
  647. SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
  648. fd = SCM_FPORT_FDES (sock);
  649. SCM_SYSCALL (rv = recv (fd, SCM_CHARS (buf), SCM_LENGTH (buf), flg));
  650. if (rv == -1)
  651. SCM_SYSERROR;
  652. return SCM_MAKINUM (rv);
  653. }
  654. #undef FUNC_NAME
  655. SCM_DEFINE (scm_send, "send", 2, 1, 0,
  656. (SCM sock, SCM message, SCM flags),
  657. "Transmits the string @var{message} on the socket port @var{socket}. \n"
  658. "@var{socket} must already be bound to a destination address. The\n"
  659. "value returned is the number of bytes transmitted -- it's possible for\n"
  660. "this to be less than the length of @var{message} if the socket is\n"
  661. "set to be non-blocking. The optional @var{flags} argument is a value or\n"
  662. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  663. "Note that the data is written directly to the socket file descriptor:\n"
  664. "any unflushed buffered port data is ignored.")
  665. #define FUNC_NAME s_scm_send
  666. {
  667. int rv;
  668. int fd;
  669. int flg;
  670. sock = SCM_COERCE_OUTPORT (sock);
  671. SCM_VALIDATE_OPFPORT (1,sock);
  672. SCM_VALIDATE_ROSTRING (2,message);
  673. SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
  674. fd = SCM_FPORT_FDES (sock);
  675. SCM_SYSCALL (rv = send (fd, SCM_ROCHARS (message), SCM_ROLENGTH (message), flg));
  676. if (rv == -1)
  677. SCM_SYSERROR;
  678. return SCM_MAKINUM (rv);
  679. }
  680. #undef FUNC_NAME
  681. SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
  682. (SCM sock, SCM buf, SCM flags, SCM start, SCM end),
  683. "Returns data from the socket port @var{socket} and also information about\n"
  684. "where the data was received from. @var{socket} must already\n"
  685. "be bound to the address from which data is to be received.\n"
  686. "@code{buf}, is a string into which\n"
  687. "the data will be written. The size of @var{buf} limits the amount of\n"
  688. "data which can be received: in the case of packet\n"
  689. "protocols, if a packet larger than this limit is encountered then some data\n"
  690. "will be irrevocably lost.\n\n"
  691. "The optional @var{flags} argument is a value or\n"
  692. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  693. "The value returned is a pair: the CAR is the number of bytes read from\n"
  694. "the socket and the CDR an address object in the same form as returned by\n"
  695. "@code{accept}.\n\n"
  696. "The @var{start} and @var{end} arguments specify a substring of @var{buf}\n"
  697. "to which the data should be written.\n\n"
  698. "Note that the data is read directly from the socket file descriptor:\n"
  699. "any unread buffered port data is ignored.")
  700. #define FUNC_NAME s_scm_recvfrom
  701. {
  702. int rv;
  703. int fd;
  704. int flg;
  705. int offset = 0;
  706. int cend;
  707. size_t tmp_size;
  708. SCM address;
  709. SCM_VALIDATE_OPFPORT (1,sock);
  710. SCM_VALIDATE_STRING (2,buf);
  711. cend = SCM_LENGTH (buf);
  712. if (SCM_UNBNDP (flags))
  713. flg = 0;
  714. else
  715. {
  716. flg = SCM_NUM2ULONG (3,flags);
  717. if (!SCM_UNBNDP (start))
  718. {
  719. offset = (int) SCM_NUM2LONG (4,start);
  720. if (offset < 0 || offset >= cend)
  721. SCM_OUT_OF_RANGE (4, start);
  722. if (!SCM_UNBNDP (end))
  723. {
  724. int tend = (int) SCM_NUM2LONG (5,end);
  725. if (tend <= offset || tend > cend)
  726. SCM_OUT_OF_RANGE (5, end);
  727. cend = tend;
  728. }
  729. }
  730. }
  731. fd = SCM_FPORT_FDES (sock);
  732. tmp_size = scm_addr_buffer_size;
  733. SCM_SYSCALL (rv = recvfrom (fd, SCM_CHARS (buf) + offset,
  734. cend - offset, flg,
  735. (struct sockaddr *) scm_addr_buffer,
  736. &tmp_size));
  737. if (rv == -1)
  738. SCM_SYSERROR;
  739. if (tmp_size > 0)
  740. address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
  741. else
  742. address = SCM_BOOL_F;
  743. return scm_cons (SCM_MAKINUM (rv), address);
  744. }
  745. #undef FUNC_NAME
  746. SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
  747. (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
  748. "Transmits the string @var{message} on the socket port @var{socket}. The\n"
  749. "destination address is specified using the @var{family}, @var{address} and\n"
  750. "@var{arg} arguments, in a similar way to the @code{connect}\n"
  751. "procedure. The\n"
  752. "value returned is the number of bytes transmitted -- it's possible for\n"
  753. "this to be less than the length of @var{message} if the socket is\n"
  754. "set to be non-blocking. The optional @var{flags} argument is a value or\n"
  755. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  756. "Note that the data is written directly to the socket file descriptor:\n"
  757. "any unflushed buffered port data is ignored.")
  758. #define FUNC_NAME s_scm_sendto
  759. {
  760. int rv;
  761. int fd;
  762. int flg;
  763. struct sockaddr *soka;
  764. scm_sizet size;
  765. int save_err;
  766. sock = SCM_COERCE_OUTPORT (sock);
  767. SCM_VALIDATE_FPORT (1,sock);
  768. SCM_VALIDATE_ROSTRING (2,message);
  769. SCM_VALIDATE_INUM (3,fam);
  770. fd = SCM_FPORT_FDES (sock);
  771. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args_and_flags, 4,
  772. FUNC_NAME, &size);
  773. if (SCM_NULLP (args_and_flags))
  774. flg = 0;
  775. else
  776. {
  777. SCM_VALIDATE_CONS (5,args_and_flags);
  778. flg = SCM_NUM2ULONG (5,SCM_CAR (args_and_flags));
  779. }
  780. SCM_SYSCALL (rv = sendto (fd, SCM_ROCHARS (message), SCM_ROLENGTH (message),
  781. flg, soka, size));
  782. save_err = errno;
  783. scm_must_free ((char *) soka);
  784. errno = save_err;
  785. if (rv == -1)
  786. SCM_SYSERROR;
  787. return SCM_MAKINUM (rv);
  788. }
  789. #undef FUNC_NAME
  790. void
  791. scm_init_socket ()
  792. {
  793. /* protocol families. */
  794. #ifdef AF_UNSPEC
  795. scm_sysintern ("AF_UNSPEC", SCM_MAKINUM (AF_UNSPEC));
  796. #endif
  797. #ifdef AF_UNIX
  798. scm_sysintern ("AF_UNIX", SCM_MAKINUM (AF_UNIX));
  799. #endif
  800. #ifdef AF_INET
  801. scm_sysintern ("AF_INET", SCM_MAKINUM (AF_INET));
  802. #endif
  803. #ifdef PF_UNSPEC
  804. scm_sysintern ("PF_UNSPEC", SCM_MAKINUM (PF_UNSPEC));
  805. #endif
  806. #ifdef PF_UNIX
  807. scm_sysintern ("PF_UNIX", SCM_MAKINUM (PF_UNIX));
  808. #endif
  809. #ifdef PF_INET
  810. scm_sysintern ("PF_INET", SCM_MAKINUM (PF_INET));
  811. #endif
  812. /* socket types. */
  813. #ifdef SOCK_STREAM
  814. scm_sysintern ("SOCK_STREAM", SCM_MAKINUM (SOCK_STREAM));
  815. #endif
  816. #ifdef SOCK_DGRAM
  817. scm_sysintern ("SOCK_DGRAM", SCM_MAKINUM (SOCK_DGRAM));
  818. #endif
  819. #ifdef SOCK_RAW
  820. scm_sysintern ("SOCK_RAW", SCM_MAKINUM (SOCK_RAW));
  821. #endif
  822. /* setsockopt level. */
  823. #ifdef SOL_SOCKET
  824. scm_sysintern ("SOL_SOCKET", SCM_MAKINUM (SOL_SOCKET));
  825. #endif
  826. #ifdef SOL_IP
  827. scm_sysintern ("SOL_IP", SCM_MAKINUM (SOL_IP));
  828. #endif
  829. #ifdef SOL_TCP
  830. scm_sysintern ("SOL_TCP", SCM_MAKINUM (SOL_TCP));
  831. #endif
  832. #ifdef SOL_UDP
  833. scm_sysintern ("SOL_UDP", SCM_MAKINUM (SOL_UDP));
  834. #endif
  835. /* setsockopt names. */
  836. #ifdef SO_DEBUG
  837. scm_sysintern ("SO_DEBUG", SCM_MAKINUM (SO_DEBUG));
  838. #endif
  839. #ifdef SO_REUSEADDR
  840. scm_sysintern ("SO_REUSEADDR", SCM_MAKINUM (SO_REUSEADDR));
  841. #endif
  842. #ifdef SO_STYLE
  843. scm_sysintern ("SO_STYLE", SCM_MAKINUM (SO_STYLE));
  844. #endif
  845. #ifdef SO_TYPE
  846. scm_sysintern ("SO_TYPE", SCM_MAKINUM (SO_TYPE));
  847. #endif
  848. #ifdef SO_ERROR
  849. scm_sysintern ("SO_ERROR", SCM_MAKINUM (SO_ERROR));
  850. #endif
  851. #ifdef SO_DONTROUTE
  852. scm_sysintern ("SO_DONTROUTE", SCM_MAKINUM (SO_DONTROUTE));
  853. #endif
  854. #ifdef SO_BROADCAST
  855. scm_sysintern ("SO_BROADCAST", SCM_MAKINUM (SO_BROADCAST));
  856. #endif
  857. #ifdef SO_SNDBUF
  858. scm_sysintern ("SO_SNDBUF", SCM_MAKINUM (SO_SNDBUF));
  859. #endif
  860. #ifdef SO_RCVBUF
  861. scm_sysintern ("SO_RCVBUF", SCM_MAKINUM (SO_RCVBUF));
  862. #endif
  863. #ifdef SO_KEEPALIVE
  864. scm_sysintern ("SO_KEEPALIVE", SCM_MAKINUM (SO_KEEPALIVE));
  865. #endif
  866. #ifdef SO_OOBINLINE
  867. scm_sysintern ("SO_OOBINLINE", SCM_MAKINUM (SO_OOBINLINE));
  868. #endif
  869. #ifdef SO_NO_CHECK
  870. scm_sysintern ("SO_NO_CHECK", SCM_MAKINUM (SO_NO_CHECK));
  871. #endif
  872. #ifdef SO_PRIORITY
  873. scm_sysintern ("SO_PRIORITY", SCM_MAKINUM (SO_PRIORITY));
  874. #endif
  875. #ifdef SO_LINGER
  876. scm_sysintern ("SO_LINGER", SCM_MAKINUM (SO_LINGER));
  877. #endif
  878. /* recv/send options. */
  879. #ifdef MSG_OOB
  880. scm_sysintern ("MSG_OOB", SCM_MAKINUM (MSG_OOB));
  881. #endif
  882. #ifdef MSG_PEEK
  883. scm_sysintern ("MSG_PEEK", SCM_MAKINUM (MSG_PEEK));
  884. #endif
  885. #ifdef MSG_DONTROUTE
  886. scm_sysintern ("MSG_DONTROUTE", SCM_MAKINUM (MSG_DONTROUTE));
  887. #endif
  888. scm_add_feature ("socket");
  889. scm_init_addr_buffer ();
  890. #include "libguile/socket.x"
  891. }
  892. /*
  893. Local Variables:
  894. c-file-style: "gnu"
  895. End:
  896. */