1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc,char *argv[]) {
int s=socket(AF_INET,SOCK_RAW,atoi(argv[2]));
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));
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);
}
}
|