/nativelib/src/main/resources/scala-native/posix/sys/socket.c

https://github.com/scala-native/scala-native · C · 203 lines · 144 code · 59 blank · 0 comment · 16 complexity · c7e61dda0243b4dcd3651ed74090893c MD5 · raw file

  1. #include <netinet/in.h>
  2. #include "../netinet/in.h"
  3. #include <sys/socket.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include "socket_conversions.h"
  9. #include "socket.h"
  10. int scalanative_SCM_RIGHTS() { return SCM_RIGHTS; }
  11. int scalanative_SOCK_DGRAM() { return SOCK_DGRAM; }
  12. int scalanative_SOCK_RAW() { return SOCK_RAW; }
  13. int scalanative_SOCK_SEQPACKET() { return SOCK_SEQPACKET; }
  14. int scalanative_SOCK_STREAM() { return SOCK_STREAM; }
  15. int scalanative_SOL_SOCKET() { return SOL_SOCKET; }
  16. int scalanative_SO_ACCEPTCONN() { return SO_ACCEPTCONN; }
  17. int scalanative_SO_BROADCAST() { return SO_BROADCAST; }
  18. int scalanative_SO_DEBUG() { return SO_DEBUG; }
  19. int scalanative_SO_DONTROUTE() { return SO_DONTROUTE; }
  20. int scalanative_SO_ERROR() { return SO_ERROR; }
  21. int scalanative_SO_KEEPALIVE() { return SO_KEEPALIVE; }
  22. int scalanative_SO_LINGER() { return SO_LINGER; }
  23. int scalanative_SO_OOBINLINE() { return SO_OOBINLINE; }
  24. int scalanative_SO_RCVBUF() { return SO_RCVBUF; }
  25. int scalanative_SO_RCVLOWAT() { return SO_RCVLOWAT; }
  26. int scalanative_SO_RCVTIMEO() { return SO_RCVTIMEO; }
  27. int scalanative_SO_REUSEADDR() { return SO_REUSEADDR; }
  28. int scalanative_SO_SNDBUF() { return SO_SNDBUF; }
  29. int scalanative_SO_SNDLOWAT() { return SO_SNDLOWAT; }
  30. int scalanative_SO_SNDTIMEO() { return SO_SNDTIMEO; }
  31. int scalanative_SO_TYPE() { return SO_TYPE; }
  32. int scalanative_SOMAXCONN() { return SOMAXCONN; }
  33. int scalanative_MSG_CTRUNC() { return MSG_CTRUNC; }
  34. int scalanative_MSG_DONTROUTE() { return MSG_DONTROUTE; }
  35. int scalanative_MSG_EOR() { return MSG_EOR; }
  36. int scalanative_MSG_OOB() { return MSG_OOB; }
  37. int scalanative_MSG_NOSIGNAL() {
  38. #ifdef MSG_NOSIGNAL
  39. return MSG_NOSIGNAL;
  40. #endif
  41. #ifndef MSG_NOSIGNAL
  42. return 0;
  43. #endif
  44. }
  45. int scalanative_MSG_PEEK() { return MSG_PEEK; }
  46. int scalanative_MSG_TRUNC() { return MSG_TRUNC; }
  47. int scalanative_MSG_WAITALL() { return MSG_WAITALL; }
  48. int scalanative_AF_INET() { return AF_INET; }
  49. int scalanative_AF_INET6() { return AF_INET6; }
  50. int scalanative_AF_UNIX() { return AF_UNIX; }
  51. int scalanative_AF_UNSPEC() { return AF_UNSPEC; }
  52. int scalanative_getsockname(int socket, struct scalanative_sockaddr *address,
  53. socklen_t *address_len) {
  54. struct sockaddr *converted_address;
  55. int convert_result =
  56. scalanative_convert_sockaddr(address, &converted_address, address_len);
  57. int result;
  58. if (convert_result == 0) {
  59. result = getsockname(socket, converted_address, address_len);
  60. convert_result = scalanative_convert_scalanative_sockaddr(
  61. converted_address, address, address_len);
  62. if (convert_result != 0) {
  63. errno = convert_result;
  64. result = -1;
  65. }
  66. } else {
  67. errno = convert_result;
  68. result = -1;
  69. }
  70. free(converted_address);
  71. return result;
  72. }
  73. int scalanative_socket(int domain, int type, int protocol) {
  74. return socket(domain, type, protocol);
  75. }
  76. int scalanative_bind(int socket, struct scalanative_sockaddr *address,
  77. socklen_t address_len) {
  78. struct sockaddr *converted_address;
  79. int convert_result =
  80. scalanative_convert_sockaddr(address, &converted_address, &address_len);
  81. int result;
  82. if (convert_result == 0) {
  83. result = bind(socket, converted_address, address_len);
  84. } else {
  85. errno = convert_result;
  86. result = -1;
  87. }
  88. free(converted_address);
  89. return result;
  90. }
  91. int scalanative_connect(int socket, struct scalanative_sockaddr *address,
  92. socklen_t address_len) {
  93. struct sockaddr *converted_address;
  94. int convert_result =
  95. scalanative_convert_sockaddr(address, &converted_address, &address_len);
  96. int result;
  97. if (convert_result == 0) {
  98. result = connect(socket, converted_address, address_len);
  99. } else {
  100. errno = convert_result;
  101. result = -1;
  102. }
  103. free(converted_address);
  104. return result;
  105. }
  106. int scalanative_listen(int socket, int backlog) {
  107. return listen(socket, backlog);
  108. }
  109. int scalanative_accept(int socket, struct scalanative_sockaddr *address,
  110. socklen_t *address_len) {
  111. struct sockaddr *converted_address;
  112. int convert_result =
  113. scalanative_convert_sockaddr(address, &converted_address, address_len);
  114. int result;
  115. if (convert_result == 0) {
  116. result = accept(socket, converted_address, address_len);
  117. convert_result = scalanative_convert_scalanative_sockaddr(
  118. converted_address, address, address_len);
  119. if (convert_result != 0) {
  120. errno = convert_result;
  121. result = -1;
  122. }
  123. } else {
  124. errno = convert_result;
  125. result = -1;
  126. }
  127. free(converted_address);
  128. return result;
  129. }
  130. int scalanative_setsockopt(int socket, int level, int option_name,
  131. void *option_value, socklen_t option_len) {
  132. return setsockopt(socket, level, option_name, option_value, option_len);
  133. }
  134. int scalanative_getsockopt(int socket, int level, int option_name,
  135. void *option_value, socklen_t *option_len) {
  136. return getsockopt(socket, level, option_name, option_value, option_len);
  137. }
  138. int scalanative_recv(int socket, void *buffer, size_t length, int flags) {
  139. return recv(socket, buffer, length, flags);
  140. }
  141. int scalanative_send(int socket, void *buffer, size_t length, int flags) {
  142. return send(socket, buffer, length, flags);
  143. }
  144. int scalanative_shutdown(int socket, int how) { return shutdown(socket, how); }