]> granicus.if.org Git - libevent/commitdiff
http-server: add -H option to change listen address
authorAzat Khuzhin <azat@libevent.org>
Mon, 26 Oct 2020 22:22:59 +0000 (01:22 +0300)
committerAzat Khuzhin <azat@libevent.org>
Mon, 26 Oct 2020 22:23:33 +0000 (01:23 +0300)
sample/http-server.c

index cf92843790d7bb83d398f0d873c7243ad5ae60f3..b006891e19dafc397d8e3a02942635152049e6ad 100644 (file)
@@ -107,6 +107,7 @@ struct options {
 
        int unlink;
        const char *unixsock;
+       const char *bind;
        const char *docroot;
 };
 
@@ -351,6 +352,7 @@ print_usage(FILE *out, const char *prog, int exit_code)
                "Syntax: %s [ OPTS ] <docroot>\n"
                " -p      - port\n"
                " -U      - bind to unix socket\n"
+               " -H      - address to bind (default: 0.0.0.0)\n"
                " -u      - unlink unix socket before bind\n"
                " -I      - IOCP\n"
                " -m      - max body size\n"
@@ -365,7 +367,7 @@ parse_opts(int argc, char **argv)
 
        memset(&o, 0, sizeof(o));
 
-       while ((opt = getopt(argc, argv, "hp:U:m:uIv")) != -1) {
+       while ((opt = getopt(argc, argv, "hp:U:m:uIvH:")) != -1) {
                switch (opt) {
                        case 'p': o.port = atoi(optarg); break;
                        case 'U': o.unixsock = optarg; break;
@@ -373,6 +375,7 @@ parse_opts(int argc, char **argv)
                        case 'I': o.iocp = 1; break;
                        case 'm': o.max_body_size = atoi(optarg); break;
                        case 'v': ++o.verbose; break;
+                       case 'H': o.bind = optarg; break;
                        case 'h': print_usage(stdout, argv[0], 0); break;
                        default : fprintf(stderr, "Unknown option %c\n", opt); break;
                }
@@ -548,9 +551,9 @@ main(int argc, char **argv)
 #endif /* EVENT__HAVE_STRUCT_SOCKADDR_UN */
        }
        else {
-               handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port);
+               handle = evhttp_bind_socket_with_handle(http, o.bind, o.port);
                if (!handle) {
-                       fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port);
+                       fprintf(stderr, "couldn't bind to %s:%d. Exiting.\n", o.bind, o.port);
                        ret = 1;
                        goto err;
                }