APPEND/PREPEND commands
[rxpd] / rxpd.c
diff --git a/rxpd.c b/rxpd.c
index 4c587d9..2fe7b77 100644 (file)
--- a/rxpd.c
+++ b/rxpd.c
@@ -85,27 +85,32 @@ rxpd_rule_new (const char* buf)
       if (*buf != '#')
         {
           int err;
-          char* rxstart = strchr (buf, ':') + 1;
+          char* rxstart = strchr (buf, ':');
 
-          err = regcomp (&self->rx, rxstart, REG_EXTENDED|REG_ICASE|REG_NOSUB);
-
-          if (!err)
-            {
-              self->string = strdup (buf);
-              if (!self->string) abort();
-            }
+          if (!rxstart)
+            self->string = strdup ("#ERROR: Syntax error, line was neither a comment nor a rule");
           else
             {
-              regfree (&self->rx);
-              char ebuf[256];
-              size_t len = regerror (err, NULL, ebuf, 256);
-              self->string = malloc(len + strlen(buf) + 14);
-              if (!self->string) abort();
-              strcpy (self->string, "#ERROR ");
-              strcat (self->string, ebuf);
-              strcat (self->string, " in '");
-              strcat (self->string, buf);
-              strcat (self->string, "'");
+              err = regcomp (&self->rx, rxstart+1, REG_EXTENDED|REG_ICASE|REG_NOSUB);
+
+              if (!err)
+                {
+                  self->string = strdup (buf);
+                  if (!self->string) abort();
+                }
+              else
+                {
+                  regfree (&self->rx);
+                  char ebuf[256];
+                  size_t len = regerror (err, NULL, ebuf, 256);
+                  self->string = malloc(len + strlen(buf) + 14);
+                  if (!self->string) abort();
+                  strcpy (self->string, "#ERROR: ");
+                  strcat (self->string, ebuf);
+                  strcat (self->string, " in '");
+                  strcat (self->string, buf);
+                  strcat (self->string, "'");
+                }
             }
         }
       else
@@ -153,7 +158,13 @@ rxpd_file_new (struct rxpd_base* base, const char* filename)
       self = malloc (sizeof (struct rxpd_file));
       if (self)
         {
-          psplay_init (&self->node, filename);
+          self->filename = filename;
+          const char* basename = strrchr (filename, '/');
+          if (basename)
+            ++basename;
+          else
+            basename = filename;
+          psplay_init (&self->node, basename);
           llist_init (&self->rules);
 
           psplay_insert (&base->files, &self->node);
@@ -173,7 +184,7 @@ rxpd_file_delete (PSplay f)
           struct rxpd_rule* node = (struct rxpd_rule*)n;
           rxpd_rule_delete (node);
         }
-      free ((void*)file->node.key);
+      free ((void*)file->filename);
       free (f);
     }
 }
