diff options
author | miau <miaukatzemiau@priveasy.de> | 2011-06-05 12:14:34 (GMT) |
---|---|---|
committer | miau <miaukatzemiau@priveasy.de> | 2011-06-05 12:14:34 (GMT) |
commit | 2ff4666452ee56f4d6c51b7050dbe77456110d2f (patch) | |
tree | e5d4d3a17b3bc84902d9a8bed22b42b25d05df67 /broadcaster.c | |
parent | ff98567423ed50673ea42404b6c5a32f011850af (diff) |
add receiver loop, service multiplexing, common.h
* Service multiplexing
Broadcaster call signature is changed to
broadcaster host service [message]
to acommodate multiple sevices listening on the same port for
broadcast information.
* Receiver Loop
Listener now loops infinitely instead of returning after the first
packet. For each packet with the content service[,message] the
programm
service [message]
will be executed. Upward directory traversal is prohibited (and
detected).
To prevent DoS only one service program runs concurrently.
* Common.h
Port numbers and maximum message sizes are now stored in
common.h. Broascaster respects max. message size.
Diffstat (limited to 'broadcaster.c')
-rw-r--r-- | broadcaster.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/broadcaster.c b/broadcaster.c index 0e4eef5..4160b93 100644 --- a/broadcaster.c +++ b/broadcaster.c @@ -14,7 +14,7 @@ #include <arpa/inet.h> #include <netdb.h> -#define SERVERPORT 4950 // the port users will be connecting to +#include "common.h" int main(int argc, char *argv[]) { @@ -23,12 +23,24 @@ int main(int argc, char *argv[]) struct hostent *he; int numbytes; int broadcast = 1; - //char broadcast = '1'; // if that doesn't work, try this - if (argc != 3) { - fprintf(stderr,"usage: broadcaster hostname message\n"); + // assemble packet + if (argc < 3 || argc > 4) { + fprintf(stderr,"usage: broadcaster hostname task [message]\n"); exit(1); } + char buf[MAXBUFLEN], + *task = argv[2], + *message = (argc == 4) ? argv[3] : NULL; + int task_len = strlen(task), + total_len = task_len + 1 + strlen(message); + if (total_len > MAXBUFLEN) { + fprintf(stderr,"payload to long: max %d bytes, was %d\n", MAXBUFLEN, total_len); + exit(-1); + } + strcpy(buf, task); + buf[task_len] = 0; + strcpy(buf + 1 + task_len, message); if ((he=gethostbyname(argv[1])) == NULL) { // get the host info perror("gethostbyname"); @@ -52,7 +64,7 @@ int main(int argc, char *argv[]) their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero); - if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, + if ((numbytes=sendto(sockfd, buf, total_len, 0, (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) { perror("sendto"); exit(1); |