c++/domain-socket-bridge/domain-socket-bridge.cpp
changeset 41 2383ed4da507
parent 40 ba40af6cf815
child 42 5bd10c0ae650
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/c++/domain-socket-bridge/domain-socket-bridge.cpp	Sun Nov 20 20:40:30 2016 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +#include <string.h>
     1.5 +#include <errno.h>
     1.6 +#include <stdio.h>
     1.7 +#include <signal.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string>
    1.10 +#include <regex>
    1.11 +
    1.12 +#include <event2/bufferevent.h>
    1.13 +#include <event2/buffer.h>
    1.14 +#include <event2/listener.h>
    1.15 +#include <event2/util.h>
    1.16 +#include <event2/event.h>
    1.17 +#include <sys/socket.h>
    1.18 +#include <sys/un.h>
    1.19 +#include <unistd.h>
    1.20 +
    1.21 +using namespace std;
    1.22 +
    1.23 +static const char MESSAGE[] = "Hello, World!\n";
    1.24 +
    1.25 +static const char PATH[] = "./roura";
    1.26 +
    1.27 +static const string COMMAND_EXIT("exit\n");
    1.28 +
    1.29 +static void listener_cb(evutil_socket_t, short, void *);
    1.30 +static void conn_read_cb(struct bufferevent *, void *);
    1.31 +static void conn_write_cb(struct bufferevent *, void *);
    1.32 +static void conn_event_cb(struct bufferevent *, short, void *);
    1.33 +static void signal_cb(evutil_socket_t, short, void *);
    1.34 +static void print_socket_info(int, int);
    1.35 +
    1.36 +int main(int argc, char **argv) {
    1.37 +	printf("%4s %8s\n", "*", "STARTED");
    1.38 +	struct event_base *base;
    1.39 +	evutil_socket_t listener;
    1.40 +	struct event *listener_event;
    1.41 +	struct event *signal_event;
    1.42 +
    1.43 +	struct sockaddr_un sun;
    1.44 +
    1.45 +	setvbuf(stdout, NULL, _IONBF, 0);
    1.46 +
    1.47 +	base = event_base_new();
    1.48 +	if (!base) {
    1.49 +		printf("%4s %8s %s\n", "*", "ERROR", "unable to initialize libevent");
    1.50 +		return 1;
    1.51 +	}
    1.52 +
    1.53 +	memset(&sun, 0, sizeof (sun));
    1.54 +	sun.sun_family = AF_UNIX;
    1.55 +	strcpy(sun.sun_path, PATH);
    1.56 +
    1.57 +	listener = socket(AF_UNIX, SOCK_STREAM, 0);
    1.58 +	evutil_make_socket_nonblocking(listener);
    1.59 +
    1.60 +	if (bind(listener, (struct sockaddr*) &sun, sizeof (sun)) < 0) {
    1.61 +		printf("%4s %8s %s: %s\n", "*", "ERROR", "unable to create domain socket:", PATH);
    1.62 +		return 1;
    1.63 +	}
    1.64 +
    1.65 +	if (listen(listener, 16) < 0) {
    1.66 +		printf("%4s %8s %s\n", "*", "ERROR", "unable to listen");
    1.67 +		return 1;
    1.68 +	}
    1.69 +
    1.70 +	// identifikátor serverového soketu (v současnosti číslo FD)
    1.71 +	printf("%4s %8s listening at: sun_path = %s → socketId = %d\n", "*", "SOCKET", sun.sun_path, listener);
    1.72 +
    1.73 +	listener_event = event_new(base, listener, EV_READ | EV_PERSIST, listener_cb, (void*) base);
    1.74 +
    1.75 +	if (!listener_event) {
    1.76 +		printf("%4s %8s %s\n", "*", "ERROR", "unable to do event_new()");
    1.77 +		return 1;
    1.78 +	}
    1.79 +
    1.80 +	event_add(listener_event, NULL);
    1.81 +
    1.82 +	signal_event = evsignal_new(base, SIGINT, signal_cb, (void *) base);
    1.83 +
    1.84 +	if (!signal_event || event_add(signal_event, NULL) < 0) {
    1.85 +		printf("%4s %8s %s\n", "*", "ERROR", "unable to create/add a signal event");
    1.86 +		return 1;
    1.87 +	}
    1.88 +
    1.89 +	event_base_dispatch(base);
    1.90 +
    1.91 +	event_free(listener_event);
    1.92 +	event_free(signal_event);
    1.93 +	event_base_free(base);
    1.94 +
    1.95 +	// smažeme soket na disku / soubor -- jinak by program příště spadl na bind()
    1.96 +	// TODO: co když soket někdo přesune a místo něj dá jiný soubor?
    1.97 +	unlink(PATH);
    1.98 +
    1.99 +	printf("%4s %8s\n", "*", "FINISHED");
   1.100 +	return 0;
   1.101 +}
   1.102 +
   1.103 +static void listener_cb(evutil_socket_t listener, short event, void *user_data) {
   1.104 +	struct event_base *base = (event_base *) user_data;
   1.105 +	struct bufferevent *bev;
   1.106 +
   1.107 +	struct sockaddr_storage ss;
   1.108 +	socklen_t slen = sizeof (ss);
   1.109 +	int fd = accept(listener, (struct sockaddr*) &ss, &slen);
   1.110 +
   1.111 +	// identifikátor navázaného spojení (v současnosti číslo FD)
   1.112 +	int * connectionId = (int*) malloc(sizeof (fd));
   1.113 +	*connectionId = fd;
   1.114 +
   1.115 +	if (fd < 0) {
   1.116 +		printf("%4d %8s %s\n", *connectionId, "ERROR", "unable to accept()");
   1.117 +		return;
   1.118 +	} else if (fd > FD_SETSIZE) {
   1.119 +		// FD_SETSIZE = 1024 -- Proč? Co když bude spojení víc?
   1.120 +		printf("%4d %8s fd (%d) > FD_SETSIZE (%d)\n", *connectionId, "ERROR", fd, FD_SETSIZE);
   1.121 +		close(fd);
   1.122 +		return;
   1.123 +	}
   1.124 +
   1.125 +	evutil_make_socket_nonblocking(fd);
   1.126 +
   1.127 +
   1.128 +	bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
   1.129 +	if (!bev) {
   1.130 +		printf("%4d %8s %s\n", *connectionId, "ERROR", "constructing bufferevent");
   1.131 +		event_base_loopbreak(base);
   1.132 +		return;
   1.133 +	}
   1.134 +
   1.135 +
   1.136 +
   1.137 +
   1.138 +	bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) connectionId);
   1.139 +	bufferevent_enable(bev, EV_READ | EV_WRITE);
   1.140 +
   1.141 +	printf("%4d %8s somebody has connected: socketId = %d → connectionId = %d\n", *connectionId, "CONN", listener, *connectionId);
   1.142 +	print_socket_info(*connectionId, fd);
   1.143 +
   1.144 +	bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
   1.145 +}
   1.146 +
   1.147 +static void conn_read_cb(struct bufferevent *bev, void *user_data) {
   1.148 +	int connectionId = *((int*) user_data);
   1.149 +
   1.150 +	/* This callback is invoked when there is data to read on bev. */
   1.151 +	struct evbuffer *input = bufferevent_get_input(bev);
   1.152 +	struct evbuffer *output = bufferevent_get_output(bev);
   1.153 +	struct event_base *base = bufferevent_get_base(bev);
   1.154 +
   1.155 +	size_t len = evbuffer_get_length(input);
   1.156 +	char *data = (char*) malloc(len);
   1.157 +	evbuffer_copyout(input, data, len);
   1.158 +
   1.159 +
   1.160 +	string dataFormated(data);
   1.161 +	dataFormated = regex_replace(dataFormated, regex("\\n"), "\\n");
   1.162 +	printf("%4d %8s '%s'\n", connectionId, "IN", dataFormated.c_str());
   1.163 +
   1.164 +	if (COMMAND_EXIT.compare(data) == 0) {
   1.165 +		struct timeval delay = {2, 123};
   1.166 +		printf("%4d %8s client asks us to terminate; finishing in %ld sesonds and %ld microseconds\n", connectionId, "EXIT", delay.tv_sec, delay.tv_usec);
   1.167 +		event_base_loopexit(base, &delay);
   1.168 +	}
   1.169 +
   1.170 +	evbuffer_add(output, "echo: ", 6);
   1.171 +	/* Copy all the data from the input buffer to the output buffer. */
   1.172 +	evbuffer_add_buffer(output, input);
   1.173 +	free(data);
   1.174 +}
   1.175 +
   1.176 +static void conn_write_cb(struct bufferevent *bev, void *user_data) {
   1.177 +	int connectionId = *((int*) user_data);
   1.178 +
   1.179 +	struct evbuffer *output = bufferevent_get_output(bev);
   1.180 +	if (evbuffer_get_length(output) == 0) {
   1.181 +		printf("%4d %8s\n", connectionId, "FLUSH");
   1.182 +		/* nebudeme ukončovat spojení
   1.183 +		bufferevent_free(bev);
   1.184 +		 */
   1.185 +	} else {
   1.186 +		// FIXME: sem to nikdy nepřijde
   1.187 +		size_t len = evbuffer_get_length(output);
   1.188 +		char *data = (char*) malloc(len);
   1.189 +		evbuffer_copyout(output, data, len);
   1.190 +		printf("%4d %8s '%s'\n", connectionId, "OUT", data);
   1.191 +		free(data);
   1.192 +	}
   1.193 +}
   1.194 +
   1.195 +static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) {
   1.196 +	int connectionId = *((int*) user_data);
   1.197 +
   1.198 +	if (events & BEV_EVENT_EOF) {
   1.199 +		printf("%4d %8s\n", connectionId, "CLOSE");
   1.200 +	} else if (events & BEV_EVENT_ERROR) {
   1.201 +		printf("%4d %8s %s\n", connectionId, "ERROR", strerror(errno));
   1.202 +	} else {
   1.203 +		printf("%4d %8s\n", connectionId, "OTHER");
   1.204 +		// None of the other events can happen here, since we haven't enabled timeouts
   1.205 +	}
   1.206 +
   1.207 +	bufferevent_free(bev);
   1.208 +	free(user_data);
   1.209 +}
   1.210 +
   1.211 +static void signal_cb(evutil_socket_t sig, short events, void *user_data) {
   1.212 +	struct event_base *base = (event_base *) user_data;
   1.213 +	struct timeval delay = {2, 123};
   1.214 +
   1.215 +	printf("\n%4s %8s got SIGINT (Ctrl+C); finishing in %ld sesonds and %ld microseconds\n", "*", "SIGNAL", delay.tv_sec, delay.tv_usec);
   1.216 +
   1.217 +	event_base_loopexit(base, &delay);
   1.218 +}
   1.219 +
   1.220 +static void print_socket_info(int connectionId, int fd) {
   1.221 +	struct ucred cr;
   1.222 +	unsigned int cl = sizeof (cr);
   1.223 +
   1.224 +	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) == 0) {
   1.225 +		printf("%4d %8s client identification: pid=%d, uid=%d, gid=%d\n", connectionId, "CONN", cr.pid, cr.uid, cr.gid);
   1.226 +	}
   1.227 +}
   1.228 \ No newline at end of file