socket.c 51 KB

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