@@ -181,9 +192,7 @@ rxpd_file_delete (PSplay f)
 int
 rxpd_file_load (struct rxpd_file* self)
 {
-  const char* filename = self->node.key;
-
-  FILE* f = fopen (filename, "r");
+  FILE* f = fopen (self->filename, "r");
   // TODO error handling
   if (f)
     {
@@ -201,7 +210,7 @@ rxpd_file_load (struct rxpd_file* self)
           if (!rule)
             abort();
 
-          printf("%s\n", rule->string);
+          printf("loaded rule '%s'\n", rule->string);
 
           llist_insert_tail (&self->rules, &rule->node);
         }
@@ -241,8 +250,14 @@ rxpd_socket_new_tcp4 (struct rxpd_base* base, const char* addr, unsigned short p
   memset (&listen_addr, 0, sizeof (listen_addr));
 
   listen_addr.sin_family = AF_INET;
-  if (inet_aton (addr?addr:"0.0.0.0", &listen_addr.sin_addr) == 0)
-    abort();
+  if (addr)
+    {
+      if (inet_aton (addr, &listen_addr.sin_addr) == 0)
+        abort();
+    }
+  else
+    listen_addr.sin_addr.s_addr = INADDR_ANY;
+
   listen_addr.sin_port = htons(port);
 
   if (bind (self->fd, (struct sockaddr*)&listen_addr, sizeof (listen_addr)) == -1)
@@ -274,7 +289,7 @@ rxpd_socket_delete (struct rxpd_socket* self)
 }
 
 struct rxpd_socket*
-rxpd_socket_activate (struct rxpd_socket* self)
+rxpd_socket_schedule (struct rxpd_socket* self)
 {
   if (self)
     {
@@ -305,8 +320,8 @@ rxpd_socket_accept (int fd, short event, void* ptr)
   struct rxpd_connection* conn =
     rxpd_connection_new (self->base, fd);
 
-  rxpd_connection_activate (conn);
-  rxpd_socket_activate (self);
+  rxpd_connection_schedule (conn);
+  rxpd_socket_schedule (self);
 }
 
 ///
@@ -327,34 +342,38 @@ rxpd_buffer_readline (struct rxpd_buffer* self, int again)
 {
   int fd = self->conn->fd;
 
-  if (self->eol != self->buffer)
+  if (self->eol < self->eob)
     {
-      //there was a line pending, discard it now
+      //there was a line pending, shift buffer left
       memmove (self->buffer, self->eol+1, self->eob - self->eol - 1);
       self->eob = (char*)(self->eob - (self->eol - self->buffer + 1));
       self->eol = self->buffer;
       // TODO handle \r's
     }
 
+
   if (!again && self->state == RXPD_OK)   // we only read when again is 0, first iteration
     {
       ssize_t r = 0;
       do
         {
-          r = read(fd, self->eob, 4096 - (self->eob - self->buffer));
+          r = read(fd, self->eob, 4095 - (self->eob - self->buffer));
         }
       while (r == -1 && errno == EINTR);
 
-      if (r == -1)
-        self->state = RXPD_ERROR;
-
-      if (r == 0)
+      if (r != -1)
         {
-          shutdown (fd, SHUT_RD);
-          self->state = RXPD_EOF;
-        }
 
-      self->eob += r;
+          if (r == 0)
+            {
+              shutdown (fd, SHUT_RD);
+              self->state = RXPD_EOF;
+            }
+
+          self->eob += r;
+        }
+      else
+        self->state = RXPD_ERROR;
     }
 
   // find next newline, terminate string there
@@ -370,7 +389,7 @@ rxpd_buffer_readline (struct rxpd_buffer* self, int again)
 
   // TODO handle buffer overfulls
 
-  return self->eol == self->buffer ? NULL : self->buffer;
+  return (self->eob == self->buffer) ? NULL : self->buffer;
 }
 
 /*
@@ -452,7 +471,7 @@ rxpd_connection_delete (struct rxpd_connection* self)
 }
 
 struct rxpd_connection*
-rxpd_connection_activate (struct rxpd_connection* self)
+rxpd_connection_schedule (struct rxpd_connection* self)
 {
   if (self)
     {
@@ -480,7 +499,7 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
 
   char* line;
-  while (!(line = rxpd_buffer_readline (&self->in, 0)) && (rxpd_buffer_state (&self->in) == RXPD_OK));
+  line = rxpd_buffer_readline (&self->in, 0);
 
   if (!line)
     {
@@ -508,11 +527,29 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
       break;
   if (!i->cmd)
     {
-      printf ("no command\n");
+      rxpd_buffer_printf (&self->out, "#ERROR: no command\n");
       rxpd_connection_delete (self);
       return;
     }
-  // TODO policy check
+  // TODO policy check here
+
+  if (line[i->sz])
+    {
+      // rulename provided
+      self->file = (struct rxpd_file*) psplay_find (&self->base->files, &line[i->sz]);
+
+      if (!self->file)
+        {
+          // todo create policy?
+          self->file = rxpd_file_new (self->base, &line[i->sz]);
+          if (!self->file)
+            {
+              rxpd_buffer_printf (&self->out, "#ERROR: illegal rule\n");
+              rxpd_connection_delete (self);
+              return;
+            }
+        }
+    }
 
   // dispatch
   switch (i->nr)
@@ -520,32 +557,119 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
 #define RXPD_CMD(cmd)                                                           \
 case RXPD_CMD_##cmd:                                                            \
   event_set (&self->ev, self->fd, EV_READ, rxpd_connection_cmd_##cmd, self);    \
+  rxpd_connection_cmd_##cmd (fd, 0, ptr);                                       \
   break;
       RXPD_COMMANDS
 #undef RXPD_CMD
     }
-  rxpd_connection_activate (self);
 }
 
+
+
 void
 rxpd_connection_cmd_CHECK (int fd, short event, void* ptr)
 {
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+
+  if (event == EV_READ)
+    {
+      int again = -1;
+      char* line;
+      while (line = rxpd_buffer_readline (&self->in, ++again))
+        {
+          if (*line == '\0')
+            {
+              rxpd_buffer_printf (&self->out, "#OK:\n");
+            }
+          else
+            {
+              LLIST_FOREACH (&self->file->rules, n)
+                {
+                  struct rxpd_rule* rule = (struct rxpd_rule*)n;
+                  if (rule->string[0] != '#')
+                    {
+                      if (regexec (&rule->rx, line, 0, NULL, 0) == 0)
+                        {
+                          rxpd_buffer_printf (&self->out, "%s\n", rule->string);
+                          break;
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+  if (rxpd_buffer_state (&self->in) == RXPD_OK)
+    rxpd_connection_schedule (self);
+  else
+    {
+      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+        rxpd_buffer_printf (&self->out, "#ERROR:\n");
+      close (fd);
+    }
+}
+
+
+
+static void
+rxpd_connection_APPEND_PREPEND_helper (int fd, short event, void* ptr, int do_append)
+{
+  struct rxpd_connection* self = (struct rxpd_connection*) ptr;
+
+  if (!event)
+    llist_init (&self->tmp_list);
+
+  if (event == EV_READ)
+    {
+      int again = -1;
+      char* line;
+
+      while (line = rxpd_buffer_readline (&self->in, ++again))
+        {
+          if (*line)
+            {
+              struct rxpd_rule* rule;
+              rule = rxpd_rule_new (line);
+              if (!rule)
+                abort();
+
+              llist_insert_tail (&self->tmp_list, &rule->node);
+            }
+          else goto finish;
+        }
+    }
+
+  if (rxpd_buffer_state (&self->in) == RXPD_OK)
+    rxpd_connection_schedule (self);
+  else
+    {
+      // TODO should also print error when any rule compilation failed
+      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+        rxpd_buffer_printf (&self->out, "#ERROR:\n");
+      else
+        {
+        finish:
+          rxpd_buffer_printf (&self->out, "#OK:\n");
+        }
+
+      if (do_append)
+        llist_insertlist_prev (&self->file->rules, &self->tmp_list);
+      else
+        llist_insertlist_next (&self->file->rules, &self->tmp_list);
+      close (fd);
+    }
 }
 
 void
 rxpd_connection_cmd_APPEND (int fd, short event, void* ptr)
 {
-  struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+  rxpd_connection_APPEND_PREPEND_helper (fd, event, ptr, 1);
 }
 
 void
 rxpd_connection_cmd_PREPEND (int fd, short event, void* ptr)
 {
-  struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+  rxpd_connection_APPEND_PREPEND_helper (fd, event, ptr, 0);
 }
 
 void
@@ -580,20 +704,65 @@ void
 rxpd_connection_cmd_DUMP (int fd, short event, void* ptr)
 {
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+
+  if (llist_is_empty (&self->file->rules))
+    rxpd_buffer_printf (&self->out, "#OK:\n");
+  else
+    {
+      LLIST_FOREACH (&self->file->rules, n)
+        {
+          struct rxpd_rule* rule = (struct rxpd_rule*)n;
+          rxpd_buffer_printf (&self->out, "%s\n", rule->string);
+        }
+    }
+
+  close (fd);
+}
+
+
+static psplay_delete_t
+walk_LIST (PSplay node, const enum psplay_order_e which, int level, void* data)
+{
+  (void) level;
+  struct rxpd_file* file = (struct rxpd_file*) node;
+  struct rxpd_connection* conn = (struct rxpd_connection*) data;
+
+  if (which == PSPLAY_INORDER)
+    rxpd_buffer_printf (&conn->out, "%s\n", file->node.key);
+
+  return PSPLAY_CONT;
 }
 
+
 void
 rxpd_connection_cmd_LIST (int fd, short event, void* ptr)
 {
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
+
+  if (psplay_isempty_root (&self->base->files))
+    rxpd_buffer_printf (&self->out, "#OK:\n");
+  else
+    psplay_walk (&self->base->files, NULL, walk_LIST, 0, ptr);
+
+  close (fd);
 }
 
 void
 rxpd_connection_cmd_SHUTDOWN (int fd, short event, void* ptr)
 {
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
-  rxpd_buffer_printf (&self->out, "#ERROR: unimplemented command %s\n", &__func__[20]);
-}
+  // destroy all sockets
+  LLIST_WHILE_HEAD (&self->base->sockets_pending, n)
+    {
+      struct rxpd_socket* socket = (struct rxpd_socket*)n;
+      rxpd_socket_delete (socket);
+    }
+  LLIST_WHILE_HEAD (&self->base->sockets_active, n)
+    {
+      struct rxpd_socket* socket = (struct rxpd_socket*)n;
+      rxpd_socket_delete (socket);
+    }
 
+  rxpd_buffer_printf (&self->out, "#OK:\n");
+  close (fd);
+}