xs_socket.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* copyright (c) 2022 - 2024 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. struct sockaddr_in host;
  42. memset(&host, '\0', sizeof(host));
  43. if (addr != NULL) {
  44. struct hostent *he;
  45. if ((he = gethostbyname(addr)) != NULL)
  46. memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
  47. else
  48. goto end;
  49. }
  50. struct servent *se;
  51. if ((se = getservbyname(serv, "tcp")) != NULL)
  52. host.sin_port = se->s_port;
  53. else
  54. host.sin_port = htons(atoi(serv));
  55. host.sin_family = AF_INET;
  56. if ((rs = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
  57. /* reuse addr */
  58. int i = 1;
  59. setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
  60. if (bind(rs, (struct sockaddr *)&host, sizeof(host)) == -1) {
  61. close(rs);
  62. rs = -1;
  63. }
  64. else
  65. listen(rs, SOMAXCONN);
  66. }
  67. end:
  68. return rs;
  69. }
  70. int xs_socket_accept(int rs)
  71. /* accepts an incoming connection */
  72. {
  73. struct sockaddr_storage addr;
  74. socklen_t l = sizeof(addr);
  75. return accept(rs, (struct sockaddr *)&addr, &l);
  76. }
  77. int _xs_socket_peername(int s, char *buf, int buf_size)
  78. /* fill the buffer with the socket peername */
  79. {
  80. struct sockaddr_storage addr;
  81. socklen_t slen = sizeof(addr);
  82. const char *p = NULL;
  83. if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) {
  84. if (addr.ss_family == AF_INET) {
  85. struct sockaddr_in *sa = (struct sockaddr_in *)&addr;
  86. p = inet_ntop(AF_INET, &sa->sin_addr, buf, buf_size);
  87. }
  88. else
  89. if (addr.ss_family == AF_INET6) {
  90. struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr;
  91. p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, buf_size);
  92. }
  93. }
  94. return p != NULL;
  95. }
  96. int xs_socket_connect(const char *addr, const char *serv)
  97. /* creates a client connection socket */
  98. {
  99. int d = -1;
  100. #ifndef WITHOUT_GETADDRINFO
  101. struct addrinfo *res;
  102. struct addrinfo hints;
  103. memset(&hints, '\0', sizeof(hints));
  104. hints.ai_socktype = SOCK_STREAM;
  105. hints.ai_flags = AI_ADDRCONFIG;
  106. if (getaddrinfo(addr, serv, &hints, &res) == 0) {
  107. struct addrinfo *r;
  108. for (r = res; r != NULL; r = r->ai_next) {
  109. d = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
  110. if (d != -1) {
  111. if (connect(d, r->ai_addr, r->ai_addrlen) == 0)
  112. break;
  113. close(d);
  114. d = -1;
  115. }
  116. }
  117. freeaddrinfo(res);
  118. }
  119. #else /* WITHOUT_GETADDRINFO */
  120. /* traditional socket interface */
  121. struct hostent *he;
  122. if ((he = gethostbyname(addr)) != NULL) {
  123. struct sockaddr_in host;
  124. memset(&host, '\0', sizeof(host));
  125. memcpy(&host.sin_addr, he->h_addr_list[0], he->h_length);
  126. host.sin_family = he->h_addrtype;
  127. struct servent *se;
  128. if ((se = getservbyname(serv, "tcp")) != NULL)
  129. host.sin_port = se->s_port;
  130. else
  131. host.sin_port = htons(atoi(serv));
  132. if ((d = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
  133. if (connect(d, (struct sockaddr *)&host, sizeof(host)) == -1) {
  134. close(d);
  135. d = -1;
  136. }
  137. }
  138. }
  139. #endif /* WITHOUT_GETADDRINFO */
  140. return d;
  141. }
  142. #ifdef _XS_H
  143. xs_str *xs_socket_peername(int s)
  144. /* returns the remote address as a string */
  145. {
  146. char buf[2028];
  147. xs_str *p = NULL;
  148. if (_xs_socket_peername(s, buf, sizeof(buf)))
  149. p = xs_str_new(buf);
  150. return p;
  151. }
  152. #endif /* _XS_H */
  153. #endif /* XS_IMPLEMENTATION */
  154. #endif /* _XS_SOCKET_H */