libevent: print_socket_info() -- vypíše pid, uid, gid
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 19 Nov 2016 23:29:47 +0100
changeset 386ec32ee08feb
parent 37 0a06481eec13
child 39 988b56d4a7b8
libevent: print_socket_info() -- vypíše pid, uid, gid
c++/domain-socket-bridge/domain-socket-bridge.c
     1.1 --- a/c++/domain-socket-bridge/domain-socket-bridge.c	Sat Nov 19 22:30:02 2016 +0100
     1.2 +++ b/c++/domain-socket-bridge/domain-socket-bridge.c	Sat Nov 19 23:29:47 2016 +0100
     1.3 @@ -9,6 +9,7 @@
     1.4  #include <event2/listener.h>
     1.5  #include <event2/util.h>
     1.6  #include <event2/event.h>
     1.7 +#include <sys/socket.h>
     1.8  #include <sys/un.h>
     1.9  #include <unistd.h>
    1.10  
    1.11 @@ -23,8 +24,7 @@
    1.12  static void conn_write_cb(struct bufferevent *, void *);
    1.13  static void conn_event_cb(struct bufferevent *, short, void *);
    1.14  static void signal_cb(evutil_socket_t, short, void *);
    1.15 -
    1.16 -static char rot13_char(char);
    1.17 +static void print_socket_info(int);
    1.18  
    1.19  int main(int argc, char **argv) {
    1.20  	struct event_base *base;
    1.21 @@ -99,11 +99,14 @@
    1.22  		fprintf(stderr, "Unable to accept(): %d", fd);
    1.23  		return;
    1.24  	} else if (fd > FD_SETSIZE) {
    1.25 +		// FD_SETSIZE = 1024 -- Proč? Co když bude spojení víc?
    1.26  		close(fd);
    1.27  		return;
    1.28  	}
    1.29  
    1.30  	evutil_make_socket_nonblocking(fd);
    1.31 +	
    1.32 +	print_socket_info(fd);
    1.33  
    1.34  	bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    1.35  	if (!bev) {
    1.36 @@ -159,7 +162,7 @@
    1.37  
    1.38  static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) {
    1.39  	printf("conn_event_cb: user_data = '%s'\n", (char*) user_data);
    1.40 -	
    1.41 +
    1.42  	if (events & BEV_EVENT_EOF) {
    1.43  		printf("Connection closed.\n");
    1.44  	} else if (events & BEV_EVENT_ERROR) {
    1.45 @@ -178,3 +181,12 @@
    1.46  
    1.47  	event_base_loopexit(base, &delay);
    1.48  }
    1.49 +
    1.50 +static void print_socket_info(int fd) {
    1.51 +	struct ucred cr;
    1.52 +	unsigned int cl = sizeof (cr);
    1.53 +
    1.54 +	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) == 0) {
    1.55 +		printf("připojený klient: pid=%d, uid=%d, gid=%d\n", cr.pid, cr.uid, cr.gid);
    1.56 +	}
    1.57 +}
    1.58 \ No newline at end of file