xs_socket.h 4.8 KB

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