Skip to content

Instantly share code, notes, and snippets.

@chrisdew
Created August 29, 2013 15:33
Show Gist options
  • Save chrisdew/6379630 to your computer and use it in GitHub Desktop.
Save chrisdew/6379630 to your computer and use it in GitHub Desktop.
udpx
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include "udpx.pb-c.h"
#define debug(M, ...) fprintf(stderr, "%i: DEBUG %10.10s:%3.0d: " M "\n", getpid(), __FILE__, __LINE__, ##__VA_ARGS__); fflush(stderr);
#define log_err(M, ...) fprintf(stderr, "%i: ERROR errno:%i %10.10s:%3.0d: " M "\n", getpid(), errno, __FILE__, __LINE__, ##__VA_ARGS__); fflush(stderr);
#define quit_if(COND, ...) do { \
if(COND) { \
log_err(__VA_ARGS__); \
if (errno != 0) perror(NULL); \
exit(errno); \
} \
} while(0);
#define MAX_ATTEMPTS 100000
#define MAX_SIZE 1500
#define RAND_DEV "/dev/urandom"
char *hex = "0123456789ABCDEF";
uint8_t *buf_to_hex(uint8_t *buf, int len) {
static uint8_t out[MAX_SIZE * 2 + 1];
quit_if(len > MAX_SIZE, "len too long");
int i;
for (i = 0; i < len; i++) {
out[2*i] = hex[buf[i] >> 4]; // top nibble
out[2*i + 1] = hex[buf[i] & 15];
}
out[2*i] = '\0';
return out;
}
int main (int argc, const char * argv[]) {
int fd = open(RAND_DEV, O_RDONLY);
quit_if(fd < 0, "couldn't open %s", RAND_DEV);
srand(time(NULL));
for (int i = 0; i < MAX_ATTEMPTS; i++) {
int len = rand() % MAX_SIZE;
uint8_t buf[len + 1];
int r = read(fd, buf, len);
quit_if(r != len, "bad read, expected %i bytes, read %i", r, len);
//debug("read %i bytes from %s", r, RAND_DEV);
Com__Thorcom__Udpx__Udpx *udpx = com__thorcom__udpx__udpx__unpack(NULL, len, buf);
if (udpx == NULL) {
//debug("error unpacking random message");
} else {
//debug("managed to unpack random message: %s", buf_to_hex(buf, len));
quit_if(1, "it happened");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment