epoll_socket.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copied from
  2. // https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/epoll-example.c
  3. // Meant to be used for edge triggered epoll
  4. #define _GNU_SOURCE
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <netdb.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/epoll.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #define MAXEVENTS 64
  16. static int make_socket_non_blocking(int sfd) {
  17. int flags, s;
  18. flags = fcntl(sfd, F_GETFL, 0);
  19. if (flags == -1) {
  20. perror("fcntl");
  21. return -1;
  22. }
  23. flags |= O_NONBLOCK;
  24. s = fcntl(sfd, F_SETFL, flags);
  25. if (s == -1) {
  26. perror("fcntl");
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. static int create_and_bind(char* port) {
  32. struct addrinfo hints;
  33. struct addrinfo *result, *rp;
  34. int s, sfd;
  35. memset(&hints, 0, sizeof(struct addrinfo));
  36. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  37. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  38. hints.ai_flags = AI_PASSIVE; /* All interfaces */
  39. s = getaddrinfo(NULL, port, &hints, &result);
  40. if (s != 0) {
  41. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  42. return -1;
  43. }
  44. for (rp = result; rp != NULL; rp = rp->ai_next) {
  45. sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  46. if (sfd == -1)
  47. continue;
  48. s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
  49. if (s == 0)
  50. /* We managed to bind successfully! */
  51. break;
  52. close(sfd);
  53. }
  54. if (rp == NULL) {
  55. fprintf(stderr, "Could not bind\n");
  56. return -1;
  57. }
  58. freeaddrinfo(result);
  59. return sfd;
  60. }
  61. int main(int argc, char* argv[]) {
  62. int sfd, s;
  63. int efd;
  64. struct epoll_event event;
  65. struct epoll_event* events;
  66. if (argc != 2) {
  67. fprintf(stderr, "Usage: %s [port]\n", argv[0]);
  68. exit(EXIT_FAILURE);
  69. }
  70. sfd = create_and_bind(argv[1]);
  71. if (sfd == -1)
  72. abort();
  73. s = make_socket_non_blocking(sfd);
  74. if (s == -1)
  75. abort();
  76. s = listen(sfd, SOMAXCONN);
  77. if (s == -1) {
  78. perror("listen");
  79. abort();
  80. }
  81. efd = epoll_create1(0);
  82. if (efd == -1) {
  83. perror("epoll_create");
  84. abort();
  85. }
  86. event.data.fd = sfd;
  87. event.events = EPOLLIN | EPOLLET;
  88. s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
  89. if (s == -1) {
  90. perror("epoll_ctl");
  91. abort();
  92. }
  93. /* Buffer where events are returned */
  94. events = calloc(MAXEVENTS, sizeof event);
  95. /* The event loop */
  96. while (1) {
  97. int n, i;
  98. n = epoll_wait(efd, events, MAXEVENTS, -1);
  99. for (i = 0; i < n; i++) {
  100. if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) ||
  101. (!(events[i].events & EPOLLIN))) {
  102. /* An error has occured on this fd, or the socket is not
  103. ready for reading (why were we notified then?) */
  104. fprintf(stderr, "epoll error\n");
  105. close(events[i].data.fd);
  106. continue;
  107. }
  108. else if (sfd == events[i].data.fd) {
  109. /* We have a notification on the listening socket, which
  110. means one or more incoming connections. */
  111. while (1) {
  112. struct sockaddr in_addr;
  113. socklen_t in_len;
  114. int infd;
  115. char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  116. in_len = sizeof in_addr;
  117. infd = accept(sfd, &in_addr, &in_len);
  118. if (infd == -1) {
  119. if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
  120. /* We have processed all incoming
  121. connections. */
  122. break;
  123. } else {
  124. perror("accept");
  125. break;
  126. }
  127. }
  128. s = getnameinfo(&in_addr, in_len, hbuf, sizeof hbuf, sbuf, sizeof sbuf,
  129. NI_NUMERICHOST | NI_NUMERICSERV);
  130. if (s == 0)
  131. printf(
  132. "Accepted connection on descriptor %d (host=%s, port=%s)\n",
  133. infd, hbuf, sbuf);
  134. /* Make the incoming socket non-blocking and add it to the
  135. list of fds to monitor. */
  136. s = make_socket_non_blocking(infd);
  137. if (s == -1)
  138. abort();
  139. event.data.fd = infd;
  140. event.events = EPOLLIN | EPOLLET;
  141. s = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
  142. if (s == -1) {
  143. perror("epoll_ctl");
  144. abort();
  145. }
  146. }
  147. continue;
  148. } else {
  149. /* We have data on the fd waiting to be read. Read and
  150. display it. We must read whatever data is available
  151. completely, as we are running in edge-triggered mode
  152. and won't get a notification again for the same
  153. data. */
  154. int done = 0;
  155. while (1) {
  156. ssize_t count;
  157. char buf[512];
  158. count = read(events[i].data.fd, buf, sizeof buf);
  159. if (count == -1) {
  160. /* If errno == EAGAIN, that means we have read all
  161. data. So go back to the main loop. */
  162. if (errno != EAGAIN) {
  163. perror("read");
  164. done = 1;
  165. }
  166. break;
  167. } else if (count == 0) {
  168. /* End of file. The remote has closed the
  169. connection. */
  170. done = 1;
  171. break;
  172. }
  173. /* Write the buffer to standard output */
  174. s = write(1, buf, count);
  175. if (s == -1) {
  176. perror("write");
  177. abort();
  178. }
  179. }
  180. if (done) {
  181. printf("Closed connection on descriptor %d\n", events[i].data.fd);
  182. /* Closing the descriptor will make epoll remove it
  183. from the set of descriptors which are monitored. */
  184. close(events[i].data.fd);
  185. }
  186. }
  187. }
  188. }
  189. free(events);
  190. close(sfd);
  191. return EXIT_SUCCESS;
  192. }