1275970Scy/* 2275970Scy * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6275970Scy * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 3. The name of the author may not be used to endorse or promote products 14275970Scy * derived from this software without specific prior written permission. 15275970Scy * 16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26275970Scy */ 27275970Scy#include "event2/event-config.h" 28275970Scy 29275970Scy#ifdef _WIN32 30275970Scy#include <winsock2.h> 31275970Scy#else 32275970Scy#include <unistd.h> 33275970Scy#endif 34275970Scy#include <sys/types.h> 35275970Scy#include <sys/stat.h> 36275970Scy#ifdef EVENT__HAVE_SYS_TIME_H 37275970Scy#include <sys/time.h> 38275970Scy#endif 39275970Scy#ifdef EVENT__HAVE_SYS_SOCKET_H 40275970Scy#include <sys/socket.h> 41275970Scy#endif 42275970Scy#include <fcntl.h> 43275970Scy#include <stdlib.h> 44275970Scy#include <stdio.h> 45275970Scy#include <string.h> 46275970Scy#include <errno.h> 47275970Scy 48275970Scy#include <event.h> 49275970Scy#include <evutil.h> 50275970Scy 51275970Scy#ifdef EVENT____func__ 52275970Scy#define __func__ EVENT____func__ 53275970Scy#endif 54275970Scy 55275970Scyint test_okay = 1; 56275970Scyint called = 0; 57275970Scystruct timeval timeout = {60, 0}; 58275970Scy 59275970Scystatic void 60275970Scyread_cb(evutil_socket_t fd, short event, void *arg) 61275970Scy{ 62275970Scy char buf[256]; 63275970Scy int len; 64275970Scy 65275970Scy if (EV_TIMEOUT & event) { 66275970Scy printf("%s: Timeout!\n", __func__); 67275970Scy exit(1); 68275970Scy } 69275970Scy 70275970Scy len = recv(fd, buf, sizeof(buf), 0); 71275970Scy 72275970Scy printf("%s: read %d%s\n", __func__, 73275970Scy len, len ? "" : " - means EOF"); 74275970Scy 75275970Scy if (len) { 76275970Scy if (!called) 77275970Scy event_add(arg, &timeout); 78275970Scy } else if (called == 1) 79275970Scy test_okay = 0; 80275970Scy 81275970Scy called++; 82275970Scy} 83275970Scy 84275970Scy#ifndef SHUT_WR 85275970Scy#define SHUT_WR 1 86275970Scy#endif 87275970Scy 88275970Scyint 89275970Scymain(int argc, char **argv) 90275970Scy{ 91275970Scy struct event ev; 92275970Scy const char *test = "test string"; 93275970Scy evutil_socket_t pair[2]; 94275970Scy 95275970Scy#ifdef _WIN32 96275970Scy WORD wVersionRequested; 97275970Scy WSADATA wsaData; 98275970Scy 99275970Scy wVersionRequested = MAKEWORD(2, 2); 100275970Scy 101275970Scy (void) WSAStartup(wVersionRequested, &wsaData); 102275970Scy#endif 103275970Scy 104275970Scy if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 105275970Scy return (1); 106275970Scy 107275970Scy 108275970Scy if (send(pair[0], test, (int)strlen(test)+1, 0) < 0) 109275970Scy return (1); 110275970Scy shutdown(pair[0], SHUT_WR); 111275970Scy 112275970Scy /* Initalize the event library */ 113275970Scy event_init(); 114275970Scy 115275970Scy /* Initalize one event */ 116275970Scy event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev); 117275970Scy 118275970Scy event_add(&ev, &timeout); 119275970Scy 120275970Scy event_dispatch(); 121275970Scy 122275970Scy return (test_okay); 123275970Scy} 124275970Scy 125