12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert * are met:
82b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert * derived from this software without specific prior written permission.
152b15cb3dSCy Schubert *
162b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert */
27*a466cc55SCy Schubert #include "../util-internal.h"
282b15cb3dSCy Schubert #include "event2/event-config.h"
292b15cb3dSCy Schubert
302b15cb3dSCy Schubert #ifdef _WIN32
312b15cb3dSCy Schubert #include <winsock2.h>
322b15cb3dSCy Schubert #else
332b15cb3dSCy Schubert #include <unistd.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert #include <sys/types.h>
362b15cb3dSCy Schubert #include <sys/stat.h>
372b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
382b15cb3dSCy Schubert #include <sys/time.h>
392b15cb3dSCy Schubert #endif
402b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_SOCKET_H
412b15cb3dSCy Schubert #include <sys/socket.h>
422b15cb3dSCy Schubert #endif
432b15cb3dSCy Schubert #include <fcntl.h>
442b15cb3dSCy Schubert #include <stdlib.h>
452b15cb3dSCy Schubert #include <stdio.h>
462b15cb3dSCy Schubert #include <string.h>
472b15cb3dSCy Schubert #include <errno.h>
482b15cb3dSCy Schubert
492b15cb3dSCy Schubert #include <event.h>
502b15cb3dSCy Schubert #include <evutil.h>
512b15cb3dSCy Schubert
522b15cb3dSCy Schubert int test_okay = 1;
532b15cb3dSCy Schubert int called = 0;
542b15cb3dSCy Schubert struct timeval timeout = {60, 0};
552b15cb3dSCy Schubert
562b15cb3dSCy Schubert static void
read_cb(evutil_socket_t fd,short event,void * arg)572b15cb3dSCy Schubert read_cb(evutil_socket_t fd, short event, void *arg)
582b15cb3dSCy Schubert {
592b15cb3dSCy Schubert char buf[256];
602b15cb3dSCy Schubert int len;
612b15cb3dSCy Schubert
622b15cb3dSCy Schubert if (EV_TIMEOUT & event) {
632b15cb3dSCy Schubert printf("%s: Timeout!\n", __func__);
642b15cb3dSCy Schubert exit(1);
652b15cb3dSCy Schubert }
662b15cb3dSCy Schubert
672b15cb3dSCy Schubert len = recv(fd, buf, sizeof(buf), 0);
682b15cb3dSCy Schubert
692b15cb3dSCy Schubert printf("%s: read %d%s\n", __func__,
702b15cb3dSCy Schubert len, len ? "" : " - means EOF");
712b15cb3dSCy Schubert
722b15cb3dSCy Schubert if (len) {
732b15cb3dSCy Schubert if (!called)
742b15cb3dSCy Schubert event_add(arg, &timeout);
752b15cb3dSCy Schubert } else if (called == 1)
762b15cb3dSCy Schubert test_okay = 0;
772b15cb3dSCy Schubert
782b15cb3dSCy Schubert called++;
792b15cb3dSCy Schubert }
802b15cb3dSCy Schubert
812b15cb3dSCy Schubert int
main(int argc,char ** argv)822b15cb3dSCy Schubert main(int argc, char **argv)
832b15cb3dSCy Schubert {
842b15cb3dSCy Schubert struct event ev;
852b15cb3dSCy Schubert const char *test = "test string";
862b15cb3dSCy Schubert evutil_socket_t pair[2];
872b15cb3dSCy Schubert
882b15cb3dSCy Schubert #ifdef _WIN32
892b15cb3dSCy Schubert WORD wVersionRequested;
902b15cb3dSCy Schubert WSADATA wsaData;
912b15cb3dSCy Schubert
922b15cb3dSCy Schubert wVersionRequested = MAKEWORD(2, 2);
932b15cb3dSCy Schubert
942b15cb3dSCy Schubert (void) WSAStartup(wVersionRequested, &wsaData);
952b15cb3dSCy Schubert #endif
962b15cb3dSCy Schubert
972b15cb3dSCy Schubert if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
982b15cb3dSCy Schubert return (1);
992b15cb3dSCy Schubert
1002b15cb3dSCy Schubert
1012b15cb3dSCy Schubert if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
1022b15cb3dSCy Schubert return (1);
103*a466cc55SCy Schubert shutdown(pair[0], EVUTIL_SHUT_WR);
1042b15cb3dSCy Schubert
105*a466cc55SCy Schubert /* Initialize the event library */
1062b15cb3dSCy Schubert event_init();
1072b15cb3dSCy Schubert
108*a466cc55SCy Schubert /* Initialize one event */
1092b15cb3dSCy Schubert event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev);
1102b15cb3dSCy Schubert
1112b15cb3dSCy Schubert event_add(&ev, &timeout);
1122b15cb3dSCy Schubert
1132b15cb3dSCy Schubert event_dispatch();
1142b15cb3dSCy Schubert
1152b15cb3dSCy Schubert return (test_okay);
1162b15cb3dSCy Schubert }
1172b15cb3dSCy Schubert
118