diff options
author | markster <markster@5390a7c7-147a-4af0-8ec9-7488f05a26cb> | 2002-12-21 22:56:39 +0000 |
---|---|---|
committer | markster <markster@5390a7c7-147a-4af0-8ec9-7488f05a26cb> | 2002-12-21 22:56:39 +0000 |
commit | d6857df41f88389433acc6d223f49be9aa3fc9ba (patch) | |
tree | 7ee4e2dece23410eb065d579f7be512512d5d560 | |
parent | 1ab5fa38aab79228f241525048c48425274d1287 (diff) |
Version 0.4.0 from FTP
git-svn-id: http://svn.digium.com/svn/zaptel/trunk@139 5390a7c7-147a-4af0-8ec9-7488f05a26cb
-rwxr-xr-x | hdlcgen.c | 105 | ||||
-rwxr-xr-x | hdlcverify.c | 106 |
2 files changed, 211 insertions, 0 deletions
diff --git a/hdlcgen.c b/hdlcgen.c new file mode 100755 index 0000000..15dc45c --- /dev/null +++ b/hdlcgen.c @@ -0,0 +1,105 @@ +#define FAST_HDLC_NEED_TABLES +#include "fasthdlc.h" +#include <stdio.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> + +#define RANDOM "/dev/urandom" /* Not genuinely random */ +/* #define RANDOM "/dev/random" */ /* Quite genuinely random */ + +int myread(int fd, char *buf, int len) +{ + int sofar; + int res; + sofar = 0; + while(sofar < len) { + res = read(fd, buf + sofar, len - sofar); + if (res < 0) + return res; + sofar += res; + } + return sofar; +} + +int main(int argc, char *argv[]) +{ + unsigned char buf[1024]; + unsigned char outbuf[2048]; + int res; + int randin; + int randout; + int hdlcout; + int cnt; + int hdlccnt; + int x; + int flags; + struct fasthdlc_state transmitter; + + fasthdlc_precalc(); + + fasthdlc_init(&transmitter); + + randin = open(RANDOM, O_RDONLY); + if (randin < 0) { + fprintf(stderr, "Unable to open %s: %s\n", RANDOM, strerror(errno)); + exit(1); + } + randout = open("random.raw", O_WRONLY|O_TRUNC|O_CREAT, 0666); + if (randout < 0) { + fprintf(stderr, "Unable to open random.raw: %s\n", strerror(errno)); + exit(1); + } + hdlcout = open("random.hdlc", O_WRONLY|O_TRUNC|O_CREAT, 0666); + if (hdlcout < 0) { + fprintf(stderr, "Unable to open random.hdlc: %s\n", strerror(errno)); + exit(1); + } + for (;;) { + cnt = (rand() % 256) + 4; /* Read a pseudo-random amount of stuff */ + res = myread(randin, buf, cnt); + if (res != cnt) { + fprintf(stderr, "Tried to read %d bytes, but read %d instead\n", cnt, res); + exit(1); + } + res = write(randout, buf, cnt); + if (res != cnt) { + fprintf(stderr, "Tried to write %d bytes, but wrote %d instead\n", cnt, res); + exit(1); + } + /* HDLC encode */ + hdlccnt = 0; + /* Start with a flag */ + fasthdlc_tx_frame(&transmitter); + if (transmitter.bits >= 8) + outbuf[hdlccnt++] = fasthdlc_tx_run(&transmitter); + for (x=0;x<cnt;x++) { + res = fasthdlc_tx_load(&transmitter, buf[x]); + if (res < 0) { + fprintf(stderr, "Unable to load byte :(\n"); + exit(1); + } + while(transmitter.bits >= 8) { + outbuf[hdlccnt++] = fasthdlc_tx_run(&transmitter); + } + } + flags = (rand() % 4); + for (x=0;x<flags;x++) { + if (transmitter.bits < 8) + fasthdlc_tx_frame(&transmitter); + else + fprintf(stderr, "Huh? Don't need a frame?\n"); + outbuf[hdlccnt++] = fasthdlc_tx_run(&transmitter); + } + if (argc > 1) + printf("Encoded %d byte message with %d bytes of HDLC and %d extra flags\n", cnt, hdlccnt, flags); + res = write(hdlcout, outbuf, hdlccnt); + if (res != hdlccnt) { + fprintf(stderr, "Tried to write %d HDLC bytes, but wrote %d instead\n", cnt, res); + exit(1); + } + + } +} diff --git a/hdlcverify.c b/hdlcverify.c new file mode 100755 index 0000000..1937876 --- /dev/null +++ b/hdlcverify.c @@ -0,0 +1,106 @@ +#define FAST_HDLC_NEED_TABLES +#include "fasthdlc.h" +#include <stdio.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> + +int myread(int fd, char *buf, int len) +{ + int sofar; + int res; + sofar = 0; + while(sofar < len) { + res = read(fd, buf + sofar, len - sofar); + if (res < 0) + return res; + sofar += res; + } + return sofar; +} + +static inline unsigned char nextchar(int fd) +{ + static unsigned char inbuf[2048]; + static int bytes = 0; + static int pos = 0; + if (pos >= bytes) { + pos = 0; + bytes = read(fd, inbuf, sizeof(inbuf)); + if (bytes < 0) { + fprintf(stderr, "Unable to read more data: %s\n", strerror(errno)); + exit(1); + } + if (bytes == 0) { + fprintf(stderr, "-- END OF DATA --\n"); + exit(0); + } + } + return inbuf[pos++]; +} + +int main(int argc, char *argv[]) +{ + unsigned char decbuf[1024]; + unsigned char actual[1024]; + int res; + int datain; + int hdlcin; + int hdlccnt; + int x; + struct fasthdlc_state receiver; + + fasthdlc_precalc(); + + fasthdlc_init(&receiver); + + hdlcin = open("random.hdlc", O_RDONLY); + if (hdlcin < 0) { + fprintf(stderr, "Unable to open %s: %s\n", "random.hdlc", strerror(errno)); + exit(1); + } + datain = open("random.raw", O_RDONLY); + if (datain < 0) { + fprintf(stderr, "Unable to open random.raw: %s\n", strerror(errno)); + exit(1); + } + hdlccnt = 0; + for (;;) { + /* Feed in some input */ + if (fasthdlc_rx_load(&receiver, nextchar(hdlcin))) { + fprintf(stderr, "Unable to feed receiver :(\n"); + exit(1); + } + res = fasthdlc_rx_run(&receiver); + if (res & RETURN_EMPTY_FLAG) + continue; + if (res & RETURN_COMPLETE_FLAG) { + if (hdlccnt) { + if (argc > 1) + printf("Got message of length %d\n", hdlccnt); + res = myread(datain, actual, hdlccnt); + if (res != hdlccnt) { + fprintf(stderr, "Tried to read %d bytes, but read %d instead\n", hdlccnt, res); + exit(1); + } + for (x=0;x<hdlccnt;x++) { + if (actual[x] != decbuf[x]) { + fprintf(stderr, "Found discrepancy at offset %d\n", x); + exit(1); + } + } + /* Reset message receiver */ + hdlccnt = 0; + } + } else if (res & RETURN_DISCARD_FLAG) { + if (1 || hdlccnt) { + fprintf(stderr, "Detected abort :(\n"); + exit(1); + } + } else { + decbuf[hdlccnt++] = res; + } + } +} |