summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2010-08-10 10:52:33 +0300
committerTimo Teräs <timo.teras@iki.fi>2010-08-10 10:52:33 +0300
commit9c29667d060e6f1d00a9ad7997c4b357db3aa2ac (patch)
tree2c457dbd29f1f081f854dea1591cac77c6a4c418
parentb5a5dd614101000f653e6ecb96ab34ae3f44353f (diff)
downloadsquark-9c29667d060e6f1d00a9ad7997c4b357db3aa2ac.tar.bz2
squark-9c29667d060e6f1d00a9ad7997c4b357db3aa2ac.tar.xz
squark: split generic blob code to it's own file
it's useful in other binaries than squark-auth too.
-rw-r--r--Makefile6
-rw-r--r--blob.c127
-rw-r--r--blob.h34
-rw-r--r--squark-auth.c140
4 files changed, 166 insertions, 141 deletions
diff --git a/Makefile b/Makefile
index 329c66a..d4b84a6 100644
--- a/Makefile
+++ b/Makefile
@@ -12,10 +12,10 @@ CFLAGS=-g -I. $(NETSNMP_CFLAGS) $(LUA_CFLAGS) $(CMPH_CFLAGS) -std=gnu99 -Wall
all: $(TARGETS)
-squark-auth: squark-auth.o
- $(CC) -o $@ $< $(NETSNMP_LIBS)
+squark-auth: squark-auth.o blob.o
+ $(CC) -o $@ $^ $(NETSNMP_LIBS)
-squarkdb.so: lua-squarkdb.o squarkdb.o
+squarkdb.so: lua-squarkdb.o squarkdb.o blob.o
$(CC) -shared -o $@ $^ $(LUA_LIBS) $(CMPH_LIBS)
clean:
diff --git a/blob.c b/blob.c
new file mode 100644
index 0000000..a25e5e8
--- /dev/null
+++ b/blob.c
@@ -0,0 +1,127 @@
+#include "blob.h"
+
+const blob_t BLOB_NULL = { NULL, 0 };
+
+char *blob_cstr_dup(blob_t b)
+{
+ char *p;
+
+ if (blob_is_null(b))
+ return NULL;
+
+ p = malloc(b.len+1);
+ if (p != NULL) {
+ memcpy(p, b.ptr, b.len);
+ p[b.len] = 0;
+ }
+ return p;
+}
+
+blob_t blob_dup(blob_t b)
+{
+ blob_t p;
+
+ if (blob_is_null(b))
+ return BLOB_NULL;
+
+ p.ptr = malloc(b.len);
+ if (p.ptr != NULL) {
+ memcpy(p.ptr, b.ptr, b.len);
+ p.len = b.len;
+ } else {
+ p.len = 0;
+ }
+ return p;
+}
+
+int blob_cmp(blob_t a, blob_t b)
+{
+ if (a.len != b.len)
+ return a.len - b.len;
+ return memcmp(a.ptr, b.ptr, a.len);
+}
+
+blob_t blob_pushed(blob_t buffer, blob_t left)
+{
+ if (buffer.ptr + buffer.len != left.ptr + left.len)
+ return BLOB_NULL;
+ return blob_dyn(buffer.ptr, left.ptr - buffer.ptr);
+}
+
+void blob_push(blob_t *b, blob_t d)
+{
+ if (b->len >= d.len) {
+ memcpy(b->ptr, d.ptr, d.len);
+ b->ptr += d.len;
+ b->len -= d.len;
+ } else {
+ *b = BLOB_NULL;
+ }
+}
+
+void blob_push_int_str(blob_t *b, int val)
+{
+ int l;
+
+ l = snprintf(b->ptr, b->len, "%d", val);
+ b->ptr += l;
+ b->len -= l;
+}
+
+void blob_push_hexdump(blob_t *to, blob_t binary)
+{
+ static const char *xd = "0123456789abcdef";
+ char *d;
+ int i;
+
+ if (blob_is_null(*to))
+ return;
+
+ if (to->len < binary.len * 2) {
+ *to = BLOB_NULL;
+ return;
+ }
+
+ for (i = 0, d = to->ptr; i < binary.len; i++) {
+ *(d++) = xd[(binary.ptr[i] >> 4) & 0xf];
+ *(d++) = xd[binary.ptr[i] & 0xf];
+ }
+ to->ptr = d;
+ to->len -= binary.len * 2;
+}
+
+blob_t blob_pull(blob_t *b, int len)
+{
+ blob_t r;
+
+ if (b->len >= len) {
+ r = blob_dyn(b->ptr, len);
+ b->ptr += len;
+ b->len -= len;
+ return r;
+ }
+ *b = BLOB_NULL;
+ return BLOB_NULL;
+}
+
+void blob_pull_skip(blob_t *b, int len)
+{
+ if (b->len >= len) {
+ b->ptr += len;
+ b->len -= len;
+ } else {
+ *b = BLOB_NULL;
+ }
+}
+
+int blob_pull_matching(blob_t *b, blob_t e)
+{
+ if (b->len < e.len)
+ return 0;
+ if (memcmp(b->ptr, e.ptr, e.len) != 0)
+ return 0;
+ b->ptr += e.len;
+ b->len -= e.len;
+ return 1;
+}
+
diff --git a/blob.h b/blob.h
new file mode 100644
index 0000000..ad242a8
--- /dev/null
+++ b/blob.h
@@ -0,0 +1,34 @@
+#ifndef BLOB_H
+#define BLOB_H
+
+#include <string.h>
+
+typedef struct blob {
+ char *ptr;
+ unsigned int len;
+} blob_t;
+
+#define blob_dyn(ptr,len) (blob_t){(void*)(ptr), (len)}
+#define blob_buf(buf) (blob_t){(void*)(buf), sizeof(buf)}
+#define blob_str(str) (blob_t){(char*)(str), strlen(str)}
+
+extern const blob_t BLOB_NULL;;
+
+static inline int blob_is_null(blob_t b)
+{
+ return b.ptr == NULL;
+}
+
+char *blob_cstr_dup(blob_t b);
+blob_t blob_dup(blob_t b);
+int blob_cmp(blob_t a, blob_t b);
+blob_t blob_pushed(blob_t buffer, blob_t left);
+void blob_push(blob_t *b, blob_t d);
+void blob_push_int_str(blob_t *b, int val);
+void blob_push_hexdump(blob_t *to, blob_t binary);
+blob_t blob_pull(blob_t *b, int len);
+void blob_pull_skip(blob_t *b, int len);
+int blob_pull_matching(blob_t *b, blob_t e);
+
+
+#endif
diff --git a/squark-auth.c b/squark-auth.c
index 88f4d61..db792e7 100644
--- a/squark-auth.c
+++ b/squark-auth.c
@@ -27,6 +27,8 @@
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
+#include "blob.h"
+
/* Compile time configurables */
#define SWITCH_HASH_SIZE 128
#define PORT_HASH_SIZE 128
@@ -100,146 +102,8 @@ static int username_format_flags;
/* ----------------------------------------------------------------- */
-typedef struct blob {
- char *ptr;
- unsigned int len;
-} blob_t;
-
-#define blob_dyn(ptr,len) (blob_t){(void*)(ptr), (len)}
-#define blob_buf(buf) (blob_t){(void*)(buf), sizeof(buf)}
#define blob_oid(objid) blob_buf(objid)
#define blob_oid_dyn(objid,len) blob_dyn(objid, (len) * sizeof(oid))
-#define blob_str(str) (blob_t){(char*)(str), strlen(str)}
-
-const blob_t BLOB_NULL = { NULL, 0 };
-
-static inline int blob_is_null(blob_t b)
-{
- return b.ptr == NULL;
-}
-
-static char *blob_cstr_dup(blob_t b)
-{
- char *p;
-
- if (blob_is_null(b))
- return NULL;
-
- p = malloc(b.len+1);
- if (p != NULL) {
- memcpy(p, b.ptr, b.len);
- p[b.len] = 0;
- }
- return p;
-}
-
-static blob_t blob_dup(blob_t b)
-{
- blob_t p;
-
- if (blob_is_null(b))
- return BLOB_NULL;
-
- p.ptr = malloc(b.len);
- if (p.ptr != NULL) {
- memcpy(p.ptr, b.ptr, b.len);
- p.len = b.len;
- } else {
- p.len = 0;
- }
- return p;
-}
-
-static int blob_cmp(blob_t a, blob_t b)
-{
- if (a.len != b.len)
- return a.len - b.len;
- return memcmp(a.ptr, b.ptr, a.len);
-}
-
-static blob_t blob_pushed(blob_t buffer, blob_t left)
-{
- if (buffer.ptr + buffer.len != left.ptr + left.len)
- return BLOB_NULL;
- return blob_dyn(buffer.ptr, left.ptr - buffer.ptr);
-}
-
-static inline void blob_push(blob_t *b, blob_t d)
-{
- if (b->len >= d.len) {
- memcpy(b->ptr, d.ptr, d.len);
- b->ptr += d.len;
- b->len -= d.len;
- } else {
- *b = BLOB_NULL;
- }
-}
-
-static void blob_push_int_str(blob_t *b, int val)
-{
- int l;
-
- l = snprintf(b->ptr, b->len, "%d", val);
- b->ptr += l;
- b->len -= l;
-}
-
-static void blob_push_hexdump(blob_t *to, blob_t binary)
-{
- static const char *xd = "0123456789abcdef";
- char *d;
- int i;
-
- if (blob_is_null(*to))
- return;
-
- if (to->len < binary.len * 2) {
- *to = BLOB_NULL;
- return;
- }
-
- for (i = 0, d = to->ptr; i < binary.len; i++) {
- *(d++) = xd[(binary.ptr[i] >> 4) & 0xf];
- *(d++) = xd[binary.ptr[i] & 0xf];
- }
- to->ptr = d;
- to->len -= binary.len * 2;
-}
-
-static inline blob_t blob_pull(blob_t *b, int len)
-{
- blob_t r;
-
- if (b->len >= len) {
- r = blob_dyn(b->ptr, len);
- b->ptr += len;
- b->len -= len;
- return r;
- }
- *b = BLOB_NULL;
- return BLOB_NULL;
-}
-
-static inline void blob_pull_skip(blob_t *b, int len)
-{
- if (b->len >= len) {
- b->ptr += len;
- b->len -= len;
- } else {
- *b = BLOB_NULL;
- }
-}
-
-static inline int blob_pull_matching(blob_t *b, blob_t e)
-{
- if (b->len < e.len)
- return 0;
- if (memcmp(b->ptr, e.ptr, e.len) != 0)
- return 0;
- b->ptr += e.len;
- b->len -= e.len;
- return 1;
-}
static inline void blob_push_oid(blob_t *b, oid objid)
{