From 819393ad18c504343370598ecc496835f9bd7d66 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Mon, 29 Oct 2007 19:48:18 +0100 Subject: [PATCH] cleaned up some code, removed/improved few comments, better logging --- src/main.c | 8 ++++++-- src/rxpd_buffer.c | 4 +--- src/rxpd_connection.c | 12 +++--------- src/rxpd_connection_cmd.c | 7 +------ src/rxpd_file.c | 5 ----- src/rxpd_socket.c | 18 +++++++++++++----- 6 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/main.c b/src/main.c index a1a740a..27aabf3 100644 --- a/src/main.c +++ b/src/main.c @@ -190,8 +190,12 @@ main (int argc, char** argv) rxpd_fail (rxpd, "Failed loading file '%s'\n", argv[i]); } - if (rxpd->daemonize && daemon(1, 0)) - rxpd_fail (rxpd, "Couldn't daemonize\n"); + if (rxpd->daemonize) + { + rxpd_log (rxpd, LOG_NOTICE, "Daemonizing ...\n"); + if (daemon(1, 0)) + rxpd_fail (rxpd, "Couldn't daemonize\n"); + } rxpd_log (rxpd, LOG_NOTICE, PACKAGE_STRING" starting up\n"); diff --git a/src/rxpd_buffer.c b/src/rxpd_buffer.c index 17a1e51..cb58484 100644 --- a/src/rxpd_buffer.c +++ b/src/rxpd_buffer.c @@ -44,7 +44,6 @@ rxpd_buffer_readline (struct rxpd_buffer* self) // TODO handle \r's } - do { // find next newline, terminate string there for (char* i = self->buffer; i < self->eob; ++i) @@ -88,7 +87,6 @@ rxpd_buffer_readline (struct rxpd_buffer* self) int rxpd_buffer_printf (struct rxpd_buffer* self, const char* fmt, ...) { - // for now we do a blocking write, needs to be fixed some day, timeout! va_list ap; va_start(ap, fmt); int n = vsnprintf (self->buffer, 4096, fmt, ap); @@ -96,7 +94,7 @@ rxpd_buffer_printf (struct rxpd_buffer* self, const char* fmt, ...) pth_write (self->fd, self->buffer, n); - if (n>4095) + if (n > 4095) return 0; return 1; diff --git a/src/rxpd_connection.c b/src/rxpd_connection.c index fa49115..51558bd 100644 --- a/src/rxpd_connection.c +++ b/src/rxpd_connection.c @@ -35,11 +35,9 @@ rxpd_connection_new (struct rxpd_socket* socket, int fd) rxpd_buffer_init (&self->in, fd); rxpd_buffer_init (&self->out, fd); - + self->connecter = NULL; - // TODO more info - rxpd_log (socket->base, LOG_INFO, "incoming connection\n"); return self; } @@ -94,8 +92,6 @@ rxpd_connection_check_policy (struct rxpd_connection* self, char* line) return 0; } - rxpd_log (base, LOG_DEBUG, "policy check '%s'\n", buf); - char* match = NULL; LLIST_FOREACH (&base->policy->rules, n) { @@ -112,9 +108,10 @@ rxpd_connection_check_policy (struct rxpd_connection* self, char* line) if (!match || !RXPD_PREFIXCMP (match, "ACCEPT:")) { - rxpd_log (base, LOG_WARNING, "access denied '%s'\n", buf); + rxpd_log (base, LOG_WARNING, "policy check: access denied '%s'\n", buf); return 0; } + rxpd_log (base, LOG_NOTICE, "policy check: permitted '%s'\n", buf); } return 1; } @@ -189,9 +186,6 @@ rxpd_connection_parse_cmd (void* ptr) } } - - //TODO memleak have to call connection_destroy instead just returning - // dispatch switch (i->nr) { diff --git a/src/rxpd_connection_cmd.c b/src/rxpd_connection_cmd.c index 3ef7c92..faf1be8 100644 --- a/src/rxpd_connection_cmd.c +++ b/src/rxpd_connection_cmd.c @@ -82,9 +82,6 @@ rxpd_connection_APPEND_PREPEND_helper (struct rxpd_connection* self) { struct rxpd_rule* rule; rule = rxpd_rule_new (line); - if (!rule) - abort(); - llist_insert_tail (&self->tmp_list, &rule->node); } else @@ -269,7 +266,6 @@ rxpd_connection_cmd_FETCH (struct rxpd_connection* self) if (*line) { /* parse line */ - char* list = strrchr (line, ':'); if (!list) goto esyntax; @@ -417,7 +413,6 @@ rxpd_connection_cmd_VERSION (struct rxpd_connection* self) void rxpd_connection_cmd_HELP (struct rxpd_connection* self) { - //struct rxpd_base* base = self->socket->base; rxpd_buffer_printf (&self->out, "# Available commands:\n#\n"); #define RXPD_CMD(cmd, help) rxpd_buffer_printf (&self->out, "# %s %s.\n", #cmd, help); RXPD_COMMANDS @@ -511,7 +506,7 @@ rxpd_connection_cmd_UPDATE (struct rxpd_connection* self) /* Now we go for each rule in source which has a atime find the matching rule in self->file and update its atime */ /* This algorihm assumes that lists stay ordered (insertions/deletions are supported, but not reordering) */ - /* advantage is that the cost is O(N+M) instead O(N*M) */ + /* advantage is that the algorithm has far less complexity */ LLIST_FORRANGE (source_itr, &source->rules, n) { struct rxpd_rule* rule_n = (struct rxpd_rule*)n; diff --git a/src/rxpd_file.c b/src/rxpd_file.c index daa6435..ca6155f 100644 --- a/src/rxpd_file.c +++ b/src/rxpd_file.c @@ -90,7 +90,6 @@ int rxpd_file_load (struct rxpd_file* self) { FILE* f = fopen (self->filename, "r"); - // TODO error handling if (f) { pth_rwlock_acquire (&self->lock, PTH_RWLOCK_RW, FALSE, NULL); @@ -115,9 +114,6 @@ rxpd_file_load (struct rxpd_file* self) struct rxpd_rule* rule; rule = rxpd_rule_new (buf); - if (!rule) - abort(); - rxpd_log (self->base, LOG_DEBUG, "new rule '%s'\n", rule->string); llist_insert_tail (&self->rules, &rule->node); @@ -150,7 +146,6 @@ rxpd_file_save (struct rxpd_file* self) } FILE* f = fopen (filename, "w"); - // TODO error handling if (f) { pth_rwlock_acquire (&self->lock, PTH_RWLOCK_RD, FALSE, NULL); diff --git a/src/rxpd_socket.c b/src/rxpd_socket.c index a1885bb..4f81960 100644 --- a/src/rxpd_socket.c +++ b/src/rxpd_socket.c @@ -31,10 +31,9 @@ rxpd_socket_new_tcp4 (struct rxpd_base* base, const char* addr, unsigned short p llist_init (&self->node); - // TODO all abort() shall become rxpd_die self->fd = socket (PF_INET, SOCK_STREAM, 0); if (self->fd == -1) - abort (); + goto esocket; struct sockaddr_in listen_addr; memset (&listen_addr, 0, sizeof (listen_addr)); @@ -69,7 +68,8 @@ rxpd_socket_new_tcp4 (struct rxpd_base* base, const char* addr, unsigned short p return self; esocket: - free (self); + rxpd_die ("failed creating socket\n"); + /* free (self); rxpd_die never returns */ return NULL; } @@ -147,11 +147,19 @@ rxpd_socket_accept (void* ptr) rxpd_die ("error accepting connection\n"); struct rxpd_connection* conn = rxpd_connection_new (self, fd); - rxpd_connection_spawn (conn); + + char buf[512]; + + if (rxpd_socket_tcp4addr (conn, buf, "", 511)) + { + rxpd_log (self->base, LOG_INFO, "incoming connection '%s'\n", buf); + rxpd_connection_spawn (conn); + } + else + rxpd_log (self->base, LOG_ERR, "illegal hostname\n", buf); } rxpd_log (NULL, LOG_NOTICE, "closed\n"); - return NULL; } -- 2.11.4.GIT