From 2ce7b5dd1409b0cb68420538c8d0c5d756d8b495 Mon Sep 17 00:00:00 2001 From: tonton Th Date: Tue, 14 Apr 2020 13:07:57 +0200 Subject: [PATCH] adding udp-dumper.c bis --- tools/Makefile | 8 +++ tools/README.md | 11 +++++ tools/udp-dumper.c | 121 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 tools/Makefile create mode 100644 tools/README.md create mode 100644 tools/udp-dumper.c diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..8498370 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,8 @@ +# +# Various OSC tools +# + +# https://git.tetalab.org/tTh/gadgets-OSC + +udp-dumper: udp-dumper.c Makefile + gcc -Wall $< -o $@ diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 0000000..3817bb3 --- /dev/null +++ b/tools/README.md @@ -0,0 +1,11 @@ +# Various OSC tools + + +## relay.py + +Quick hack build with a disgusting language. + +## udp-dumper + +This is not really an OSC dedicated tools, but a general purpose one. + diff --git a/tools/udp-dumper.c b/tools/udp-dumper.c new file mode 100644 index 0000000..3f41ec5 --- /dev/null +++ b/tools/udp-dumper.c @@ -0,0 +1,121 @@ +/* + * general purpose UDP dumper + * + * made by tTh, around 2019... + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT 5002 +#define BUFLEN 1024 + +int verbosity = 0; + +/* ----------------------------------------------------------------- */ +int dumpln(unsigned char *ptr) +{ +int foo; + +for (foo=0; foo<16; foo++) + printf("%02x ", ptr[foo]); + +printf("| "); +for (foo=0; foo<16; foo++) + if (isprint(ptr[foo])) putchar(ptr[foo]); + else putchar('.'); + +printf(" |\n"); +return 0; +} +/* ----------------------------------------------------------------- */ +void udp_dumper(uint16_t port) +{ +unsigned char buffer[BUFLEN]; +struct sockaddr_in si_me, si_other; +int sock, foo, bar, flag_exit; +unsigned int slen=sizeof(si_other); +long serial; +double starttime, curtime; +struct timeval tp; + +if (-1==(sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))) { + perror("socket fail "); + exit(1); + } + +#if DEBUG_LEVEL +fprintf(stderr, "port %d / sock -> %d\n", port, sock); +#endif + +memset((char *) &si_me, 0, sizeof(si_me)); +si_me.sin_family = AF_INET; +si_me.sin_port = htons(port); +si_me.sin_addr.s_addr = htonl(INADDR_ANY); +if (bind(sock, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { + perror("'bind' failure "); + exit(1); + } + +flag_exit = 0; serial = 0L; + +gettimeofday(&tp, NULL); +starttime = tp.tv_sec + tp.tv_usec / 1e6; + +do { + memset(buffer, 0, BUFLEN); + slen = sizeof(si_other); + foo = recvfrom(sock, buffer, BUFLEN, 0, + (struct sockaddr *)&si_other, &slen); + + if (verbosity) { + gettimeofday(&tp, NULL); + curtime = tp.tv_sec + tp.tv_usec / 1e6; + printf("----+ %5d b ts: %.3f\n", foo, curtime - starttime); + } + + for (bar=0; bar<=foo; bar+=16) { + printf("%3ld | ", serial); + dumpln(buffer+bar); + } + serial++; + } while ( !flag_exit); +} +/* ----------------------------------------------------------------- */ +void help(int foo) +{ +puts("\t-h\tthis help text"); +puts("\t-p NNNN\treceiving port number"); +puts("\t-v\tincrease verbosity"); +exit(0); +return ; +} +/* ----------------------------------------------------------------- */ +int main(int argc, char *argv[]) +{ +int opt, port; + +port = PORT; +/* parsing command line options */ +while ((opt = getopt(argc, argv, "hp:v")) != -1) { + switch (opt) { + case 'h': help(0); break; + case 'p': port = atoi(optarg); break; + case 'v': verbosity++; break; + default: exit(1); + } + } + + +udp_dumper(port); + +return 0; +} +/* ----------------------------------------------------------------- */ +