9 #include <event2/bufferevent.h>
10 #include <event2/buffer.h>
11 #include <event2/listener.h>
12 #include <event2/util.h>
13 #include <event2/event.h>
14 #include <sys/socket.h>
20 static const char MESSAGE[] = "Hello, World!\n";
22 static const char PATH[] = "./roura";
24 static const string COMMAND_EXIT("exit\n");
26 static void listener_cb(evutil_socket_t, short, void *);
27 static void conn_read_cb(struct bufferevent *, void *);
28 static void conn_write_cb(struct bufferevent *, void *);
29 static void conn_event_cb(struct bufferevent *, short, void *);
30 static void signal_cb(evutil_socket_t, short, void *);
31 static void print_socket_info(int, int);
33 int main(int argc, char **argv) {
34 printf("%4s %8s\n", "*", "STARTED");
35 struct event_base *base;
36 evutil_socket_t listener;
37 struct event *listener_event;
38 struct event *signal_event;
40 struct sockaddr_un sun;
42 setvbuf(stdout, NULL, _IONBF, 0);
44 base = event_base_new();
46 printf("%4s %8s %s\n", "*", "ERROR", "unable to initialize libevent");
50 memset(&sun, 0, sizeof (sun));
51 sun.sun_family = AF_UNIX;
52 strcpy(sun.sun_path, PATH);
54 listener = socket(AF_UNIX, SOCK_STREAM, 0);
55 evutil_make_socket_nonblocking(listener);
57 if (bind(listener, (struct sockaddr*) &sun, sizeof (sun)) < 0) {
58 printf("%4s %8s %s: %s\n", "*", "ERROR", "unable to create domain socket:", PATH);
62 if (listen(listener, 16) < 0) {
63 printf("%4s %8s %s\n", "*", "ERROR", "unable to listen");
67 // identifikátor serverového soketu (v současnosti číslo FD)
68 printf("%4s %8s listening at: sun_path = %s → socketId = %d\n", "*", "SOCKET", sun.sun_path, listener);
70 listener_event = event_new(base, listener, EV_READ | EV_PERSIST, listener_cb, (void*) base);
72 if (!listener_event) {
73 printf("%4s %8s %s\n", "*", "ERROR", "unable to do event_new()");
77 event_add(listener_event, NULL);
79 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *) base);
81 if (!signal_event || event_add(signal_event, NULL) < 0) {
82 printf("%4s %8s %s\n", "*", "ERROR", "unable to create/add a signal event");
86 event_base_dispatch(base);
88 event_free(listener_event);
89 event_free(signal_event);
90 event_base_free(base);
92 // smažeme soket na disku / soubor -- jinak by program příště spadl na bind()
93 // TODO: co když soket někdo přesune a místo něj dá jiný soubor?
96 printf("%4s %8s\n", "*", "FINISHED");
100 static void listener_cb(evutil_socket_t listener, short event, void *user_data) {
101 struct event_base *base = (event_base *) user_data;
102 struct bufferevent *bev;
104 struct sockaddr_storage ss;
105 socklen_t slen = sizeof (ss);
106 int fd = accept(listener, (struct sockaddr*) &ss, &slen);
108 // identifikátor navázaného spojení (v současnosti číslo FD)
109 int * connectionId = (int*) malloc(sizeof (fd));
113 printf("%4d %8s %s\n", *connectionId, "ERROR", "unable to accept()");
115 } else if (fd > FD_SETSIZE) {
116 // FD_SETSIZE = 1024 -- Proč? Co když bude spojení víc?
117 printf("%4d %8s fd (%d) > FD_SETSIZE (%d)\n", *connectionId, "ERROR", fd, FD_SETSIZE);
122 evutil_make_socket_nonblocking(fd);
125 bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
127 printf("%4d %8s %s\n", *connectionId, "ERROR", "constructing bufferevent");
128 event_base_loopbreak(base);
135 bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) connectionId);
136 bufferevent_enable(bev, EV_READ | EV_WRITE);
138 printf("%4d %8s somebody has connected: socketId = %d → connectionId = %d\n", *connectionId, "CONN", listener, *connectionId);
139 print_socket_info(*connectionId, fd);
141 bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
144 static void conn_read_cb(struct bufferevent *bev, void *user_data) {
145 int connectionId = *((int*) user_data);
147 /* This callback is invoked when there is data to read on bev. */
148 struct evbuffer *input = bufferevent_get_input(bev);
149 struct evbuffer *output = bufferevent_get_output(bev);
150 struct event_base *base = bufferevent_get_base(bev);
152 size_t len = evbuffer_get_length(input);
153 char *data = (char*) malloc(len);
154 evbuffer_copyout(input, data, len);
157 string dataFormated(data);
158 dataFormated = regex_replace(dataFormated, regex("\\n"), "\\n");
159 printf("%4d %8s '%s'\n", connectionId, "IN", dataFormated.c_str());
161 if (COMMAND_EXIT.compare(data) == 0) {
162 struct timeval delay = {2, 123};
163 printf("%4d %8s client asks us to terminate; finishing in %ld sesonds and %ld microseconds\n", connectionId, "EXIT", delay.tv_sec, delay.tv_usec);
164 event_base_loopexit(base, &delay);
167 evbuffer_add(output, "echo: ", 6);
168 /* Copy all the data from the input buffer to the output buffer. */
169 evbuffer_add_buffer(output, input);
173 static void conn_write_cb(struct bufferevent *bev, void *user_data) {
174 int connectionId = *((int*) user_data);
176 struct evbuffer *output = bufferevent_get_output(bev);
177 if (evbuffer_get_length(output) == 0) {
178 printf("%4d %8s\n", connectionId, "FLUSH");
179 /* nebudeme ukončovat spojení
180 bufferevent_free(bev);
183 // FIXME: sem to nikdy nepřijde
184 size_t len = evbuffer_get_length(output);
185 char *data = (char*) malloc(len);
186 evbuffer_copyout(output, data, len);
187 printf("%4d %8s '%s'\n", connectionId, "OUT", data);
192 static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) {
193 int connectionId = *((int*) user_data);
195 if (events & BEV_EVENT_EOF) {
196 printf("%4d %8s\n", connectionId, "CLOSE");
197 } else if (events & BEV_EVENT_ERROR) {
198 printf("%4d %8s %s\n", connectionId, "ERROR", strerror(errno));
200 printf("%4d %8s\n", connectionId, "OTHER");
201 // None of the other events can happen here, since we haven't enabled timeouts
204 bufferevent_free(bev);
208 static void signal_cb(evutil_socket_t sig, short events, void *user_data) {
209 struct event_base *base = (event_base *) user_data;
210 struct timeval delay = {2, 123};
212 printf("\n%4s %8s got SIGINT (Ctrl+C); finishing in %ld sesonds and %ld microseconds\n", "*", "SIGNAL", delay.tv_sec, delay.tv_usec);
214 event_base_loopexit(base, &delay);
217 static void print_socket_info(int connectionId, int fd) {
219 unsigned int cl = sizeof (cr);
221 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) == 0) {
222 printf("%4d %8s client identification: pid=%d, uid=%d, gid=%d\n", connectionId, "CONN", cr.pid, cr.uid, cr.gid);