#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#define RTXSIP { 10,63,128,53 }
#define RTXPORT 8000
int main(int argc, char **argv) {
int sock, n;
char buffer[2048];
unsigned char *iphead;
int sourcePort,desPort;
const int rtxip[4]=RTXSIP;
if ( (sock=socket(PF_PACKET, SOCK_RAW,
htons(ETH_P_IP)))<0) {
perror("socket");
exit(1);
}
printf("----------\n");
while (1) {
n = recvfrom(sock,buffer,2048,0,NULL,NULL);
/* Check to see if the packet contains at least
* complete Ethernet (14), IP (20) and TCP/UDP
* (8) headers.
*/
if (n<42) {
perror("recvfrom():");
printf("Incomplete packet (errno is %d)\n",
errno);
close(sock);
exit(0);
}
iphead = buffer+14; /* Skip Ethernet header */
if (*iphead==0x45) { /* Double check for IPv4
* and no options present */
sourcePort = (iphead[20]<<8)+iphead[21];
desPort = (iphead[22]<<8)+iphead[23];
if (iphead[12]==rtxip[0] && iphead[13]==rtxip[1] && iphead[14]==rtxip[2] && iphead[15]==rtxip[3] && sourcePort==RTXPORT && n > 1000 ){
printf("%d bytes read\n",n);
printf("Source,Dest ports %d,%d\n",sourcePort,desPort);
}
}
}
}
|