diff options
author | Linus Nordberg <linus@nordu.net> | 2010-09-03 00:58:55 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordu.net> | 2010-09-03 00:58:55 +0200 |
commit | 8455dc9801730e599510c92cdb3e05da351aa7a5 (patch) | |
tree | bb3b8fca4cdda6ace2d632ab8df5b9b0c72172e3 /lib/blocking.c | |
parent | 5f3d7c31ce76cac39449f1603d09dfadcfcfcc70 (diff) |
Low level connect and read working, kind of. At least TCP.
Next: serializing.
Diffstat (limited to 'lib/blocking.c')
-rw-r--r-- | lib/blocking.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/blocking.c b/lib/blocking.c new file mode 100644 index 0000000..6ee4ad3 --- /dev/null +++ b/lib/blocking.c @@ -0,0 +1,67 @@ +/* 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: len: %d\n", len); + if (p) + { + /* Read the rest of the packet. */ + 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); + } + } + + 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; +} |