socket.c 52 KB

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