socket.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. *
  3. * Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Marcus Crestani
  4. */
  5. #define NO_OLD_FFI 1
  6. /*
  7. * Unix-specific sockets stuff.
  8. */
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <stdlib.h>
  15. #ifdef HAVE_PTHREAD_H
  16. #include <pthread.h>
  17. #endif
  18. #include <scheme48.h>
  19. #include "c-mods.h"
  20. #include "unix.h"
  21. #include "fd-io.h" /* ps_close_fd() */
  22. #include "event.h" /* add_pending_fd() */
  23. #include "sysdep.h"
  24. #include "socket.h"
  25. #include "address.h"
  26. static s48_ref_t
  27. s48_socket(s48_call_t call, s48_ref_t sch_af, s48_ref_t sch_type, s48_ref_t sch_protocol)
  28. {
  29. socket_t fd;
  30. int mode, status;
  31. s48_ref_t sch_channel;
  32. int af = s48_extract_af(call, sch_af);
  33. int socktype = s48_extract_socket_type(call, sch_type);
  34. int protocol = s48_extract_long_2(call, sch_protocol);
  35. RETRY_OR_RAISE_NEG(fd, socket(af, socktype, protocol));
  36. RETRY_OR_RAISE_NEG(status, fcntl(fd, F_SETFL, O_NONBLOCK));
  37. sch_channel = s48_add_channel_2(call, s48_channel_status_special_input_2(call),
  38. s48_enter_string_latin_1_2(call, "socket"), fd);
  39. if (!s48_channel_p_2(call, sch_channel))
  40. {
  41. ps_close_fd(fd); /* retries if interrupted */
  42. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, sch_channel), 0);
  43. }
  44. return sch_channel;
  45. }
  46. static s48_ref_t
  47. s48_socketpair(s48_call_t call, s48_ref_t sch_af, s48_ref_t sch_type, s48_ref_t sch_protocol)
  48. {
  49. int status;
  50. s48_ref_t sch_channel0, sch_channel1;
  51. s48_ref_t sch_result;
  52. int af = s48_extract_af(call, sch_af);
  53. int socktype = s48_extract_socket_type(call, sch_type);
  54. int protocol = s48_extract_long_2(call, sch_protocol);
  55. socket_t fds[2];
  56. RETRY_OR_RAISE_NEG(status, socketpair(af, socktype, protocol, fds));
  57. RETRY_OR_RAISE_NEG(status, fcntl(fds[0], F_SETFL, O_NONBLOCK));
  58. RETRY_OR_RAISE_NEG(status, fcntl(fds[1], F_SETFL, O_NONBLOCK));
  59. sch_channel0 = s48_add_channel_2(call, s48_channel_status_input_2(call),
  60. s48_enter_string_latin_1_2(call, "socket"), fds[0]);
  61. sch_channel1 = s48_add_channel_2(call, s48_channel_status_input_2(call),
  62. s48_enter_string_latin_1_2(call, "socket"), fds[1]);
  63. sch_result = s48_cons_2(call, sch_channel0, sch_channel1);
  64. return sch_result;
  65. }
  66. /*
  67. * dup() `socket_fd' and return an output channel holding the result.
  68. *
  69. * We have to versions, one for calling from C and one for calling from Scheme.
  70. */
  71. static s48_ref_t
  72. dup_socket_channel(s48_call_t call, socket_t socket_fd)
  73. {
  74. socket_t output_fd;
  75. s48_ref_t output_channel;
  76. int flags;
  77. RETRY_OR_RAISE_NEG(output_fd, dup(socket_fd));
  78. RETRY_OR_RAISE_NEG(flags, fcntl(output_fd, F_GETFL));
  79. flags |= O_NONBLOCK;
  80. RETRY_OR_RAISE_NEG(flags, fcntl(output_fd, F_SETFL, flags));
  81. output_channel = s48_add_channel_2(call, s48_channel_status_output_2(call),
  82. s48_enter_string_latin_1_2(call, "socket connection"),
  83. output_fd);
  84. if (!s48_channel_p_2(call, output_channel))
  85. {
  86. ps_close_fd(output_fd); /* retries if interrupted */
  87. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, output_channel), 0);
  88. };
  89. return output_channel;
  90. }
  91. socket_t
  92. s48_extract_socket_fd(s48_call_t call, s48_ref_t sch_channel)
  93. {
  94. s48_check_channel_2(call, sch_channel);
  95. return s48_extract_long_2(call, s48_unsafe_channel_os_index_2(call, sch_channel));
  96. }
  97. static s48_ref_t
  98. s48_dup_socket_channel(s48_call_t call, s48_ref_t sch_channel)
  99. {
  100. return dup_socket_channel(call, s48_extract_socket_fd(call, sch_channel));
  101. }
  102. /*
  103. * Given a bound socket, accept a connection and return a pair of the
  104. * input channel and the raw socket address.
  105. *
  106. * If the accept fails because the client hasn't connected yet, then we
  107. * return #f.
  108. *
  109. * If it fails for any other reason, then an exception is raised.
  110. */
  111. static s48_ref_t
  112. s48_accept(s48_call_t call, s48_ref_t sch_channel, s48_ref_t sch_retry_p)
  113. {
  114. socket_t socket_fd = s48_extract_socket_fd(call, sch_channel);
  115. socket_t connect_fd;
  116. int status;
  117. struct sockaddr_storage address;
  118. socklen_t len;
  119. s48_ref_t input_channel, output_channel;
  120. len = sizeof(address);
  121. connect_fd = accept(socket_fd, (struct sockaddr *)&address, &len);
  122. if (connect_fd >= 0) {
  123. RETRY_OR_RAISE_NEG(status, fcntl(connect_fd, F_SETFL, O_NONBLOCK));
  124. input_channel = s48_add_channel_2(call, s48_channel_status_input_2(call),
  125. s48_enter_string_latin_1_2(call, "socket connection"),
  126. connect_fd);
  127. if (!s48_channel_p_2(call, input_channel))
  128. {
  129. ps_close_fd(connect_fd); /* retries if interrupted */
  130. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, input_channel), 0);
  131. }
  132. return s48_cons_2(call,
  133. input_channel,
  134. s48_enter_sockaddr(call, (const struct sockaddr*)&address, len));
  135. }
  136. /*
  137. * Check for errors. If we need to retry we mark the socket as pending
  138. * and return #F to tell the Scheme procedure to wait.
  139. */
  140. if ((errno != EWOULDBLOCK) && (errno != EINTR) && (errno != EAGAIN))
  141. s48_os_error_2(call, "s48_accept", errno, 2, sch_channel, sch_retry_p);
  142. if (! s48_add_pending_fd(socket_fd, PSTRUE))
  143. s48_out_of_memory_error_2(call);
  144. return s48_false_2(call);
  145. }
  146. /*
  147. * Given a socket and an address, connect the socket.
  148. *
  149. * If this succeeds, it returns an output channel for the connection.
  150. * If it fails because the connect would block, add the socket to the
  151. * pending queue (for output) and return #f.
  152. * If it fails for any other reason, raise an exception.
  153. */
  154. static s48_ref_t
  155. s48_connect(s48_call_t call, s48_ref_t sch_channel,
  156. s48_ref_t sch_address, s48_ref_t sch_retry_p)
  157. {
  158. socket_t socket_fd = s48_extract_socket_fd(call, sch_channel);
  159. /*
  160. * Try the connection. If it works we make an output channel and return it.
  161. * The original socket channel will be used as the input channel.
  162. *
  163. * FreeBSD's connect() behaves oddly. If you get told to wait, wait for
  164. * select() to signal the all-clear, and then try to connect again, you
  165. * get an `already connected' (EISCONN) error. To handle this we pass in
  166. * a retry_p flag. If retry_p is true the `already connected' error is
  167. * ignored.
  168. */
  169. if (connect(socket_fd,
  170. s48_extract_value_pointer_2(call, sch_address, struct sockaddr),
  171. s48_value_size_2(call, sch_address)) >= 0
  172. || ((errno == EISCONN) && (s48_true_p_2(call, sch_retry_p))))
  173. {
  174. s48_unsafe_stob_set_2(call, sch_channel,
  175. s48_channel_status_offset, s48_channel_status_input_2(call));
  176. return dup_socket_channel(call, socket_fd);
  177. }
  178. /*
  179. * Check for errors. If we need to retry we mark the socket as pending
  180. * and return #F to tell the Scheme procedure to wait.
  181. */
  182. /* already connected, will raise an error from Scheme */
  183. if (errno == EISCONN)
  184. return s48_true_2(call);
  185. if (errno != EWOULDBLOCK && errno != EINTR && errno != EALREADY
  186. && errno != EINPROGRESS && errno != EAGAIN)
  187. s48_os_error_2(call, "s48_connect", errno, 3, sch_channel, sch_address, sch_retry_p);
  188. if (! (s48_add_pending_fd(socket_fd, PSFALSE)))
  189. s48_out_of_memory_error_2(call);
  190. return s48_false_2(call);
  191. }
  192. /*
  193. * Receive a message. Returns pair (<byte-count> . <sender>) or just
  194. * <byte-count> if want_sender_p is false.
  195. */
  196. static s48_ref_t
  197. s48_recvfrom(s48_call_t call, s48_ref_t sch_channel,
  198. s48_ref_t sch_buffer, s48_ref_t sch_start, s48_ref_t sch_count,
  199. s48_ref_t sch_flags,
  200. s48_ref_t sch_want_sender_p,
  201. s48_ref_t sch_retry_p)
  202. {
  203. socket_t socket_fd = s48_extract_socket_fd(call, sch_channel);
  204. int want_sender_p = !(s48_false_p_2(call, sch_want_sender_p));
  205. struct sockaddr_storage from;
  206. socklen_t from_len = (want_sender_p ? sizeof(struct sockaddr_storage) : 0);
  207. int flags = s48_extract_msg_flags(call, sch_flags);
  208. size_t buffer_size = s48_byte_vector_length_2(call, sch_buffer);
  209. size_t start = s48_extract_unsigned_long_2(call, sch_start);
  210. size_t count = s48_extract_unsigned_long_2(call, sch_count);
  211. ssize_t status;
  212. if ((start + count) > buffer_size)
  213. s48_assertion_violation_2(call, "s48_sendto", "buffer start or count is wrong", 3,
  214. sch_buffer, sch_start, sch_count);
  215. status = recvfrom(socket_fd,
  216. s48_extract_byte_vector_2(call, sch_buffer) + start,
  217. count,
  218. flags,
  219. want_sender_p ? (struct sockaddr*)&from : NULL,
  220. &from_len);
  221. if (0 <= status)
  222. {
  223. if (want_sender_p)
  224. {
  225. s48_ref_t sch_count, sch_saddr;
  226. s48_ref_t sch_result;
  227. sch_count = s48_enter_unsigned_long_2(call, status);
  228. sch_saddr = s48_enter_sockaddr(call, (struct sockaddr *)&from, from_len);
  229. sch_result = s48_cons_2(call, sch_count, sch_saddr);
  230. return sch_result;
  231. }
  232. else
  233. return s48_enter_unsigned_long_2(call, status);
  234. }
  235. /*
  236. * Check for errors. If we need to retry we mark the socket as pending
  237. * and return #F to tell the Scheme procedure to wait.
  238. */
  239. if (errno != EWOULDBLOCK && errno != EINTR && errno != EALREADY
  240. && errno != EINPROGRESS && errno != EAGAIN)
  241. s48_os_error_2(call, "s48_recv", errno, 6,
  242. sch_channel, sch_buffer, sch_start, sch_count,
  243. sch_flags, sch_want_sender_p);
  244. if (! (s48_add_pending_fd(socket_fd, PSTRUE)))
  245. s48_out_of_memory_error_2(call);
  246. return s48_false_2(call);
  247. }
  248. static s48_ref_t
  249. s48_sendto(s48_call_t call, s48_ref_t sch_channel,
  250. s48_ref_t sch_buffer, s48_ref_t sch_start, s48_ref_t sch_count,
  251. s48_ref_t sch_flags,
  252. s48_ref_t sch_saddr,
  253. s48_ref_t sch_retry_p) /* ignored on Unix */
  254. {
  255. socket_t socket_fd = s48_extract_socket_fd(call, sch_channel);
  256. ssize_t sent;
  257. const struct sockaddr *sa
  258. = s48_extract_value_pointer_2(call, sch_saddr, const struct sockaddr);
  259. socklen_t salen = s48_value_size_2(call, sch_saddr);
  260. int flags = s48_extract_msg_flags(call, sch_flags);
  261. size_t buffer_size = s48_byte_vector_length_2(call, sch_buffer);
  262. size_t start = s48_extract_unsigned_long_2(call, sch_start);
  263. size_t count = s48_extract_unsigned_long_2(call, sch_count);
  264. if ((start + count) > buffer_size)
  265. s48_assertion_violation_2(call, "s48_sendto", "buffer start or count is wrong", 3,
  266. sch_buffer, sch_start, sch_count);
  267. sent = sendto(socket_fd,
  268. s48_extract_byte_vector_readonly_2(call, sch_buffer) + start,
  269. count,
  270. flags,
  271. (struct sockaddr *) sa, salen);
  272. if (0 <= sent)
  273. return s48_enter_unsigned_long_2(call, sent);
  274. /*
  275. * Check for errors. If we need to retry we mark the socket as pending
  276. * and return #F to tell the Scheme procedure to wait.
  277. */
  278. if (errno != EWOULDBLOCK && errno != EINTR && errno != EALREADY
  279. && errno != EINPROGRESS && errno != EAGAIN)
  280. s48_os_error_2(call, "s48_sendto", errno, 6,
  281. sch_channel, sch_saddr, sch_flags, sch_buffer, sch_start, sch_count);
  282. if (! (s48_add_pending_fd(socket_fd, PSFALSE)))
  283. s48_out_of_memory_error_2(call);
  284. return s48_false_2(call);
  285. }
  286. void
  287. s48_init_os_sockets(void)
  288. {
  289. S48_EXPORT_FUNCTION(s48_socket);
  290. S48_EXPORT_FUNCTION(s48_socketpair);
  291. S48_EXPORT_FUNCTION(s48_dup_socket_channel);
  292. S48_EXPORT_FUNCTION(s48_accept);
  293. S48_EXPORT_FUNCTION(s48_connect);
  294. S48_EXPORT_FUNCTION(s48_recvfrom);
  295. S48_EXPORT_FUNCTION(s48_sendto);
  296. }