socket.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /* Copyright (C) 1996-1998, 2000-2007, 2009, 2011-2015
  2. * 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. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <errno.h>
  23. #include <verify.h>
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  34. #include <sys/un.h>
  35. #endif
  36. #include <netinet/in.h>
  37. #include <netinet/tcp.h>
  38. #include <netdb.h>
  39. #include <arpa/inet.h>
  40. #include <gmp.h>
  41. #include "libguile/_scm.h"
  42. #include "libguile/arrays.h"
  43. #include "libguile/feature.h"
  44. #include "libguile/fports.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/vectors.h"
  47. #include "libguile/dynwind.h"
  48. #include "libguile/srfi-13.h"
  49. #include "libguile/validate.h"
  50. #include "libguile/socket.h"
  51. #if SCM_ENABLE_DEPRECATED == 1
  52. # include "libguile/deprecation.h"
  53. #endif
  54. #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
  55. #define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path) \
  56. + strlen ((ptr)->sun_path))
  57. #endif
  58. /* The largest possible socket address. Wrapping it in a union guarantees
  59. that the compiler will make it suitably aligned. */
  60. typedef union
  61. {
  62. struct sockaddr sockaddr;
  63. struct sockaddr_in sockaddr_in;
  64. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  65. struct sockaddr_un sockaddr_un;
  66. #endif
  67. #ifdef HAVE_IPV6
  68. struct sockaddr_in6 sockaddr_in6;
  69. #endif
  70. } scm_t_max_sockaddr;
  71. /* Maximum size of a socket address. */
  72. #define MAX_ADDR_SIZE (sizeof (scm_t_max_sockaddr))
  73. #ifdef HAVE_INET_NETOF
  74. SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0,
  75. (SCM address),
  76. "Return the network number part of the given IPv4\n"
  77. "Internet address. E.g.,\n\n"
  78. "@lisp\n"
  79. "(inet-netof 2130706433) @result{} 127\n"
  80. "@end lisp")
  81. #define FUNC_NAME s_scm_inet_netof
  82. {
  83. struct in_addr addr;
  84. addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  85. return scm_from_ulong (inet_netof (addr));
  86. }
  87. #undef FUNC_NAME
  88. #endif
  89. #ifdef HAVE_INET_LNAOF
  90. SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0,
  91. (SCM address),
  92. "Return the local-address-with-network part of the given\n"
  93. "IPv4 Internet address, using the obsolete class A/B/C system.\n"
  94. "E.g.,\n\n"
  95. "@lisp\n"
  96. "(inet-lnaof 2130706433) @result{} 1\n"
  97. "@end lisp")
  98. #define FUNC_NAME s_scm_lnaof
  99. {
  100. struct in_addr addr;
  101. addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  102. return scm_from_ulong (inet_lnaof (addr));
  103. }
  104. #undef FUNC_NAME
  105. #endif
  106. #ifdef HAVE_INET_MAKEADDR
  107. SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
  108. (SCM net, SCM lna),
  109. "Make an IPv4 Internet address by combining the network number\n"
  110. "@var{net} with the local-address-within-network number\n"
  111. "@var{lna}. E.g.,\n\n"
  112. "@lisp\n"
  113. "(inet-makeaddr 127 1) @result{} 2130706433\n"
  114. "@end lisp")
  115. #define FUNC_NAME s_scm_inet_makeaddr
  116. {
  117. struct in_addr addr;
  118. unsigned long netnum;
  119. unsigned long lnanum;
  120. netnum = SCM_NUM2ULONG (1, net);
  121. lnanum = SCM_NUM2ULONG (2, lna);
  122. addr = inet_makeaddr (netnum, lnanum);
  123. return scm_from_ulong (ntohl (addr.s_addr));
  124. }
  125. #undef FUNC_NAME
  126. #endif
  127. #ifdef HAVE_IPV6
  128. /* flip a 128 bit IPv6 address between host and network order. */
  129. #ifdef WORDS_BIGENDIAN
  130. #define FLIP_NET_HOST_128(addr)
  131. #else
  132. #define FLIP_NET_HOST_128(addr)\
  133. {\
  134. int i;\
  135. \
  136. for (i = 0; i < 8; i++)\
  137. {\
  138. scm_t_uint8 c = (addr)[i];\
  139. \
  140. (addr)[i] = (addr)[15 - i];\
  141. (addr)[15 - i] = c;\
  142. }\
  143. }
  144. #endif
  145. #ifdef WORDS_BIGENDIAN
  146. #define FLIPCPY_NET_HOST_128(dest, src) memcpy (dest, src, 16)
  147. #else
  148. #define FLIPCPY_NET_HOST_128(dest, src) \
  149. { \
  150. const scm_t_uint8 *tmp_srcp = (src) + 15; \
  151. scm_t_uint8 *tmp_destp = (dest); \
  152. \
  153. do { \
  154. *tmp_destp++ = *tmp_srcp--; \
  155. } while (tmp_srcp != (src)); \
  156. }
  157. #endif
  158. #if (SIZEOF_SCM_T_BITS * SCM_CHAR_BIT) > 128
  159. #error "Assumption that scm_t_bits <= 128 bits has been violated."
  160. #endif
  161. #if (SIZEOF_UNSIGNED_LONG * SCM_CHAR_BIT) > 128
  162. #error "Assumption that unsigned long <= 128 bits has been violated."
  163. #endif
  164. #if (SIZEOF_UNSIGNED_LONG_LONG * SCM_CHAR_BIT) > 128
  165. #error "Assumption that unsigned long long <= 128 bits has been violated."
  166. #endif
  167. /* convert a 128 bit IPv6 address in network order to a host ordered
  168. SCM integer. */
  169. static SCM
  170. scm_from_ipv6 (const scm_t_uint8 *src)
  171. {
  172. SCM result = scm_i_mkbig ();
  173. mpz_import (SCM_I_BIG_MPZ (result),
  174. 1, /* chunk */
  175. 1, /* big-endian chunk ordering */
  176. 16, /* chunks are 16 bytes long */
  177. 1, /* big-endian byte ordering */
  178. 0, /* "nails" -- leading unused bits per chunk */
  179. src);
  180. return scm_i_normbig (result);
  181. }
  182. /* convert a host ordered SCM integer to a 128 bit IPv6 address in
  183. network order. */
  184. static void
  185. scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
  186. {
  187. if (SCM_I_INUMP (src))
  188. {
  189. scm_t_signed_bits n = SCM_I_INUM (src);
  190. if (n < 0)
  191. scm_out_of_range (NULL, src);
  192. #ifdef WORDS_BIGENDIAN
  193. memset (dst, 0, 16 - sizeof (scm_t_signed_bits));
  194. memcpy (dst + (16 - sizeof (scm_t_signed_bits)),
  195. &n,
  196. sizeof (scm_t_signed_bits));
  197. #else
  198. memset (dst + sizeof (scm_t_signed_bits),
  199. 0,
  200. 16 - sizeof (scm_t_signed_bits));
  201. /* FIXME: this pair of ops is kinda wasteful -- should rewrite as
  202. a single loop perhaps, similar to the handling of bignums. */
  203. memcpy (dst, &n, sizeof (scm_t_signed_bits));
  204. FLIP_NET_HOST_128 (dst);
  205. #endif
  206. }
  207. else if (SCM_BIGP (src))
  208. {
  209. size_t count;
  210. if ((mpz_sgn (SCM_I_BIG_MPZ (src)) < 0)
  211. || mpz_sizeinbase (SCM_I_BIG_MPZ (src), 2) > 128)
  212. scm_out_of_range (NULL, src);
  213. memset (dst, 0, 16);
  214. mpz_export (dst,
  215. &count,
  216. 1, /* big-endian chunk ordering */
  217. 16, /* chunks are 16 bytes long */
  218. 1, /* big-endian byte ordering */
  219. 0, /* "nails" -- leading unused bits per chunk */
  220. SCM_I_BIG_MPZ (src));
  221. scm_remember_upto_here_1 (src);
  222. }
  223. else
  224. scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer");
  225. }
  226. #endif /* HAVE_IPV6 */
  227. SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
  228. (SCM family, SCM address),
  229. "Convert a network address into a printable string.\n"
  230. "Note that unlike the C version of this function,\n"
  231. "the input is an integer with normal host byte ordering.\n"
  232. "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
  233. "@lisp\n"
  234. "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
  235. "(inet-ntop AF_INET6 (- (expt 2 128) 1))\n"
  236. " @result{} \"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"\n"
  237. "@end lisp")
  238. #define FUNC_NAME s_scm_inet_ntop
  239. {
  240. int af;
  241. #ifdef INET6_ADDRSTRLEN
  242. char dst[INET6_ADDRSTRLEN];
  243. #else
  244. char dst[46];
  245. #endif
  246. const char *result;
  247. af = scm_to_int (family);
  248. SCM_ASSERT_RANGE (1, family,
  249. af == AF_INET
  250. #ifdef HAVE_IPV6
  251. || af == AF_INET6
  252. #endif
  253. );
  254. if (af == AF_INET)
  255. {
  256. scm_t_uint32 addr4;
  257. addr4 = htonl (SCM_NUM2ULONG (2, address));
  258. result = inet_ntop (af, &addr4, dst, sizeof (dst));
  259. }
  260. #ifdef HAVE_IPV6
  261. else if (af == AF_INET6)
  262. {
  263. char addr6[16];
  264. scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
  265. result = inet_ntop (af, &addr6, dst, sizeof (dst));
  266. }
  267. #endif
  268. else
  269. SCM_MISC_ERROR ("unsupported address family", family);
  270. if (result == NULL)
  271. SCM_SYSERROR;
  272. return scm_from_locale_string (dst);
  273. }
  274. #undef FUNC_NAME
  275. SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
  276. (SCM family, SCM address),
  277. "Convert a string containing a printable network address to\n"
  278. "an integer address. Note that unlike the C version of this\n"
  279. "function,\n"
  280. "the result is an integer with normal host byte ordering.\n"
  281. "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
  282. "@lisp\n"
  283. "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
  284. "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
  285. "@end lisp")
  286. #define FUNC_NAME s_scm_inet_pton
  287. {
  288. int af;
  289. char *src;
  290. scm_t_uint32 dst[4];
  291. int rv, eno;
  292. af = scm_to_int (family);
  293. SCM_ASSERT_RANGE (1, family,
  294. af == AF_INET
  295. #ifdef HAVE_IPV6
  296. || af == AF_INET6
  297. #endif
  298. );
  299. src = scm_to_locale_string (address);
  300. rv = inet_pton (af, src, dst);
  301. eno = errno;
  302. free (src);
  303. errno = eno;
  304. if (rv == -1)
  305. SCM_SYSERROR;
  306. else if (rv == 0)
  307. SCM_MISC_ERROR ("Bad address", SCM_EOL);
  308. if (af == AF_INET)
  309. return scm_from_ulong (ntohl (*dst));
  310. #ifdef HAVE_IPV6
  311. else if (af == AF_INET6)
  312. return scm_from_ipv6 ((scm_t_uint8 *) dst);
  313. #endif
  314. else
  315. SCM_MISC_ERROR ("unsupported address family", family);
  316. }
  317. #undef FUNC_NAME
  318. SCM_SYMBOL (sym_socket, "socket");
  319. #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
  320. SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
  321. (SCM family, SCM style, SCM proto),
  322. "Return a new socket port of the type specified by @var{family},\n"
  323. "@var{style} and @var{proto}. All three parameters are\n"
  324. "integers. Supported values for @var{family} are\n"
  325. "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n"
  326. "Typical values for @var{style} are @code{SOCK_STREAM},\n"
  327. "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
  328. "@var{proto} can be obtained from a protocol name using\n"
  329. "@code{getprotobyname}. A value of zero specifies the default\n"
  330. "protocol, which is usually right.\n\n"
  331. "A single socket port cannot by used for communication until it\n"
  332. "has been connected to another socket.")
  333. #define FUNC_NAME s_scm_socket
  334. {
  335. int fd;
  336. fd = socket (scm_to_int (family),
  337. scm_to_int (style),
  338. scm_to_int (proto));
  339. if (fd == -1)
  340. SCM_SYSERROR;
  341. return SCM_SOCK_FD_TO_PORT (fd);
  342. }
  343. #undef FUNC_NAME
  344. #ifdef HAVE_SOCKETPAIR
  345. SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
  346. (SCM family, SCM style, SCM proto),
  347. "Return a pair of connected (but unnamed) socket ports of the\n"
  348. "type specified by @var{family}, @var{style} and @var{proto}.\n"
  349. "Many systems support only socket pairs of the @code{AF_UNIX}\n"
  350. "family. Zero is likely to be the only meaningful value for\n"
  351. "@var{proto}.")
  352. #define FUNC_NAME s_scm_socketpair
  353. {
  354. int fam;
  355. int fd[2];
  356. fam = scm_to_int (family);
  357. if (socketpair (fam, scm_to_int (style), scm_to_int (proto), fd) == -1)
  358. SCM_SYSERROR;
  359. return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
  360. }
  361. #undef FUNC_NAME
  362. #endif
  363. /* Possible results for `getsockopt ()'. Wrapping it into a union guarantees
  364. suitable alignment. */
  365. typedef union
  366. {
  367. #ifdef HAVE_STRUCT_LINGER
  368. struct linger linger;
  369. #endif
  370. size_t size;
  371. int integer;
  372. } scm_t_getsockopt_result;
  373. SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
  374. (SCM sock, SCM level, SCM optname),
  375. "Return an option value from socket port @var{sock}.\n"
  376. "\n"
  377. "@var{level} is an integer specifying a protocol layer, either\n"
  378. "@code{SOL_SOCKET} for socket level options, or a protocol\n"
  379. "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
  380. "(@pxref{Network Databases}).\n"
  381. "\n"
  382. "@defvar SOL_SOCKET\n"
  383. "@defvarx IPPROTO_IP\n"
  384. "@defvarx IPPROTO_TCP\n"
  385. "@defvarx IPPROTO_UDP\n"
  386. "@end defvar\n"
  387. "\n"
  388. "@var{optname} is an integer specifying an option within the\n"
  389. "protocol layer.\n"
  390. "\n"
  391. "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
  392. "defined (when provided by the system). For their meaning see\n"
  393. "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
  394. "Manual}, or @command{man 7 socket}.\n"
  395. "\n"
  396. "@defvar SO_DEBUG\n"
  397. "@defvarx SO_REUSEADDR\n"
  398. "@defvarx SO_STYLE\n"
  399. "@defvarx SO_TYPE\n"
  400. "@defvarx SO_ERROR\n"
  401. "@defvarx SO_DONTROUTE\n"
  402. "@defvarx SO_BROADCAST\n"
  403. "@defvarx SO_SNDBUF\n"
  404. "@defvarx SO_RCVBUF\n"
  405. "@defvarx SO_KEEPALIVE\n"
  406. "@defvarx SO_OOBINLINE\n"
  407. "@defvarx SO_NO_CHECK\n"
  408. "@defvarx SO_PRIORITY\n"
  409. "@defvarx SO_REUSEPORT\n"
  410. "The value returned is an integer.\n"
  411. "@end defvar\n"
  412. "\n"
  413. "@defvar SO_LINGER\n"
  414. "The value returned is a pair of integers\n"
  415. "@code{(@var{enable} . @var{timeout})}. On old systems without\n"
  416. "timeout support (ie.@: without @code{struct linger}), only\n"
  417. "@var{enable} has an effect but the value in Guile is always a\n"
  418. "pair.\n"
  419. "@end defvar")
  420. #define FUNC_NAME s_scm_getsockopt
  421. {
  422. int fd;
  423. /* size of optval is the largest supported option. */
  424. scm_t_getsockopt_result optval;
  425. socklen_t optlen = sizeof (optval);
  426. int ilevel;
  427. int ioptname;
  428. sock = SCM_COERCE_OUTPORT (sock);
  429. SCM_VALIDATE_OPFPORT (1, sock);
  430. ilevel = scm_to_int (level);
  431. ioptname = scm_to_int (optname);
  432. fd = SCM_FPORT_FDES (sock);
  433. if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
  434. SCM_SYSERROR;
  435. if (ilevel == SOL_SOCKET)
  436. {
  437. #ifdef SO_LINGER
  438. if (ioptname == SO_LINGER)
  439. {
  440. #ifdef HAVE_STRUCT_LINGER
  441. struct linger *ling = (struct linger *) &optval;
  442. return scm_cons (scm_from_long (ling->l_onoff),
  443. scm_from_long (ling->l_linger));
  444. #else
  445. return scm_cons (scm_from_long (*(int *) &optval),
  446. scm_from_int (0));
  447. #endif
  448. }
  449. #endif
  450. }
  451. return scm_from_int (*(int *) &optval);
  452. }
  453. #undef FUNC_NAME
  454. SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
  455. (SCM sock, SCM level, SCM optname, SCM value),
  456. "Set an option on socket port @var{sock}. The return value is\n"
  457. "unspecified.\n"
  458. "\n"
  459. "@var{level} is an integer specifying a protocol layer, either\n"
  460. "@code{SOL_SOCKET} for socket level options, or a protocol\n"
  461. "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
  462. "(@pxref{Network Databases}).\n"
  463. "\n"
  464. "@defvar SOL_SOCKET\n"
  465. "@defvarx IPPROTO_IP\n"
  466. "@defvarx IPPROTO_TCP\n"
  467. "@defvarx IPPROTO_UDP\n"
  468. "@end defvar\n"
  469. "\n"
  470. "@var{optname} is an integer specifying an option within the\n"
  471. "protocol layer.\n"
  472. "\n"
  473. "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
  474. "defined (when provided by the system). For their meaning see\n"
  475. "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
  476. "Manual}, or @command{man 7 socket}.\n"
  477. "\n"
  478. "@defvar SO_DEBUG\n"
  479. "@defvarx SO_REUSEADDR\n"
  480. "@defvarx SO_STYLE\n"
  481. "@defvarx SO_TYPE\n"
  482. "@defvarx SO_ERROR\n"
  483. "@defvarx SO_DONTROUTE\n"
  484. "@defvarx SO_BROADCAST\n"
  485. "@defvarx SO_SNDBUF\n"
  486. "@defvarx SO_RCVBUF\n"
  487. "@defvarx SO_KEEPALIVE\n"
  488. "@defvarx SO_OOBINLINE\n"
  489. "@defvarx SO_NO_CHECK\n"
  490. "@defvarx SO_PRIORITY\n"
  491. "@defvarx SO_REUSEPORT\n"
  492. "@var{value} is an integer.\n"
  493. "@end defvar\n"
  494. "\n"
  495. "@defvar SO_LINGER\n"
  496. "@var{value} is a pair of integers @code{(@var{ENABLE}\n"
  497. ". @var{TIMEOUT})}. On old systems without timeout support\n"
  498. "(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
  499. "effect but the value in Guile is always a pair.\n"
  500. "@end defvar\n"
  501. "\n"
  502. "@c Note that we refer only to ``man ip'' here. On GNU/Linux it's\n"
  503. "@c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
  504. "@c \n"
  505. "For IP level (@code{IPPROTO_IP}) the following @var{optname}s\n"
  506. "are defined (when provided by the system). See @command{man\n"
  507. "ip} for what they mean.\n"
  508. "\n"
  509. "@defvar IP_MULTICAST_IF\n"
  510. "This sets the source interface used by multicast traffic.\n"
  511. "@end defvar\n"
  512. "\n"
  513. "@defvar IP_MULTICAST_TTL\n"
  514. "This sets the default TTL for multicast traffic. This defaults \n"
  515. "to 1 and should be increased to allow traffic to pass beyond the\n"
  516. "local network.\n"
  517. "@end defvar\n"
  518. "\n"
  519. "@defvar IP_ADD_MEMBERSHIP\n"
  520. "@defvarx IP_DROP_MEMBERSHIP\n"
  521. "These can be used only with @code{setsockopt}, not\n"
  522. "@code{getsockopt}. @var{value} is a pair\n"
  523. "@code{(@var{MULTIADDR} . @var{INTERFACEADDR})} of IPv4\n"
  524. "addresses (@pxref{Network Address Conversion}).\n"
  525. "@var{MULTIADDR} is a multicast address to be added to or\n"
  526. "dropped from the interface @var{INTERFACEADDR}.\n"
  527. "@var{INTERFACEADDR} can be @code{INADDR_ANY} to have the system\n"
  528. "select the interface. @var{INTERFACEADDR} can also be an\n"
  529. "interface index number, on systems supporting that.\n"
  530. "@end defvar")
  531. #define FUNC_NAME s_scm_setsockopt
  532. {
  533. int fd;
  534. int opt_int;
  535. #ifdef HAVE_STRUCT_LINGER
  536. struct linger opt_linger;
  537. #endif
  538. #ifdef HAVE_STRUCT_IP_MREQ
  539. struct ip_mreq opt_mreq;
  540. #endif
  541. const void *optval = NULL;
  542. socklen_t optlen = 0;
  543. int ilevel, ioptname;
  544. sock = SCM_COERCE_OUTPORT (sock);
  545. SCM_VALIDATE_OPFPORT (1, sock);
  546. ilevel = scm_to_int (level);
  547. ioptname = scm_to_int (optname);
  548. fd = SCM_FPORT_FDES (sock);
  549. if (ilevel == SOL_SOCKET)
  550. {
  551. #ifdef SO_LINGER
  552. if (ioptname == SO_LINGER)
  553. {
  554. #ifdef HAVE_STRUCT_LINGER
  555. SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
  556. opt_linger.l_onoff = scm_to_int (SCM_CAR (value));
  557. opt_linger.l_linger = scm_to_int (SCM_CDR (value));
  558. optlen = sizeof (struct linger);
  559. optval = &opt_linger;
  560. #else
  561. SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
  562. opt_int = scm_to_int (SCM_CAR (value));
  563. /* timeout is ignored, but may as well validate it. */
  564. scm_to_int (SCM_CDR (value));
  565. optlen = sizeof (int);
  566. optval = &opt_int;
  567. #endif
  568. }
  569. #endif
  570. }
  571. #ifdef HAVE_STRUCT_IP_MREQ
  572. if (ilevel == IPPROTO_IP &&
  573. (ioptname == IP_ADD_MEMBERSHIP || ioptname == IP_DROP_MEMBERSHIP))
  574. {
  575. /* Fourth argument must be a pair of addresses. */
  576. SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
  577. opt_mreq.imr_multiaddr.s_addr = htonl (scm_to_ulong (SCM_CAR (value)));
  578. opt_mreq.imr_interface.s_addr = htonl (scm_to_ulong (SCM_CDR (value)));
  579. optlen = sizeof (opt_mreq);
  580. optval = &opt_mreq;
  581. }
  582. #endif
  583. if (optval == NULL)
  584. {
  585. /* Most options take an int. */
  586. opt_int = scm_to_int (value);
  587. optlen = sizeof (int);
  588. optval = &opt_int;
  589. }
  590. if (setsockopt (fd, ilevel, ioptname, optval, optlen) == -1)
  591. SCM_SYSERROR;
  592. return SCM_UNSPECIFIED;
  593. }
  594. #undef FUNC_NAME
  595. /* Our documentation hard-codes this mapping, so make sure it holds. */
  596. verify (SHUT_RD == 0);
  597. verify (SHUT_WR == 1);
  598. verify (SHUT_RDWR == 2);
  599. SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
  600. (SCM sock, SCM how),
  601. "Sockets can be closed simply by using @code{close-port}. The\n"
  602. "@code{shutdown} procedure allows reception or transmission on a\n"
  603. "connection to be shut down individually, according to the parameter\n"
  604. "@var{how}:\n\n"
  605. "@table @asis\n"
  606. "@item 0\n"
  607. "Stop receiving data for this socket. If further data arrives, reject it.\n"
  608. "@item 1\n"
  609. "Stop trying to transmit data from this socket. Discard any\n"
  610. "data waiting to be sent. Stop looking for acknowledgement of\n"
  611. "data already sent; don't retransmit it if it is lost.\n"
  612. "@item 2\n"
  613. "Stop both reception and transmission.\n"
  614. "@end table\n\n"
  615. "The return value is unspecified.")
  616. #define FUNC_NAME s_scm_shutdown
  617. {
  618. int fd;
  619. sock = SCM_COERCE_OUTPORT (sock);
  620. SCM_VALIDATE_OPFPORT (1, sock);
  621. fd = SCM_FPORT_FDES (sock);
  622. if (shutdown (fd, scm_to_signed_integer (how, 0, 2)) == -1)
  623. SCM_SYSERROR;
  624. return SCM_UNSPECIFIED;
  625. }
  626. #undef FUNC_NAME
  627. /* convert fam/address/args into a sockaddr of the appropriate type.
  628. args is modified by removing the arguments actually used.
  629. which_arg and proc are used when reporting errors:
  630. which_arg is the position of address in the original argument list.
  631. proc is the name of the original procedure.
  632. size returns the size of the structure allocated. */
  633. static struct sockaddr *
  634. scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
  635. const char *proc, size_t *size)
  636. #define FUNC_NAME proc
  637. {
  638. switch (fam)
  639. {
  640. case AF_INET:
  641. {
  642. struct sockaddr_in *soka;
  643. unsigned long addr;
  644. int port;
  645. SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
  646. SCM_VALIDATE_CONS (which_arg + 1, *args);
  647. port = scm_to_int (SCM_CAR (*args));
  648. *args = SCM_CDR (*args);
  649. soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));
  650. memset (soka, '\0', sizeof (struct sockaddr_in));
  651. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  652. soka->sin_len = sizeof (struct sockaddr_in);
  653. #endif
  654. soka->sin_family = AF_INET;
  655. soka->sin_addr.s_addr = htonl (addr);
  656. soka->sin_port = htons (port);
  657. *size = sizeof (struct sockaddr_in);
  658. return (struct sockaddr *) soka;
  659. }
  660. #ifdef HAVE_IPV6
  661. case AF_INET6:
  662. {
  663. /* see RFC2553. */
  664. int port;
  665. struct sockaddr_in6 *soka;
  666. unsigned long flowinfo = 0;
  667. unsigned long scope_id = 0;
  668. SCM_VALIDATE_CONS (which_arg + 1, *args);
  669. port = scm_to_int (SCM_CAR (*args));
  670. *args = SCM_CDR (*args);
  671. if (scm_is_pair (*args))
  672. {
  673. SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
  674. *args = SCM_CDR (*args);
  675. if (scm_is_pair (*args))
  676. {
  677. SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
  678. scope_id);
  679. *args = SCM_CDR (*args);
  680. }
  681. }
  682. soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
  683. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
  684. soka->sin6_len = sizeof (struct sockaddr_in6);
  685. #endif
  686. soka->sin6_family = AF_INET6;
  687. scm_to_ipv6 (soka->sin6_addr.s6_addr, address);
  688. soka->sin6_port = htons (port);
  689. soka->sin6_flowinfo = flowinfo;
  690. #ifdef HAVE_SIN6_SCOPE_ID
  691. soka->sin6_scope_id = scope_id;
  692. #endif
  693. *size = sizeof (struct sockaddr_in6);
  694. return (struct sockaddr *) soka;
  695. }
  696. #endif
  697. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  698. case AF_UNIX:
  699. {
  700. struct sockaddr_un *soka;
  701. int addr_size;
  702. char *c_address;
  703. scm_dynwind_begin (0);
  704. c_address = scm_to_locale_string (address);
  705. scm_dynwind_free (c_address);
  706. /* the static buffer size in sockaddr_un seems to be arbitrary
  707. and not necessarily a hard limit. e.g., the glibc manual
  708. suggests it may be possible to declare it size 0. let's
  709. ignore it. if the O/S doesn't like the size it will cause
  710. connect/bind etc., to fail. sun_path is always the last
  711. member of the structure. */
  712. addr_size = sizeof (struct sockaddr_un)
  713. + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
  714. soka = (struct sockaddr_un *) scm_malloc (addr_size);
  715. memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */
  716. soka->sun_family = AF_UNIX;
  717. strcpy (soka->sun_path, c_address);
  718. *size = SUN_LEN (soka);
  719. scm_dynwind_end ();
  720. return (struct sockaddr *) soka;
  721. }
  722. #endif
  723. default:
  724. scm_out_of_range (proc, scm_from_int (fam));
  725. }
  726. }
  727. #undef FUNC_NAME
  728. SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
  729. (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
  730. "Initiate a connection from a socket using a specified address\n"
  731. "family to the address\n"
  732. "specified by @var{address} and possibly @var{args}.\n"
  733. "The format required for @var{address}\n"
  734. "and @var{args} depends on the family of the socket.\n\n"
  735. "For a socket of family @code{AF_UNIX},\n"
  736. "only @var{address} is specified and must be a string with the\n"
  737. "filename where the socket is to be created.\n\n"
  738. "For a socket of family @code{AF_INET},\n"
  739. "@var{address} must be an integer IPv4 host address and\n"
  740. "@var{args} must be a single integer port number.\n\n"
  741. "For a socket of family @code{AF_INET6},\n"
  742. "@var{address} must be an integer IPv6 host address and\n"
  743. "@var{args} may be up to three integers:\n"
  744. "port [flowinfo] [scope_id],\n"
  745. "where flowinfo and scope_id default to zero.\n\n"
  746. "Alternatively, the second argument can be a socket address object "
  747. "as returned by @code{make-socket-address}, in which case the "
  748. "no additional arguments should be passed.\n\n"
  749. "Return true, unless the socket was configured to be non-blocking\n"
  750. "and the operation has not finished yet.\n")
  751. #define FUNC_NAME s_scm_connect
  752. {
  753. int fd;
  754. struct sockaddr *soka;
  755. size_t size;
  756. sock = SCM_COERCE_OUTPORT (sock);
  757. SCM_VALIDATE_OPFPORT (1, sock);
  758. fd = SCM_FPORT_FDES (sock);
  759. if (scm_is_eq (address, SCM_UNDEFINED))
  760. /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
  761. `socket address' object. */
  762. soka = scm_to_sockaddr (fam_or_sockaddr, &size);
  763. else
  764. soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
  765. &args, 3, FUNC_NAME, &size);
  766. if (connect (fd, soka, size) == -1)
  767. {
  768. int save_errno = errno;
  769. free (soka);
  770. errno = save_errno;
  771. if (errno == EINPROGRESS)
  772. return SCM_BOOL_F;
  773. SCM_SYSERROR;
  774. }
  775. free (soka);
  776. return SCM_BOOL_T;
  777. }
  778. #undef FUNC_NAME
  779. SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
  780. (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
  781. "Assign an address to the socket port @var{sock}.\n"
  782. "Generally this only needs to be done for server sockets,\n"
  783. "so they know where to look for incoming connections. A socket\n"
  784. "without an address will be assigned one automatically when it\n"
  785. "starts communicating.\n\n"
  786. "The format of @var{address} and @var{args} depends\n"
  787. "on the family of the socket.\n\n"
  788. "For a socket of family @code{AF_UNIX}, only @var{address}\n"
  789. "is specified and must be a string with the filename where\n"
  790. "the socket is to be created.\n\n"
  791. "For a socket of family @code{AF_INET}, @var{address}\n"
  792. "must be an integer IPv4 address and @var{args}\n"
  793. "must be a single integer port number.\n\n"
  794. "The values of the following variables can also be used for\n"
  795. "@var{address}:\n\n"
  796. "@defvar INADDR_ANY\n"
  797. "Allow connections from any address.\n"
  798. "@end defvar\n\n"
  799. "@defvar INADDR_LOOPBACK\n"
  800. "The address of the local host using the loopback device.\n"
  801. "@end defvar\n\n"
  802. "@defvar INADDR_BROADCAST\n"
  803. "The broadcast address on the local network.\n"
  804. "@end defvar\n\n"
  805. "@defvar INADDR_NONE\n"
  806. "No address.\n"
  807. "@end defvar\n\n"
  808. "For a socket of family @code{AF_INET6}, @var{address}\n"
  809. "must be an integer IPv6 address and @var{args}\n"
  810. "may be up to three integers:\n"
  811. "port [flowinfo] [scope_id],\n"
  812. "where flowinfo and scope_id default to zero.\n\n"
  813. "Alternatively, the second argument can be a socket address object "
  814. "as returned by @code{make-socket-address}, in which case the "
  815. "no additional arguments should be passed.\n\n"
  816. "The return value is unspecified.")
  817. #define FUNC_NAME s_scm_bind
  818. {
  819. struct sockaddr *soka;
  820. size_t size;
  821. int fd;
  822. sock = SCM_COERCE_OUTPORT (sock);
  823. SCM_VALIDATE_OPFPORT (1, sock);
  824. fd = SCM_FPORT_FDES (sock);
  825. if (scm_is_eq (address, SCM_UNDEFINED))
  826. /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
  827. `socket address' object. */
  828. soka = scm_to_sockaddr (fam_or_sockaddr, &size);
  829. else
  830. soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
  831. &args, 3, FUNC_NAME, &size);
  832. if (bind (fd, soka, size) == -1)
  833. {
  834. int save_errno = errno;
  835. free (soka);
  836. errno = save_errno;
  837. SCM_SYSERROR;
  838. }
  839. free (soka);
  840. return SCM_UNSPECIFIED;
  841. }
  842. #undef FUNC_NAME
  843. SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
  844. (SCM sock, SCM backlog),
  845. "Enable @var{sock} to accept connection\n"
  846. "requests. @var{backlog} is an integer specifying\n"
  847. "the maximum length of the queue for pending connections.\n"
  848. "If the queue fills, new clients will fail to connect until\n"
  849. "the server calls @code{accept} to accept a connection from\n"
  850. "the queue.\n\n"
  851. "The return value is unspecified.")
  852. #define FUNC_NAME s_scm_listen
  853. {
  854. int fd;
  855. sock = SCM_COERCE_OUTPORT (sock);
  856. SCM_VALIDATE_OPFPORT (1, sock);
  857. fd = SCM_FPORT_FDES (sock);
  858. if (listen (fd, scm_to_int (backlog)) == -1)
  859. SCM_SYSERROR;
  860. return SCM_UNSPECIFIED;
  861. }
  862. #undef FUNC_NAME
  863. /* Put the components of a sockaddr into a new SCM vector. */
  864. static SCM_C_INLINE_KEYWORD SCM
  865. _scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
  866. const char *proc)
  867. {
  868. SCM result = SCM_EOL;
  869. short int fam = ((struct sockaddr *) address)->sa_family;
  870. switch (fam)
  871. {
  872. case AF_INET:
  873. {
  874. const struct sockaddr_in *nad = (struct sockaddr_in *) address;
  875. result = scm_c_make_vector (3, SCM_UNSPECIFIED);
  876. SCM_SIMPLE_VECTOR_SET(result, 0,
  877. scm_from_short (fam));
  878. SCM_SIMPLE_VECTOR_SET(result, 1,
  879. scm_from_ulong (ntohl (nad->sin_addr.s_addr)));
  880. SCM_SIMPLE_VECTOR_SET(result, 2,
  881. scm_from_ushort (ntohs (nad->sin_port)));
  882. }
  883. break;
  884. #ifdef HAVE_IPV6
  885. case AF_INET6:
  886. {
  887. const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
  888. result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  889. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
  890. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_ipv6 (nad->sin6_addr.s6_addr));
  891. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ushort (ntohs (nad->sin6_port)));
  892. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_uint32 (nad->sin6_flowinfo));
  893. #ifdef HAVE_SIN6_SCOPE_ID
  894. SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id));
  895. #else
  896. SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0);
  897. #endif
  898. }
  899. break;
  900. #endif
  901. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  902. case AF_UNIX:
  903. {
  904. const struct sockaddr_un *nad = (struct sockaddr_un *) address;
  905. result = scm_c_make_vector (2, SCM_UNSPECIFIED);
  906. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
  907. /* When addr_size is not enough to cover sun_path, do not try
  908. to access it. */
  909. if (addr_size <= offsetof (struct sockaddr_un, sun_path))
  910. SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
  911. else
  912. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path));
  913. }
  914. break;
  915. #endif
  916. default:
  917. result = SCM_UNSPECIFIED;
  918. scm_misc_error (proc, "unrecognised address family: ~A",
  919. scm_list_1 (scm_from_int (fam)));
  920. }
  921. return result;
  922. }
  923. /* The publicly-visible function. Return a Scheme object representing
  924. ADDRESS, an address of ADDR_SIZE bytes. */
  925. SCM
  926. scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size)
  927. {
  928. return (_scm_from_sockaddr ((scm_t_max_sockaddr *) address,
  929. addr_size, "scm_from_sockaddr"));
  930. }
  931. /* Convert ADDRESS, an address object returned by either
  932. `scm_from_sockaddr ()' or `scm_make_socket_address ()', into its C
  933. representation. On success, a non-NULL pointer is returned and
  934. ADDRESS_SIZE is updated to the actual size (in bytes) of the returned
  935. address. The result must eventually be freed using `free ()'. */
  936. struct sockaddr *
  937. scm_to_sockaddr (SCM address, size_t *address_size)
  938. #define FUNC_NAME "scm_to_sockaddr"
  939. {
  940. short int family;
  941. struct sockaddr *c_address = NULL;
  942. SCM_VALIDATE_VECTOR (1, address);
  943. *address_size = 0;
  944. family = scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0));
  945. switch (family)
  946. {
  947. case AF_INET:
  948. {
  949. if (SCM_SIMPLE_VECTOR_LENGTH (address) != 3)
  950. scm_misc_error (FUNC_NAME,
  951. "invalid inet address representation: ~A",
  952. scm_list_1 (address));
  953. else
  954. {
  955. struct sockaddr_in c_inet;
  956. memset (&c_inet, '\0', sizeof (struct sockaddr_in));
  957. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  958. c_inet.sin_len = sizeof (struct sockaddr_in);
  959. #endif
  960. c_inet.sin_addr.s_addr =
  961. htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1)));
  962. c_inet.sin_port =
  963. htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
  964. c_inet.sin_family = AF_INET;
  965. *address_size = sizeof (c_inet);
  966. c_address = scm_malloc (sizeof (c_inet));
  967. memcpy (c_address, &c_inet, sizeof (c_inet));
  968. }
  969. break;
  970. }
  971. #ifdef HAVE_IPV6
  972. case AF_INET6:
  973. {
  974. if (SCM_SIMPLE_VECTOR_LENGTH (address) != 5)
  975. scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A",
  976. scm_list_1 (address));
  977. else
  978. {
  979. struct sockaddr_in6 c_inet6;
  980. scm_to_ipv6 (c_inet6.sin6_addr.s6_addr,
  981. SCM_SIMPLE_VECTOR_REF (address, 1));
  982. c_inet6.sin6_port =
  983. htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
  984. c_inet6.sin6_flowinfo =
  985. scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3));
  986. #ifdef HAVE_SIN6_SCOPE_ID
  987. c_inet6.sin6_scope_id =
  988. scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4));
  989. #endif
  990. c_inet6.sin6_family = AF_INET6;
  991. *address_size = sizeof (c_inet6);
  992. c_address = scm_malloc (sizeof (c_inet6));
  993. memcpy (c_address, &c_inet6, sizeof (c_inet6));
  994. }
  995. break;
  996. }
  997. #endif
  998. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  999. case AF_UNIX:
  1000. {
  1001. if (SCM_SIMPLE_VECTOR_LENGTH (address) != 2)
  1002. scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A",
  1003. scm_list_1 (address));
  1004. else
  1005. {
  1006. SCM path;
  1007. size_t path_len = 0;
  1008. path = SCM_SIMPLE_VECTOR_REF (address, 1);
  1009. if (!scm_is_string (path) && !scm_is_false (path))
  1010. scm_misc_error (FUNC_NAME, "invalid unix address "
  1011. "path: ~A", scm_list_1 (path));
  1012. else
  1013. {
  1014. struct sockaddr_un c_unix;
  1015. if (scm_is_false (path))
  1016. path_len = 0;
  1017. else
  1018. path_len = scm_c_string_length (path);
  1019. #ifdef UNIX_PATH_MAX
  1020. if (path_len >= UNIX_PATH_MAX)
  1021. #else
  1022. /* We can hope that this limit will eventually vanish, at least on GNU.
  1023. However, currently, while glibc doesn't define `UNIX_PATH_MAX', it
  1024. documents it has being limited to 108 bytes. */
  1025. if (path_len >= sizeof (c_unix.sun_path))
  1026. #endif
  1027. scm_misc_error (FUNC_NAME, "unix address path "
  1028. "too long: ~A", scm_list_1 (path));
  1029. else
  1030. {
  1031. if (path_len)
  1032. {
  1033. scm_to_locale_stringbuf (path, c_unix.sun_path,
  1034. #ifdef UNIX_PATH_MAX
  1035. UNIX_PATH_MAX);
  1036. #else
  1037. sizeof (c_unix.sun_path));
  1038. #endif
  1039. c_unix.sun_path[path_len] = '\0';
  1040. /* Sanity check. */
  1041. if (strlen (c_unix.sun_path) != path_len)
  1042. scm_misc_error (FUNC_NAME, "unix address path "
  1043. "contains nul characters: ~A",
  1044. scm_list_1 (path));
  1045. }
  1046. else
  1047. c_unix.sun_path[0] = '\0';
  1048. c_unix.sun_family = AF_UNIX;
  1049. *address_size = SUN_LEN (&c_unix);
  1050. c_address = scm_malloc (sizeof (c_unix));
  1051. memcpy (c_address, &c_unix, sizeof (c_unix));
  1052. }
  1053. }
  1054. }
  1055. break;
  1056. }
  1057. #endif
  1058. default:
  1059. scm_misc_error (FUNC_NAME, "unrecognised address family: ~A",
  1060. scm_list_1 (scm_from_ushort (family)));
  1061. }
  1062. return c_address;
  1063. }
  1064. #undef FUNC_NAME
  1065. /* Return a newly-allocated `sockaddr' structure that reflects ADDRESS, being
  1066. an address of family FAMILY, with the family-specific parameters ARGS (see
  1067. the description of `connect' for details). The returned structure may be
  1068. freed using `free ()'. */
  1069. struct sockaddr *
  1070. scm_c_make_socket_address (SCM family, SCM address, SCM args,
  1071. size_t *address_size)
  1072. {
  1073. struct sockaddr *soka;
  1074. soka = scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1,
  1075. "scm_c_make_socket_address", address_size);
  1076. return soka;
  1077. }
  1078. SCM_DEFINE (scm_make_socket_address, "make-socket-address", 2, 0, 1,
  1079. (SCM family, SCM address, SCM args),
  1080. "Return a Scheme address object that reflects @var{address}, "
  1081. "being an address of family @var{family}, with the "
  1082. "family-specific parameters @var{args} (see the description of "
  1083. "@code{connect} for details).")
  1084. #define FUNC_NAME s_scm_make_socket_address
  1085. {
  1086. SCM result = SCM_BOOL_F;
  1087. struct sockaddr *c_address;
  1088. size_t c_address_size;
  1089. c_address = scm_c_make_socket_address (family, address, args,
  1090. &c_address_size);
  1091. if (c_address != NULL)
  1092. {
  1093. result = scm_from_sockaddr (c_address, c_address_size);
  1094. free (c_address);
  1095. }
  1096. return result;
  1097. }
  1098. #undef FUNC_NAME
  1099. SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
  1100. (SCM sock),
  1101. "Accept a connection on a bound, listening socket. If there\n"
  1102. "are no pending connections in the queue, there are two\n"
  1103. "possibilities: if the socket has been configured as\n"
  1104. "non-blocking, return @code{#f} directly. Otherwise wait\n"
  1105. "until a connection is available. When a connection comes,\n"
  1106. "the return value is a pair in which the @emph{car} is a new\n"
  1107. "socket port for the connection and the @emph{cdr} is an\n"
  1108. "object with address information about the client which\n"
  1109. "initiated the connection.\n\n"
  1110. "@var{sock} does not become part of the\n"
  1111. "connection and will continue to accept new requests.")
  1112. #define FUNC_NAME s_scm_accept
  1113. {
  1114. int fd;
  1115. int newfd;
  1116. SCM address;
  1117. SCM newsock;
  1118. socklen_t addr_size = MAX_ADDR_SIZE;
  1119. scm_t_max_sockaddr addr;
  1120. sock = SCM_COERCE_OUTPORT (sock);
  1121. SCM_VALIDATE_OPFPORT (1, sock);
  1122. fd = SCM_FPORT_FDES (sock);
  1123. SCM_SYSCALL (newfd = accept (fd, (struct sockaddr *) &addr, &addr_size));
  1124. if (newfd == -1)
  1125. {
  1126. if (errno == EAGAIN || errno == EWOULDBLOCK)
  1127. return SCM_BOOL_F;
  1128. SCM_SYSERROR;
  1129. }
  1130. newsock = SCM_SOCK_FD_TO_PORT (newfd);
  1131. address = _scm_from_sockaddr (&addr, addr_size,
  1132. FUNC_NAME);
  1133. return scm_cons (newsock, address);
  1134. }
  1135. #undef FUNC_NAME
  1136. SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
  1137. (SCM sock),
  1138. "Return the address of @var{sock}, in the same form as the\n"
  1139. "object returned by @code{accept}. On many systems the address\n"
  1140. "of a socket in the @code{AF_FILE} namespace cannot be read.")
  1141. #define FUNC_NAME s_scm_getsockname
  1142. {
  1143. int fd;
  1144. socklen_t addr_size = MAX_ADDR_SIZE;
  1145. scm_t_max_sockaddr addr;
  1146. sock = SCM_COERCE_OUTPORT (sock);
  1147. SCM_VALIDATE_OPFPORT (1, sock);
  1148. fd = SCM_FPORT_FDES (sock);
  1149. if (getsockname (fd, (struct sockaddr *) &addr, &addr_size) == -1)
  1150. SCM_SYSERROR;
  1151. return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
  1152. }
  1153. #undef FUNC_NAME
  1154. SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
  1155. (SCM sock),
  1156. "Return the address that @var{sock}\n"
  1157. "is connected to, in the same form as the object returned by\n"
  1158. "@code{accept}. On many systems the address of a socket in the\n"
  1159. "@code{AF_FILE} namespace cannot be read.")
  1160. #define FUNC_NAME s_scm_getpeername
  1161. {
  1162. int fd;
  1163. socklen_t addr_size = MAX_ADDR_SIZE;
  1164. scm_t_max_sockaddr addr;
  1165. sock = SCM_COERCE_OUTPORT (sock);
  1166. SCM_VALIDATE_OPFPORT (1, sock);
  1167. fd = SCM_FPORT_FDES (sock);
  1168. if (getpeername (fd, (struct sockaddr *) &addr, &addr_size) == -1)
  1169. SCM_SYSERROR;
  1170. return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
  1171. }
  1172. #undef FUNC_NAME
  1173. SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
  1174. (SCM sock, SCM buf, SCM flags),
  1175. "Receive data from a socket port.\n"
  1176. "@var{sock} must already\n"
  1177. "be bound to the address from which data is to be received.\n"
  1178. "@var{buf} is a bytevector into which\n"
  1179. "the data will be written. The size of @var{buf} limits\n"
  1180. "the amount of\n"
  1181. "data which can be received: in the case of packet\n"
  1182. "protocols, if a packet larger than this limit is encountered\n"
  1183. "then some data\n"
  1184. "will be irrevocably lost.\n\n"
  1185. "The optional @var{flags} argument is a value or\n"
  1186. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1187. "The value returned is the number of bytes read from the\n"
  1188. "socket.\n\n"
  1189. "Note that the data is read directly from the socket file\n"
  1190. "descriptor:\n"
  1191. "any unread buffered port data is ignored.")
  1192. #define FUNC_NAME s_scm_recv
  1193. {
  1194. int rv, fd, flg;
  1195. SCM_VALIDATE_OPFPORT (1, sock);
  1196. if (SCM_UNBNDP (flags))
  1197. flg = 0;
  1198. else
  1199. flg = scm_to_int (flags);
  1200. fd = SCM_FPORT_FDES (sock);
  1201. SCM_VALIDATE_BYTEVECTOR (1, buf);
  1202. SCM_SYSCALL (rv = recv (fd,
  1203. SCM_BYTEVECTOR_CONTENTS (buf),
  1204. SCM_BYTEVECTOR_LENGTH (buf),
  1205. flg));
  1206. if (SCM_UNLIKELY (rv == -1))
  1207. SCM_SYSERROR;
  1208. scm_remember_upto_here (buf);
  1209. return scm_from_int (rv);
  1210. }
  1211. #undef FUNC_NAME
  1212. SCM_DEFINE (scm_send, "send", 2, 1, 0,
  1213. (SCM sock, SCM message, SCM flags),
  1214. "Transmit bytevector @var{message} on socket port @var{sock}.\n"
  1215. "@var{sock} must already be bound to a destination address. The\n"
  1216. "value returned is the number of bytes transmitted --\n"
  1217. "it's possible for\n"
  1218. "this to be less than the length of @var{message}\n"
  1219. "if the socket is\n"
  1220. "set to be non-blocking. The optional @var{flags} argument\n"
  1221. "is a value or\n"
  1222. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1223. "Note that the data is written directly to the socket\n"
  1224. "file descriptor:\n"
  1225. "any unflushed buffered port data is ignored.\n\n"
  1226. "This operation is defined only for strings containing codepoints\n"
  1227. "zero to 255.")
  1228. #define FUNC_NAME s_scm_send
  1229. {
  1230. int rv, fd, flg;
  1231. sock = SCM_COERCE_OUTPORT (sock);
  1232. SCM_VALIDATE_OPFPORT (1, sock);
  1233. if (SCM_UNBNDP (flags))
  1234. flg = 0;
  1235. else
  1236. flg = scm_to_int (flags);
  1237. fd = SCM_FPORT_FDES (sock);
  1238. SCM_VALIDATE_BYTEVECTOR (1, message);
  1239. SCM_SYSCALL (rv = send (fd,
  1240. SCM_BYTEVECTOR_CONTENTS (message),
  1241. SCM_BYTEVECTOR_LENGTH (message),
  1242. flg));
  1243. if (rv == -1)
  1244. SCM_SYSERROR;
  1245. scm_remember_upto_here_1 (message);
  1246. return scm_from_int (rv);
  1247. }
  1248. #undef FUNC_NAME
  1249. SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
  1250. (SCM sock, SCM buf, SCM flags, SCM start, SCM end),
  1251. "Receive data from socket port @var{sock} (which must be already\n"
  1252. "bound), returning the originating address as well as the data.\n"
  1253. "This is usually for use on datagram sockets, but can be used on\n"
  1254. "stream-oriented sockets too.\n"
  1255. "\n"
  1256. "The data received is stored in bytevector @var{buf}, using\n"
  1257. "either the whole bytevector or just the region between the optional\n"
  1258. "@var{start} and @var{end} positions. The size of @var{buf}\n"
  1259. "limits the amount of data that can be received. For datagram\n"
  1260. "protocols, if a packet larger than this is received then excess\n"
  1261. "bytes are irrevocably lost.\n"
  1262. "\n"
  1263. "The return value is a pair. The @code{car} is the number of\n"
  1264. "bytes read. The @code{cdr} is a socket address object which is\n"
  1265. "where the data came from, or @code{#f} if the origin is\n"
  1266. "unknown.\n"
  1267. "\n"
  1268. "The optional @var{flags} argument is a or bitwise OR\n"
  1269. "(@code{logior}) of @code{MSG_OOB}, @code{MSG_PEEK},\n"
  1270. "@code{MSG_DONTROUTE} etc.\n"
  1271. "\n"
  1272. "Data is read directly from the socket file descriptor, any\n"
  1273. "buffered port data is ignored.\n"
  1274. "\n"
  1275. "On a GNU/Linux system @code{recvfrom!} is not multi-threading,\n"
  1276. "all threads stop while a @code{recvfrom!} call is in progress.\n"
  1277. "An application may need to use @code{select}, @code{O_NONBLOCK}\n"
  1278. "or @code{MSG_DONTWAIT} to avoid this.")
  1279. #define FUNC_NAME s_scm_recvfrom
  1280. {
  1281. int rv, fd, flg;
  1282. SCM address;
  1283. size_t offset, cend;
  1284. socklen_t addr_size = MAX_ADDR_SIZE;
  1285. scm_t_max_sockaddr addr;
  1286. SCM_VALIDATE_OPFPORT (1, sock);
  1287. fd = SCM_FPORT_FDES (sock);
  1288. if (SCM_UNBNDP (flags))
  1289. flg = 0;
  1290. else
  1291. SCM_VALIDATE_ULONG_COPY (3, flags, flg);
  1292. ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
  1293. SCM_VALIDATE_BYTEVECTOR (1, buf);
  1294. if (SCM_UNBNDP (start))
  1295. offset = 0;
  1296. else
  1297. offset = scm_to_size_t (start);
  1298. if (SCM_UNBNDP (end))
  1299. cend = SCM_BYTEVECTOR_LENGTH (buf);
  1300. else
  1301. {
  1302. cend = scm_to_size_t (end);
  1303. if (SCM_UNLIKELY (cend >= SCM_BYTEVECTOR_LENGTH (buf)
  1304. || cend < offset))
  1305. scm_out_of_range (FUNC_NAME, end);
  1306. }
  1307. SCM_SYSCALL (rv = recvfrom (fd,
  1308. SCM_BYTEVECTOR_CONTENTS (buf) + offset,
  1309. cend - offset, flg,
  1310. (struct sockaddr *) &addr, &addr_size));
  1311. if (rv == -1)
  1312. SCM_SYSERROR;
  1313. /* `recvfrom' does not necessarily return an address. Usually nothing
  1314. is returned for stream sockets. */
  1315. if (((struct sockaddr *) &addr)->sa_family != AF_UNSPEC)
  1316. address = _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
  1317. else
  1318. address = SCM_BOOL_F;
  1319. scm_remember_upto_here_1 (buf);
  1320. return scm_cons (scm_from_int (rv), address);
  1321. }
  1322. #undef FUNC_NAME
  1323. SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
  1324. (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
  1325. "Transmit bytevector @var{message} on socket port\n"
  1326. "@var{sock}. The\n"
  1327. "destination address is specified using the @var{fam_or_sockaddr},\n"
  1328. "@var{address} and\n"
  1329. "@var{args_and_flags} arguments, or just a socket address object "
  1330. "returned by @code{make-socket-address}, in a similar way to the\n"
  1331. "@code{connect} procedure. @var{args_and_flags} contains\n"
  1332. "the usual connection arguments optionally followed by\n"
  1333. "a flags argument, which is a value or\n"
  1334. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1335. "The value returned is the number of bytes transmitted --\n"
  1336. "it's possible for\n"
  1337. "this to be less than the length of @var{message} if the\n"
  1338. "socket is\n"
  1339. "set to be non-blocking.\n"
  1340. "Note that the data is written directly to the socket\n"
  1341. "file descriptor:\n"
  1342. "any unflushed buffered port data is ignored.\n"
  1343. "This operation is defined only for strings containing codepoints\n"
  1344. "zero to 255.")
  1345. #define FUNC_NAME s_scm_sendto
  1346. {
  1347. int rv, fd, flg;
  1348. struct sockaddr *soka;
  1349. size_t size;
  1350. sock = SCM_COERCE_OUTPORT (sock);
  1351. SCM_VALIDATE_FPORT (1, sock);
  1352. fd = SCM_FPORT_FDES (sock);
  1353. if (!scm_is_number (fam_or_sockaddr))
  1354. {
  1355. /* FAM_OR_SOCKADDR must actually be a `socket address' object. This
  1356. means that the following arguments, i.e. ADDRESS and those listed in
  1357. ARGS_AND_FLAGS, are the `MSG_' flags. */
  1358. soka = scm_to_sockaddr (fam_or_sockaddr, &size);
  1359. if (!scm_is_eq (address, SCM_UNDEFINED))
  1360. args_and_flags = scm_cons (address, args_and_flags);
  1361. }
  1362. else
  1363. soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
  1364. &args_and_flags, 3, FUNC_NAME, &size);
  1365. if (scm_is_null (args_and_flags))
  1366. flg = 0;
  1367. else
  1368. {
  1369. SCM_VALIDATE_CONS (5, args_and_flags);
  1370. flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
  1371. }
  1372. SCM_VALIDATE_BYTEVECTOR (1, message);
  1373. SCM_SYSCALL (rv = sendto (fd,
  1374. SCM_BYTEVECTOR_CONTENTS (message),
  1375. SCM_BYTEVECTOR_LENGTH (message),
  1376. flg, soka, size));
  1377. if (rv == -1)
  1378. {
  1379. int save_errno = errno;
  1380. free (soka);
  1381. errno = save_errno;
  1382. SCM_SYSERROR;
  1383. }
  1384. free (soka);
  1385. scm_remember_upto_here_1 (message);
  1386. return scm_from_int (rv);
  1387. }
  1388. #undef FUNC_NAME
  1389. void
  1390. scm_init_socket ()
  1391. {
  1392. /* protocol families. */
  1393. #ifdef AF_UNSPEC
  1394. scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC));
  1395. #endif
  1396. #if defined HAVE_UNIX_DOMAIN_SOCKETS && defined AF_UNIX
  1397. scm_c_define ("AF_UNIX", scm_from_int (AF_UNIX));
  1398. #endif
  1399. #ifdef AF_INET
  1400. scm_c_define ("AF_INET", scm_from_int (AF_INET));
  1401. #endif
  1402. #ifdef AF_INET6
  1403. scm_c_define ("AF_INET6", scm_from_int (AF_INET6));
  1404. #endif
  1405. #ifdef PF_UNSPEC
  1406. scm_c_define ("PF_UNSPEC", scm_from_int (PF_UNSPEC));
  1407. #endif
  1408. #ifdef PF_UNIX
  1409. scm_c_define ("PF_UNIX", scm_from_int (PF_UNIX));
  1410. #endif
  1411. #ifdef PF_INET
  1412. scm_c_define ("PF_INET", scm_from_int (PF_INET));
  1413. #endif
  1414. #ifdef PF_INET6
  1415. scm_c_define ("PF_INET6", scm_from_int (PF_INET6));
  1416. #endif
  1417. /* standard addresses. */
  1418. #ifdef INADDR_ANY
  1419. scm_c_define ("INADDR_ANY", scm_from_ulong (INADDR_ANY));
  1420. #endif
  1421. #ifdef INADDR_BROADCAST
  1422. scm_c_define ("INADDR_BROADCAST", scm_from_ulong (INADDR_BROADCAST));
  1423. #endif
  1424. #ifdef INADDR_NONE
  1425. scm_c_define ("INADDR_NONE", scm_from_ulong (INADDR_NONE));
  1426. #endif
  1427. #ifdef INADDR_LOOPBACK
  1428. scm_c_define ("INADDR_LOOPBACK", scm_from_ulong (INADDR_LOOPBACK));
  1429. #endif
  1430. /* socket types.
  1431. SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and
  1432. packet(7) advise that it's obsolete and strongly deprecated. */
  1433. #ifdef SOCK_STREAM
  1434. scm_c_define ("SOCK_STREAM", scm_from_int (SOCK_STREAM));
  1435. #endif
  1436. #ifdef SOCK_DGRAM
  1437. scm_c_define ("SOCK_DGRAM", scm_from_int (SOCK_DGRAM));
  1438. #endif
  1439. #ifdef SOCK_SEQPACKET
  1440. scm_c_define ("SOCK_SEQPACKET", scm_from_int (SOCK_SEQPACKET));
  1441. #endif
  1442. #ifdef SOCK_RAW
  1443. scm_c_define ("SOCK_RAW", scm_from_int (SOCK_RAW));
  1444. #endif
  1445. #ifdef SOCK_RDM
  1446. scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM));
  1447. #endif
  1448. /* setsockopt level.
  1449. SOL_IP, SOL_TCP and SOL_UDP are defined on gnu/linux, but not on for
  1450. instance NetBSD. We define IPPROTOs because that's what the posix spec
  1451. shows in its example at
  1452. http://www.opengroup.org/onlinepubs/007904975/functions/getsockopt.html
  1453. */
  1454. #ifdef SOL_SOCKET
  1455. scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET));
  1456. #endif
  1457. #ifdef IPPROTO_IP
  1458. scm_c_define ("IPPROTO_IP", scm_from_int (IPPROTO_IP));
  1459. #endif
  1460. #ifdef IPPROTO_TCP
  1461. scm_c_define ("IPPROTO_TCP", scm_from_int (IPPROTO_TCP));
  1462. #endif
  1463. #ifdef IPPROTO_UDP
  1464. scm_c_define ("IPPROTO_UDP", scm_from_int (IPPROTO_UDP));
  1465. #endif
  1466. /* setsockopt names. */
  1467. #ifdef SO_DEBUG
  1468. scm_c_define ("SO_DEBUG", scm_from_int (SO_DEBUG));
  1469. #endif
  1470. #ifdef SO_REUSEADDR
  1471. scm_c_define ("SO_REUSEADDR", scm_from_int (SO_REUSEADDR));
  1472. #endif
  1473. #ifdef SO_STYLE
  1474. scm_c_define ("SO_STYLE", scm_from_int (SO_STYLE));
  1475. #endif
  1476. #ifdef SO_TYPE
  1477. scm_c_define ("SO_TYPE", scm_from_int (SO_TYPE));
  1478. #endif
  1479. #ifdef SO_ERROR
  1480. scm_c_define ("SO_ERROR", scm_from_int (SO_ERROR));
  1481. #endif
  1482. #ifdef SO_DONTROUTE
  1483. scm_c_define ("SO_DONTROUTE", scm_from_int (SO_DONTROUTE));
  1484. #endif
  1485. #ifdef SO_BROADCAST
  1486. scm_c_define ("SO_BROADCAST", scm_from_int (SO_BROADCAST));
  1487. #endif
  1488. #ifdef SO_SNDBUF
  1489. scm_c_define ("SO_SNDBUF", scm_from_int (SO_SNDBUF));
  1490. #endif
  1491. #ifdef SO_RCVBUF
  1492. scm_c_define ("SO_RCVBUF", scm_from_int (SO_RCVBUF));
  1493. #endif
  1494. #ifdef SO_KEEPALIVE
  1495. scm_c_define ("SO_KEEPALIVE", scm_from_int (SO_KEEPALIVE));
  1496. #endif
  1497. #ifdef SO_OOBINLINE
  1498. scm_c_define ("SO_OOBINLINE", scm_from_int (SO_OOBINLINE));
  1499. #endif
  1500. #ifdef SO_NO_CHECK
  1501. scm_c_define ("SO_NO_CHECK", scm_from_int (SO_NO_CHECK));
  1502. #endif
  1503. #ifdef SO_PRIORITY
  1504. scm_c_define ("SO_PRIORITY", scm_from_int (SO_PRIORITY));
  1505. #endif
  1506. #ifdef SO_LINGER
  1507. scm_c_define ("SO_LINGER", scm_from_int (SO_LINGER));
  1508. #endif
  1509. #ifdef SO_REUSEPORT /* new in Linux 3.9 */
  1510. scm_c_define ("SO_REUSEPORT", scm_from_int (SO_REUSEPORT));
  1511. #endif
  1512. /* recv/send options. */
  1513. #ifdef MSG_DONTWAIT
  1514. scm_c_define ("MSG_DONTWAIT", scm_from_int (MSG_DONTWAIT));
  1515. #endif
  1516. #ifdef MSG_OOB
  1517. scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB));
  1518. #endif
  1519. #ifdef MSG_PEEK
  1520. scm_c_define ("MSG_PEEK", scm_from_int (MSG_PEEK));
  1521. #endif
  1522. #ifdef MSG_DONTROUTE
  1523. scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE));
  1524. #endif
  1525. /* TCP options. */
  1526. #ifdef TCP_NODELAY
  1527. scm_c_define ("TCP_NODELAY", scm_from_int (TCP_NODELAY));
  1528. #endif
  1529. #ifdef TCP_CORK
  1530. scm_c_define ("TCP_CORK", scm_from_int (TCP_CORK));
  1531. #endif
  1532. #ifdef IP_ADD_MEMBERSHIP
  1533. scm_c_define ("IP_ADD_MEMBERSHIP", scm_from_int (IP_ADD_MEMBERSHIP));
  1534. scm_c_define ("IP_DROP_MEMBERSHIP", scm_from_int (IP_DROP_MEMBERSHIP));
  1535. #endif
  1536. #ifdef IP_MULTICAST_TTL
  1537. scm_c_define ("IP_MULTICAST_TTL", scm_from_int ( IP_MULTICAST_TTL));
  1538. #endif
  1539. #ifdef IP_MULTICAST_IF
  1540. scm_c_define ("IP_MULTICAST_IF", scm_from_int ( IP_MULTICAST_IF));
  1541. #endif
  1542. scm_add_feature ("socket");
  1543. #include "libguile/socket.x"
  1544. }
  1545. /*
  1546. Local Variables:
  1547. c-file-style: "gnu"
  1548. End:
  1549. */