You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 lines
1.9 KiB

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <signal.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#define HOST_IF_IP "192.168.31.89"
#define GROUP_ADDR "225.0.0.5"
#define PORTRECV 8310
static int got_sig = 0;
static void sigterm(int nr)
{
got_sig = 1;
}
int main(int argc, char *argv[])
{
FILE *myfile;
int sock, count;
socklen_t slen;
struct sockaddr_in ser_addr;
struct ip_mreq mreq;
int yes=1;
unsigned char buf[64] = {0};
signal(SIGINT, sigterm);
sock = socket(AF_INET,SOCK_DGRAM,0);
printf("sockfd: %d\n",sock);
if (sock == -1) {
printf("socket error");
exit(1);
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0){
perror("Reusing ADDR failed");
exit(1);
}
memset(&ser_addr,0,sizeof(ser_addr));
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr(GROUP_ADDR);
ser_addr.sin_port = htons(PORTRECV);
if (bind(sock,(struct sockaddr *) &ser_addr,sizeof(ser_addr)) < 0){
perror("bind");
exit(1);
}
mreq.imr_multiaddr.s_addr = inet_addr(GROUP_ADDR);
mreq.imr_interface.s_addr = inet_addr(HOST_IF_IP);
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) {
perror("setsock");
close(sock);
exit(1);
}
myfile = fopen("output.raw", "w");
while (!got_sig) {
slen = sizeof ser_addr;
memset(&ser_addr, 0, sizeof ser_addr);
memset(buf, 0, sizeof buf);
if ((count = recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &ser_addr,(socklen_t*)&slen)) < 0) {
perror("recvfrom");
exit(1);
}
printf("recv data %d\n", count);
if (count == 64) {
if (buf[0] == 0xac && buf[1] == 0x93 && buf[63] == 0x14) {
fwrite(buf + 16, 1, 6, myfile);
}
}
//fflush(myfile);
}
printf("---------------\n");
fclose(myfile);
return 0;
}