c++/domain-socket-bridge/domain-socket-bridge.c
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Nov 2016 19:58:52 +0100
changeset 33 6f68b5e695df
parent 32 0eaa8735a083
child 34 117e8ff77326
permissions -rw-r--r--
libevent: možnost ukončení zasláním příkazu "exit"
franta-hg@28
     1
#include <string.h>
franta-hg@28
     2
#include <errno.h>
franta-hg@28
     3
#include <stdio.h>
franta-hg@28
     4
#include <signal.h>
franta-hg@30
     5
#include <stdlib.h>
franta-hg@28
     6
franta-hg@28
     7
#include <event2/bufferevent.h>
franta-hg@28
     8
#include <event2/buffer.h>
franta-hg@28
     9
#include <event2/listener.h>
franta-hg@28
    10
#include <event2/util.h>
franta-hg@28
    11
#include <event2/event.h>
franta-hg@29
    12
#include <sys/un.h>
franta-hg@29
    13
#include <unistd.h>
franta-hg@28
    14
franta-hg@30
    15
#define MAX_LINE 16384
franta-hg@30
    16
franta-hg@28
    17
static const char MESSAGE[] = "Hello, World!\n";
franta-hg@28
    18
franta-hg@29
    19
static const char PATH[] = "./roura";
franta-hg@28
    20
franta-hg@31
    21
static void listener_cb(evutil_socket_t, short, void *);
franta-hg@30
    22
static void conn_read_cb(struct bufferevent *, void *);
franta-hg@28
    23
static void conn_write_cb(struct bufferevent *, void *);
franta-hg@28
    24
static void conn_event_cb(struct bufferevent *, short, void *);
franta-hg@28
    25
static void signal_cb(evutil_socket_t, short, void *);
franta-hg@28
    26
franta-hg@31
    27
static char rot13_char(char);
franta-hg@31
    28
franta-hg@28
    29
int main(int argc, char **argv) {
franta-hg@28
    30
	struct event_base *base;
franta-hg@31
    31
	evutil_socket_t listener;
franta-hg@31
    32
	struct event *listener_event;
franta-hg@28
    33
	struct event *signal_event;
franta-hg@28
    34
franta-hg@29
    35
	struct sockaddr_un sun;
franta-hg@28
    36
franta-hg@31
    37
	setvbuf(stdout, NULL, _IONBF, 0);
franta-hg@31
    38
franta-hg@28
    39
	base = event_base_new();
franta-hg@28
    40
	if (!base) {
franta-hg@28
    41
		fprintf(stderr, "Could not initialize libevent!\n");
franta-hg@28
    42
		return 1;
franta-hg@28
    43
	}
franta-hg@28
    44
franta-hg@29
    45
	memset(&sun, 0, sizeof (sun));
franta-hg@29
    46
	sun.sun_family = AF_UNIX;
franta-hg@29
    47
	strcpy(sun.sun_path, PATH);
franta-hg@28
    48
franta-hg@31
    49
	listener = socket(AF_UNIX, SOCK_STREAM, 0);
franta-hg@31
    50
	evutil_make_socket_nonblocking(listener);
franta-hg@28
    51
franta-hg@31
    52
	if (bind(listener, (struct sockaddr*) &sun, sizeof (sun)) < 0) {
franta-hg@29
    53
		fprintf(stderr, "Could not create domain socket: %s!\n", PATH);
franta-hg@28
    54
		return 1;
franta-hg@28
    55
	}
franta-hg@28
    56
franta-hg@31
    57
	if (listen(listener, 16) < 0) {
franta-hg@31
    58
		fprintf(stderr, "Could not listen\n");
franta-hg@31
    59
		return 1;
franta-hg@31
    60
	}
franta-hg@31
    61
franta-hg@31
    62
	listener_event = event_new(base, listener, EV_READ | EV_PERSIST, listener_cb, (void*) base);
franta-hg@31
    63
franta-hg@31
    64
	if (!listener_event) {
franta-hg@31
    65
		fprintf(stderr, "Could not do event_new\n");
franta-hg@31
    66
		return 1;
franta-hg@31
    67
	}
franta-hg@31
    68
franta-hg@31
    69
	event_add(listener_event, NULL);
franta-hg@31
    70
franta-hg@28
    71
	signal_event = evsignal_new(base, SIGINT, signal_cb, (void *) base);
franta-hg@28
    72
franta-hg@28
    73
	if (!signal_event || event_add(signal_event, NULL) < 0) {
franta-hg@28
    74
		fprintf(stderr, "Could not create/add a signal event!\n");
franta-hg@28
    75
		return 1;
franta-hg@28
    76
	}
franta-hg@28
    77
franta-hg@28
    78
	event_base_dispatch(base);
franta-hg@28
    79
franta-hg@31
    80
	event_free(listener_event);
franta-hg@28
    81
	event_free(signal_event);
franta-hg@28
    82
	event_base_free(base);
franta-hg@30
    83
franta-hg@32
    84
	// smažeme soket na disku / soubor -- jinak by program příště spadl na bind()
franta-hg@29
    85
	unlink(PATH);
franta-hg@28
    86
franta-hg@28
    87
	printf("done\n");
franta-hg@28
    88
	return 0;
franta-hg@28
    89
}
franta-hg@28
    90
