testpolling.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2018-2023 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "tap.h"
  27. ks_socket_t start_listen(ks_sockaddr_t *addr)
  28. {
  29. ks_socket_t listener = KS_SOCK_INVALID;
  30. ks_status_t ret = KS_STATUS_SUCCESS;
  31. ks_assert(addr);
  32. if ((listener = socket(addr->family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) {
  33. ks_log(KS_LOG_DEBUG, "listener == KS_SOCK_INVALID\n");
  34. ret = KS_STATUS_FAIL;
  35. goto done;
  36. }
  37. ks_socket_option(listener, SO_REUSEADDR, KS_TRUE);
  38. ks_socket_option(listener, TCP_NODELAY, KS_TRUE);
  39. if (addr->family == AF_INET6) ks_socket_option(listener, IPV6_V6ONLY, KS_TRUE);
  40. if (ks_addr_bind(listener, addr) != KS_STATUS_SUCCESS) {
  41. ks_log(KS_LOG_DEBUG, "ks_addr_bind(listener, addr) != KS_STATUS_SUCCESS\n");
  42. ret = KS_STATUS_FAIL;
  43. goto done;
  44. }
  45. if (listen(listener, 4) != 0) {
  46. ks_log(KS_LOG_DEBUG, "listen(listener, backlog) != 0\n");
  47. ret = KS_STATUS_FAIL;
  48. goto done;
  49. }
  50. done:
  51. if (ret != KS_STATUS_SUCCESS) {
  52. if (listener != KS_SOCK_INVALID) {
  53. ks_socket_shutdown(listener, SHUT_RDWR);
  54. ks_socket_close(&listener);
  55. listener = KS_SOCK_INVALID;
  56. }
  57. }
  58. return listener;
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. ks_pool_t *pool = NULL;
  63. struct pollfd *listeners_poll = NULL;
  64. int32_t listeners_count = 0;
  65. int32_t listener_index = -1;
  66. ks_sockaddr_t addr;
  67. ks_socket_t listener = KS_SOCK_INVALID;
  68. ks_socket_t sock = KS_SOCK_INVALID;
  69. ks_init();
  70. plan(2);
  71. ks_pool_open(&pool);
  72. ks_addr_set(&addr, "0.0.0.0", 1234, AF_INET);
  73. listener = start_listen(&addr);
  74. listener_index = listeners_count++;
  75. listeners_poll = (struct pollfd *)ks_pool_alloc(pool, sizeof(struct pollfd) * listeners_count);
  76. ok(listeners_poll != NULL);
  77. listeners_poll[listener_index].fd = listener;
  78. listeners_poll[listener_index].events = POLLIN;
  79. while (1) {
  80. int p = ks_poll(listeners_poll, listeners_count, 100);
  81. if (p > 0) {
  82. printf("POLL event occurred\n");
  83. for (int32_t index = 0; index < listeners_count; ++index) {
  84. if (listeners_poll[index].revents & POLLERR) {
  85. printf("POLLERR on index %d\n", index);
  86. break;
  87. }
  88. if (!(listeners_poll[index].revents & POLLIN)) continue;
  89. printf("POLLIN on index %d\n", index);
  90. if ((sock = accept(listeners_poll[index].fd, NULL, NULL)) == KS_SOCK_INVALID) {
  91. printf("Accept failed on index %d\n", index);
  92. continue;
  93. }
  94. printf("Accept success on index %d\n", index);
  95. }
  96. break;
  97. } else if (p < 0) {
  98. printf("Polling socket error %d\n", WSAGetLastError());
  99. }
  100. }
  101. ok(sock != KS_SOCK_INVALID);
  102. if (sock != KS_SOCK_INVALID) ks_socket_close(&sock);
  103. for (int index = 0; index < listeners_count; ++index) {
  104. listener = listeners_poll[index].fd;
  105. ks_socket_close(&listener);
  106. }
  107. ks_pool_free(&listeners_poll);
  108. ks_pool_close(&pool);
  109. ks_shutdown();
  110. done_testing();
  111. }