xref: /freebsd/contrib/ntp/sntp/libevent/sample/hello-world.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert   This example program provides a trivial server program that listens for TCP
3*a466cc55SCy Schubert   connections on port 9995.  When they arrive, it writes a short message to
4*a466cc55SCy Schubert   each client connection, and closes each connection once it is flushed.
5*a466cc55SCy Schubert 
6*a466cc55SCy Schubert   Where possible, it exits cleanly in response to a SIGINT (ctrl-c).
7*a466cc55SCy Schubert */
8*a466cc55SCy Schubert 
9*a466cc55SCy Schubert 
10*a466cc55SCy Schubert #include <string.h>
11*a466cc55SCy Schubert #include <errno.h>
12*a466cc55SCy Schubert #include <stdio.h>
13*a466cc55SCy Schubert #include <signal.h>
14*a466cc55SCy Schubert #ifndef _WIN32
15*a466cc55SCy Schubert #include <netinet/in.h>
16*a466cc55SCy Schubert # ifdef _XOPEN_SOURCE_EXTENDED
17*a466cc55SCy Schubert #  include <arpa/inet.h>
18*a466cc55SCy Schubert # endif
19*a466cc55SCy Schubert #include <sys/socket.h>
20*a466cc55SCy Schubert #endif
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <event2/bufferevent.h>
23*a466cc55SCy Schubert #include <event2/buffer.h>
24*a466cc55SCy Schubert #include <event2/listener.h>
25*a466cc55SCy Schubert #include <event2/util.h>
26*a466cc55SCy Schubert #include <event2/event.h>
27*a466cc55SCy Schubert 
28*a466cc55SCy Schubert static const char MESSAGE[] = "Hello, World!\n";
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert static const int PORT = 9995;
31*a466cc55SCy Schubert 
32*a466cc55SCy Schubert static void listener_cb(struct evconnlistener *, evutil_socket_t,
33*a466cc55SCy Schubert     struct sockaddr *, int socklen, void *);
34*a466cc55SCy Schubert static void conn_writecb(struct bufferevent *, void *);
35*a466cc55SCy Schubert static void conn_eventcb(struct bufferevent *, short, void *);
36*a466cc55SCy Schubert static void signal_cb(evutil_socket_t, short, void *);
37*a466cc55SCy Schubert 
38*a466cc55SCy Schubert int
main(int argc,char ** argv)39*a466cc55SCy Schubert main(int argc, char **argv)
40*a466cc55SCy Schubert {
41*a466cc55SCy Schubert 	struct event_base *base;
42*a466cc55SCy Schubert 	struct evconnlistener *listener;
43*a466cc55SCy Schubert 	struct event *signal_event;
44*a466cc55SCy Schubert 
45*a466cc55SCy Schubert 	struct sockaddr_in sin = {0};
46*a466cc55SCy Schubert #ifdef _WIN32
47*a466cc55SCy Schubert 	WSADATA wsa_data;
48*a466cc55SCy Schubert 	WSAStartup(0x0201, &wsa_data);
49*a466cc55SCy Schubert #endif
50*a466cc55SCy Schubert 
51*a466cc55SCy Schubert 	base = event_base_new();
52*a466cc55SCy Schubert 	if (!base) {
53*a466cc55SCy Schubert 		fprintf(stderr, "Could not initialize libevent!\n");
54*a466cc55SCy Schubert 		return 1;
55*a466cc55SCy Schubert 	}
56*a466cc55SCy Schubert 
57*a466cc55SCy Schubert 	sin.sin_family = AF_INET;
58*a466cc55SCy Schubert 	sin.sin_port = htons(PORT);
59*a466cc55SCy Schubert 
60*a466cc55SCy Schubert 	listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
61*a466cc55SCy Schubert 	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
62*a466cc55SCy Schubert 	    (struct sockaddr*)&sin,
63*a466cc55SCy Schubert 	    sizeof(sin));
64*a466cc55SCy Schubert 
65*a466cc55SCy Schubert 	if (!listener) {
66*a466cc55SCy Schubert 		fprintf(stderr, "Could not create a listener!\n");
67*a466cc55SCy Schubert 		return 1;
68*a466cc55SCy Schubert 	}
69*a466cc55SCy Schubert 
70*a466cc55SCy Schubert 	signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
71*a466cc55SCy Schubert 
72*a466cc55SCy Schubert 	if (!signal_event || event_add(signal_event, NULL)<0) {
73*a466cc55SCy Schubert 		fprintf(stderr, "Could not create/add a signal event!\n");
74*a466cc55SCy Schubert 		return 1;
75*a466cc55SCy Schubert 	}
76*a466cc55SCy Schubert 
77*a466cc55SCy Schubert 	event_base_dispatch(base);
78*a466cc55SCy Schubert 
79*a466cc55SCy Schubert 	evconnlistener_free(listener);
80*a466cc55SCy Schubert 	event_free(signal_event);
81*a466cc55SCy Schubert 	event_base_free(base);
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert 	printf("done\n");
84*a466cc55SCy Schubert 	return 0;
85*a466cc55SCy Schubert }
86*a466cc55SCy Schubert 
87*a466cc55SCy Schubert static void
listener_cb(struct evconnlistener * listener,evutil_socket_t fd,struct sockaddr * sa,int socklen,void * user_data)88*a466cc55SCy Schubert listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
89*a466cc55SCy Schubert     struct sockaddr *sa, int socklen, void *user_data)
90*a466cc55SCy Schubert {
91*a466cc55SCy Schubert 	struct event_base *base = user_data;
92*a466cc55SCy Schubert 	struct bufferevent *bev;
93*a466cc55SCy Schubert 
94*a466cc55SCy Schubert 	bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
95*a466cc55SCy Schubert 	if (!bev) {
96*a466cc55SCy Schubert 		fprintf(stderr, "Error constructing bufferevent!");
97*a466cc55SCy Schubert 		event_base_loopbreak(base);
98*a466cc55SCy Schubert 		return;
99*a466cc55SCy Schubert 	}
100*a466cc55SCy Schubert 	bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
101*a466cc55SCy Schubert 	bufferevent_enable(bev, EV_WRITE);
102*a466cc55SCy Schubert 	bufferevent_disable(bev, EV_READ);
103*a466cc55SCy Schubert 
104*a466cc55SCy Schubert 	bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
105*a466cc55SCy Schubert }
106*a466cc55SCy Schubert 
107*a466cc55SCy Schubert static void
conn_writecb(struct bufferevent * bev,void * user_data)108*a466cc55SCy Schubert conn_writecb(struct bufferevent *bev, void *user_data)
109*a466cc55SCy Schubert {
110*a466cc55SCy Schubert 	struct evbuffer *output = bufferevent_get_output(bev);
111*a466cc55SCy Schubert 	if (evbuffer_get_length(output) == 0) {
112*a466cc55SCy Schubert 		printf("flushed answer\n");
113*a466cc55SCy Schubert 		bufferevent_free(bev);
114*a466cc55SCy Schubert 	}
115*a466cc55SCy Schubert }
116*a466cc55SCy Schubert 
117*a466cc55SCy Schubert static void
conn_eventcb(struct bufferevent * bev,short events,void * user_data)118*a466cc55SCy Schubert conn_eventcb(struct bufferevent *bev, short events, void *user_data)
119*a466cc55SCy Schubert {
120*a466cc55SCy Schubert 	if (events & BEV_EVENT_EOF) {
121*a466cc55SCy Schubert 		printf("Connection closed.\n");
122*a466cc55SCy Schubert 	} else if (events & BEV_EVENT_ERROR) {
123*a466cc55SCy Schubert 		printf("Got an error on the connection: %s\n",
124*a466cc55SCy Schubert 		    strerror(errno));/*XXX win32*/
125*a466cc55SCy Schubert 	}
126*a466cc55SCy Schubert 	/* None of the other events can happen here, since we haven't enabled
127*a466cc55SCy Schubert 	 * timeouts */
128*a466cc55SCy Schubert 	bufferevent_free(bev);
129*a466cc55SCy Schubert }
130*a466cc55SCy Schubert 
131*a466cc55SCy Schubert static void
signal_cb(evutil_socket_t sig,short events,void * user_data)132*a466cc55SCy Schubert signal_cb(evutil_socket_t sig, short events, void *user_data)
133*a466cc55SCy Schubert {
134*a466cc55SCy Schubert 	struct event_base *base = user_data;
135*a466cc55SCy Schubert 	struct timeval delay = { 2, 0 };
136*a466cc55SCy Schubert 
137*a466cc55SCy Schubert 	printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
138*a466cc55SCy Schubert 
139*a466cc55SCy Schubert 	event_base_loopexit(base, &delay);
140*a466cc55SCy Schubert }
141