franta-hg@31
    91
static void listener_cb(evutil_socket_t listener, short event, void *user_data) {
franta-hg@28
    92
	struct event_base *base = (event_base *) user_data;
franta-hg@28
    93
	struct bufferevent *bev;
franta-hg@28
    94
franta-hg@31
    95
	struct sockaddr_storage ss;
franta-hg@31
    96
	socklen_t slen = sizeof (ss);
franta-hg@31
    97
	int fd = accept(listener, (struct sockaddr*) &ss, &slen);
franta-hg@31
    98
	if (fd < 0) {
franta-hg@31
    99
		fprintf(stderr, "Unable to accept(): %d", fd);
franta-hg@31
   100
		return;
franta-hg@31
   101
	} else if (fd > FD_SETSIZE) {
franta-hg@31
   102
		close(fd);
franta-hg@31
   103
		return;
franta-hg@31
   104
	}
franta-hg@31
   105
franta-hg@31
   106
	evutil_make_socket_nonblocking(fd);
franta-hg@31
   107
franta-hg@31
   108
	bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
franta-hg@28
   109
	if (!bev) {
franta-hg@28
   110
		fprintf(stderr, "Error constructing bufferevent!");
franta-hg@28
   111
		event_base_loopbreak(base);
franta-hg@28
   112
		return;
franta-hg@28
   113
	}
franta-hg@30
   114
	bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, NULL);
franta-hg@30
   115
	bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE);
franta-hg@30
   116
	bufferevent_enable(bev, EV_READ | EV_WRITE);
franta-hg@28
   117
franta-hg@28
   118
	printf("někdo se k nám připojil! / %p\n", bev);
franta-hg@28
   119
franta-hg@28
   120
	bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
franta-hg@28
   121
}
franta-hg@28
   122
franta-hg@30
   123
static void conn_read_cb(struct bufferevent *bev, void *user_data) {
franta-hg@30
   124
	/* This callback is invoked when there is data to read on bev. */
franta-hg@30
   125
	struct evbuffer *input = bufferevent_get_input(bev);
franta-hg@30
   126
	struct evbuffer *output = bufferevent_get_output(bev);
franta-hg@33
   127
	struct event_base *base = bufferevent_get_base(bev);
franta-hg@33
   128
	;
franta-hg@30
   129
franta-hg@30
   130
	size_t len = evbuffer_get_length(input);
franta-hg@32
   131
	char *data = (char*) malloc(len);
franta-hg@30
   132
	evbuffer_copyout(input, data, len);
franta-hg@32
   133
	printf("we got some data: %s\n", data);
franta-hg@31
   134
franta-hg@30
   135
	if (memcmp(data, "exit\n", len) == 0) {
franta-hg@30
   136
		struct timeval delay = {2, 123};
franta-hg@33
   137
		printf("Klient říká, že máme končit; ukončuji program během %ld sekund a %ld mikrosekund.\n", delay.tv_sec, delay.tv_usec);
franta-hg@30
   138
		event_base_loopexit(base, &delay);
franta-hg@30
   139
	}
franta-hg@31
   140
franta-hg@30
   141
franta-hg@32
   142
	evbuffer_add(output, "echo: ", 6);
franta-hg@30
   143
	/* Copy all the data from the input buffer to the output buffer. */
franta-hg@30
   144
	evbuffer_add_buffer(output, input);
franta-hg@30
   145
	free(data);
franta-hg@30
   146
}
franta-hg@30
   147
franta-hg@28
   148
static void conn_write_cb(struct bufferevent *bev, void *user_data) {
franta-hg@28
   149
	struct evbuffer *output = bufferevent_get_output(bev);
franta-hg@28
   150
	if (evbuffer_get_length(output) == 0) {
franta-hg@28
   151
		printf("flushed answer / %p\n", bev);
franta-hg@31
   152
		/* nebudeme ukončovat spojení
franta-hg@28
   153
		bufferevent_free(bev);
franta-hg@31
   154
		 */
franta-hg@28
   155
	}
franta-hg@28
   156
}
franta-hg@28
   157
franta-hg@28
   158
static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) {
franta-hg@28
   159
	if (events & BEV_EVENT_EOF) {
franta-hg@28
   160
		printf("Connection closed.\n");
franta-hg@28
   161
	} else if (events & BEV_EVENT_ERROR) {
franta-hg@30
   162
		printf("Got an error on the connection: %s\n", strerror(errno));
franta-hg@28
   163
	}
franta-hg@29
   164
franta-hg@28
   165
	// None of the other events can happen here, since we haven't enabled timeouts
franta-hg@28
   166
	bufferevent_free(bev);
franta-hg@28
   167
}
franta-hg@28
   168
franta-hg@28
   169
static void signal_cb(evutil_socket_t sig, short events, void *user_data) {
franta-hg@28
   170
	struct event_base *base = (event_base *) user_data;
franta-hg@28
   171
	struct timeval delay = {2, 123};
franta-hg@28
   172
franta-hg@28
   173
	printf("Zachycen SIGINT (Ctrl+C); ukončuji program během %ld sekund a %ld mikrosekund.\n", delay.tv_sec, delay.tv_usec);
franta-hg@28
   174
franta-hg@28
   175
	event_base_loopexit(base, &delay);
franta-hg@28
   176
}