xs_socket.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  2. #ifndef _XS_SOCKET_H
  3. #define _XS_SOCKET_H
  4. int xs_socket_timeout(int s, double rto, double sto);
  5. int xs_socket_server(const char *addr, const char *serv);
  6. int xs_socket_accept(int rs);
  7. int _xs_socket_peername(int s, char *buf, int buf_size);
  8. int xs_socket_connect(const char *addr, const char *serv);
  9. #ifdef _XS_H
  10. xs_str *xs_socket_peername(int s);
  11. #endif
  12. #ifdef XS_IMPLEMENTATION
  13. #include <sys/socket.h>
  14. #include <netdb.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. int xs_socket_timeout(int s, double rto, double sto)
  21. /* sets the socket timeout in seconds */
  22. {
  23. struct timeval tv;
  24. int ret = 0;
  25. if (rto > 0.0) {
  26. tv.tv_sec = (int)rto;
  27. tv.tv_usec = (int)((rto - (double)(int)rto) * 1000000.0);
  28. ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  29. }
  30. if (sto > 0.0) {
  31. tv.tv_sec = (int)sto;
  32. tv.tv_usec = (int)((sto - (double)(int)sto) * 1000000.0);
  33. ret = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
  34. }
  35. return ret;
  36. }
  37. int xs_socket_server(const char *addr, const char *serv)
  38. /* opens a server socket by service name (or port as string) */
  39. {
  40. int rs = -1;
  41. #ifndef WITHOUT_GETADDRINFO
  42. struct addrinfo *res;
  43. struct addrinfo hints;
  44. int status;
  45. memset(&hints, '\0', sizeof(hints));
  46. hints.ai_family = AF_UNSPEC;
  47. hints.ai_socktype = SOCK_STREAM;
  48. if (getaddrinfo(addr, serv, &hints, &res) != 0) {
  49. goto end;
  50. }
  51. rs = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  52. /* reuse addr */
  53. int i = 1;
  54. setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
  55. status = bind(rs, res->ai_addr, res->ai_addrlen);
  56. if (status == -1) {
  57. fprintf(stderr, "Error binding with status %d\n", status);
  58. close(rs);
  59. rs = -1;
  60. }
  61. else {
  62. listen(rs, SOMAXCONN);
  63. }
  64. freeaddrinfo(res);
  65. #else /* WITHOUT_GETADDRINFO */
  66. struct sockaddr_in host;
  67. memset(&host, '\0', sizeof(host));
  68. if (addr != NULL) {
  69. struct hostent *he;
  70. if ((he = gethostbyname(addr)) != NULL)
  71. memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
  72. else
  73. goto end;
  74. }
  75. struct servent *se;
  76. if ((se = getservbyname(serv, "tcp")) != NULL)
  77. host.sin_port = se->s_port;
  78. else
  79. host.sin_port = htons(atoi(serv));
  80. host.sin_family = AF_INET;
  81. if ((rs = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
  82. /* reuse addr */
  83. int i = 1;
  84. setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
  85. if (bind(rs, (struct sockaddr *)&host, sizeof(host)) == -1) {
  86. close(rs);
  87. rs = -1;
  88. }
  89. else
  90. listen(rs, SOMAXCONN);
  91. }
  92. #endif /* WITHOUT_GETADDRINFO */
  93. end:
  94. return rs;
  95. }
  96. int xs_socket_accept(int rs)
  97. /* accepts an incoming connection */
  98. {
  99. struct sockaddr_storage addr;
  100. socklen_t l = sizeof(addr);
  101. return accept(rs, (struct sockaddr *)&addr, &l);
  102. }
  103. int _xs_socket_peername(int s, char *buf, int buf_size)
  104. /* fill the buffer with the socket peername */
  105. {
  106. struct sockaddr_storage addr;
  107. socklen_t slen = sizeof(addr);
  108. const char *p = NULL;
  109. if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) {
  110. if (addr.ss_family == AF_INET) {
  111. struct sockaddr_in *sa = (struct sockaddr_in *)&addr;
  112. p = inet_ntop(AF_INET, &sa->sin_addr, buf, buf_size);
  113. }
  114. else
  115. if (addr.ss_family == AF_INET6) {
  116. struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr;
  117. p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, buf_size);
  118. }
  119. }
  120. return p != NULL;
  121. }
  122. int xs_socket_connect(const char *addr, const char *serv)
  123. /* creates a client connection socket */
  124. {
  125. int d = -1;
  126. #ifndef WITHOUT_GETADDRINFO
  127. struct addrinfo *res;
  128. struct addrinfo hints;
  129. memset(&hints, '\0', sizeof(hints));
  130. hints.ai_socktype = SOCK_STREAM;
  131. hints.ai_flags = AI_ADDRCONFIG;
  132. if (getaddrinfo(addr, serv, &hints, &res) == 0) {
  133. struct addrinfo *r;
  134. for (r = res; r != NULL; r = r->ai_next) {
  135. d = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
  136. if (d != -1) {
  137. if (connect(d, r->ai_addr, r->ai_addrlen) == 0)
  138. break;
  139. close(d);
  140. d = -1;
  141. }
  142. }
  143. freeaddrinfo(res);
  144. }
  145. #else /* WITHOUT_GETADDRINFO */
  146. /* traditional socket interface */
  147. struct hostent *he;
  148. if ((he = gethostbyname(addr)) != NULL) {
  149. struct sockaddr_in host;
  150. memset(&host, '\0', sizeof(host));
  151. memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
  152. host.sin_family = he->h_addrtype;
  153. struct servent *se;
  154. if ((se = getservbyname(serv, "tcp")) != NULL)
  155. host.sin_port = se->s_port;
  156. else
  157. host.sin_port = htons(atoi(serv));
  158. if ((d = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
  159. if (connect(d, (struct sockaddr *)&host, sizeof(host)) == -1) {
  160. close(d);
  161. d = -1;
  162. }
  163. }
  164. }
  165. #endif /* WITHOUT_GETADDRINFO */
  166. return d;
  167. }
  168. #ifdef _XS_H
  169. xs_str *xs_socket_peername(int s)
  170. /* returns the remote address as a string */
  171. {
  172. char buf[2028];
  173. xs_str *p = NULL;
  174. if (_xs_socket_peername(s, buf, sizeof(buf)))
  175. p = xs_str_new(buf);
  176. return p;
  177. }
  178. #endif /* _XS_H */
  179. #endif /* XS_IMPLEMENTATION */
  180. #endif /* _XS_SOCKET_H */