socket.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <errno.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/unif.h"
  44. #include "libguile/feature.h"
  45. #include "libguile/fports.h"
  46. #include "libguile/strings.h"
  47. #include "libguile/vectors.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/socket.h"
  50. #ifdef HAVE_STRING_H
  51. #include <string.h>
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h>
  55. #endif
  56. #include <sys/types.h>
  57. #ifdef HAVE_WINSOCK2_H
  58. #include <winsock2.h>
  59. #else
  60. #include <sys/socket.h>
  61. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  62. #include <sys/un.h>
  63. #endif
  64. #include <netinet/in.h>
  65. #include <netdb.h>
  66. #include <arpa/inet.h>
  67. #endif
  68. /* Temporary hack -- in the next major Guile release, this will be
  69. handled more generally. */
  70. #ifndef HAVE_SOCKLEN_T
  71. typedef int socklen_t;
  72. #endif
  73. #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
  74. #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
  75. + strlen ((ptr)->sun_path))
  76. #endif
  77. #if !defined (HAVE_UINT32_T)
  78. #if SIZEOF_INT == 4
  79. typedef unsigned int uint32_t;
  80. #elif SIZEOF_LONG == 4
  81. typedef unsigned long uint32_t;
  82. #else
  83. #error can not define uint32_t
  84. #endif
  85. #endif
  86. /* we are not currently using socklen_t. it's not defined on all systems,
  87. so would need to be checked by configure. in the meantime, plain
  88. int is the best alternative. */
  89. SCM_DEFINE (scm_htons, "htons", 1, 0, 0,
  90. (SCM value),
  91. "Convert a 16 bit quantity from host to network byte ordering.\n"
  92. "@var{value} is packed into 2 bytes, which are then converted\n"
  93. "and returned as a new integer.")
  94. #define FUNC_NAME s_scm_htons
  95. {
  96. unsigned short c_in;
  97. SCM_VALIDATE_INUM_COPY (1, value, c_in);
  98. if (c_in != SCM_INUM (value))
  99. SCM_OUT_OF_RANGE (1, value);
  100. return SCM_MAKINUM (htons (c_in));
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0,
  104. (SCM value),
  105. "Convert a 16 bit quantity from network to host byte ordering.\n"
  106. "@var{value} is packed into 2 bytes, which are then converted\n"
  107. "and returned as a new integer.")
  108. #define FUNC_NAME s_scm_ntohs
  109. {
  110. unsigned short c_in;
  111. SCM_VALIDATE_INUM_COPY (1, value, c_in);
  112. if (c_in != SCM_INUM (value))
  113. SCM_OUT_OF_RANGE (1, value);
  114. return SCM_MAKINUM (ntohs (c_in));
  115. }
  116. #undef FUNC_NAME
  117. static uint32_t
  118. scm_num2uint32 (SCM num, unsigned long int pos, const char *func)
  119. {
  120. unsigned long uu = scm_num2ulong (num, pos, func);
  121. #if SIZEOF_LONG > 4
  122. if (uu > 0xFFFFFFFFL)
  123. scm_out_of_range (s_caller, num);
  124. #endif
  125. return (uint32_t) uu;
  126. }
  127. SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0,
  128. (SCM value),
  129. "Convert a 32 bit quantity from host to network byte ordering.\n"
  130. "@var{value} is packed into 4 bytes, which are then converted\n"
  131. "and returned as a new integer.")
  132. #define FUNC_NAME s_scm_htonl
  133. {
  134. uint32_t c_in = scm_num2uint32 (value, SCM_ARG1, FUNC_NAME);
  135. return scm_ulong2num (htonl (c_in));
  136. }
  137. #undef FUNC_NAME
  138. SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0,
  139. (SCM value),
  140. "Convert a 32 bit quantity from network to host byte ordering.\n"
  141. "@var{value} is packed into 4 bytes, which are then converted\n"
  142. "and returned as a new integer.")
  143. #define FUNC_NAME s_scm_ntohl
  144. {
  145. uint32_t c_in = scm_num2uint32 (value, SCM_ARG1, FUNC_NAME);
  146. return scm_ulong2num (ntohl (c_in));
  147. }
  148. #undef FUNC_NAME
  149. #ifndef HAVE_INET_ATON
  150. /* for our definition in inet_aton.c, not usually needed. */
  151. extern int inet_aton ();
  152. #endif
  153. SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0,
  154. (SCM address),
  155. "Convert an IPv4 Internet address from printable string\n"
  156. "(dotted decimal notation) to an integer. E.g.,\n\n"
  157. "@lisp\n"
  158. "(inet-aton \"127.0.0.1\") @result{} 2130706433\n"
  159. "@end lisp")
  160. #define FUNC_NAME s_scm_inet_aton
  161. {
  162. struct in_addr soka;
  163. SCM_VALIDATE_STRING (1, address);
  164. SCM_STRING_COERCE_0TERMINATION_X (address);
  165. if (inet_aton (SCM_STRING_CHARS (address), &soka) == 0)
  166. SCM_MISC_ERROR ("bad address", SCM_EOL);
  167. return scm_ulong2num (ntohl (soka.s_addr));
  168. }
  169. #undef FUNC_NAME
  170. SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0,
  171. (SCM inetid),
  172. "Convert an IPv4 Internet address to a printable\n"
  173. "(dotted decimal notation) string. E.g.,\n\n"
  174. "@lisp\n"
  175. "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
  176. "@end lisp")
  177. #define FUNC_NAME s_scm_inet_ntoa
  178. {
  179. struct in_addr addr;
  180. char *s;
  181. SCM answer;
  182. addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
  183. s = inet_ntoa (addr);
  184. answer = scm_mem2string (s, strlen (s));
  185. return answer;
  186. }
  187. #undef FUNC_NAME
  188. #ifdef HAVE_INET_NETOF
  189. SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0,
  190. (SCM address),
  191. "Return the network number part of the given IPv4\n"
  192. "Internet address. E.g.,\n\n"
  193. "@lisp\n"
  194. "(inet-netof 2130706433) @result{} 127\n"
  195. "@end lisp")
  196. #define FUNC_NAME s_scm_inet_netof
  197. {
  198. struct in_addr addr;
  199. addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  200. return scm_ulong2num ((unsigned long) inet_netof (addr));
  201. }
  202. #undef FUNC_NAME
  203. #endif
  204. #ifdef HAVE_INET_LNAOF
  205. SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0,
  206. (SCM address),
  207. "Return the local-address-with-network part of the given\n"
  208. "IPv4 Internet address, using the obsolete class A/B/C system.\n"
  209. "E.g.,\n\n"
  210. "@lisp\n"
  211. "(inet-lnaof 2130706433) @result{} 1\n"
  212. "@end lisp")
  213. #define FUNC_NAME s_scm_lnaof
  214. {
  215. struct in_addr addr;
  216. addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
  217. return scm_ulong2num ((unsigned long) inet_lnaof (addr));
  218. }
  219. #undef FUNC_NAME
  220. #endif
  221. #ifdef HAVE_INET_MAKEADDR
  222. SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
  223. (SCM net, SCM lna),
  224. "Make an IPv4 Internet address by combining the network number\n"
  225. "@var{net} with the local-address-within-network number\n"
  226. "@var{lna}. E.g.,\n\n"
  227. "@lisp\n"
  228. "(inet-makeaddr 127 1) @result{} 2130706433\n"
  229. "@end lisp")
  230. #define FUNC_NAME s_scm_inet_makeaddr
  231. {
  232. struct in_addr addr;
  233. unsigned long netnum;
  234. unsigned long lnanum;
  235. netnum = SCM_NUM2ULONG (1, net);
  236. lnanum = SCM_NUM2ULONG (2, lna);
  237. addr = inet_makeaddr (netnum, lnanum);
  238. return scm_ulong2num (ntohl (addr.s_addr));
  239. }
  240. #undef FUNC_NAME
  241. #endif
  242. #ifdef HAVE_IPV6
  243. /* flip a 128 bit IPv6 address between host and network order. */
  244. #ifdef WORDS_BIGENDIAN
  245. #define FLIP_NET_HOST_128(addr)
  246. #else
  247. #define FLIP_NET_HOST_128(addr)\
  248. {\
  249. int i;\
  250. \
  251. for (i = 0; i < 8; i++)\
  252. {\
  253. char c = (addr)[i];\
  254. \
  255. (addr)[i] = (addr)[15 - i];\
  256. (addr)[15 - i] = c;\
  257. }\
  258. }
  259. #endif
  260. /* convert a 128 bit IPv6 address in network order to a host ordered
  261. SCM integer. */
  262. static SCM ipv6_net_to_num (const unsigned char *src)
  263. {
  264. int big_digits = 128 / SCM_BITSPERDIG;
  265. const int bytes_per_dig = SCM_BITSPERDIG / 8;
  266. unsigned char addr[16];
  267. unsigned char *ptr = addr;
  268. SCM result;
  269. memcpy (addr, src, 16);
  270. /* get rid of leading zeros. */
  271. while (big_digits > 0)
  272. {
  273. long test = 0;
  274. memcpy (&test, ptr, bytes_per_dig);
  275. if (test != 0)
  276. break;
  277. ptr += bytes_per_dig;
  278. big_digits--;
  279. }
  280. FLIP_NET_HOST_128 (addr);
  281. if (big_digits * bytes_per_dig <= sizeof (unsigned long))
  282. {
  283. /* this is just so that we use INUM where possible. */
  284. unsigned long l_addr;
  285. memcpy (&l_addr, addr, sizeof (unsigned long));
  286. result = scm_ulong2num (l_addr);
  287. }
  288. else
  289. {
  290. result = scm_i_mkbig (big_digits, 0);
  291. memcpy (SCM_BDIGITS (result), addr, big_digits * bytes_per_dig);
  292. }
  293. return result;
  294. }
  295. /* convert a host ordered SCM integer to a 128 bit IPv6 address in
  296. network order. */
  297. static void ipv6_num_to_net (SCM src, unsigned char *dst)
  298. {
  299. if (SCM_INUMP (src))
  300. {
  301. uint32_t addr = htonl (SCM_INUM (src));
  302. memset (dst, 0, 12);
  303. memcpy (dst + 12, &addr, 4);
  304. }
  305. else
  306. {
  307. memset (dst, 0, 16);
  308. memcpy (dst, SCM_BDIGITS (src),
  309. SCM_NUMDIGS (src) * (SCM_BITSPERDIG / 8));
  310. FLIP_NET_HOST_128 (dst);
  311. }
  312. }
  313. /* check that an SCM variable contains an IPv6 integer address. */
  314. #define VALIDATE_INET6(which_arg, address)\
  315. if (SCM_INUMP (address))\
  316. SCM_ASSERT_RANGE (which_arg, address, SCM_INUM (address) >= 0);\
  317. else\
  318. {\
  319. SCM_VALIDATE_BIGINT (which_arg, address);\
  320. SCM_ASSERT_RANGE (which_arg, address,\
  321. !SCM_BIGSIGN (address)\
  322. && (SCM_BITSPERDIG\
  323. * SCM_NUMDIGS (address) <= 128));\
  324. }
  325. #ifdef HAVE_INET_PTON
  326. SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
  327. (SCM family, SCM address),
  328. "Convert a string containing a printable network address to\n"
  329. "an integer address. Note that unlike the C version of this\n"
  330. "function,\n"
  331. "the result is an integer with normal host byte ordering.\n"
  332. "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
  333. "@lisp\n"
  334. "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
  335. "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
  336. "@end lisp")
  337. #define FUNC_NAME s_scm_inet_pton
  338. {
  339. int af;
  340. char *src;
  341. unsigned char dst[16];
  342. int rv;
  343. SCM_VALIDATE_INUM_COPY (1, family, af);
  344. SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
  345. SCM_VALIDATE_STRING_COPY (2, address, src);
  346. rv = inet_pton (af, src, dst);
  347. if (rv == -1)
  348. SCM_SYSERROR;
  349. else if (rv == 0)
  350. SCM_MISC_ERROR ("Bad address", SCM_EOL);
  351. if (af == AF_INET)
  352. return scm_ulong2num (ntohl (*(uint32_t *) dst));
  353. else
  354. return ipv6_net_to_num (dst);
  355. }
  356. #undef FUNC_NAME
  357. #endif
  358. #ifdef HAVE_INET_NTOP
  359. SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
  360. (SCM family, SCM address),
  361. "Convert a network address into a printable string.\n"
  362. "Note that unlike the C version of this function,\n"
  363. "the input is an integer with normal host byte ordering.\n"
  364. "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
  365. "@lisp\n"
  366. "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
  367. "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n"
  368. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
  369. "@end lisp")
  370. #define FUNC_NAME s_scm_inet_ntop
  371. {
  372. int af;
  373. #ifdef INET6_ADDRSTRLEN
  374. char dst[INET6_ADDRSTRLEN];
  375. #else
  376. char dst[46];
  377. #endif
  378. unsigned char addr6[16];
  379. SCM_VALIDATE_INUM_COPY (1, family, af);
  380. SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
  381. if (af == AF_INET)
  382. *(uint32_t *) addr6 = htonl (SCM_NUM2ULONG (2, address));
  383. else
  384. {
  385. VALIDATE_INET6 (2, address);
  386. ipv6_num_to_net (address, addr6);
  387. }
  388. if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL)
  389. SCM_SYSERROR;
  390. return scm_makfrom0str (dst);
  391. }
  392. #undef FUNC_NAME
  393. #endif
  394. #endif /* HAVE_IPV6 */
  395. SCM_SYMBOL (sym_socket, "socket");
  396. #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
  397. SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
  398. (SCM family, SCM style, SCM proto),
  399. "Return a new socket port of the type specified by @var{family},\n"
  400. "@var{style} and @var{proto}. All three parameters are\n"
  401. "integers. Supported values for @var{family} are\n"
  402. "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n"
  403. "Typical values for @var{style} are @code{SOCK_STREAM},\n"
  404. "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
  405. "@var{proto} can be obtained from a protocol name using\n"
  406. "@code{getprotobyname}. A value of zero specifies the default\n"
  407. "protocol, which is usually right.\n\n"
  408. "A single socket port cannot by used for communication until it\n"
  409. "has been connected to another socket.")
  410. #define FUNC_NAME s_scm_socket
  411. {
  412. int fd;
  413. SCM_VALIDATE_INUM (1, family);
  414. SCM_VALIDATE_INUM (2, style);
  415. SCM_VALIDATE_INUM (3, proto);
  416. fd = socket (SCM_INUM (family), SCM_INUM (style), SCM_INUM (proto));
  417. if (fd == -1)
  418. SCM_SYSERROR;
  419. return SCM_SOCK_FD_TO_PORT (fd);
  420. }
  421. #undef FUNC_NAME
  422. #ifdef HAVE_SOCKETPAIR
  423. SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
  424. (SCM family, SCM style, SCM proto),
  425. "Return a pair of connected (but unnamed) socket ports of the\n"
  426. "type specified by @var{family}, @var{style} and @var{proto}.\n"
  427. "Many systems support only socket pairs of the @code{AF_UNIX}\n"
  428. "family. Zero is likely to be the only meaningful value for\n"
  429. "@var{proto}.")
  430. #define FUNC_NAME s_scm_socketpair
  431. {
  432. int fam;
  433. int fd[2];
  434. SCM_VALIDATE_INUM (1,family);
  435. SCM_VALIDATE_INUM (2,style);
  436. SCM_VALIDATE_INUM (3,proto);
  437. fam = SCM_INUM (family);
  438. if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
  439. SCM_SYSERROR;
  440. return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
  441. }
  442. #undef FUNC_NAME
  443. #endif
  444. SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
  445. (SCM sock, SCM level, SCM optname),
  446. "Return the value of a particular socket option for the socket\n"
  447. "port @var{sock}. @var{level} is an integer code for type of\n"
  448. "option being requested, e.g., @code{SOL_SOCKET} for\n"
  449. "socket-level options. @var{optname} is an integer code for the\n"
  450. "option required and should be specified using one of the\n"
  451. "symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
  452. "The returned value is typically an integer but @code{SO_LINGER}\n"
  453. "returns a pair of integers.")
  454. #define FUNC_NAME s_scm_getsockopt
  455. {
  456. int fd;
  457. /* size of optval is the largest supported option. */
  458. #ifdef HAVE_STRUCT_LINGER
  459. char optval[sizeof (struct linger)];
  460. socklen_t optlen = sizeof (struct linger);
  461. #else
  462. char optval[sizeof (size_t)];
  463. socklen_t optlen = sizeof (size_t);
  464. #endif
  465. int ilevel;
  466. int ioptname;
  467. sock = SCM_COERCE_OUTPORT (sock);
  468. SCM_VALIDATE_OPFPORT (1, sock);
  469. SCM_VALIDATE_INUM_COPY (2, level, ilevel);
  470. SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
  471. fd = SCM_FPORT_FDES (sock);
  472. if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
  473. SCM_SYSERROR;
  474. if (ilevel == SOL_SOCKET)
  475. {
  476. #ifdef SO_LINGER
  477. if (ioptname == SO_LINGER)
  478. {
  479. #ifdef HAVE_STRUCT_LINGER
  480. struct linger *ling = (struct linger *) optval;
  481. return scm_cons (scm_long2num (ling->l_onoff),
  482. scm_long2num (ling->l_linger));
  483. #else
  484. return scm_cons (scm_long2num (*(int *) optval),
  485. SCM_MAKINUM (0));
  486. #endif
  487. }
  488. else
  489. #endif
  490. if (0
  491. #ifdef SO_SNDBUF
  492. || ioptname == SO_SNDBUF
  493. #endif
  494. #ifdef SO_RCVBUF
  495. || ioptname == SO_RCVBUF
  496. #endif
  497. )
  498. {
  499. return scm_long2num (*(size_t *) optval);
  500. }
  501. }
  502. return scm_long2num (*(int *) optval);
  503. }
  504. #undef FUNC_NAME
  505. SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
  506. (SCM sock, SCM level, SCM optname, SCM value),
  507. "Set the value of a particular socket option for the socket\n"
  508. "port @var{sock}. @var{level} is an integer code for type of option\n"
  509. "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n"
  510. "@var{optname} is an\n"
  511. "integer code for the option to set and should be specified using one of\n"
  512. "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n"
  513. "@var{value} is the value to which the option should be set. For\n"
  514. "most options this must be an integer, but for @code{SO_LINGER} it must\n"
  515. "be a pair.\n\n"
  516. "The return value is unspecified.")
  517. #define FUNC_NAME s_scm_setsockopt
  518. {
  519. int fd;
  520. int optlen = -1;
  521. /* size of optval is the largest supported option. */
  522. #ifdef HAVE_STRUCT_LINGER
  523. char optval[sizeof (struct linger)];
  524. #else
  525. char optval[sizeof (size_t)];
  526. #endif
  527. int ilevel, ioptname;
  528. sock = SCM_COERCE_OUTPORT (sock);
  529. SCM_VALIDATE_OPFPORT (1, sock);
  530. SCM_VALIDATE_INUM_COPY (2, level, ilevel);
  531. SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
  532. fd = SCM_FPORT_FDES (sock);
  533. if (ilevel == SOL_SOCKET)
  534. {
  535. #ifdef SO_LINGER
  536. if (ioptname == SO_LINGER)
  537. {
  538. #ifdef HAVE_STRUCT_LINGER
  539. struct linger ling;
  540. long lv;
  541. SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
  542. lv = SCM_NUM2LONG (4, SCM_CAR (value));
  543. ling.l_onoff = (int) lv;
  544. SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_onoff == lv);
  545. lv = SCM_NUM2LONG (4, SCM_CDR (value));
  546. ling.l_linger = (int) lv;
  547. SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_linger == lv);
  548. optlen = (int) sizeof (struct linger);
  549. memcpy (optval, (void *) &ling, optlen);
  550. #else
  551. int ling;
  552. long lv;
  553. SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
  554. /* timeout is ignored, but may as well validate it. */
  555. lv = SCM_NUM2LONG (4, SCM_CDR (value));
  556. ling = (int) lv;
  557. SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
  558. lv = SCM_NUM2LONG (4, SCM_CAR (value));
  559. ling = (int) lv;
  560. SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
  561. optlen = (int) sizeof (int);
  562. (*(int *) optval) = ling;
  563. #endif
  564. }
  565. else
  566. #endif
  567. if (0
  568. #ifdef SO_SNDBUF
  569. || ioptname == SO_SNDBUF
  570. #endif
  571. #ifdef SO_RCVBUF
  572. || ioptname == SO_RCVBUF
  573. #endif
  574. )
  575. {
  576. long lv = SCM_NUM2LONG (4, value);
  577. optlen = (int) sizeof (size_t);
  578. (*(size_t *) optval) = (size_t) lv;
  579. }
  580. }
  581. if (optlen == -1)
  582. {
  583. /* Most options take an int. */
  584. long lv = SCM_NUM2LONG (4, value);
  585. int val = (int) lv;
  586. SCM_ASSERT_RANGE (SCM_ARG4, value, val == lv);
  587. optlen = (int) sizeof (int);
  588. (*(int *) optval) = val;
  589. }
  590. if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
  591. SCM_SYSERROR;
  592. return SCM_UNSPECIFIED;
  593. }
  594. #undef FUNC_NAME
  595. SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
  596. (SCM sock, SCM how),
  597. "Sockets can be closed simply by using @code{close-port}. The\n"
  598. "@code{shutdown} procedure allows reception or transmission on a\n"
  599. "connection to be shut down individually, according to the parameter\n"
  600. "@var{how}:\n\n"
  601. "@table @asis\n"
  602. "@item 0\n"
  603. "Stop receiving data for this socket. If further data arrives, reject it.\n"
  604. "@item 1\n"
  605. "Stop trying to transmit data from this socket. Discard any\n"
  606. "data waiting to be sent. Stop looking for acknowledgement of\n"
  607. "data already sent; don't retransmit it if it is lost.\n"
  608. "@item 2\n"
  609. "Stop both reception and transmission.\n"
  610. "@end table\n\n"
  611. "The return value is unspecified.")
  612. #define FUNC_NAME s_scm_shutdown
  613. {
  614. int fd;
  615. sock = SCM_COERCE_OUTPORT (sock);
  616. SCM_VALIDATE_OPFPORT (1,sock);
  617. SCM_VALIDATE_INUM (2,how);
  618. SCM_ASSERT_RANGE(2,how,0 <= SCM_INUM (how) && 2 >= SCM_INUM (how));
  619. fd = SCM_FPORT_FDES (sock);
  620. if (shutdown (fd, SCM_INUM (how)) == -1)
  621. SCM_SYSERROR;
  622. return SCM_UNSPECIFIED;
  623. }
  624. #undef FUNC_NAME
  625. /* convert fam/address/args into a sockaddr of the appropriate type.
  626. args is modified by removing the arguments actually used.
  627. which_arg and proc are used when reporting errors:
  628. which_arg is the position of address in the original argument list.
  629. proc is the name of the original procedure.
  630. size returns the size of the structure allocated. */
  631. static struct sockaddr *
  632. scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
  633. const char *proc, int *size)
  634. #define FUNC_NAME proc
  635. {
  636. switch (fam)
  637. {
  638. case AF_INET:
  639. {
  640. struct sockaddr_in *soka;
  641. unsigned long addr;
  642. int port;
  643. SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
  644. SCM_VALIDATE_CONS (which_arg + 1, *args);
  645. SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
  646. *args = SCM_CDR (*args);
  647. soka = (struct sockaddr_in *) malloc (sizeof (struct sockaddr_in));
  648. if (!soka)
  649. scm_memory_error (proc);
  650. #if HAVE_STRUCT_SOCKADDR_SIN_LEN
  651. soka->sin_len = sizeof (struct sockaddr_in);
  652. #endif
  653. soka->sin_family = AF_INET;
  654. soka->sin_addr.s_addr = htonl (addr);
  655. soka->sin_port = htons (port);
  656. *size = sizeof (struct sockaddr_in);
  657. return (struct sockaddr *) soka;
  658. }
  659. #ifdef HAVE_IPV6
  660. case AF_INET6:
  661. {
  662. /* see RFC2553. */
  663. int port;
  664. struct sockaddr_in6 *soka;
  665. unsigned long flowinfo = 0;
  666. unsigned long scope_id = 0;
  667. VALIDATE_INET6 (which_arg, address);
  668. SCM_VALIDATE_CONS (which_arg + 1, *args);
  669. SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
  670. *args = SCM_CDR (*args);
  671. if (SCM_CONSP (*args))
  672. {
  673. SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
  674. *args = SCM_CDR (*args);
  675. if (SCM_CONSP (*args))
  676. {
  677. SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
  678. scope_id);
  679. *args = SCM_CDR (*args);
  680. }
  681. }
  682. soka = (struct sockaddr_in6 *) malloc (sizeof (struct sockaddr_in6));
  683. if (!soka)
  684. scm_memory_error (proc);
  685. #if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
  686. soka->sin6_len = sizeof (struct sockaddr_in6);
  687. #endif
  688. soka->sin6_family = AF_INET6;
  689. ipv6_num_to_net (address, soka->sin6_addr.s6_addr);
  690. soka->sin6_port = htons (port);
  691. soka->sin6_flowinfo = flowinfo;
  692. #ifdef HAVE_SIN6_SCOPE_ID
  693. soka->sin6_scope_id = scope_id;
  694. #endif
  695. *size = sizeof (struct sockaddr_in6);
  696. return (struct sockaddr *) soka;
  697. }
  698. #endif
  699. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  700. case AF_UNIX:
  701. {
  702. struct sockaddr_un *soka;
  703. int addr_size;
  704. SCM_ASSERT (SCM_STRINGP (address), address, which_arg, proc);
  705. /* the static buffer size in sockaddr_un seems to be arbitrary
  706. and not necessarily a hard limit. e.g., the glibc manual
  707. suggests it may be possible to declare it size 0. let's
  708. ignore it. if the O/S doesn't like the size it will cause
  709. connect/bind etc., to fail. sun_path is always the last
  710. member of the structure. */
  711. addr_size = sizeof (struct sockaddr_un)
  712. + max (0, SCM_STRING_LENGTH (address) + 1 - (sizeof soka->sun_path));
  713. soka = (struct sockaddr_un *) malloc (addr_size);
  714. if (!soka)
  715. scm_memory_error (proc);
  716. memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */
  717. soka->sun_family = AF_UNIX;
  718. memcpy (soka->sun_path, SCM_STRING_CHARS (address),
  719. SCM_STRING_LENGTH (address));
  720. *size = SUN_LEN (soka);
  721. return (struct sockaddr *) soka;
  722. }
  723. #endif
  724. default:
  725. scm_out_of_range (proc, SCM_MAKINUM (fam));
  726. }
  727. }
  728. #undef FUNC_NAME
  729. SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
  730. (SCM sock, SCM fam, SCM address, SCM args),
  731. "Initiate a connection from a socket using a specified address\n"
  732. "family to the address\n"
  733. "specified by @var{address} and possibly @var{args}.\n"
  734. "The format required for @var{address}\n"
  735. "and @var{args} depends on the family of the socket.\n\n"
  736. "For a socket of family @code{AF_UNIX},\n"
  737. "only @var{address} is specified and must be a string with the\n"
  738. "filename where the socket is to be created.\n\n"
  739. "For a socket of family @code{AF_INET},\n"
  740. "@var{address} must be an integer IPv4 host address and\n"
  741. "@var{args} must be a single integer port number.\n\n"
  742. "For a socket of family @code{AF_INET6},\n"
  743. "@var{address} must be an integer IPv6 host address and\n"
  744. "@var{args} may be up to three integers:\n"
  745. "port [flowinfo] [scope_id],\n"
  746. "where flowinfo and scope_id default to zero.\n\n"
  747. "The return value is unspecified.")
  748. #define FUNC_NAME s_scm_connect
  749. {
  750. int fd;
  751. struct sockaddr *soka;
  752. int size;
  753. sock = SCM_COERCE_OUTPORT (sock);
  754. SCM_VALIDATE_OPFPORT (1,sock);
  755. SCM_VALIDATE_INUM (2,fam);
  756. fd = SCM_FPORT_FDES (sock);
  757. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
  758. &size);
  759. if (connect (fd, soka, size) == -1)
  760. {
  761. int save_errno = errno;
  762. free (soka);
  763. errno = save_errno;
  764. SCM_SYSERROR;
  765. }
  766. free (soka);
  767. return SCM_UNSPECIFIED;
  768. }
  769. #undef FUNC_NAME
  770. SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
  771. (SCM sock, SCM fam, SCM address, SCM args),
  772. "Assign an address to the socket port @var{sock}.\n"
  773. "Generally this only needs to be done for server sockets,\n"
  774. "so they know where to look for incoming connections. A socket\n"
  775. "without an address will be assigned one automatically when it\n"
  776. "starts communicating.\n\n"
  777. "The format of @var{address} and @var{args} depends\n"
  778. "on the family of the socket.\n\n"
  779. "For a socket of family @code{AF_UNIX}, only @var{address}\n"
  780. "is specified and must be a string with the filename where\n"
  781. "the socket is to be created.\n\n"
  782. "For a socket of family @code{AF_INET}, @var{address}\n"
  783. "must be an integer IPv4 address and @var{args}\n"
  784. "must be a single integer port number.\n\n"
  785. "The values of the following variables can also be used for\n"
  786. "@var{address}:\n\n"
  787. "@defvar INADDR_ANY\n"
  788. "Allow connections from any address.\n"
  789. "@end defvar\n\n"
  790. "@defvar INADDR_LOOPBACK\n"
  791. "The address of the local host using the loopback device.\n"
  792. "@end defvar\n\n"
  793. "@defvar INADDR_BROADCAST\n"
  794. "The broadcast address on the local network.\n"
  795. "@end defvar\n\n"
  796. "@defvar INADDR_NONE\n"
  797. "No address.\n"
  798. "@end defvar\n\n"
  799. "For a socket of family @code{AF_INET6}, @var{address}\n"
  800. "must be an integer IPv6 address and @var{args}\n"
  801. "may be up to three integers:\n"
  802. "port [flowinfo] [scope_id],\n"
  803. "where flowinfo and scope_id default to zero.\n\n"
  804. "The return value is unspecified.")
  805. #define FUNC_NAME s_scm_bind
  806. {
  807. struct sockaddr *soka;
  808. int size;
  809. int fd;
  810. sock = SCM_COERCE_OUTPORT (sock);
  811. SCM_VALIDATE_OPFPORT (1, sock);
  812. SCM_VALIDATE_INUM (2, fam);
  813. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
  814. &size);
  815. fd = SCM_FPORT_FDES (sock);
  816. if (bind (fd, soka, size) == -1)
  817. {
  818. int save_errno = errno;
  819. free (soka);
  820. errno = save_errno;
  821. SCM_SYSERROR;
  822. }
  823. free (soka);
  824. return SCM_UNSPECIFIED;
  825. }
  826. #undef FUNC_NAME
  827. SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
  828. (SCM sock, SCM backlog),
  829. "Enable @var{sock} to accept connection\n"
  830. "requests. @var{backlog} is an integer specifying\n"
  831. "the maximum length of the queue for pending connections.\n"
  832. "If the queue fills, new clients will fail to connect until\n"
  833. "the server calls @code{accept} to accept a connection from\n"
  834. "the queue.\n\n"
  835. "The return value is unspecified.")
  836. #define FUNC_NAME s_scm_listen
  837. {
  838. int fd;
  839. sock = SCM_COERCE_OUTPORT (sock);
  840. SCM_VALIDATE_OPFPORT (1,sock);
  841. SCM_VALIDATE_INUM (2,backlog);
  842. fd = SCM_FPORT_FDES (sock);
  843. if (listen (fd, SCM_INUM (backlog)) == -1)
  844. SCM_SYSERROR;
  845. return SCM_UNSPECIFIED;
  846. }
  847. #undef FUNC_NAME
  848. /* Put the components of a sockaddr into a new SCM vector. */
  849. static SCM
  850. scm_addr_vector (const struct sockaddr *address, const char *proc)
  851. {
  852. short int fam = address->sa_family;
  853. SCM result;
  854. SCM *ve;
  855. switch (fam)
  856. {
  857. case AF_INET:
  858. {
  859. const struct sockaddr_in *nad = (struct sockaddr_in *) address;
  860. result = scm_c_make_vector (3, SCM_UNSPECIFIED);
  861. ve = SCM_VELTS (result);
  862. ve[0] = scm_ulong2num ((unsigned long) fam);
  863. ve[1] = scm_ulong2num (ntohl (nad->sin_addr.s_addr));
  864. ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin_port));
  865. }
  866. break;
  867. #ifdef HAVE_IPV6
  868. case AF_INET6:
  869. {
  870. const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
  871. result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  872. ve = SCM_VELTS (result);
  873. ve[0] = scm_ulong2num ((unsigned long) fam);
  874. ve[1] = ipv6_net_to_num (nad->sin6_addr.s6_addr);
  875. ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin6_port));
  876. ve[3] = scm_ulong2num ((unsigned long) nad->sin6_flowinfo);
  877. #ifdef HAVE_SIN6_SCOPE_ID
  878. ve[4] = scm_ulong2num ((unsigned long) nad->sin6_scope_id);
  879. #else
  880. ve[4] = SCM_INUM0;
  881. #endif
  882. }
  883. break;
  884. #endif
  885. #ifdef HAVE_UNIX_DOMAIN_SOCKETS
  886. case AF_UNIX:
  887. {
  888. const struct sockaddr_un *nad = (struct sockaddr_un *) address;
  889. result = scm_c_make_vector (2, SCM_UNSPECIFIED);
  890. ve = SCM_VELTS (result);
  891. ve[0] = scm_ulong2num ((unsigned long) fam);
  892. ve[1] = scm_mem2string (nad->sun_path, strlen (nad->sun_path));
  893. }
  894. break;
  895. #endif
  896. default:
  897. scm_misc_error (proc, "Unrecognised address family: ~A",
  898. scm_list_1 (SCM_MAKINUM (fam)));
  899. }
  900. return result;
  901. }
  902. /* calculate the size of a buffer large enough to hold any supported
  903. sockaddr type. if the buffer isn't large enough, certain system
  904. calls will return a truncated address. */
  905. #if defined (HAVE_UNIX_DOMAIN_SOCKETS)
  906. #define MAX_SIZE_UN sizeof (struct sockaddr_un)
  907. #else
  908. #define MAX_SIZE_UN 0
  909. #endif
  910. #if defined (HAVE_IPV6)
  911. #define MAX_SIZE_IN6 sizeof (struct sockaddr_in6)
  912. #else
  913. #define MAX_SIZE_IN6 0
  914. #endif
  915. #define MAX_ADDR_SIZE max (max (sizeof (struct sockaddr_in), MAX_SIZE_IN6),\
  916. MAX_SIZE_UN)
  917. SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
  918. (SCM sock),
  919. "Accept a connection on a bound, listening socket.\n"
  920. "If there\n"
  921. "are no pending connections in the queue, wait until\n"
  922. "one is available unless the non-blocking option has been\n"
  923. "set on the socket.\n\n"
  924. "The return value is a\n"
  925. "pair in which the @emph{car} is a new socket port for the\n"
  926. "connection and\n"
  927. "the @emph{cdr} is an object with address information about the\n"
  928. "client which initiated the connection.\n\n"
  929. "@var{sock} does not become part of the\n"
  930. "connection and will continue to accept new requests.")
  931. #define FUNC_NAME s_scm_accept
  932. {
  933. int fd;
  934. int newfd;
  935. SCM address;
  936. SCM newsock;
  937. socklen_t addr_size = MAX_ADDR_SIZE;
  938. char max_addr[MAX_ADDR_SIZE];
  939. struct sockaddr *addr = (struct sockaddr *) max_addr;
  940. sock = SCM_COERCE_OUTPORT (sock);
  941. SCM_VALIDATE_OPFPORT (1, sock);
  942. fd = SCM_FPORT_FDES (sock);
  943. newfd = accept (fd, addr, &addr_size);
  944. if (newfd == -1)
  945. SCM_SYSERROR;
  946. newsock = SCM_SOCK_FD_TO_PORT (newfd);
  947. address = scm_addr_vector (addr, FUNC_NAME);
  948. return scm_cons (newsock, address);
  949. }
  950. #undef FUNC_NAME
  951. SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
  952. (SCM sock),
  953. "Return the address of @var{sock}, in the same form as the\n"
  954. "object returned by @code{accept}. On many systems the address\n"
  955. "of a socket in the @code{AF_FILE} namespace cannot be read.")
  956. #define FUNC_NAME s_scm_getsockname
  957. {
  958. int fd;
  959. socklen_t addr_size = MAX_ADDR_SIZE;
  960. char max_addr[MAX_ADDR_SIZE];
  961. struct sockaddr *addr = (struct sockaddr *) max_addr;
  962. sock = SCM_COERCE_OUTPORT (sock);
  963. SCM_VALIDATE_OPFPORT (1,sock);
  964. fd = SCM_FPORT_FDES (sock);
  965. if (getsockname (fd, addr, &addr_size) == -1)
  966. SCM_SYSERROR;
  967. return scm_addr_vector (addr, FUNC_NAME);
  968. }
  969. #undef FUNC_NAME
  970. SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
  971. (SCM sock),
  972. "Return the address that @var{sock}\n"
  973. "is connected to, in the same form as the object returned by\n"
  974. "@code{accept}. On many systems the address of a socket in the\n"
  975. "@code{AF_FILE} namespace cannot be read.")
  976. #define FUNC_NAME s_scm_getpeername
  977. {
  978. int fd;
  979. socklen_t addr_size = MAX_ADDR_SIZE;
  980. char max_addr[MAX_ADDR_SIZE];
  981. struct sockaddr *addr = (struct sockaddr *) max_addr;
  982. sock = SCM_COERCE_OUTPORT (sock);
  983. SCM_VALIDATE_OPFPORT (1,sock);
  984. fd = SCM_FPORT_FDES (sock);
  985. if (getpeername (fd, addr, &addr_size) == -1)
  986. SCM_SYSERROR;
  987. return scm_addr_vector (addr, FUNC_NAME);
  988. }
  989. #undef FUNC_NAME
  990. SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
  991. (SCM sock, SCM buf, SCM flags),
  992. "Receive data from a socket port.\n"
  993. "@var{sock} must already\n"
  994. "be bound to the address from which data is to be received.\n"
  995. "@var{buf} is a string into which\n"
  996. "the data will be written. The size of @var{buf} limits\n"
  997. "the amount of\n"
  998. "data which can be received: in the case of packet\n"
  999. "protocols, if a packet larger than this limit is encountered\n"
  1000. "then some data\n"
  1001. "will be irrevocably lost.\n\n"
  1002. "The optional @var{flags} argument is a value or\n"
  1003. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1004. "The value returned is the number of bytes read from the\n"
  1005. "socket.\n\n"
  1006. "Note that the data is read directly from the socket file\n"
  1007. "descriptor:\n"
  1008. "any unread buffered port data is ignored.")
  1009. #define FUNC_NAME s_scm_recv
  1010. {
  1011. int rv;
  1012. int fd;
  1013. int flg;
  1014. SCM_VALIDATE_OPFPORT (1,sock);
  1015. SCM_VALIDATE_STRING (2,buf);
  1016. SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
  1017. fd = SCM_FPORT_FDES (sock);
  1018. SCM_SYSCALL (rv = recv (fd, SCM_STRING_CHARS (buf), SCM_STRING_LENGTH (buf), flg));
  1019. if (rv == -1)
  1020. SCM_SYSERROR;
  1021. return SCM_MAKINUM (rv);
  1022. }
  1023. #undef FUNC_NAME
  1024. SCM_DEFINE (scm_send, "send", 2, 1, 0,
  1025. (SCM sock, SCM message, SCM flags),
  1026. "Transmit the string @var{message} on a socket port @var{sock}.\n"
  1027. "@var{sock} must already be bound to a destination address. The\n"
  1028. "value returned is the number of bytes transmitted --\n"
  1029. "it's possible for\n"
  1030. "this to be less than the length of @var{message}\n"
  1031. "if the socket is\n"
  1032. "set to be non-blocking. The optional @var{flags} argument\n"
  1033. "is a value or\n"
  1034. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1035. "Note that the data is written directly to the socket\n"
  1036. "file descriptor:\n"
  1037. "any unflushed buffered port data is ignored.")
  1038. #define FUNC_NAME s_scm_send
  1039. {
  1040. int rv;
  1041. int fd;
  1042. int flg;
  1043. sock = SCM_COERCE_OUTPORT (sock);
  1044. SCM_VALIDATE_OPFPORT (1,sock);
  1045. SCM_VALIDATE_STRING (2, message);
  1046. SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
  1047. fd = SCM_FPORT_FDES (sock);
  1048. SCM_SYSCALL (rv = send (fd, SCM_STRING_CHARS (message), SCM_STRING_LENGTH (message), flg));
  1049. if (rv == -1)
  1050. SCM_SYSERROR;
  1051. return SCM_MAKINUM (rv);
  1052. }
  1053. #undef FUNC_NAME
  1054. SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
  1055. (SCM sock, SCM str, SCM flags, SCM start, SCM end),
  1056. "Return data from the socket port @var{sock} and also\n"
  1057. "information about where the data was received from.\n"
  1058. "@var{sock} must already be bound to the address from which\n"
  1059. "data is to be received. @code{str}, is a string into which the\n"
  1060. "data will be written. The size of @var{str} limits the amount\n"
  1061. "of data which can be received: in the case of packet protocols,\n"
  1062. "if a packet larger than this limit is encountered then some\n"
  1063. "data will be irrevocably lost.\n\n"
  1064. "The optional @var{flags} argument is a value or bitwise OR of\n"
  1065. "@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.\n\n"
  1066. "The value returned is a pair: the @emph{car} is the number of\n"
  1067. "bytes read from the socket and the @emph{cdr} an address object\n"
  1068. "in the same form as returned by @code{accept}. The address\n"
  1069. "will given as @code{#f} if not available, as is usually the\n"
  1070. "case for stream sockets.\n\n"
  1071. "The @var{start} and @var{end} arguments specify a substring of\n"
  1072. "@var{str} to which the data should be written.\n\n"
  1073. "Note that the data is read directly from the socket file\n"
  1074. "descriptor: any unread buffered port data is ignored.")
  1075. #define FUNC_NAME s_scm_recvfrom
  1076. {
  1077. int rv;
  1078. int fd;
  1079. int flg;
  1080. char *buf;
  1081. int offset;
  1082. int cend;
  1083. SCM address;
  1084. socklen_t addr_size = MAX_ADDR_SIZE;
  1085. char max_addr[MAX_ADDR_SIZE];
  1086. struct sockaddr *addr = (struct sockaddr *) max_addr;
  1087. SCM_VALIDATE_OPFPORT (1,sock);
  1088. fd = SCM_FPORT_FDES (sock);
  1089. SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, str, buf, 4, start, offset,
  1090. 5, end, cend);
  1091. if (SCM_UNBNDP (flags))
  1092. flg = 0;
  1093. else
  1094. SCM_VALIDATE_ULONG_COPY (3, flags, flg);
  1095. /* recvfrom will not necessarily return an address. usually nothing
  1096. is returned for stream sockets. */
  1097. addr->sa_family = AF_UNSPEC;
  1098. SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
  1099. cend - offset, flg,
  1100. addr, &addr_size));
  1101. if (rv == -1)
  1102. SCM_SYSERROR;
  1103. if (addr->sa_family != AF_UNSPEC)
  1104. address = scm_addr_vector (addr, FUNC_NAME);
  1105. else
  1106. address = SCM_BOOL_F;
  1107. return scm_cons (SCM_MAKINUM (rv), address);
  1108. }
  1109. #undef FUNC_NAME
  1110. SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
  1111. (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
  1112. "Transmit the string @var{message} on the socket port\n"
  1113. "@var{sock}. The\n"
  1114. "destination address is specified using the @var{fam},\n"
  1115. "@var{address} and\n"
  1116. "@var{args_and_flags} arguments, in a similar way to the\n"
  1117. "@code{connect} procedure. @var{args_and_flags} contains\n"
  1118. "the usual connection arguments optionally followed by\n"
  1119. "a flags argument, which is a value or\n"
  1120. "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
  1121. "The value returned is the number of bytes transmitted --\n"
  1122. "it's possible for\n"
  1123. "this to be less than the length of @var{message} if the\n"
  1124. "socket is\n"
  1125. "set to be non-blocking.\n"
  1126. "Note that the data is written directly to the socket\n"
  1127. "file descriptor:\n"
  1128. "any unflushed buffered port data is ignored.")
  1129. #define FUNC_NAME s_scm_sendto
  1130. {
  1131. int rv;
  1132. int fd;
  1133. int flg;
  1134. struct sockaddr *soka;
  1135. int size;
  1136. sock = SCM_COERCE_OUTPORT (sock);
  1137. SCM_VALIDATE_FPORT (1,sock);
  1138. SCM_VALIDATE_STRING (2, message);
  1139. SCM_VALIDATE_INUM (3,fam);
  1140. fd = SCM_FPORT_FDES (sock);
  1141. soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args_and_flags, 4,
  1142. FUNC_NAME, &size);
  1143. if (SCM_NULLP (args_and_flags))
  1144. flg = 0;
  1145. else
  1146. {
  1147. SCM_VALIDATE_CONS (5,args_and_flags);
  1148. flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
  1149. }
  1150. SCM_SYSCALL (rv = sendto (fd, SCM_STRING_CHARS (message),
  1151. SCM_STRING_LENGTH (message),
  1152. flg, soka, size));
  1153. if (rv == -1)
  1154. {
  1155. int save_errno = errno;
  1156. free (soka);
  1157. errno = save_errno;
  1158. SCM_SYSERROR;
  1159. }
  1160. free (soka);
  1161. return SCM_MAKINUM (rv);
  1162. }
  1163. #undef FUNC_NAME
  1164. void
  1165. scm_init_socket ()
  1166. {
  1167. /* protocol families. */
  1168. #ifdef AF_UNSPEC
  1169. scm_c_define ("AF_UNSPEC", SCM_MAKINUM (AF_UNSPEC));
  1170. #endif
  1171. #ifdef AF_UNIX
  1172. scm_c_define ("AF_UNIX", SCM_MAKINUM (AF_UNIX));
  1173. #endif
  1174. #ifdef AF_INET
  1175. scm_c_define ("AF_INET", SCM_MAKINUM (AF_INET));
  1176. #endif
  1177. #ifdef AF_INET6
  1178. scm_c_define ("AF_INET6", SCM_MAKINUM (AF_INET6));
  1179. #endif
  1180. #ifdef PF_UNSPEC
  1181. scm_c_define ("PF_UNSPEC", SCM_MAKINUM (PF_UNSPEC));
  1182. #endif
  1183. #ifdef PF_UNIX
  1184. scm_c_define ("PF_UNIX", SCM_MAKINUM (PF_UNIX));
  1185. #endif
  1186. #ifdef PF_INET
  1187. scm_c_define ("PF_INET", SCM_MAKINUM (PF_INET));
  1188. #endif
  1189. #ifdef PF_INET6
  1190. scm_c_define ("PF_INET6", SCM_MAKINUM (PF_INET6));
  1191. #endif
  1192. /* standard addresses. */
  1193. #ifdef INADDR_ANY
  1194. scm_c_define ("INADDR_ANY", scm_ulong2num (INADDR_ANY));
  1195. #endif
  1196. #ifdef INADDR_BROADCAST
  1197. scm_c_define ("INADDR_BROADCAST", scm_ulong2num (INADDR_BROADCAST));
  1198. #endif
  1199. #ifdef INADDR_NONE
  1200. scm_c_define ("INADDR_NONE", scm_ulong2num (INADDR_NONE));
  1201. #endif
  1202. #ifdef INADDR_LOOPBACK
  1203. scm_c_define ("INADDR_LOOPBACK", scm_ulong2num (INADDR_LOOPBACK));
  1204. #endif
  1205. /* socket types.
  1206. SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and
  1207. packet(7) man pages advise that it's obsolete and strongly
  1208. deprecated. */
  1209. #ifdef SOCK_STREAM
  1210. scm_c_define ("SOCK_STREAM", SCM_MAKINUM (SOCK_STREAM));
  1211. #endif
  1212. #ifdef SOCK_DGRAM
  1213. scm_c_define ("SOCK_DGRAM", SCM_MAKINUM (SOCK_DGRAM));
  1214. #endif
  1215. #ifdef SOCK_SEQPACKET
  1216. scm_c_define ("SOCK_SEQPACKET", SCM_MAKINUM (SOCK_SEQPACKET));
  1217. #endif
  1218. #ifdef SOCK_RAW
  1219. scm_c_define ("SOCK_RAW", SCM_MAKINUM (SOCK_RAW));
  1220. #endif
  1221. #ifdef SOCK_RDM
  1222. scm_c_define ("SOCK_RDM", SCM_MAKINUM (SOCK_RDM));
  1223. #endif
  1224. /* setsockopt level. */
  1225. #ifdef SOL_SOCKET
  1226. scm_c_define ("SOL_SOCKET", SCM_MAKINUM (SOL_SOCKET));
  1227. #endif
  1228. #ifdef SOL_IP
  1229. scm_c_define ("SOL_IP", SCM_MAKINUM (SOL_IP));
  1230. #endif
  1231. #ifdef SOL_TCP
  1232. scm_c_define ("SOL_TCP", SCM_MAKINUM (SOL_TCP));
  1233. #endif
  1234. #ifdef SOL_UDP
  1235. scm_c_define ("SOL_UDP", SCM_MAKINUM (SOL_UDP));
  1236. #endif
  1237. /* setsockopt names. */
  1238. #ifdef SO_DEBUG
  1239. scm_c_define ("SO_DEBUG", SCM_MAKINUM (SO_DEBUG));
  1240. #endif
  1241. #ifdef SO_REUSEADDR
  1242. scm_c_define ("SO_REUSEADDR", SCM_MAKINUM (SO_REUSEADDR));
  1243. #endif
  1244. #ifdef SO_STYLE
  1245. scm_c_define ("SO_STYLE", SCM_MAKINUM (SO_STYLE));
  1246. #endif
  1247. #ifdef SO_TYPE
  1248. scm_c_define ("SO_TYPE", SCM_MAKINUM (SO_TYPE));
  1249. #endif
  1250. #ifdef SO_ERROR
  1251. scm_c_define ("SO_ERROR", SCM_MAKINUM (SO_ERROR));
  1252. #endif
  1253. #ifdef SO_DONTROUTE
  1254. scm_c_define ("SO_DONTROUTE", SCM_MAKINUM (SO_DONTROUTE));
  1255. #endif
  1256. #ifdef SO_BROADCAST
  1257. scm_c_define ("SO_BROADCAST", SCM_MAKINUM (SO_BROADCAST));
  1258. #endif
  1259. #ifdef SO_SNDBUF
  1260. scm_c_define ("SO_SNDBUF", SCM_MAKINUM (SO_SNDBUF));
  1261. #endif
  1262. #ifdef SO_RCVBUF
  1263. scm_c_define ("SO_RCVBUF", SCM_MAKINUM (SO_RCVBUF));
  1264. #endif
  1265. #ifdef SO_KEEPALIVE
  1266. scm_c_define ("SO_KEEPALIVE", SCM_MAKINUM (SO_KEEPALIVE));
  1267. #endif
  1268. #ifdef SO_OOBINLINE
  1269. scm_c_define ("SO_OOBINLINE", SCM_MAKINUM (SO_OOBINLINE));
  1270. #endif
  1271. #ifdef SO_NO_CHECK
  1272. scm_c_define ("SO_NO_CHECK", SCM_MAKINUM (SO_NO_CHECK));
  1273. #endif
  1274. #ifdef SO_PRIORITY
  1275. scm_c_define ("SO_PRIORITY", SCM_MAKINUM (SO_PRIORITY));
  1276. #endif
  1277. #ifdef SO_LINGER
  1278. scm_c_define ("SO_LINGER", SCM_MAKINUM (SO_LINGER));
  1279. #endif
  1280. /* recv/send options. */
  1281. #ifdef MSG_OOB
  1282. scm_c_define ("MSG_OOB", SCM_MAKINUM (MSG_OOB));
  1283. #endif
  1284. #ifdef MSG_PEEK
  1285. scm_c_define ("MSG_PEEK", SCM_MAKINUM (MSG_PEEK));
  1286. #endif
  1287. #ifdef MSG_DONTROUTE
  1288. scm_c_define ("MSG_DONTROUTE", SCM_MAKINUM (MSG_DONTROUTE));
  1289. #endif
  1290. scm_add_feature ("socket");
  1291. #include "libguile/socket.x"
  1292. }
  1293. /*
  1294. Local Variables:
  1295. c-file-style: "gnu"
  1296. End:
  1297. */