-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcshd.c
71 lines (58 loc) · 1.88 KB
/
mcshd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <syslog.h>
#include "wrappedsockets.h"
#define MAXLEN 1024
#define DELAY 2
#define TTL 5
int main(int argc, char *argv[])
{
int receivingSocket, sendingSocket;
u_char ttl;
struct sockaddr_in mcast_group;
struct utsname name;
if ((argc < 3) || (argc > 4)) {
fprintf(stderr, "Usage: %s mcast_group port [ttl]\n", argv[0]);
exit(1);
}
memset(&mcast_group, 0, sizeof(mcast_group));
mcast_group.sin_family = AF_INET;
mcast_group.sin_port = htons((unsigned short int)strtol(argv[2], NULL, 0));
mcast_group.sin_addr.s_addr = inet_addr(argv[1]);
allocateSocket(&sendingSocket);
ttl = (argc == 4) ? strtol(argv[3], NULL, 0) : TTL;
allocateSocket(&receivingSocket);
enableSocketReuse(&receivingSocket);
enableMulticastLoop(&receivingSocket);
bindSocket(&receivingSocket, mcast_group);
joinMulticastGroup(mcast_group.sin_addr, &receivingSocket);
getSystemNodeName(&name);
char buffer[MAXLEN + 1];
FILE *fp;
if (fork() == 0) {
setsid();
chdir("/");
uname(0);
openlog("mcshd", LOG_CONS, LOG_USER);
for (;;) {
memset(buffer, 0, sizeof(buffer));
recvfrom(receivingSocket, buffer, MAXLEN, 0, NULL, NULL);
if (strstr(buffer, "STARTOFCOMMAND: ") != NULL) {
syslog(LOG_INFO, "Received command: %s", buffer);
fp = popen(buffer+16, "r");
if (fp != NULL) {
while (fgets(buffer, MAXLEN, fp) != NULL) {
sendto(sendingSocket, buffer, strlen(buffer)+1, 0, (const struct sockaddr *)&mcast_group, sizeof(mcast_group));
}
snprintf(buffer, MAXLEN, "ENDOFCOMMAND");
sendto(sendingSocket, buffer, strlen(buffer)+1, 0, (const struct sockaddr *)&mcast_group, sizeof(mcast_group));
pclose(fp);
}
}
}
}
exit(0);
}