BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
860e91b
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
tools
BlinkenRecv.c
format
Stefan Schuermans
commited
860e91b
at 2023-08-18 09:55:23
BlinkenRecv.c
Blame
History
Raw
/* BlinkenLib Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #include <winsock2.h> #include <ws2tcpip.h> #define close closesocket #else #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #endif #include "Tools2.h" #include <BlinkenLib/BlinkenLib.h> int main(int argCnt, char **args) { int i, bound, val, timeout, ipv6; SOCKET udpSocket, tmpSock; struct sockaddr_in addr; struct sockaddr_in6 addr6; stBlinkenMovie *pMovie; // print info printf("BlinkenLib - BlinkenRecv\n" "version %d.%d.%d\n" "config " BLINKENLIB_CONFIG "\n" "Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info>\n" "Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html\n" "a blinkenarea.org project\n\n", BLINKENLIB_VERSION_MAJOR, BLINKENLIB_VERSION_MINOR, BLINKENLIB_VERSION_REVISION); // print syntax if (argCnt <= 1) { #ifdef BLINKENLIB_CFG_MNG #define MNGEXT ", *.mng" #else #define MNGEXT #endif #ifdef BLINKENLIB_CFG_GIF #define GIFEXT ", *.gif" #else #define GIFEXT #endif printf("syntax: %s <parameter> [...]\n\n" "parameters:\n" " -4\n" " use IPv4 (default for now, subject to change)\n" " -6\n" " use IPv6\n" " -l [<ip>:]<port>\n" " local address (defaults to 0.0.0.0:2323)\n" " must occur before -r and -o, may only occur once\n" " -r <ip>[:<port>]\n" " remote addess (defaults to every remote address)\n" " -t <milliseconds>\n" " set timeout (to detect movie end, defaults to 5000)\n" " -o <file>\n" " receive movie and write it to file (*.blm, *.bmm, *.bml, " "*.bbm" MNGEXT GIFEXT ")\n\n", args[0]); return 0; } #ifdef WIN32 { WSADATA WsaData; WSAStartup(0x0101, &WsaData); } #endif // create UDP socket ipv6 = 0; udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpSocket == INVALID_SOCKET) { printf("cannot create IPv4 UDP socket\n"); #ifdef WIN32 WSACleanup(); #endif return -1; } bound = 0; // process parameters timeout = 5000; for (i = 1; i < argCnt; i++) { // IPv4 if (strcmp(args[i], "-4") == 0) { if (ipv6) { // replace socket with new IPv4 UDP socket tmpSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (tmpSock != INVALID_SOCKET) { close(udpSocket); udpSocket = tmpSock; ipv6 = 0; } else { printf("cannot create IPv4 UDP socket\n"); } } } // IPv6 else if (strcmp(args[i], "-6") == 0) { if (!ipv6) { // replace socket with new IPv6 UDP socket tmpSock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (tmpSock != INVALID_SOCKET) { close(udpSocket); udpSocket = tmpSock; ipv6 = 1; } else { printf("cannot create IPv6 UDP socket\n"); } } } // local address else if (strcmp(args[i], "-l") == 0) { if (i + 1 < argCnt) { i++; if (!ipv6) { if (!txt2addr(args[i], &addr) || bind(udpSocket, (struct sockaddr *)&addr, sizeof(addr)) != 0) printf("could not set local address to \"%s\"\n", args[i]); else bound = 1; } else { if (!txt2addr6(args[i], &addr6) || bind(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set local address to \"%s\"\n", args[i]); else bound = 1; } } else printf("missing local address for \"-l\"\n"); } // remote address else if (strcmp(args[i], "-r") == 0) { if (i + 1 < argCnt) { i++; if (!ipv6) { if (!txt2addr(args[i], &addr) || bind(udpSocket, (struct sockaddr *)&addr, sizeof(addr)) != 0) printf("could not set remote address to \"%s\"\n", args[i]); } else { if (!txt2addr6(args[i], &addr6) || bind(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set remote address to \"%s\"\n", args[i]); } } else printf("missing remote address for \"-r\"\n"); } // timeout else if (strcmp(args[i], "-t") == 0) { if (i + 1 < argCnt) { i++; if (sscanf(args[i], "%u", &val) == 1) timeout = val; else printf("invalid timeout value \"%s\"\n", args[i]); } else printf("missing timeout value for \"-t\"\n"); } // receive movie and write it to file else if (strcmp(args[i], "-o") == 0) { if (i + 1 < argCnt) { i++; if (!bound) // try to bind if not bound { if (!ipv6) { printf("no local address to receive movie for file \"%s\" to,\n" " using default local address \"0.0.0.0:2323\"\n", args[i]); if (!txt2addr("127.0.0.1:2323", &addr) || bind(udpSocket, (struct sockaddr *)&addr, sizeof(addr)) != 0) printf("could not set local address to \"127.0.0.1:2323\"\n"); else bound = 1; } else { printf("no local address to receive movie for file \"%s\" to,\n" " using default local address \"[::1]:2323\"\n", args[i]); if (!txt2addr6("[::1]:2323", &addr6) || bind(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set local address to \"[::1]:2323\"\n"); else bound = 1; } } if (bound) { printf("receiving movie for file \"%s\"...\n", args[i]); pMovie = BlinkenMovieReceive(udpSocket, timeout, NULL); if (pMovie == NULL) printf("could not receive movie for file \"%s\"\n", args[i]); else { if (BlinkenMovieSave(pMovie, args[i]) < 0) printf("could not write movie \"%s\"\n", args[i]); else printf("movie \"%s\" written\n", args[i]); BlinkenMovieFree(pMovie); } } else printf("no local address set to receive movie for file \"%s\" on\n", args[i]); } else printf("missing output filename for \"-o\"\n"); } // unknown parameter else printf("unknown parameter \"%s\", call without parameters to get help\n", args[i]); } // for( i ... // close socket close(udpSocket); #ifdef WIN32 WSACleanup(); #endif return 0; }