1*c43e99fdSEd Maste /*
2*c43e99fdSEd Maste * Copyright (c) 2012 Niels Provos and Nick Mathewson
3*c43e99fdSEd Maste *
4*c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
5*c43e99fdSEd Maste * modification, are permitted provided that the following conditions
6*c43e99fdSEd Maste * are met:
7*c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
8*c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
9*c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
10*c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
11*c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
12*c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products
13*c43e99fdSEd Maste * derived from this software without specific prior written permission.
14*c43e99fdSEd Maste *
15*c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*c43e99fdSEd Maste */
26*c43e99fdSEd Maste #include "util-internal.h"
27*c43e99fdSEd Maste #include "event2/event-config.h"
28*c43e99fdSEd Maste
29*c43e99fdSEd Maste #ifdef _WIN32
30*c43e99fdSEd Maste #include <winsock2.h>
31*c43e99fdSEd Maste #include <windows.h>
32*c43e99fdSEd Maste #else
33*c43e99fdSEd Maste #include <unistd.h>
34*c43e99fdSEd Maste #endif
35*c43e99fdSEd Maste
36*c43e99fdSEd Maste #include <stdio.h>
37*c43e99fdSEd Maste #include <event2/event.h>
38*c43e99fdSEd Maste #include <signal.h>
39*c43e99fdSEd Maste
40*c43e99fdSEd Maste static void
sock_perror(const char * s)41*c43e99fdSEd Maste sock_perror(const char *s)
42*c43e99fdSEd Maste {
43*c43e99fdSEd Maste #ifdef _WIN32
44*c43e99fdSEd Maste const char *err = evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR());
45*c43e99fdSEd Maste fprintf(stderr, "%s: %s\n", s, err);
46*c43e99fdSEd Maste #else
47*c43e99fdSEd Maste perror(s);
48*c43e99fdSEd Maste #endif
49*c43e99fdSEd Maste }
50*c43e99fdSEd Maste
51*c43e99fdSEd Maste static void
callback1(evutil_socket_t fd,short events,void * arg)52*c43e99fdSEd Maste callback1(evutil_socket_t fd, short events, void *arg)
53*c43e99fdSEd Maste {
54*c43e99fdSEd Maste }
55*c43e99fdSEd Maste static void
callback2(evutil_socket_t fd,short events,void * arg)56*c43e99fdSEd Maste callback2(evutil_socket_t fd, short events, void *arg)
57*c43e99fdSEd Maste {
58*c43e99fdSEd Maste }
59*c43e99fdSEd Maste
60*c43e99fdSEd Maste /* Testing code for event_base_dump_events().
61*c43e99fdSEd Maste
62*c43e99fdSEd Maste Notes that just because we have code to exercise this function,
63*c43e99fdSEd Maste doesn't mean that *ANYTHING* about the output format is guaranteed to
64*c43e99fdSEd Maste remain in the future.
65*c43e99fdSEd Maste */
66*c43e99fdSEd Maste int
main(int argc,char ** argv)67*c43e99fdSEd Maste main(int argc, char **argv)
68*c43e99fdSEd Maste {
69*c43e99fdSEd Maste #define N_EVENTS 13
70*c43e99fdSEd Maste int i;
71*c43e99fdSEd Maste struct event *ev[N_EVENTS];
72*c43e99fdSEd Maste evutil_socket_t pair1[2];
73*c43e99fdSEd Maste evutil_socket_t pair2[2];
74*c43e99fdSEd Maste struct timeval tv_onesec = {1,0};
75*c43e99fdSEd Maste struct timeval tv_two5sec = {2,500*1000};
76*c43e99fdSEd Maste const struct timeval *tv_onesec_common;
77*c43e99fdSEd Maste const struct timeval *tv_two5sec_common;
78*c43e99fdSEd Maste struct event_base *base;
79*c43e99fdSEd Maste struct timeval now;
80*c43e99fdSEd Maste
81*c43e99fdSEd Maste #ifdef _WIN32
82*c43e99fdSEd Maste WORD wVersionRequested;
83*c43e99fdSEd Maste WSADATA wsaData;
84*c43e99fdSEd Maste
85*c43e99fdSEd Maste wVersionRequested = MAKEWORD(2, 2);
86*c43e99fdSEd Maste
87*c43e99fdSEd Maste WSAStartup(wVersionRequested, &wsaData);
88*c43e99fdSEd Maste #endif
89*c43e99fdSEd Maste
90*c43e99fdSEd Maste #ifdef _WIN32
91*c43e99fdSEd Maste #define LOCAL_SOCKETPAIR_AF AF_INET
92*c43e99fdSEd Maste #else
93*c43e99fdSEd Maste #define LOCAL_SOCKETPAIR_AF AF_UNIX
94*c43e99fdSEd Maste #endif
95*c43e99fdSEd Maste
96*c43e99fdSEd Maste if (evutil_make_internal_pipe_(pair1) < 0 ||
97*c43e99fdSEd Maste evutil_make_internal_pipe_(pair2) < 0) {
98*c43e99fdSEd Maste sock_perror("evutil_make_internal_pipe_");
99*c43e99fdSEd Maste return 1;
100*c43e99fdSEd Maste }
101*c43e99fdSEd Maste
102*c43e99fdSEd Maste if (!(base = event_base_new())) {
103*c43e99fdSEd Maste fprintf(stderr,"Couldn't make event_base\n");
104*c43e99fdSEd Maste return 2;
105*c43e99fdSEd Maste }
106*c43e99fdSEd Maste
107*c43e99fdSEd Maste tv_onesec_common = event_base_init_common_timeout(base, &tv_onesec);
108*c43e99fdSEd Maste tv_two5sec_common = event_base_init_common_timeout(base, &tv_two5sec);
109*c43e99fdSEd Maste
110*c43e99fdSEd Maste ev[0] = event_new(base, pair1[0], EV_WRITE, callback1, NULL);
111*c43e99fdSEd Maste ev[1] = event_new(base, pair1[1], EV_READ|EV_PERSIST, callback1, NULL);
112*c43e99fdSEd Maste ev[2] = event_new(base, pair2[0], EV_WRITE|EV_PERSIST, callback2, NULL);
113*c43e99fdSEd Maste ev[3] = event_new(base, pair2[1], EV_READ, callback2, NULL);
114*c43e99fdSEd Maste
115*c43e99fdSEd Maste /* For timers */
116*c43e99fdSEd Maste ev[4] = evtimer_new(base, callback1, NULL);
117*c43e99fdSEd Maste ev[5] = evtimer_new(base, callback1, NULL);
118*c43e99fdSEd Maste ev[6] = evtimer_new(base, callback1, NULL);
119*c43e99fdSEd Maste ev[7] = event_new(base, -1, EV_PERSIST, callback2, NULL);
120*c43e99fdSEd Maste ev[8] = event_new(base, -1, EV_PERSIST, callback2, NULL);
121*c43e99fdSEd Maste ev[9] = event_new(base, -1, EV_PERSIST, callback2, NULL);
122*c43e99fdSEd Maste
123*c43e99fdSEd Maste /* To activate */
124*c43e99fdSEd Maste ev[10] = event_new(base, -1, 0, callback1, NULL);
125*c43e99fdSEd Maste ev[11] = event_new(base, -1, 0, callback2, NULL);
126*c43e99fdSEd Maste
127*c43e99fdSEd Maste /* Signals */
128*c43e99fdSEd Maste ev[12] = evsignal_new(base, SIGINT, callback2, NULL);
129*c43e99fdSEd Maste
130*c43e99fdSEd Maste event_add(ev[0], NULL);
131*c43e99fdSEd Maste event_add(ev[1], &tv_onesec);
132*c43e99fdSEd Maste event_add(ev[2], tv_onesec_common);
133*c43e99fdSEd Maste event_add(ev[3], tv_two5sec_common);
134*c43e99fdSEd Maste
135*c43e99fdSEd Maste event_add(ev[4], tv_onesec_common);
136*c43e99fdSEd Maste event_add(ev[5], tv_onesec_common);
137*c43e99fdSEd Maste event_add(ev[6], &tv_onesec);
138*c43e99fdSEd Maste event_add(ev[7], tv_two5sec_common);
139*c43e99fdSEd Maste event_add(ev[8], tv_onesec_common);
140*c43e99fdSEd Maste event_add(ev[9], &tv_two5sec);
141*c43e99fdSEd Maste
142*c43e99fdSEd Maste event_active(ev[10], EV_READ, 1);
143*c43e99fdSEd Maste event_active(ev[11], EV_READ|EV_WRITE|EV_TIMEOUT, 1);
144*c43e99fdSEd Maste event_active(ev[1], EV_READ, 1);
145*c43e99fdSEd Maste
146*c43e99fdSEd Maste event_add(ev[12], NULL);
147*c43e99fdSEd Maste
148*c43e99fdSEd Maste evutil_gettimeofday(&now,NULL);
149*c43e99fdSEd Maste puts("=====expected");
150*c43e99fdSEd Maste printf("Now= %ld.%06d\n",(long)now.tv_sec,(int)now.tv_usec);
151*c43e99fdSEd Maste puts("Inserted:");
152*c43e99fdSEd Maste printf(" %p [fd %ld] Write\n",ev[0],(long)pair1[0]);
153*c43e99fdSEd Maste printf(" %p [fd %ld] Read Persist Timeout=T+1\n",ev[1],(long)pair1[1]);
154*c43e99fdSEd Maste printf(" %p [fd %ld] Write Persist Timeout=T+1\n",ev[2],(long)pair2[0]);
155*c43e99fdSEd Maste printf(" %p [fd %ld] Read Timeout=T+2.5\n",ev[3],(long)pair2[1]);
156*c43e99fdSEd Maste printf(" %p [fd -1] Timeout=T+1\n",ev[4]);
157*c43e99fdSEd Maste printf(" %p [fd -1] Timeout=T+1\n",ev[5]);
158*c43e99fdSEd Maste printf(" %p [fd -1] Timeout=T+1\n",ev[6]);
159*c43e99fdSEd Maste printf(" %p [fd -1] Persist Timeout=T+2.5\n",ev[7]);
160*c43e99fdSEd Maste printf(" %p [fd -1] Persist Timeout=T+1\n",ev[8]);
161*c43e99fdSEd Maste printf(" %p [fd -1] Persist Timeout=T+2.5\n",ev[9]);
162*c43e99fdSEd Maste printf(" %p [sig %d] Signal Persist\n", ev[12], (int)SIGINT);
163*c43e99fdSEd Maste
164*c43e99fdSEd Maste puts("Active:");
165*c43e99fdSEd Maste printf(" %p [fd -1, priority=0] Read active\n", ev[10]);
166*c43e99fdSEd Maste printf(" %p [fd -1, priority=0] Read Write Timeout active\n", ev[11]);
167*c43e99fdSEd Maste printf(" %p [fd %ld, priority=0] Read active\n", ev[1], (long)pair1[1]);
168*c43e99fdSEd Maste
169*c43e99fdSEd Maste puts("======received");
170*c43e99fdSEd Maste event_base_dump_events(base, stdout);
171*c43e99fdSEd Maste
172*c43e99fdSEd Maste for (i = 0; i < N_EVENTS; ++i) {
173*c43e99fdSEd Maste event_free(ev[i]);
174*c43e99fdSEd Maste }
175*c43e99fdSEd Maste event_base_free(base);
176*c43e99fdSEd Maste
177*c43e99fdSEd Maste return 0;
178*c43e99fdSEd Maste }
179*c43e99fdSEd Maste
180