/otherlibs/win32unix/sendrecv.c

https://github.com/colinbenner/ocaml-llvm · C · 141 lines · 116 code · 12 blank · 13 comment · 20 complexity · 44f6579c202ce714744eeff209416b7d MD5 · raw file

  1. /***********************************************************************/
  2. /* */
  3. /* Objective Caml */
  4. /* */
  5. /* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
  6. /* */
  7. /* Copyright 1996 Institut National de Recherche en Informatique et */
  8. /* en Automatique. All rights reserved. This file is distributed */
  9. /* under the terms of the GNU Library General Public License, with */
  10. /* the special exception on linking described in file ../../LICENSE. */
  11. /* */
  12. /***********************************************************************/
  13. /* $Id$ */
  14. #include <mlvalues.h>
  15. #include <alloc.h>
  16. #include <memory.h>
  17. #include <signals.h>
  18. #include "unixsupport.h"
  19. #include "socketaddr.h"
  20. static int msg_flag_table[] = {
  21. MSG_OOB, MSG_DONTROUTE, MSG_PEEK
  22. };
  23. CAMLprim value unix_recv(value sock, value buff, value ofs, value len, value flags)
  24. {
  25. SOCKET s = Socket_val(sock);
  26. int flg = convert_flag_list(flags, msg_flag_table);
  27. int ret;
  28. intnat numbytes;
  29. char iobuf[UNIX_BUFFER_SIZE];
  30. DWORD err = 0;
  31. Begin_root (buff);
  32. numbytes = Long_val(len);
  33. if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
  34. enter_blocking_section();
  35. ret = recv(s, iobuf, (int) numbytes, flg);
  36. if (ret == -1) err = WSAGetLastError();
  37. leave_blocking_section();
  38. if (ret == -1) {
  39. win32_maperr(err);
  40. uerror("recv", Nothing);
  41. }
  42. memmove (&Byte(buff, Long_val(ofs)), iobuf, ret);
  43. End_roots();
  44. return Val_int(ret);
  45. }
  46. CAMLprim value unix_recvfrom(value sock, value buff, value ofs, value len, value flags)
  47. {
  48. SOCKET s = Socket_val(sock);
  49. int flg = convert_flag_list(flags, msg_flag_table);
  50. int ret;
  51. intnat numbytes;
  52. char iobuf[UNIX_BUFFER_SIZE];
  53. value res;
  54. value adr = Val_unit;
  55. union sock_addr_union addr;
  56. socklen_param_type addr_len;
  57. DWORD err = 0;
  58. Begin_roots2 (buff, adr);
  59. numbytes = Long_val(len);
  60. if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
  61. addr_len = sizeof(sock_addr);
  62. enter_blocking_section();
  63. ret = recvfrom(s, iobuf, (int) numbytes, flg, &addr.s_gen, &addr_len);
  64. if (ret == -1) err = WSAGetLastError();
  65. leave_blocking_section();
  66. if (ret == -1) {
  67. win32_maperr(err);
  68. uerror("recvfrom", Nothing);
  69. }
  70. memmove (&Byte(buff, Long_val(ofs)), iobuf, ret);
  71. adr = alloc_sockaddr(&addr, addr_len, -1);
  72. res = alloc_small(2, 0);
  73. Field(res, 0) = Val_int(ret);
  74. Field(res, 1) = adr;
  75. End_roots();
  76. return res;
  77. }
  78. CAMLprim value unix_send(value sock, value buff, value ofs, value len, value flags)
  79. {
  80. SOCKET s = Socket_val(sock);
  81. int flg = convert_flag_list(flags, msg_flag_table);
  82. int ret;
  83. intnat numbytes;
  84. char iobuf[UNIX_BUFFER_SIZE];
  85. DWORD err = 0;
  86. numbytes = Long_val(len);
  87. if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
  88. memmove (iobuf, &Byte(buff, Long_val(ofs)), numbytes);
  89. enter_blocking_section();
  90. ret = send(s, iobuf, (int) numbytes, flg);
  91. if (ret == -1) err = WSAGetLastError();
  92. leave_blocking_section();
  93. if (ret == -1) {
  94. win32_maperr(err);
  95. uerror("send", Nothing);
  96. }
  97. return Val_int(ret);
  98. }
  99. value unix_sendto_native(value sock, value buff, value ofs, value len, value flags, value dest)
  100. {
  101. SOCKET s = Socket_val(sock);
  102. int flg = convert_flag_list(flags, msg_flag_table);
  103. int ret;
  104. intnat numbytes;
  105. char iobuf[UNIX_BUFFER_SIZE];
  106. union sock_addr_union addr;
  107. socklen_param_type addr_len;
  108. DWORD err = 0;
  109. get_sockaddr(dest, &addr, &addr_len);
  110. numbytes = Long_val(len);
  111. if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
  112. memmove (iobuf, &Byte(buff, Long_val(ofs)), numbytes);
  113. enter_blocking_section();
  114. ret = sendto(s, iobuf, (int) numbytes, flg, &addr.s_gen, addr_len);
  115. if (ret == -1) err = WSAGetLastError();
  116. leave_blocking_section();
  117. if (ret == -1) {
  118. win32_maperr(err);
  119. uerror("sendto", Nothing);
  120. }
  121. return Val_int(ret);
  122. }
  123. CAMLprim value unix_sendto(argv, argc)
  124. value * argv;
  125. int argc;
  126. {
  127. return unix_sendto_native
  128. (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
  129. }