BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
c9469b6
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
BlinkenSend.c
restructure directories
Stefan Schuermans
commited
c9469b6
at 2019-05-30 18:28:57
BlinkenSend.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 #define strcasecmp stricmp #else #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #endif #include <BlinkenLib/BlinkenLib.h> #include "Tools2.h" int main(int argCnt, char **args) { int i, connected, ipv6; SOCKET udpSocket, tmpSock; etBlinkenProto proto; unsigned int maxidle, send_cnt, loop_cnt, loop, ui; struct sockaddr_in addr; struct sockaddr_in6 addr6; stBlinkenMovie *pMovie; // print info printf("BlinkenLib - BlinkenSend\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" " -s [<ip>:]<port>\n" " source address (defaults to 0.0.0.0:0)\n" " must occur before -d and -i, may only occur once\n" " -d <ip>[:<port>]\n" " destination addess (defaults to 127.0.0.1:2323)\n" " -p [BLP|EBLP|MCUF]\n" " protocol to use (defaults to MCUF)\n" " -t <milliseconds>\n" " set maximum idle time between frames (defaults to 3000, use 0 for infinite)\n" " -n <number>\n" " set number of times to send movies (defaults to 1)\n" " -i <file>\n" " read movie from file (*.blm, *.bmm, *.bml, *.bbm" MNGEXT GIFEXT ") and send it\n" " -l <number>\n" " loop number of times (defaults to 1, use 0 for forever)\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; } // loop loop_cnt = 1; for (loop = 0; loop < loop_cnt || loop_cnt == 0; loop++) { // print loop message if (loop_cnt > 1) printf("--- loop %u/%u ---\n", loop + 1, loop_cnt); if (loop_cnt == 0) printf("--- loop %u ---\n", loop + 1); // process parameters connected = 0; proto = BlinkenProtoMcuf; maxidle = 3000; send_cnt = 1; 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"); } } } // source address else if (strcmp(args[i], "-s") == 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 source address to \"%s\"\n", args[i]); } else { if (!txt2addr6(args[i], &addr6) || bind(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set source address to \"%s\"\n", args[i]); } } else printf("missing source address for \"-s\"\n"); } // destination address else if (strcmp(args[i], "-d") == 0) { if (i + 1 < argCnt) { i++; if (!ipv6) { if (!txt2addr(args[i], &addr) || connect(udpSocket, (struct sockaddr *)&addr, sizeof(addr)) != 0) printf("could not set destination address to \"%s\"\n", args[i]); else connected = 1; } else { if (!txt2addr6(args[i], &addr6) || connect(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set destination address to \"%s\"\n", args[i]); else connected = 1; } } else printf("missing destination address for \"-d\"\n"); } // protocol to use else if (strcmp(args[i], "-p") == 0) { if (i + 1 < argCnt) { i++; if (strcasecmp(args[i], "BLP") == 0) proto = BlinkenProtoBlp; else if (strcasecmp(args[i], "EBLP") == 0) proto = BlinkenProtoEblp; else if (strcasecmp(args[i], "MCUF") == 0) proto = BlinkenProtoMcuf; else printf("unknown protocol \"%s\"\n", args[i]); } else printf("missing protocol for \"-p\"\n"); } // maximum idle time else if (strcmp(args[i], "-t") == 0) { if (i + 1 < argCnt) { i++; if (sscanf(args[i], "%u", &ui) == 1) maxidle = ui; else printf("invalid maximum idle time value \"%s\"\n", args[i]); } else printf("missing maximum idle time value for \"-t\"\n"); } // number of times to send movies else if (strcmp(args[i], "-n") == 0) { if (i + 1 < argCnt) { i++; if (sscanf(args[i], "%u", &ui) == 1 && ui > 0) send_cnt = ui; else printf("invalid number \"%s\"\n", args[i]); } else printf("missing number for \"-n\"\n"); } // read movie and send it else if (strcmp(args[i], "-i") == 0) { if (i + 1 < argCnt) { i++; if (!connected) // try to connect if not yet connected { if (!ipv6) { printf("no destination address to sent movie \"%s\" to,\n" " using default destination address \"127.0.0.1:2323\"\n", args[i]); if (!txt2addr("127.0.0.1:2323", &addr) || connect(udpSocket, (struct sockaddr *)&addr, sizeof(addr)) != 0) printf("could not set destination address to \"127.0.0.1:2323\"\n"); else connected = 1; } else { printf("no destination address to sent movie \"%s\" to,\n" " using default destination address \"[::1]:2323\"\n", args[i]); if (!txt2addr6("[::1]:2323", &addr6) || connect(udpSocket, (struct sockaddr *)&addr6, sizeof(addr6)) != 0) printf("could not set destination address to \"[::1]:2323\"\n"); else connected = 1; } } if (connected) { pMovie = BlinkenMovieLoad(args[i]); if (pMovie == NULL) printf("could not read movie \"%s\"\n", args[i]); else { printf("movie \"%s\" read\n", args[i]); for (ui = 0; ui < send_cnt; ui++) { printf("sending movie \"%s\" (%u/%u)...\n", args[i], ui + 1, send_cnt); BlinkenMovieSend(pMovie, udpSocket, proto, maxidle); printf("movie \"%s\" sent\n", args[i]); } BlinkenMovieFree(pMovie); } } else printf("no destination address to sent movie \"%s\" to\n", args[i]); } else printf("missing input filename for \"-i\"\n"); } // number of times to loop else if (strcmp(args[i], "-l") == 0) { if (i + 1 < argCnt) { i++; if (sscanf(args[i], "%u", &ui) == 1) loop_cnt = ui; else printf("invalid number \"%s\"\n", args[i]); } else printf("missing number for \"-l\"\n"); } // unknown parameter else printf ("unknown parameter \"%s\", call without parameters to get help\n", args[i]); } // for( i ... } // for( loop ... // close socket close(udpSocket); #ifdef WIN32 WSACleanup(); #endif return 0; }