summaryrefslogtreecommitdiff
path: root/lib/examples
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2010-09-13 17:27:40 +0200
committerLinus Nordberg <linus@nordu.net>2010-09-13 17:27:40 +0200
commit23dfea91ef5e921da0b09d91004436a7d8715c42 (patch)
tree3ef5ba7681bbe664efb51064f29adc5cec2778b7 /lib/examples
parent8455dc9801730e599510c92cdb3e05da351aa7a5 (diff)
First stab at a working blocking example.
Diffstat (limited to 'lib/examples')
-rw-r--r--lib/examples/Makefile9
-rw-r--r--lib/examples/blocking.c69
-rw-r--r--lib/examples/blocking.h4
3 files changed, 82 insertions, 0 deletions
diff --git a/lib/examples/Makefile b/lib/examples/Makefile
new file mode 100644
index 0000000..abced14
--- /dev/null
+++ b/lib/examples/Makefile
@@ -0,0 +1,9 @@
+CFLAGS = -Wall -g
+
+all: blocking.o
+
+blocking.o: blocking.c blocking.h ../libradsec-base.h ../libradsec.h
+ $(CC) $(CFLAGS) -c -I .. $^
+
+clean:
+ -rm *.o
diff --git a/lib/examples/blocking.c b/lib/examples/blocking.c
new file mode 100644
index 0000000..f72eab4
--- /dev/null
+++ b/lib/examples/blocking.c
@@ -0,0 +1,69 @@
+/* Example usage of libradsec-base, using blocking i/o. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "blocking.h"
+
+struct rs_packet *
+next_packet (const struct rs_config *ctx, int fd)
+{
+ uint8_t hdr[RS_HEADER_LEN];
+ uint8_t *buf;
+ size_t len;
+ struct rs_packet *p;
+ ssize_t n;
+
+ /* Read fixed length header. */
+ n = 0;
+ while (n < RS_HEADER_LEN)
+ n += read (fd, hdr, RS_HEADER_LEN - n);
+
+ p = rs_packet_new (ctx, hdr, &len);
+ fprintf (stderr, "DEBUG: got header, total packet len is %d\n",
+ len + RS_HEADER_LEN);
+ if (p)
+ {
+ buf = malloc (len);
+ if (buf)
+ {
+ n = 0;
+ while (n < len)
+ n += read (fd, buf, len - n);
+ p = rs_packet_parse (ctx, &p, buf, len);
+ free (buf);
+ }
+ else
+ rs_packet_free (ctx, &p);
+ }
+
+ return p;
+}
+
+int
+send_packet(const struct rs_config *ctx, int fd, struct rs_packet *p)
+{
+ uint8_t *buf = NULL;
+ ssize_t n = -20; /* Arbitrary packet size -- a guess. */
+
+ while (n < 0)
+ {
+ buf = realloc (buf, -n);
+ if (buf == NULL)
+ return -1;
+ n = rs_packet_serialize (p, buf, -n);
+ }
+
+ while (n)
+ {
+ ssize_t count = write (fd, buf, n);
+ if (count == -1)
+ return -1;
+ n -= count;
+ }
+
+ free (buf);
+ rs_packet_free (ctx, &p);
+ return 0;
+}
diff --git a/lib/examples/blocking.h b/lib/examples/blocking.h
new file mode 100644
index 0000000..04a07a8
--- /dev/null
+++ b/lib/examples/blocking.h
@@ -0,0 +1,4 @@
+#include "libradsec-base.h"
+
+struct rs_packet *next_packet (const struct rs_config *ctx, int fd);
+int send_packet (const struct rs_config *ctx, int fd, struct rs_packet *p);