summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/WELL1024a.c25
-rw-r--r--src/common/acl.c55
-rw-r--r--src/common/acl.h27
-rw-r--r--src/common/errors.c1
-rw-r--r--src/common/evsched.c14
-rw-r--r--src/common/fdset_kqueue.c2
-rw-r--r--src/common/libtap/tap.c60
-rw-r--r--src/common/libtap/tap.h21
8 files changed, 119 insertions, 86 deletions
diff --git a/src/common/WELL1024a.c b/src/common/WELL1024a.c
index dddf75e..0d1526b 100644
--- a/src/common/WELL1024a.c
+++ b/src/common/WELL1024a.c
@@ -10,6 +10,7 @@
#include <string.h>
#include <stdlib.h>
#include <time.h>
+#include <sys/time.h>
#include <stdio.h>
#include "WELL1024a.h"
@@ -87,11 +88,27 @@ double tls_rand()
/* Initialize seed from system PRNG generator. */
unsigned init[WELL1024_WIDTH];
FILE *fp = fopen("/dev/urandom", "r");
- for (unsigned i = 0; i < WELL1024_WIDTH; ++i) {
- int rc = fread(&init[i], sizeof(unsigned), 1, fp);
- rc = rc;
+ if (fp == NULL) {
+ fp = fopen("/dev/random", "r");
+ }
+ if (fp == NULL) {
+ fprintf(stderr, "error: PRNG: cannot seed from "
+ "/dev/urandom, seeding from local time\n");
+ struct timeval tv;
+ if (gettimeofday(&tv, NULL) == 0) {
+ memcpy(init, &tv, sizeof(struct timeval));
+ } else {
+ /* Last resort. */
+ time_t tm = time(NULL);
+ memcpy(init, &tm, sizeof(time_t));
+ }
+ } else {
+ for (unsigned i = 0; i < WELL1024_WIDTH; ++i) {
+ int rc = fread(&init[i], sizeof(unsigned), 1, fp);
+ rc = rc;
+ }
+ fclose(fp);
}
- fclose(fp);
/* Initialize PRNG state. */
s = InitWELLRNG1024a(init);
diff --git a/src/common/acl.c b/src/common/acl.c
index e73c4dd..a1c3a6e 100644
--- a/src/common/acl.c
+++ b/src/common/acl.c
@@ -62,18 +62,14 @@ static int acl_compare(void *k1, void *k2)
if (a1->len == sizeof(struct sockaddr_in6)) {
/* Compare address. */
- /*! \todo Maybe use memcmp()? */
- ldiff = 0;
- const unsigned int *a6 = (const unsigned int *)&a1->addr6.sin6_addr;
- const unsigned int *b6 = (const unsigned int *)&a2->addr6.sin6_addr;
- for (int i = 0; i < (sizeof(struct in6_addr)/ sizeof(int)) ; ++i) {
- ldiff = a6[i] - b6[i];
- if (ldiff < 0) {
- return -1;
- }
- if (ldiff > 0) {
- return 1;
- }
+ ldiff = memcmp(&a1->addr6.sin6_addr,
+ &a2->addr6.sin6_addr,
+ sizeof(struct in6_addr));
+ if (ldiff < 0) {
+ return -1;
+ }
+ if (ldiff > 0) {
+ return 1;
}
/* Port = 0 means any port match. */
@@ -138,38 +134,43 @@ void acl_delete(acl_t **acl)
*acl = 0;
}
-int acl_create(acl_t *acl, const sockaddr_t* addr, acl_rule_t rule)
+int acl_create(acl_t *acl, const sockaddr_t* addr, acl_rule_t rule, void *val)
{
- if (!acl || !addr || rule < 0) {
+ if (!acl || !addr) {
return ACL_ERROR;
}
/* Insert into skip list. */
- sockaddr_t *key = malloc(sizeof(sockaddr_t));
- memcpy(key, addr, sizeof(sockaddr_t));
+ acl_key_t *key = malloc(sizeof(acl_key_t));
+ memcpy(&key->addr, addr, sizeof(sockaddr_t));
+ sockaddr_update(&key->addr);
+ key->rule = rule;
+ key->val = val;
- skip_insert(acl->rules, key, (void*)((ssize_t)rule + 1), 0);
+ skip_insert(acl->rules, &key->addr, key, 0);
return ACL_ACCEPT;
}
-int acl_match(acl_t *acl, sockaddr_t* addr)
+int acl_match(acl_t *acl, const sockaddr_t* addr, acl_key_t **key)
{
if (!acl || !addr) {
return ACL_ERROR;
}
- /* Return default rule if not found.
- * Conversion to the same length integer is made,
- * but we can be sure, that the value range is within <-1,1>.
- */
- ssize_t val = ((ssize_t)skip_find(acl->rules, addr)) - 1;
- if (val < 0) {
+ acl_key_t *found = skip_find(acl->rules, (void*)addr);
+
+ /* Set stored value if exists. */
+ if (key != 0) {
+ *key = found;
+ }
+
+ /* Return appropriate rule. */
+ if (!found) {
return acl->default_rule;
}
- /* Return stored rule if found. */
- return (int)val;
+ return found->rule;
}
int acl_truncate(acl_t *acl)
@@ -179,7 +180,7 @@ int acl_truncate(acl_t *acl)
}
/* Destroy all rules. */
- skip_destroy_list(&acl->rules, free, 0);
+ skip_destroy_list(&acl->rules, 0, free);
return ACL_ACCEPT;
}
diff --git a/src/common/acl.h b/src/common/acl.h
index c79db7f..1b7e97f 100644
--- a/src/common/acl.h
+++ b/src/common/acl.h
@@ -42,11 +42,18 @@ typedef enum acl_rule_t {
/*! \brief ACL structure. */
typedef struct acl_t {
- acl_rule_t default_rule;
- skip_list_t *rules;
- const char name[];
+ acl_rule_t default_rule; /*!< \brief Default rule. */
+ skip_list_t *rules; /*!< \brief Data container. */
+ const char name[]; /*!< \brief ACL semantic name. */
} acl_t;
+/*! \brief Single ACL value. */
+typedef struct acl_key_t {
+ sockaddr_t addr; /*!< \brief Address for comparison. */
+ acl_rule_t rule; /*!< \brief Rule for address. */
+ void *val; /*!< \brief Associated value (or NULL). */
+} acl_key_t;
+
/*!
* \brief Create a new ACL.
*
@@ -71,25 +78,27 @@ void acl_delete(acl_t **acl);
* \todo Support address subnets.
*
* \param acl Pointer to ACL instance.
- * \param addr IP address (will be duplicated).
- * \param rule Rule.
+ * \param addr IP address.
+ * \param rule Rule for given address.
+ * \param val Value to be stored for given address (or NULL).
*
* \retval ACL_ACCEPT if successful.
* \retval ACP_ERROR on error.
*/
-int acl_create(acl_t *acl, const sockaddr_t* addr, acl_rule_t rule);
+int acl_create(acl_t *acl, const sockaddr_t* addr, acl_rule_t rule, void *val);
/*!
* \brief Match address against ACL.
*
* \param acl Pointer to ACL instance.
* \param addr IP address.
+ * \param key Set to related key or NULL if not found.
*
- * \retval ACL_ACCEPT if the address is accepted.
- * \retval ACL_DENY if the address is not accepted.
+ * \retval Address rule if the address is accepted.
+ * \retval Default rule if the address is not accepted.
* \retval ACP_ERROR on error.
*/
-int acl_match(acl_t *acl, sockaddr_t* addr);
+int acl_match(acl_t *acl, const sockaddr_t* addr, acl_key_t **key);
/*!
* \brief Truncate ACL.
diff --git a/src/common/errors.c b/src/common/errors.c
index f1e650d..3b770e9 100644
--- a/src/common/errors.c
+++ b/src/common/errors.c
@@ -64,6 +64,7 @@ int _map_errno(int fallback_value, int arg0, ...)
/* Error code matches with mapped. */
if (c == errno) {
/* Return negative value of the code. */
+ va_end(ap);
return -abs(c);
}
}
diff --git a/src/common/evsched.c b/src/common/evsched.c
index 4e56028..c4a6786 100644
--- a/src/common/evsched.c
+++ b/src/common/evsched.c
@@ -187,7 +187,7 @@ int evsched_event_finished(evsched_t *s)
int evsched_schedule(evsched_t *s, event_t *ev, uint32_t dt)
{
- if (!s || !ev || dt < 0) {
+ if (!s || !ev) {
return -1;
}
@@ -276,12 +276,12 @@ int evsched_cancel(evsched_t *s, event_t *ev)
return -1;
}
- /* Lock calendar. */
- pthread_mutex_lock(&s->mx);
-
/* Make sure not running. */
pthread_mutex_lock(&s->rl);
+ /* Lock calendar. */
+ pthread_mutex_lock(&s->mx);
+
/* Find in list. */
event_t *n = 0;
int found = 0;
@@ -297,13 +297,13 @@ int evsched_cancel(evsched_t *s, event_t *ev)
rem_node(&ev->n);
}
- /* Enable running events. */
- pthread_mutex_unlock(&s->rl);
-
/* Unlock calendar. */
pthread_cond_signal(&s->notify);
pthread_mutex_unlock(&s->mx);
+ /* Enable running events. */
+ pthread_mutex_unlock(&s->rl);
+
return 0;
}
diff --git a/src/common/fdset_kqueue.c b/src/common/fdset_kqueue.c
index c7199ae..b0daa33 100644
--- a/src/common/fdset_kqueue.c
+++ b/src/common/fdset_kqueue.c
@@ -14,8 +14,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <config.h>
-
#ifdef HAVE_KQUEUE
#include <stdint.h>
diff --git a/src/common/libtap/tap.c b/src/common/libtap/tap.c
index 61e0528..8b660fe 100644
--- a/src/common/libtap/tap.c
+++ b/src/common/libtap/tap.c
@@ -51,38 +51,41 @@ vstrdupf (const char *fmt, va_list args) {
}
int
-vok_at_loc (const char *file, int line, int test, const char *fmt,
+vok_at_loc (const char *file, int line, int test, int verbose, const char *fmt,
va_list args)
{
- char *name = vstrdupf(fmt, args);
- printf("%sok %d", test ? "" : "not ", ++current_test);
- if (*name)
- printf(" - %s", name);
- if (todo_mesg) {
- printf(" # TODO");
- if (*todo_mesg)
- printf(" %s", todo_mesg);
+ if (verbose) {
+ char *name = vstrdupf(fmt, args);
+ printf("%sok %d", test ? "" : "not ", ++current_test);
+ if (*name)
+ printf(" - %s", name);
+ if (todo_mesg) {
+ printf(" # TODO");
+ if (*todo_mesg)
+ printf(" %s", todo_mesg);
+ }
+ printf("\n");
+ if (!test) {
+ if (*name)
+ diag(" Failed%s test '%s'\n at %s line %d.",
+ todo_mesg ? " (TODO)" : "", name, file, line);
+ else
+ diag(" Failed%s test at %s line %d.",
+ todo_mesg ? " (TODO)" : "", file, line);
+ if (!todo_mesg)
+ failed_tests++;
+
+ free(name);
+ }
}
- printf("\n");
- if (!test) {
- if (*name)
- diag(" Failed%s test '%s'\n at %s line %d.",
- todo_mesg ? " (TODO)" : "", name, file, line);
- else
- diag(" Failed%s test at %s line %d.",
- todo_mesg ? " (TODO)" : "", file, line);
- if (!todo_mesg)
- failed_tests++;
- }
- free(name);
return test;
}
int
-ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
+ok_at_loc (const char *file, int line, int verbose, int test, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- vok_at_loc(file, line, test, fmt, args);
+ vok_at_loc(file, line, test, verbose, fmt, args);
va_end(args);
return test;
}
@@ -102,7 +105,7 @@ is_at_loc (const char *file, int line, const char *got, const char *expected,
int test = eq(got, expected);
va_list args;
va_start(args, fmt);
- vok_at_loc(file, line, test, fmt, args);
+ vok_at_loc(file, line, test, 1, fmt, args);
va_end(args);
if (!test) {
diag(" got: '%s'", got);
@@ -113,12 +116,13 @@ is_at_loc (const char *file, int line, const char *got, const char *expected,
int
isnt_at_loc (const char *file, int line, const char *got, const char *expected,
+ int verbose,
const char *fmt, ...)
{
int test = ne(got, expected);
va_list args;
va_start(args, fmt);
- vok_at_loc(file, line, test, fmt, args);
+ vok_at_loc(file, line, test, verbose, fmt, args);
va_end(args);
if (!test) {
diag(" got: '%s'", got);
@@ -152,7 +156,7 @@ cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
: diag("unrecognized operator '%s'", op);
va_list args;
va_start(args, fmt);
- vok_at_loc(file, line, test, fmt, args);
+ vok_at_loc(file, line, test, 1, fmt, args);
va_end(args);
if (!test) {
diag(" %d", a);
@@ -278,7 +282,7 @@ tap_test_died (int status) {
int
like_at_loc (int for_match, const char *file, int line, const char *got,
- const char *expected, const char *fmt, ...)
+ const char *expected, int verbose, const char *fmt, ...)
{
int test;
regex_t re;
@@ -295,7 +299,7 @@ like_at_loc (int for_match, const char *file, int line, const char *got,
test = for_match ? !err : err;
va_list args;
va_start(args, fmt);
- vok_at_loc(file, line, test, fmt, args);
+ vok_at_loc(file, line, test, verbose, fmt, args);
va_end(args);
if (!test) {
if (for_match) {
diff --git a/src/common/libtap/tap.h b/src/common/libtap/tap.h
index 2e89b90..4522fbb 100644
--- a/src/common/libtap/tap.h
+++ b/src/common/libtap/tap.h
@@ -22,17 +22,17 @@
#include <stdarg.h>
#define NO_PLAN -1
-#define ok(...) ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
+#define ok(...) ok_at_loc(__FILE__, __LINE__, 1, __VA_ARGS__, NULL)
#define pass(...) ok(1, ## __VA_ARGS__)
#define fail(...) ok(0, ## __VA_ARGS__)
#define is(...) is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define isnt(...) isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define cmp_ok(...) cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
-int vok_at_loc (const char *file, int line, int test, const char *fmt,
+int vok_at_loc (const char *file, int line, int test, int verbose, const char *fmt,
va_list args);
void plan (int tests);
-int ok_at_loc (const char *file, int line, int test, const char *fmt,
+int ok_at_loc (const char *file, int line, int test, int verbose, const char *fmt,
...);
int diag (const char *fmt, ...);
int note (const char *fmt, ...);
@@ -41,9 +41,10 @@ void skippy (int n, const char *fmt, ...);
void ctodo (int ignore, const char *fmt, ...);
void cendtodo (void);
int is_at_loc (const char *file, int line, const char *got,
- const char *expected, const char *fmt, ...);
+ const char *expected,
+ const char *fmt, ...);
int isnt_at_loc (const char *file, int line, const char *got,
- const char *expected, const char *fmt, ...);
+ const char *expected, int verbose, const char *fmt, ...);
int cmp_ok_at_loc (const char *file, int line, int a, const char *op,
int b, const char *fmt, ...);
@@ -55,6 +56,7 @@ int cmp_ok_at_loc (const char *file, int line, int a, const char *op,
#define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
int like_at_loc (int for_match, const char *file, int line,
const char *got, const char *expected,
+ int verbose,
const char *fmt, ...);
#endif
@@ -64,8 +66,9 @@ int like_at_loc (int for_match, const char *file, int line,
#define todo(...) ctodo(0, ## __VA_ARGS__, NULL)
#define endtodo cendtodo()
-#define dies_ok(code, ...) dies_ok_common(code, 1, ## __VA_ARGS__)
-#define lives_ok(code, ...) dies_ok_common(code, 0, ## __VA_ARGS__)
+#define dies_ok(code, ...) dies_ok_common(code, 1, 1, ## __VA_ARGS__)
+#define lives_ok(code, ...) dies_ok_common(code, 0, 1, ## __VA_ARGS__)
+#define lives_ok_silent(code, ...) dies_ok_common(code, 0, 0, ## __VA_ARGS__)
#ifdef _WIN32
#define dies_ok_common(...) \
@@ -75,7 +78,7 @@ int like_at_loc (int for_match, const char *file, int line,
#include <sys/types.h>
#include <sys/wait.h>
int tap_test_died (int status);
-#define dies_ok_common(code, for_death, ...) \
+#define dies_ok_common(code, for_death, verbose, ...) \
do { \
tap_test_died(1); \
int cpid = fork(); \
@@ -95,7 +98,7 @@ int tap_test_died (int status);
} \
int it_died = tap_test_died(0); \
if (!it_died) {code} \
- ok(for_death ? it_died : !it_died, ## __VA_ARGS__); \
+ ok_at_loc(__FILE__, __LINE__, verbose, for_death ? it_died : !it_died, ## __VA_ARGS__); \
} while (0)
#endif
#endif