1245450Sganbold/*
2266337Sian * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3245450Sganbold * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4245450Sganbold *
5245450Sganbold * Redistribution and use in source and binary forms, with or without
6245450Sganbold * modification, are permitted provided that the following conditions
7245450Sganbold * are met:
8245453Sganbold * 1. Redistributions of source code must retain the above copyright
9245453Sganbold *    notice, this list of conditions and the following disclaimer.
10245453Sganbold * 2. Redistributions in binary form must reproduce the above copyright
11245453Sganbold *    notice, this list of conditions and the following disclaimer in the
12245453Sganbold *    documentation and/or other materials provided with the distribution.
13245450Sganbold * 3. The name of the author may not be used to endorse or promote products
14245450Sganbold *    derived from this software without specific prior written permission.
15245450Sganbold *
16245450Sganbold * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17245454Sganbold * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18245450Sganbold * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19245450Sganbold * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20245450Sganbold * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21245450Sganbold * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22245450Sganbold * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23245450Sganbold * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24245450Sganbold * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25245450Sganbold * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26245450Sganbold */
27245450Sganbold#include "../util-internal.h"
28245450Sganbold#include "event2/event-config.h"
29245450Sganbold
30245450Sganbold#ifdef _WIN32
31245450Sganbold#include <winsock2.h>
32245450Sganbold#else
33245450Sganbold#include <unistd.h>
34245450Sganbold#endif
35245450Sganbold#include <sys/types.h>
36245450Sganbold#include <sys/stat.h>
37245450Sganbold#ifdef EVENT__HAVE_SYS_TIME_H
38245450Sganbold#include <sys/time.h>
39245450Sganbold#endif
40245450Sganbold#ifdef EVENT__HAVE_SYS_SOCKET_H
41245450Sganbold#include <sys/socket.h>
42245450Sganbold#endif
43245500Sganbold#include <fcntl.h>
44245450Sganbold#include <stdlib.h>
45245500Sganbold#include <stdio.h>
46245500Sganbold#include <string.h>
47245500Sganbold#include <signal.h>
48245500Sganbold#include <errno.h>
49245500Sganbold
50245500Sganbold#include "event2/event.h"
51245500Sganbold#include "event2/event_struct.h"
52245500Sganbold#include "event2/event_compat.h"
53245500Sganbold#include "event2/util.h"
54245500Sganbold
55245450Sganboldevutil_socket_t pair[2];
56245500Sganboldint test_okay = 1;
57245500Sganboldint called = 0;
58245450Sganbold
59245500Sganboldstatic void
60245450Sganboldwrite_cb(evutil_socket_t fd, short event, void *arg)
61245450Sganbold{
62245450Sganbold	const char *test = "test string";
63245500Sganbold	int len;
64245450Sganbold
65245500Sganbold	len = send(fd, test, (int)strlen(test) + 1, 0);
66245450Sganbold
67245450Sganbold	printf("%s: write %d%s\n", __func__,
68245450Sganbold	    len, len ? "" : " - means EOF");
69245450Sganbold
70245450Sganbold	if (len > 0) {
71245500Sganbold		if (!called)
72245500Sganbold			event_add(arg, NULL);
73245450Sganbold		evutil_closesocket(pair[0]);
74245450Sganbold	} else if (called == 1)
75245500Sganbold		test_okay = 0;
76245450Sganbold
77245450Sganbold	called++;
78245450Sganbold}
79245450Sganbold
80245450Sganboldint
81245450Sganboldmain(int argc, char **argv)
82245450Sganbold{
83245450Sganbold	struct event ev;
84245500Sganbold
85245500Sganbold#ifdef _WIN32
86245450Sganbold	WORD wVersionRequested;
87245450Sganbold	WSADATA wsaData;
88245500Sganbold
89245450Sganbold	wVersionRequested = MAKEWORD(2, 2);
90245450Sganbold
91245450Sganbold	(void) WSAStartup(wVersionRequested, &wsaData);
92245450Sganbold#endif
93245450Sganbold
94245450Sganbold#ifndef _WIN32
95245450Sganbold	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
96245450Sganbold		return (1);
97245450Sganbold#endif
98245450Sganbold
99245450Sganbold	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
100245450Sganbold		return (1);
101245450Sganbold
102245450Sganbold	/* Initialize the event library */
103245450Sganbold	event_init();
104245450Sganbold
105245450Sganbold	/* Initialize one event */
106245450Sganbold	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
107245450Sganbold
108245450Sganbold	event_add(&ev, NULL);
109245450Sganbold
110245450Sganbold	event_dispatch();
111245450Sganbold
112245450Sganbold	return (test_okay);
113245450Sganbold}
114245450Sganbold
115245450Sganbold