static struct rxpd_base global_base;
struct rxpd_base*
-rxpd_init (char* rulesdir)
+rxpd_init (struct event_base* eventbase)
{
- if (global_base.rulesdir)
+ if (global_base.basedir)
return NULL;
- global_base.rulesdir = strdup (rulesdir);
- if (!global_base.rulesdir) abort();
+ global_base.basedir = NULL;
- psplay_init_root (&global_base.files, rxpd_file_cmp, rxpd_file_delete);
+ global_base.verbosity = LOG_WARNING;
+ global_base.daemonize = 0;
+ global_base.regflags = 0;
+ global_base.policy = NULL;
+
+ if (!eventbase)
+ rxpd_die ("no eventbase provided");
+ global_base.eventbase = eventbase;
+
+ psplay_init_root (&global_base.files, rxpd_file_cmp, rxpd_file_delete);
llist_init (&global_base.sockets);
return &global_base;
}
+
void
rxpd_destroy (void)
{
- if (global_base.rulesdir)
+ if (global_base.basedir)
{
- free (global_base.rulesdir);
+ free (global_base.basedir);
psplay_destroy_root (&global_base.files);
LLIST_WHILE_HEAD (&global_base.sockets, n)
{
}
}
+void
+rxpd_log (struct rxpd_base* self, int level, const char* fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+ if (level <= self->verbosity)
+ {
+ if (self->daemonize)
+ vsyslog (level, fmt, ap);
+ else
+ vfprintf (stderr, fmt, ap);
+ }
+ va_end (ap);
+}
+
+void
+rxpd_die (const char* fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vsyslog (LOG_EMERG, fmt, ap);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ abort ();
+}
+
+void*
+rxpd_malloc (size_t size)
+{
+ void* r;
+ r = malloc (size);
+ if (!r)
+ rxpd_die ("Out of Memeory\n");
+ return r;
+}
+
+char*
+rxpd_strdup (const char* str)
+{
+ char* r;
+ r = strdup (str);
+ if (!r)
+ rxpd_die ("Out of Memeory\n");
+ return r;
+}
//
struct rxpd_rule*
rxpd_rule_new (const char* buf)
{
- struct rxpd_rule* self = malloc (sizeof (struct rxpd_rule));
- if (self)
+ struct rxpd_rule* self = rxpd_malloc (sizeof (struct rxpd_rule));
+
+ llist_init (&self->node);
+
+ if (*buf != '#')
{
- llist_init (&self->node);
+ int err;
+ char* rxstart = strchr (buf, ':');
- if (*buf != '#')
+ if (!rxstart)
+ self->string = rxpd_strdup ("#ERROR: Syntax error, line was neither a comment nor a rule");
+ else
{
- int err;
- char* rxstart = strchr (buf, ':');
+ // TODO regflags from base
+ err = regcomp (&self->rx, rxstart+1, REG_EXTENDED|REG_ICASE|REG_NOSUB);
- if (!rxstart)
- self->string = strdup ("#ERROR: Syntax error, line was neither a comment nor a rule");
+ if (!err)
+ self->string = rxpd_strdup (buf);
else
{
- 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, "'");
- }
+ regfree (&self->rx);
+ char ebuf[256];
+ size_t len = regerror (err, NULL, ebuf, 256);
+ self->string = rxpd_malloc (len + strlen(buf) + 14);
+ strcpy (self->string, "#ERROR: ");
+ strcat (self->string, ebuf);
+ strcat (self->string, " in '");
+ strcat (self->string, buf);
+ strcat (self->string, "'");
}
}
- else
- {
- self->string = strdup (buf);
- if (!self->string) abort();
- }
}
+ else
+ self->string = rxpd_strdup (buf);
+
return self;
}
if (!filename ||
strcspn(filename, RXPD_FILE_ILG_CHARS) != strlen (filename) ||
- strlen (filename) + strlen (base->rulesdir) > 4095)
+ strlen (filename) + strlen (base->basedir) > 4095)
return NULL;
- strcpy (buf, base->rulesdir);
+ strcpy (buf, base->basedir);
strcat (buf, filename);
- filename = strdup (buf);
- if (filename)
- {
- self = malloc (sizeof (struct rxpd_file));
- if (self)
- {
- self->filename = filename;
- const char* basename = strrchr (filename, '/');
- if (basename)
- ++basename;
- else
- basename = filename;
- psplay_init (&self->node, basename);
- llist_init (&self->rules);
+ filename = rxpd_strdup (buf);
- psplay_insert (&base->files, &self->node);
- }
- }
+ self = rxpd_malloc (sizeof (struct rxpd_file));
+ 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);
return self;
}
struct rxpd_socket*
rxpd_socket_new_tcp4 (struct rxpd_base* base, const char* addr, unsigned short port)
{
- struct rxpd_socket* self = malloc (sizeof (struct rxpd_socket));
- if (!self)
- abort();
+ struct rxpd_socket* self;
+ self = rxpd_malloc (sizeof (struct rxpd_socket));
self->base = base;
void
rxpd_socket_accept (int fd, short event, void* ptr)
{
- printf ("incoming connection\n");
-
+ (void) event;
struct rxpd_socket* self = ptr;
struct rxpd_connection* conn =
struct rxpd_connection*
rxpd_connection_new (struct rxpd_base* base, int fd)
{
- struct rxpd_connection* self = malloc (sizeof (struct rxpd_connection));
- if (!self)
- abort();
+ struct rxpd_connection* self;
+ self = rxpd_malloc (sizeof (struct rxpd_connection));
socklen_t addr_sz = sizeof (self->peer_addr);
self->fd = accept (fd, (struct sockaddr*)&self->peer_addr, &addr_sz);
if (self->fd == -1)
abort ();
- static int yes = 1;
- if (setsockopt (self->fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
- abort ();
+ //static int yes = 1;
+ //if (setsockopt (self->fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
+ // abort ();
+
+ // TODO
+ printf ("incoming connection\n");
+
self->base = base;
self->file = NULL;
void
rxpd_connection_parse_cmd (int fd, short event, void* ptr)
{
- printf ("parse cmd\n");
+ (void) event;
+
+ //TODO printf ("parse cmd\n");
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
+
char* line;
line = rxpd_buffer_readline (&self->in, 0);
void
rxpd_connection_cmd_CHECK (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (event == EV_READ)
{
int again = -1;
char* line;
- while (line = rxpd_buffer_readline (&self->in, ++again))
+ while ((line = rxpd_buffer_readline (&self->in, ++again)))
{
if (*line == '\0')
{
static void
-rxpd_connection_APPEND_PREPEND_helper (int fd, short event, void* ptr, int do_append)
+rxpd_connection_APPEND_PREPEND_helper (short event, void* ptr, int do_append)
{
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
int again = -1;
char* line;
- while (line = rxpd_buffer_readline (&self->in, ++again))
+ while ((line = rxpd_buffer_readline (&self->in, ++again)))
{
if (*line)
{
void
rxpd_connection_cmd_APPEND (int fd, short event, void* ptr)
{
- rxpd_connection_APPEND_PREPEND_helper (fd, event, ptr, 1);
+ (void) fd;
+ rxpd_connection_APPEND_PREPEND_helper (event, ptr, 1);
}
void
rxpd_connection_cmd_PREPEND (int fd, short event, void* ptr)
{
- rxpd_connection_APPEND_PREPEND_helper (fd, event, ptr, 0);
+ (void) fd;
+ rxpd_connection_APPEND_PREPEND_helper (event, ptr, 0);
}
void
rxpd_connection_cmd_REMOVE (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (event == EV_READ)
{
int again = -1;
char* line;
- while (line = rxpd_buffer_readline (&self->in, ++again))
+ while ((line = rxpd_buffer_readline (&self->in, ++again)))
{
LLIST_FOREACH (&self->file->rules, n)
{
void
rxpd_connection_cmd_REPLACE (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (event == EV_READ)
{
int again = -1;
char* line;
- while (line = rxpd_buffer_readline (&self->in, ++again))
+ while ((line = rxpd_buffer_readline (&self->in, ++again)))
{
if (self->tmp_str)
{
/* TODO handle empty lines? */
}
else
- {
- self->tmp_str = strdup (line);
- if (!self->tmp_str) abort();
- }
+ self->tmp_str = rxpd_strdup (line);
}
}
else if (!self->file)
void
rxpd_connection_cmd_LOAD (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (!event)
void
rxpd_connection_cmd_SAVE (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (!event)
void
rxpd_connection_cmd_DUMP (int fd, short event, void* ptr)
{
+ (void) fd;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (!event && !self->file)
void
rxpd_connection_cmd_LIST (int fd, short event, void* ptr)
{
+ (void) fd;
+ (void) event;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
if (psplay_isempty_root (&self->base->files))
void
rxpd_connection_cmd_SHUTDOWN (int fd, short event, void* ptr)
{
+ (void) fd;
+ (void) event;
struct rxpd_connection* self = (struct rxpd_connection*) ptr;
// destroy all sockets
LLIST_WHILE_HEAD (&self->base->sockets, n)