12b15cb3dSCy Schubert /* 22b15cb3dSCy Schubert * Copyright (c) 2012 Ross Lagerwall <rosslagerwall@gmail.com> 32b15cb3dSCy Schubert * 42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without 52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions 62b15cb3dSCy Schubert * are met: 72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright 82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer. 92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the 112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution. 122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products 132b15cb3dSCy Schubert * derived from this software without specific prior written permission. 142b15cb3dSCy Schubert * 152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 252b15cb3dSCy Schubert */ 262b15cb3dSCy Schubert 272b15cb3dSCy Schubert #include "event2/event-config.h" 282b15cb3dSCy Schubert 292b15cb3dSCy Schubert #ifdef _WIN32 302b15cb3dSCy Schubert #define WIN32_LEAN_AND_MEAN 312b15cb3dSCy Schubert #include <windows.h> 322b15cb3dSCy Schubert #endif 332b15cb3dSCy Schubert #include <string.h> 342b15cb3dSCy Schubert #include <stdlib.h> 352b15cb3dSCy Schubert #include <errno.h> 362b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H 372b15cb3dSCy Schubert #include <sys/time.h> 382b15cb3dSCy Schubert #endif 392b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_RESOURCE_H 402b15cb3dSCy Schubert #include <sys/resource.h> 412b15cb3dSCy Schubert #endif 422b15cb3dSCy Schubert #ifdef EVENT__HAVE_NETINET_IN_H 432b15cb3dSCy Schubert #include <netinet/in.h> 442b15cb3dSCy Schubert #endif 452b15cb3dSCy Schubert 462b15cb3dSCy Schubert #include "event2/event.h" 472b15cb3dSCy Schubert #include "event2/bufferevent.h" 482b15cb3dSCy Schubert #include "event2/buffer.h" 492b15cb3dSCy Schubert #include "event2/listener.h" 502b15cb3dSCy Schubert 512b15cb3dSCy Schubert /* Number of requests to make. Setting this too high might result in the machine 522b15cb3dSCy Schubert running out of ephemeral ports */ 532b15cb3dSCy Schubert #ifdef _WIN32 542b15cb3dSCy Schubert #define MAX_REQUESTS 1000 552b15cb3dSCy Schubert #else 562b15cb3dSCy Schubert #define MAX_REQUESTS 4000 572b15cb3dSCy Schubert #endif 582b15cb3dSCy Schubert 592b15cb3dSCy Schubert /* Provide storage for the address, both for the server & the clients */ 60*a25439b6SCy Schubert static struct sockaddr_in saddr; 612b15cb3dSCy Schubert 622b15cb3dSCy Schubert /* Number of sucessful requests so far */ 632b15cb3dSCy Schubert static int num_requests; 642b15cb3dSCy Schubert 652b15cb3dSCy Schubert static void start_client(struct event_base *base); 662b15cb3dSCy Schubert 672b15cb3dSCy Schubert static void 682b15cb3dSCy Schubert my_perror(const char *s) 692b15cb3dSCy Schubert { 702b15cb3dSCy Schubert fprintf(stderr, "%s: %s", 712b15cb3dSCy Schubert s, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())); 722b15cb3dSCy Schubert } 732b15cb3dSCy Schubert 742b15cb3dSCy Schubert /* 752b15cb3dSCy Schubert =============================================== 762b15cb3dSCy Schubert Server functions 772b15cb3dSCy Schubert =============================================== 782b15cb3dSCy Schubert */ 792b15cb3dSCy Schubert 802b15cb3dSCy Schubert /* Read a byte from the client and write it back */ 812b15cb3dSCy Schubert static void 822b15cb3dSCy Schubert server_read_cb(struct bufferevent *bev, void *ctx) 832b15cb3dSCy Schubert { 842b15cb3dSCy Schubert while (evbuffer_get_length(bufferevent_get_input(bev))) { 852b15cb3dSCy Schubert unsigned char tmp; 862b15cb3dSCy Schubert bufferevent_read(bev, &tmp, 1); 872b15cb3dSCy Schubert bufferevent_write(bev, &tmp, 1); 882b15cb3dSCy Schubert } 892b15cb3dSCy Schubert } 902b15cb3dSCy Schubert 912b15cb3dSCy Schubert /* Wait for an EOF and then free the bufferevent */ 922b15cb3dSCy Schubert static void 932b15cb3dSCy Schubert server_event_cb(struct bufferevent *bev, short events, void *ctx) 942b15cb3dSCy Schubert { 952b15cb3dSCy Schubert if (events & BEV_EVENT_ERROR) { 962b15cb3dSCy Schubert my_perror("Error from bufferevent"); 972b15cb3dSCy Schubert exit(1); 982b15cb3dSCy Schubert } else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { 992b15cb3dSCy Schubert bufferevent_free(bev); 1002b15cb3dSCy Schubert } 1012b15cb3dSCy Schubert } 1022b15cb3dSCy Schubert 1032b15cb3dSCy Schubert /* Accept a client socket and set it up to for reading & writing */ 1042b15cb3dSCy Schubert static void 1052b15cb3dSCy Schubert listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock, 1062b15cb3dSCy Schubert struct sockaddr *addr, int len, void *ptr) 1072b15cb3dSCy Schubert { 1082b15cb3dSCy Schubert struct event_base *base = evconnlistener_get_base(listener); 1092b15cb3dSCy Schubert struct bufferevent *bev = bufferevent_socket_new(base, sock, 1102b15cb3dSCy Schubert BEV_OPT_CLOSE_ON_FREE); 1112b15cb3dSCy Schubert 1122b15cb3dSCy Schubert bufferevent_setcb(bev, server_read_cb, NULL, server_event_cb, NULL); 1132b15cb3dSCy Schubert bufferevent_enable(bev, EV_READ|EV_WRITE); 1142b15cb3dSCy Schubert } 1152b15cb3dSCy Schubert 1162b15cb3dSCy Schubert /* Start the server listening on a random port and start the first client. */ 1172b15cb3dSCy Schubert static void 1182b15cb3dSCy Schubert start_loop(void) 1192b15cb3dSCy Schubert { 1202b15cb3dSCy Schubert struct event_base *base; 1212b15cb3dSCy Schubert struct evconnlistener *listener; 1222b15cb3dSCy Schubert struct sockaddr_storage ss; 1232b15cb3dSCy Schubert ev_socklen_t socklen = sizeof(ss); 1242b15cb3dSCy Schubert evutil_socket_t fd; 1252b15cb3dSCy Schubert 1262b15cb3dSCy Schubert base = event_base_new(); 1272b15cb3dSCy Schubert if (base == NULL) { 1282b15cb3dSCy Schubert puts("Could not open event base!"); 1292b15cb3dSCy Schubert exit(1); 1302b15cb3dSCy Schubert } 1312b15cb3dSCy Schubert 1322b15cb3dSCy Schubert listener = evconnlistener_new_bind(base, listener_accept_cb, NULL, 1332b15cb3dSCy Schubert LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, 134*a25439b6SCy Schubert -1, (struct sockaddr *)&saddr, sizeof(saddr)); 1352b15cb3dSCy Schubert if (listener == NULL) { 1362b15cb3dSCy Schubert my_perror("Could not create listener!"); 1372b15cb3dSCy Schubert exit(1); 1382b15cb3dSCy Schubert } 1392b15cb3dSCy Schubert fd = evconnlistener_get_fd(listener); 1402b15cb3dSCy Schubert if (fd < 0) { 1412b15cb3dSCy Schubert puts("Couldn't get fd from listener"); 1422b15cb3dSCy Schubert exit(1); 1432b15cb3dSCy Schubert } 1442b15cb3dSCy Schubert if (getsockname(fd, (struct sockaddr *)&ss, &socklen) < 0) { 1452b15cb3dSCy Schubert my_perror("getsockname()"); 1462b15cb3dSCy Schubert exit(1); 1472b15cb3dSCy Schubert } 148*a25439b6SCy Schubert memcpy(&saddr, &ss, sizeof(saddr)); 149*a25439b6SCy Schubert if (saddr.sin_family != AF_INET) { 1502b15cb3dSCy Schubert puts("AF mismatch from getsockname()."); 1512b15cb3dSCy Schubert exit(1); 1522b15cb3dSCy Schubert } 1532b15cb3dSCy Schubert 1542b15cb3dSCy Schubert start_client(base); 1552b15cb3dSCy Schubert 1562b15cb3dSCy Schubert event_base_dispatch(base); 1572b15cb3dSCy Schubert } 1582b15cb3dSCy Schubert 1592b15cb3dSCy Schubert /* 1602b15cb3dSCy Schubert =============================================== 1612b15cb3dSCy Schubert Client functions 1622b15cb3dSCy Schubert =============================================== 1632b15cb3dSCy Schubert */ 1642b15cb3dSCy Schubert 1652b15cb3dSCy Schubert /* Check that the server sends back the same byte that the client sent. 1662b15cb3dSCy Schubert If MAX_REQUESTS have been reached, exit. Otherwise, start another client. */ 1672b15cb3dSCy Schubert static void 1682b15cb3dSCy Schubert client_read_cb(struct bufferevent *bev, void *ctx) 1692b15cb3dSCy Schubert { 1702b15cb3dSCy Schubert unsigned char tmp; 1712b15cb3dSCy Schubert struct event_base *base = bufferevent_get_base(bev); 1722b15cb3dSCy Schubert 1732b15cb3dSCy Schubert bufferevent_read(bev, &tmp, 1); 1742b15cb3dSCy Schubert if (tmp != 'A') { 1752b15cb3dSCy Schubert puts("Incorrect data received!"); 1762b15cb3dSCy Schubert exit(2); 1772b15cb3dSCy Schubert } 1782b15cb3dSCy Schubert bufferevent_free(bev); 1792b15cb3dSCy Schubert 1802b15cb3dSCy Schubert num_requests++; 1812b15cb3dSCy Schubert if (num_requests == MAX_REQUESTS) { 1822b15cb3dSCy Schubert event_base_loopbreak(base); 1832b15cb3dSCy Schubert } else { 1842b15cb3dSCy Schubert start_client(base); 1852b15cb3dSCy Schubert } 1862b15cb3dSCy Schubert } 1872b15cb3dSCy Schubert 1882b15cb3dSCy Schubert /* Send a byte to the server. */ 1892b15cb3dSCy Schubert static void 1902b15cb3dSCy Schubert client_event_cb(struct bufferevent *bev, short events, void *ctx) 1912b15cb3dSCy Schubert { 1922b15cb3dSCy Schubert if (events & BEV_EVENT_CONNECTED) { 1932b15cb3dSCy Schubert unsigned char tmp = 'A'; 1942b15cb3dSCy Schubert bufferevent_write(bev, &tmp, 1); 1952b15cb3dSCy Schubert } else if (events & BEV_EVENT_ERROR) { 1962b15cb3dSCy Schubert puts("Client socket got error!"); 1972b15cb3dSCy Schubert exit(2); 1982b15cb3dSCy Schubert } 1992b15cb3dSCy Schubert 2002b15cb3dSCy Schubert bufferevent_enable(bev, EV_READ); 2012b15cb3dSCy Schubert } 2022b15cb3dSCy Schubert 2032b15cb3dSCy Schubert /* Open a client socket to connect to localhost on sin */ 2042b15cb3dSCy Schubert static void 2052b15cb3dSCy Schubert start_client(struct event_base *base) 2062b15cb3dSCy Schubert { 2072b15cb3dSCy Schubert struct bufferevent *bev = bufferevent_socket_new(base, -1, 2082b15cb3dSCy Schubert BEV_OPT_CLOSE_ON_FREE); 2092b15cb3dSCy Schubert bufferevent_setcb(bev, client_read_cb, NULL, client_event_cb, NULL); 2102b15cb3dSCy Schubert 211*a25439b6SCy Schubert if (bufferevent_socket_connect(bev, (struct sockaddr *)&saddr, 212*a25439b6SCy Schubert sizeof(saddr)) < 0) { 2132b15cb3dSCy Schubert my_perror("Could not connect!"); 2142b15cb3dSCy Schubert bufferevent_free(bev); 2152b15cb3dSCy Schubert exit(2); 2162b15cb3dSCy Schubert } 2172b15cb3dSCy Schubert } 2182b15cb3dSCy Schubert 2192b15cb3dSCy Schubert int 2202b15cb3dSCy Schubert main(int argc, char **argv) 2212b15cb3dSCy Schubert { 2222b15cb3dSCy Schubert #ifdef EVENT__HAVE_SETRLIMIT 2232b15cb3dSCy Schubert /* Set the fd limit to a low value so that any fd leak is caught without 2242b15cb3dSCy Schubert making many requests. */ 2252b15cb3dSCy Schubert struct rlimit rl; 2262b15cb3dSCy Schubert rl.rlim_cur = rl.rlim_max = 20; 2272b15cb3dSCy Schubert if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { 2282b15cb3dSCy Schubert my_perror("setrlimit"); 2292b15cb3dSCy Schubert exit(3); 2302b15cb3dSCy Schubert } 2312b15cb3dSCy Schubert #endif 2322b15cb3dSCy Schubert 2332b15cb3dSCy Schubert #ifdef _WIN32 2342b15cb3dSCy Schubert WSADATA WSAData; 2352b15cb3dSCy Schubert WSAStartup(0x101, &WSAData); 2362b15cb3dSCy Schubert #endif 2372b15cb3dSCy Schubert 2382b15cb3dSCy Schubert /* Set up an address, used by both client & server. */ 239*a25439b6SCy Schubert memset(&saddr, 0, sizeof(saddr)); 240*a25439b6SCy Schubert saddr.sin_family = AF_INET; 241*a25439b6SCy Schubert saddr.sin_addr.s_addr = htonl(0x7f000001); 242*a25439b6SCy Schubert saddr.sin_port = 0; /* Tell the implementation to pick a port. */ 2432b15cb3dSCy Schubert 2442b15cb3dSCy Schubert start_loop(); 2452b15cb3dSCy Schubert 2462b15cb3dSCy Schubert return 0; 2472b15cb3dSCy Schubert } 2482b15cb3dSCy Schubert 2492b15cb3dSCy Schubert /* XXX why does this test cause so much latency sometimes (OSX 10.5)? */ 250