summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorAzat Khuzhin <azat@libevent.org>2019-05-11 18:49:28 +0300
committerAzat Khuzhin <azat@libevent.org>2019-05-12 18:21:21 +0300
commit737d1beb14423dc212b0c53edf536b3172c87571 (patch)
tree4e3eaece7943cecfa9932c70e8f1a9b97d939cb4 /sample
parentda11217544727cda01a23db2198d24a591d993a3 (diff)
downloadlibevent-737d1beb14423dc212b0c53edf536b3172c87571.tar.gz
http-server: add ability to bind to unix-socket
Usage example: http-server -u -U /tmp/sock /tmp/no-such-dir curl -v --unix-socket /tmp/sock 127.1:8080/foo
Diffstat (limited to 'sample')
-rw-r--r--sample/http-server.c151
1 files changed, 108 insertions, 43 deletions
diff --git a/sample/http-server.c b/sample/http-server.c
index 75379968..dc42ee02 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 <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
-#endif
+#endif /* _WIN32 */
#include <signal.h>
+#ifdef EVENT__HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+#ifdef EVENT__HAVE_AFUNIX_H
+#include <afunix.h>
+#endif
+
#include <event2/event.h>
#include <event2/http.h>
+#include <event2/listener.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#ifdef _WIN32
#include <event2/thread.h>
-#endif
+#endif /* _WIN32 */
#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
@@ -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' */
@@ -340,9 +351,11 @@ parse_opts(int argc, char **argv)
memset(&o, 0, sizeof(o));
- while ((opt = getopt(argc, argv, "p:Iv")) != -1) {
+ while ((opt = getopt(argc, argv, "p: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;
default : fprintf(stderr, "Unknown option %c\n", opt); break;
@@ -365,6 +378,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 +436,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 +458,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 +495,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);