From 7c4da93715dc2d70ab5338887ab35f93a95a541a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 12 May 2019 19:06:43 +0300 Subject: [PATCH] Merge branch 'issue-807-accept4-getnameinfo-AF_UNIX' * issue-807-accept4-getnameinfo-AF_UNIX: http-server: add usage/help dialog http: avoid use of uninitialized value for AF_UNIX/AF_LOCAL sockaddr http-server: add ability to bind to unix-socket build: struct sockaddr_un detection (sys/un.h, afunix.h) Fixes: #807 (cherry picked from commit 76eded24d3b0c3fc48c5a888906cc9043223101b) --- CMakeLists.txt | 13 ++++ configure.ac | 6 +- event-config.h.cmake | 9 +++ http.c | 18 ++++- sample/http-server.c | 166 +++++++++++++++++++++++++++++++------------ 5 files changed, 164 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc8ea3fb..4a9c6a8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -357,6 +357,19 @@ if(EVENT__HAVE_NETINET_IN_H) list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in.h) endif() +CHECK_INCLUDE_FILE(sys/un.h EVENT__HAVE_SYS_UN_H) +if(EVENT__HAVE_SYS_UN_H) + list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/un.h) +endif() + +if(WIN32) + CHECK_INCLUDE_FILE(afunix.h EVENT__HAVE_AFUNIX_H) + if(EVENT__HAVE_AFUNIX_H) + list(APPEND CMAKE_EXTRA_INCLUDE_FILES afunix.h) + endif() +endif() +CHECK_TYPE_SIZE("struct sockaddr_un" EVENT__HAVE_STRUCT_SOCKADDR_UN) + CHECK_INCLUDE_FILE(netinet/in6.h EVENT__HAVE_NETINET_IN6_H) if(EVENT__HAVE_NETINET_IN6_H) list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in6.h) diff --git a/configure.ac b/configure.ac index f3be20c7..fbebcf12 100644 --- a/configure.ac +++ b/configure.ac @@ -230,6 +230,7 @@ AC_CHECK_HEADERS([ \ netinet/in.h \ netinet/in6.h \ netinet/tcp.h \ + sys/un.h \ poll.h \ port.h \ stdarg.h \ @@ -666,7 +667,7 @@ AC_CHECK_SIZEOF(void *) AC_CHECK_SIZEOF(off_t) AC_CHECK_SIZEOF(time_t) -AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, sa_family_t, struct addrinfo, struct sockaddr_storage], , , +AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, struct sockaddr_un, sa_family_t, struct addrinfo, struct sockaddr_storage], , , [#define _GNU_SOURCE 1 #include #ifdef HAVE_NETINET_IN_H @@ -675,6 +676,9 @@ AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, sa_family_t, struct addrin #ifdef HAVE_NETINET_IN6_H #include #endif +#ifdef HAVE_SYS_UN_H +#include +#endif #ifdef HAVE_SYS_SOCKET_H #include #endif diff --git a/event-config.h.cmake b/event-config.h.cmake index ee0df54b..498ab1ea 100644 --- a/event-config.h.cmake +++ b/event-config.h.cmake @@ -202,6 +202,12 @@ /* Define to 1 if you have the header file. */ #cmakedefine EVENT__HAVE_NETINET_TCP_H 1 +/* Define to 1 if you have the header file. */ +#cmakedefine EVENT__HAVE_SYS_UN_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine EVENT__HAVE_AFUNIX_H 1 + /* Define if the system has openssl */ #cmakedefine EVENT__HAVE_OPENSSL 1 @@ -307,6 +313,9 @@ /* Define to 1 if `sin_len' is member of `struct sockaddr_in'. */ #cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 1 +/* Define to 1 if the system has the type `struct sockaddr_un'. */ +#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_UN 1 + /* Define to 1 if the system has the type `struct sockaddr_storage'. */ #cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_STORAGE 1 diff --git a/http.c b/http.c index 5bc4a24c..dbef7fc7 100644 --- a/http.c +++ b/http.c @@ -51,9 +51,16 @@ #ifndef _WIN32 #include #include -#else +#else /* _WIN32 */ #include #include +#endif /* _WIN32 */ + +#ifdef EVENT__HAVE_SYS_UN_H +#include +#endif +#ifdef EVENT__HAVE_AFUNIX_H +#include #endif #include @@ -78,7 +85,7 @@ #include #ifndef _WIN32 #include -#endif +#endif /* !_WIN32 */ #include #ifdef EVENT__HAVE_UNISTD_H #include @@ -4210,6 +4217,13 @@ evhttp_get_request_connection( char *hostname = NULL, *portname = NULL; struct bufferevent* bev = NULL; +#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN + if (sa->sa_family == AF_UNIX) { + struct sockaddr_un *sun = (struct sockaddr_un *)sa; + sun->sun_path[0] = '\0'; + } +#endif + name_from_addr(sa, salen, &hostname, &portname); if (hostname == NULL || portname == NULL) { if (hostname) mm_free(hostname); diff --git a/sample/http-server.c b/sample/http-server.c index 75379968..cedb2af8 100644 --- a/sample/http-server.c +++ b/sample/http-server.c @@ -26,24 +26,32 @@ #ifndef S_ISDIR #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR) #endif -#else +#else /* !_WIN32 */ #include #include #include #include #include -#endif +#endif /* _WIN32 */ #include +#ifdef EVENT__HAVE_SYS_UN_H +#include +#endif +#ifdef EVENT__HAVE_AFUNIX_H +#include +#endif + #include #include +#include #include #include #include #ifdef _WIN32 #include -#endif +#endif /* _WIN32 */ #ifdef EVENT__HAVE_NETINET_IN_H #include @@ -68,7 +76,7 @@ #ifndef O_RDONLY #define O_RDONLY _O_RDONLY #endif -#endif +#endif /* _WIN32 */ char uri_root[512]; @@ -96,6 +104,9 @@ struct options int port; int iocp; int verbose; + + int unlink; + const char *unixsock; }; /* Try to guess a good content-type for 'path' */ @@ -332,6 +343,17 @@ done: evbuffer_free(evb); } +static void +print_usage(FILE *out, const char *prog, int exit_code) +{ + fprintf(out, "Syntax: [ OPTS ] %s \n", prog); + fprintf(out, " -p - port\n"); + fprintf(out, " -U - bind to unix socket\n"); + fprintf(out, " -u - unlink unix socket before bind\n"); + fprintf(out, " -I - IOCP\n"); + fprintf(out, " -v - verbosity, enables libevent debug logging too\n"); + exit(exit_code); +} static struct options parse_opts(int argc, char **argv) { @@ -340,18 +362,20 @@ parse_opts(int argc, char **argv) memset(&o, 0, sizeof(o)); - while ((opt = getopt(argc, argv, "p:Iv")) != -1) { + while ((opt = getopt(argc, argv, "hp:U:uIv")) != -1) { switch (opt) { case 'p': o.port = atoi(optarg); break; + case 'U': o.unixsock = optarg; break; + case 'u': o.unlink = 1; break; case 'I': o.iocp = 1; break; case 'v': ++o.verbose; break; + case 'h': print_usage(stdout, argv[0], 0); break; default : fprintf(stderr, "Unknown option %c\n", opt); break; } } if (optind >= argc || (argc-optind) > 1) { - fprintf(stdout, "Syntax: %s \n", argv[0]); - exit(1); + print_usage(stdout, argv[0], 1); } return o; @@ -365,6 +389,57 @@ do_term(int sig, short events, void *arg) fprintf(stderr, "Got %i, Terminating\n", sig); } +static int +display_listen_sock(struct evhttp_bound_socket *handle) +{ + struct sockaddr_storage ss; + evutil_socket_t fd; + ev_socklen_t socklen = sizeof(ss); + char addrbuf[128]; + void *inaddr; + const char *addr; + int got_port = -1; + + fd = evhttp_bound_socket_get_fd(handle); + memset(&ss, 0, sizeof(ss)); + if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) { + perror("getsockname() failed"); + return 1; + } + + if (ss.ss_family == AF_INET) { + got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port); + inaddr = &((struct sockaddr_in*)&ss)->sin_addr; + } else if (ss.ss_family == AF_INET6) { + got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port); + inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr; + } +#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN + else if (ss.ss_family == AF_UNIX) { + printf("Listening on <%s>\n", ((struct sockaddr_un*)&ss)->sun_path); + return 0; + } +#endif + else { + fprintf(stderr, "Weird address family %d\n", + ss.ss_family); + return 1; + } + + addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf, + sizeof(addrbuf)); + if (addr) { + printf("Listening on %s:%d\n", addr, got_port); + evutil_snprintf(uri_root, sizeof(uri_root), + "http://%s:%d",addr,got_port); + } else { + fprintf(stderr, "evutil_inet_ntop failed\n"); + return 1; + } + + return 0; +} + int main(int argc, char **argv) { @@ -372,6 +447,7 @@ main(int argc, char **argv) struct event_base *base = NULL; struct evhttp *http = NULL; struct evhttp_bound_socket *handle = NULL; + struct evconnlistener *lev = NULL; struct event *term = NULL; struct options o = parse_opts(argc, argv); int ret = 0; @@ -393,7 +469,7 @@ main(int argc, char **argv) setbuf(stdout, NULL); setbuf(stderr, NULL); - /** Read env like in regress" */ + /** Read env like in regress */ if (o.verbose || getenv("EVENT_DEBUG_LOGGING_ALL")) event_enable_debug_logging(EVENT_DBG_ALL); @@ -430,52 +506,52 @@ main(int argc, char **argv) * cb. We can also add callbacks for specific paths. */ evhttp_set_gencb(http, send_document_cb, argv[1]); - /* Now we tell the evhttp what port to listen on */ - handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port); - if (!handle) { - fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port); - ret = 1; - goto err; - } + if (o.unixsock) { +#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN + struct sockaddr_un addr; - { - /* Extract and display the address we're listening on. */ - struct sockaddr_storage ss; - evutil_socket_t fd; - ev_socklen_t socklen = sizeof(ss); - char addrbuf[128]; - void *inaddr; - const char *addr; - int got_port = -1; - fd = evhttp_bound_socket_get_fd(handle); - memset(&ss, 0, sizeof(ss)); - if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) { - perror("getsockname() failed"); + if (o.unlink && (unlink(o.unixsock) && errno != ENOENT)) { + perror(o.unixsock); ret = 1; + goto err; } - if (ss.ss_family == AF_INET) { - got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port); - inaddr = &((struct sockaddr_in*)&ss)->sin_addr; - } else if (ss.ss_family == AF_INET6) { - got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port); - inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr; - } else { - fprintf(stderr, "Weird address family %d\n", - ss.ss_family); + + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, o.unixsock); + + lev = evconnlistener_new_bind(base, NULL, NULL, + LEV_OPT_CLOSE_ON_FREE, -1, + (struct sockaddr *)&addr, sizeof(addr)); + if (!lev) { + perror("Cannot create listener"); ret = 1; goto err; } - addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf, - sizeof(addrbuf)); - if (addr) { - printf("Listening on %s:%d\n", addr, got_port); - evutil_snprintf(uri_root, sizeof(uri_root), - "http://%s:%d",addr,got_port); - } else { - fprintf(stderr, "evutil_inet_ntop failed\n"); + + handle = evhttp_bind_listener(http, lev); + if (!handle) { + fprintf(stderr, "couldn't bind to %s. Exiting.\n", o.unixsock); ret = 1; goto err; } +#else /* !EVENT__HAVE_STRUCT_SOCKADDR_UN */ + fprintf(stderr, "-U is not supported on this platform. Exiting.\n"); + ret = 1; + goto err; +#endif /* EVENT__HAVE_STRUCT_SOCKADDR_UN */ + } + else { + handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port); + if (!handle) { + fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port); + ret = 1; + goto err; + } + } + + if (display_listen_sock(handle)) { + ret = 1; + goto err; } term = evsignal_new(base, SIGINT, do_term, base); -- 2.40.0