Cześć,
Mam problem z odbieraniem pakietów. Napisałem krótki kod, który otwiera RAW socket w promiscuous mode, tak aby odbierał jakiekolwiek pakiety. Gdy program ten zostanie uruchomiony i ładuje mi się jakakolwiek strona www otrzymuje pakiety tylko z dwóch IP. Co może być przyczyną wyświetlania takiego dziwnego IP? Problem w tym, że nie mogę odróżnić pakietów skąd pochodzą.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <linux/if_packet.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <sys/ioctl.h>
#include <linux/if_ether.h>
#include <netdb.h>
#define DEFAULT_IF "eth0"
#define PACKET_SIZE 1 << 16
int main(int argc, char **argv){
int raw_socket = 0;
struct ifreq if_index;
bzero(&if_index,sizeof(struct ifreq));
/* Obtain the device name */
char IFACE_NAME[10];
if (argc > 1)
strcpy(IFACE_NAME, argv[1]);
else
strcpy(IFACE_NAME, DEFAULT_IF);
printf("device/interface name: %s\n",IFACE_NAME);
/* Open a raw socket */
if ((raw_socket = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL))) < 0){
perror("socket");
return errno;
}
printf("opening raw socket\n");
/* Set the device to use */
strncpy(if_index.ifr_name, IFACE_NAME, IFNAMSIZ-1);
/* Get the current flags that the device might have */
if (ioctl(raw_socket,SIOCGIFINDEX, &if_index) < 0){
perror("SIOCGIFINDEX");
return errno;
}
/* Set the old flags plus the IFF_PROMISC flag */
if_index.ifr_flags |= IFF_PROMISC;
if(ioctl(raw_socket, SIOCSIFFLAGS, &if_index) < 0){
perror("SOICSIFFLAGS");
return errno;
}
printf("entering promiscuous mode\n");
/* Configuring the device */
if (ioctl(raw_socket,SIOCGIFINDEX, &if_index) < 0){
perror("SIOCGIFINDEX");
return errno;
}
/* Waiting for data */
while(1){
char packet[PACKET_SIZE];
ssize_t bytes = 0;
struct sockaddr_in address;
socklen_t length;
if ((bytes = recvfrom(raw_socket,&packet,sizeof(packet),0,(struct sockaddr *)&address,&length)) < 1){
perror("recvfrom");
return errno;
}
printf("DG received (size: %li)\nIP: %s\n",bytes,inet_ntoa(address.sin_addr));
}
return 0;
}