WIP: FETCH, first part
authorChristian Thaeter <ct@pipapo.org>
Sun, 21 Oct 2007 13:17:53 +0000 (15:17 +0200)
committerChristian Thaeter <ct@pipapo.org>
Sun, 21 Oct 2007 13:17:53 +0000 (15:17 +0200)
src/rxpd.h
src/rxpd_connection_cmd.c

index 4ccf228..ac3e39d 100644 (file)
@@ -39,6 +39,7 @@
 #include <arpa/inet.h>
 #include <event.h>
 #include <time.h>
+#include <netdb.h>
 
 #define RXPD_COMMANDS                                           \
   RXPD_CMD(CHECK,       "data against regular expressions")     \
@@ -49,6 +50,7 @@
   RXPD_CMD(DELETE,      "a list from memory")                   \
   RXPD_CMD(LOAD,        "a list from disk")                     \
   RXPD_CMD(SAVE,        "a list to disk")                       \
+  RXPD_CMD(FETCH,       "a list from a remote server")          \
   RXPD_CMD(EXPIRE,      "aged rules from a list")               \
   RXPD_CMD(DUMP,        "rules in a list")                      \
   RXPD_CMD(LIST,        "all existing lists")                   \
index 9008ec2..af14b38 100644 (file)
@@ -331,6 +331,70 @@ rxpd_connection_cmd_DELETE (int fd, short event, void* ptr)
     }
 }
 
+void
+rxpd_connection_cmd_FETCH (int fd, short event, void* ptr)
+{
+  (void) fd;
+  struct rxpd_connection* self = (struct rxpd_connection*) ptr;
+  struct rxpd_base* base = self->socket->base;
+
+  if (event == EV_READ)
+    {
+      char* line;
+      line = rxpd_buffer_readline (&self->in, 0);
+      if (*line)
+        {
+          rxpd_file_rules_delete (self->file);
+
+          char* list = strrchr (line, '/');
+          // TODO error handling
+          if (!list)
+            rxpd_die ("syntax error");
+
+          *list = '\0';
+          ++ list;
+
+          char* port = strrchr (line, ':');
+          // TODO error handling / unix domain sockets
+          if (!port)
+            rxpd_die ("syntax error");
+
+          *port = '\0';
+          ++ port;
+
+          struct addrinfo* addrs = NULL;
+          int aierr;
+
+          aierr = getaddrinfo (line, port, NULL, &addrs);
+
+          rxpd_log (base, LOG_INFO, "fetching list '%s' from '%s(%s)' at port '%s' to '%s'\n",
+                    list,
+                    line,
+                    inet_ntoa (((struct sockaddr_in*)addrs->ai_addr)->sin_addr),
+                    port,
+                    self->file->node.key);
+
+          if (aierr)
+            rxpd_die ("resolv error %s\n", gai_strerror (aierr));
+
+          // connect
+          // send dump
+          // add recieving event
+
+          freeaddrinfo (addrs);
+        }
+    }
+  else if (!self->file)
+    {
+      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
+      rxpd_connection_delete (self);
+      return;
+    }
+
+  if (!event && rxpd_buffer_state (&self->in) == RXPD_OK)
+    rxpd_connection_schedule (self);
+}
+
 void
 rxpd_connection_cmd_DUMP (int fd, short event, void* ptr)
 {