about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2024-07-10 18:58:44 +0200
committerLeah Neukirchen <leah@vuxu.org>2024-07-10 18:58:44 +0200
commitd2daffcdcd51ecd79e79c0c22f7936f037b637c1 (patch)
treebd71a6da57e5d063bb143d4e3731fa85cc9cdea0
parent653dc9cd6836d505abc1cf988b7e7186ff742f6d (diff)
downloadnano-exporter-d2daffcdcd51ecd79e79c0c22f7936f037b637c1.tar.gz
nano-exporter-d2daffcdcd51ecd79e79c0c22f7936f037b637c1.tar.xz
nano-exporter-d2daffcdcd51ecd79e79c0c22f7936f037b637c1.zip
support binding to a specific host address
-rw-r--r--main.c9
-rw-r--r--scrape.c4
-rw-r--r--scrape.h4
3 files changed, 12 insertions, 5 deletions
diff --git a/main.c b/main.c
index 7e37094..9e747c0 100644
--- a/main.c
+++ b/main.c
@@ -67,11 +67,13 @@ static const struct collector *collectors[] = {
 
 struct config {
   const char *port;
+  const char *host;
   bool print;
 };
 
 const struct config default_config = {
   .port = "9100",
+  .host = 0,
   .print = false,
 };
 
@@ -95,7 +97,7 @@ int main(int argc, char *argv[]) {
     return 0;
   }
 
-  scrape_server *server = scrape_listen(cfg.port);
+  scrape_server *server = scrape_listen(cfg.host, cfg.port);
   if (!server)
     return 1;
 
@@ -154,6 +156,11 @@ static bool initialize(int argc, char *argv[], struct config *cfg, struct collec
       goto next_arg;
     }
 
+    if (strncmp(argv[arg], "--host=", 7) == 0) {
+      cfg->host = &argv[arg][7];
+      goto next_arg;
+    }
+
     if (strncmp(argv[arg], "--stdout", 8) == 0) {
       cfg->print = true;
       goto next_arg;
diff --git a/scrape.c b/scrape.c
index f68f9b9..f753a87 100644
--- a/scrape.c
+++ b/scrape.c
@@ -89,7 +89,7 @@ static int timeout_next_millis(scrape_req *reqs);
 
 // TCP socket server
 
-scrape_server *scrape_listen(const char *port) {
+scrape_server *scrape_listen(const char *host, const char *port) {
   scrape_server *srv = must_malloc(sizeof *srv);
 
   srv->nfds_listen = 0;
@@ -110,7 +110,7 @@ scrape_server *scrape_listen(const char *port) {
     };
     struct addrinfo *addrs;
 
-    ret = getaddrinfo(0, port, &hints, &addrs);
+    ret = getaddrinfo(host, port, &hints, &addrs);
     if (ret != 0) {
       fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
       return false;
diff --git a/scrape.h b/scrape.h
index f163e52..df1d38a 100644
--- a/scrape.h
+++ b/scrape.h
@@ -34,8 +34,8 @@ struct collector {
   bool has_args;
 };
 
-/** Sets up a scrape server listening at the given port. */
-scrape_server *scrape_listen(const char *port);
+/** Sets up a scrape server listening at the given host:port. */
+scrape_server *scrape_listen(const char *host, const char *port);
 
 /** Enters a loop serving scrape requests of the provided collectors. */
 void scrape_serve(scrape_server *server, unsigned ncoll, const struct collector *coll[], void *coll_ctx[]);