Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. /* For sockaddr_in */
  2. #include <netinet/in.h>
  3. /* For socket functions */
  4. #include <sys/socket.h>
  5. /* For fcntl */
  6. #include <fcntl.h>
  7.  
  8. #include <event2/event.h>
  9. #include <event2/buffer.h>
  10. #include <event2/bufferevent.h>
  11.  
  12. #include <assert.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18.  
  19. #define MAX_LINE 16384
  20.  
  21. void do_read(evutil_socket_t fd, short events, void *arg);
  22. void do_write(evutil_socket_t fd, short events, void *arg);
  23.  
  24. char
  25. rot13_char(char c)
  26. {
  27.     /* We don't want to use isalpha here; setting the locale would change
  28.      * which characters are considered alphabetical. */
  29.     if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
  30.         return c + 13;
  31.     else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
  32.         return c - 13;
  33.     else
  34.         return c;
  35. }
  36.  
  37. void
  38. readcb(struct bufferevent *bev, void *ctx)
  39. {
  40.     struct evbuffer *input, *output;
  41.     char *line;
  42.     size_t n;
  43.     int i;
  44.     input = bufferevent_get_input(bev);
  45.     output = bufferevent_get_output(bev);
  46.  
  47.     while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
  48.         for (i = 0; i < n; ++i)
  49.             line[i] = rot13_char(line[i]);
  50.         evbuffer_add(output, line, n);
  51.         evbuffer_add(output, "\n", 1);
  52.         free(line);
  53.     }
  54.  
  55.     if (evbuffer_get_length(input) >= MAX_LINE) {
  56.         /* Too long; just process what there is and go on so that the buffer
  57.          * doesn't grow infinitely long. */
  58.         char buf[1024];
  59.         while (evbuffer_get_length(input)) {
  60.             int n = evbuffer_remove(input, buf, sizeof(buf));
  61.             for (i = 0; i < n; ++i)
  62.                 buf[i] = rot13_char(buf[i]);
  63.             evbuffer_add(output, buf, n);
  64.         }
  65.         evbuffer_add(output, "\n", 1);
  66.     }
  67. }
  68.  
  69. void
  70. errorcb(struct bufferevent *bev, short error, void *ctx)
  71. {
  72.     if (error & BEV_EVENT_EOF) {
  73.         /* connection has been closed, do any clean up here */
  74.         /* ... */
  75.     } else if (error & BEV_EVENT_ERROR) {
  76.         /* check errno to see what error occurred */
  77.         /* ... */
  78.     } else if (error & BEV_EVENT_TIMEOUT) {
  79.         /* must be a timeout event handle, handle it */
  80.         /* ... */
  81.     }
  82.     bufferevent_free(bev);
  83. }
  84.  
  85. void
  86. do_accept(evutil_socket_t listener, short event, void *arg)
  87. {
  88.     struct event_base *base = arg;
  89.     struct sockaddr_storage ss;
  90.     socklen_t slen = sizeof(ss);
  91.     int fd = accept(listener, (struct sockaddr*)&ss, &slen);
  92.     if (fd < 0) {
  93.         perror("accept");
  94.     } else if (fd > FD_SETSIZE) {
  95.         close(fd);
  96.     } else {
  97.         struct bufferevent *bev;
  98.         evutil_make_socket_nonblocking(fd);
  99.         bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
  100.         bufferevent_setcb(bev, readcb, NULL, errorcb, NULL);
  101.         bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE);
  102.         bufferevent_enable(bev, EV_READ|EV_WRITE);
  103.     }
  104. }
  105.  
  106. void
  107. run(void)
  108. {
  109.     evutil_socket_t listener;
  110.     struct sockaddr_in sin;
  111.     struct event_base *base;
  112.     struct event *listener_event;
  113.  
  114.     base = event_base_new();
  115.     if (!base)
  116.         return; /*XXXerr*/
  117.  
  118.     sin.sin_family = AF_INET;
  119.     sin.sin_addr.s_addr = 0;
  120.     sin.sin_port = htons(40713);
  121.  
  122.     listener = socket(AF_INET, SOCK_STREAM, 0);
  123.     evutil_make_socket_nonblocking(listener);
  124.  
  125. #ifndef WIN32
  126.     {
  127.         int one = 1;
  128.         setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  129.     }
  130. #endif
  131.  
  132.     if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
  133.         perror("bind");
  134.         return;
  135.     }
  136.  
  137.     if (listen(listener, 16)<0) {
  138.         perror("listen");
  139.         return;
  140.     }
  141.  
  142.     listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
  143.     /*XXX check it */
  144.     event_add(listener_event, NULL);
  145.  
  146.     event_base_dispatch(base);
  147. }
  148.  
  149. int
  150. main(int c, char **v)
  151. {
  152.     setvbuf(stdout, NULL, _IONBF, 0);
  153.  
  154.     run();
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement