_socket.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "_socket.h"
  2. #include "_socket_socket.h"
  3. #ifdef __linux__
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #else
  7. /* you need to implement this for your platform */
  8. #include "__platform_socket.h"
  9. #endif
  10. #if !(PIKASCRIPT_VERSION_NUM >= PIKASCRIPT_VERSION_TO_NUM(1, 10, 4))
  11. #error "require pikascript kernal version >= v1.10.4"
  12. #endif
  13. PIKA_WEAK int __platform_socket(int __domain, int __type, int __protocol) {
  14. #ifdef __linux__
  15. return socket(__domain, __type, __protocol);
  16. #else
  17. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  18. #endif
  19. }
  20. PIKA_WEAK int __platform_bind(int __fd,
  21. const struct sockaddr* __addr,
  22. socklen_t __addr_len) {
  23. #ifdef __linux__
  24. return bind(__fd, __addr, __addr_len);
  25. #else
  26. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  27. #endif
  28. }
  29. PIKA_WEAK int __platform_listen(int __fd, int __n) {
  30. #ifdef __linux__
  31. return listen(__fd, __n);
  32. #else
  33. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  34. #endif
  35. }
  36. PIKA_WEAK int __platform_accept(int __fd,
  37. struct sockaddr* __addr,
  38. socklen_t* __addr_len) {
  39. #ifdef __linux__
  40. return accept(__fd, __addr, __addr_len);
  41. #else
  42. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  43. #endif
  44. }
  45. PIKA_WEAK int __platform_connect(int __fd,
  46. const struct sockaddr* __addr,
  47. socklen_t __addr_len) {
  48. #ifdef __linux__
  49. return connect(__fd, __addr, __addr_len);
  50. #else
  51. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  52. #endif
  53. }
  54. PIKA_WEAK int __platform_send(int __fd,
  55. const void* __buf,
  56. size_t __n,
  57. int __flags) {
  58. #ifdef __linux__
  59. return send(__fd, __buf, __n, __flags);
  60. #else
  61. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  62. #endif
  63. }
  64. PIKA_WEAK int __platform_recv(int __fd, void* __buf, size_t __n, int __flags) {
  65. #ifdef __linux__
  66. return recv(__fd, __buf, __n, __flags);
  67. #else
  68. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  69. #endif
  70. }
  71. PIKA_WEAK int __platform_close(int __fd) {
  72. #ifdef __linux__
  73. return close(__fd);
  74. #else
  75. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  76. #endif
  77. }
  78. /* gethostname */
  79. PIKA_WEAK int __platform_gethostname(char* __name, size_t __len) {
  80. #ifdef __linux__
  81. return gethostname(__name, __len);
  82. #else
  83. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  84. #endif
  85. }
  86. void _socket_socket__init(PikaObj* self) {
  87. int family = obj_getInt(self, "family");
  88. int type = obj_getInt(self, "type");
  89. int protocol = obj_getInt(self, "protocol");
  90. int sockfd = 0;
  91. sockfd = __platform_socket(family, type, protocol);
  92. if (sockfd < 0) {
  93. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  94. __platform_printf("socket error\n");
  95. return;
  96. }
  97. obj_setInt(self, "sockfd", sockfd);
  98. }
  99. void _socket_socket__close(PikaObj* self) {
  100. int sockfd = obj_getInt(self, "sockfd");
  101. __platform_close(sockfd);
  102. }
  103. void _socket_socket__send(PikaObj* self, Arg* data) {
  104. uint8_t* data_send = NULL;
  105. int len = 0;
  106. if (arg_getType(data) == ARG_TYPE_STRING) {
  107. data_send = (uint8_t*)arg_getStr(data);
  108. len = strGetSize((char*)data_send);
  109. }
  110. if (arg_getType(data) == ARG_TYPE_BYTES) {
  111. data_send = (uint8_t*)arg_getBytes(data);
  112. len = arg_getBytesSize(data);
  113. }
  114. int sockfd = obj_getInt(self, "sockfd");
  115. int ret = 0;
  116. ret = __platform_send(sockfd, data_send, len, 0);
  117. if (ret < 0) {
  118. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  119. __platform_printf("send error\n");
  120. return;
  121. }
  122. }
  123. void _socket_socket__accept(PikaObj* self) {
  124. int sockfd = obj_getInt(self, "sockfd");
  125. int client_sockfd = 0;
  126. struct sockaddr_in client_addr;
  127. socklen_t client_addr_len = sizeof(client_addr);
  128. client_sockfd = __platform_accept(sockfd, (struct sockaddr*)&client_addr,
  129. &client_addr_len);
  130. if (client_sockfd < 0) {
  131. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  132. __platform_printf("accept error\n");
  133. return;
  134. }
  135. obj_setInt(self, "client_sockfd", client_sockfd);
  136. obj_setStr(self, "client_addr", inet_ntoa(client_addr.sin_addr));
  137. }
  138. Arg* _socket_socket__recv(PikaObj* self, int num) {
  139. int sockfd = obj_getInt(self, "sockfd");
  140. int ret = 0;
  141. uint8_t* data_recv = NULL;
  142. Arg* res = arg_newBytes(NULL, num);
  143. data_recv = arg_getBytes(res);
  144. ret = __platform_recv(sockfd, data_recv, num, 0);
  145. if (ret < 0) {
  146. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  147. __platform_printf("recv error\n");
  148. return NULL;
  149. }
  150. return res;
  151. }
  152. void _socket_socket__listen(PikaObj* self, int num) {
  153. int sockfd = obj_getInt(self, "sockfd");
  154. __platform_listen(sockfd, num);
  155. }
  156. void _socket_socket__connect(PikaObj* self, char* host, int port) {
  157. int sockfd = obj_getInt(self, "sockfd");
  158. struct sockaddr_in server_addr;
  159. server_addr.sin_family = AF_INET;
  160. server_addr.sin_port = htons(port);
  161. server_addr.sin_addr.s_addr = inet_addr(host);
  162. __platform_connect(sockfd, (struct sockaddr*)&server_addr,
  163. sizeof(server_addr));
  164. }
  165. void _socket_socket__bind(PikaObj* self, char* host, int port) {
  166. int sockfd = obj_getInt(self, "sockfd");
  167. struct sockaddr_in server_addr;
  168. server_addr.sin_family = AF_INET;
  169. server_addr.sin_port = htons(port);
  170. server_addr.sin_addr.s_addr = inet_addr(host);
  171. int res = __platform_bind(sockfd, (struct sockaddr*)&server_addr,
  172. sizeof(server_addr));
  173. if (res < 0) {
  174. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  175. __platform_printf("bind error\n");
  176. return;
  177. }
  178. }
  179. char* _socket__gethostname(PikaObj* self) {
  180. char hostname_buff[128] = {0};
  181. char* hostname = (char*)hostname_buff;
  182. __platform_gethostname(hostname_buff, 128);
  183. return obj_cacheStr(self, hostname);
  184. }