socket.c 52 KB

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