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
|
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc,char *argv[]) {
int s=socket(AF_INET,SOCK_DGRAM,0);
char packet[1501];//mtu of an interface. including headers though. meh.
int n;
struct ip_mreq mreq;
struct sockaddr_in sa;
int one = sizeof(struct sockaddr_in);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&sa,0,sizeof(struct sockaddr_in));
sa.sin_addr.s_addr=htonl(INADDR_ANY);
sa.sin_port=htons(atoi(argv[2]));
sa.sin_family=AF_INET;
bind(s,(struct sockaddr *)&sa,sizeof(struct sockaddr_in));
mreq.imr_multiaddr.s_addr=inet_addr(argv[1]);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
setsockopt(s,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
while((n=recvfrom(s,packet,sizeof(packet),0,(struct sockaddr *)&sa,&one)) > 0) {
printf("%s ",inet_ntoa(sa.sin_addr));
fflush(stdout);
write(1,packet,n);
}
}
|