Salve,
ho un problema con un esempio che ho trovato su internet sull'uso di WSAIoctl
//
#pragma warning( disable: 4996 )
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment( lib, "ws2_32.lib" )
void translate_ip(DWORD _ip,char *_cip);
void decode_tcp(char *_packet);
void decode_icmp(char *_packet);
void get_this_machine_ip(char *_retIp);
#define LS_HI_PART(x) ((x >> 4) & 0x0F)
#define LS_LO_PART(x) ((x) & 0x0F)
#define LS_MAX_PACKET_SIZE 65535
#ifndef SIO_RCVALL
# define SIO_RCVALL _WSAIOW(IOC_VENDOR,1);
#endif
typedef struct _IP_HEADER_
{
BYTE ver_ihl; // Version (4 bits) and Internet Header Length (4 bits)
BYTE type; // Type of Service (8 bits)
WORD length; // Total size of packet (header + data)(16 bits)
WORD packet_id; // (16 bits)
WORD flags_foff; // Flags (3 bits) and Fragment Offset (13 bits)
BYTE time_to_live; // (8 bits)
BYTE protocol; // (8 bits)
WORD hdr_chksum; // Header check sum (16 bits)
DWORD source_ip; // Source Address (32 bits)
DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;
typedef struct _TCP_HEADER_
{
WORD source_port; // (16 bits)
WORD destination_port; // (16 bits)
DWORD seq_number; // Sequence Number (32 bits)
DWORD ack_number; // Acknowledgment Number (32 bits)
WORD info_ctrl; // Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits)
WORD window; // (16 bits)
WORD checksum; // (16 bits)
WORD urgent_pointer; // (16 bits)
} TCPHEADER;
typedef struct _ICMP_HEADER_
{
BYTE type; // (8 bits)
BYTE code; // (8 bits)
WORD checksum; // (16 bits)
} ICMPHEADER;
int main(int _argc, char *_argv[]){
struct sockaddr_in sock_sniff;
SOCKET sniff_socket = -1;
WSAData sa_data;
WORD ver;
IPHEADER *ip_header = NULL;
int optval = 1;
DWORD dwLen = 0;
char packet[LS_MAX_PACKET_SIZE];
int iRet = 0;
int ip_header_size = 0;
char ipSrc[20],ipDest[20],thisIp[20];
BOOL bShowTCP = true, bShowICMP = true;
if(_argc > 1)
{
if(!_stricmp(_argv[1],"icmp"))
bShowTCP = false;
else if(!_stricmp(_argv[1],"tcp"))
bShowICMP = false;
else
{
printf("\nUsage lsniff [ICMP|TCP]\n");
exit(0);
}
}
ver = MAKEWORD(2,2);
WSAStartup(ver,&sa_data);
sniff_socket = socket (AF_INET,SOCK_RAW,IPPROTO_IP);
if(sniff_socket == SOCKET_ERROR)
{
printf("Error: socket %ld\n",WSAGetLastError());
exit(-1);
}
memset(thisIp,0x00,sizeof(thisIp));
get_this_machine_ip(thisIp);
sock_sniff.sin_family = AF_INET;
sock_sniff.sin_port = htons(0);
sock_sniff.sin_addr.s_addr = inet_addr(thisIp);
bind(sniff_socket,(struct sockaddr *)&sock_sniff,sizeof(sock_sniff));
if( WSAIoctl( sniff_socket,
SIO_RCVALL,
&optval,
sizeof(optval),
NULL,
0,
&dwLen,
NULL,
NULL) == SOCKET_ERROR)
{
printf("Error: WSAIoctl = %ld\n",WSAGetLastError());
exit(-3);
}
while(true)
{
(void) memset (packet,0x00,sizeof(packet));
iRet = recv(sniff_socket,packet,LS_MAX_PACKET_SIZE,0);
if(iRet < sizeof(IPHEADER))
continue;
ip_header = (IPHEADER *)packet;
if(LS_HI_PART(ip_header->ver_ihl) != 4)
continue;
ip_header_size = LS_LO_PART(ip_header->ver_ihl);
ip_header_size *= sizeof(DWORD);
memset(ipSrc,0x00,sizeof(ipSrc));
memset(ipDest,0x00,sizeof(ipDest));
translate_ip(ip_header->source_ip,ipSrc);
translate_ip(ip_header->destination_ip,ipDest);
switch (ip_header->protocol)
{
case 1:
if(bShowICMP)
{
printf("\n--------------------------------// -----------------------");
printf("\n IP Header:");
printf("\n Source IP: %s", ipSrc);
printf("\n Destination IP::%s", ipDest);
printf("\n ICMP Header: ");
decode_icmp(&packet[ip_header_size]);
}
break;
case 6:
if(bShowTCP)
{
printf("\n ------------------------- // --------------------");
printf("\n IP Header: ");
printf("\n Source IP: %s",ipSrc);
printf("\n Destination IP: %s", ipDest);
printf("\n TCP Header: ");
decode_tcp(&packet[ip_header_size]);
}
break;
default:
break;
}
}
return 0;
}
void get_this_machine_ip(char *_retIP)
{
char host_name[128];
hostent *hs;
in_addr in;
memset(host_name,0x00,sizeof(host_name));
gethostname(host_name,128);
hs = gethostbyname(host_name);
memcpy(&in,hs->h_addr_list,hs->h_length);
strcpy(_retIP,inet_ntoa(in));
}
void translate_ip(DWORD _ip,char *_cip)
{
in_addr in;
in.S_un.S_addr = _ip;
strcpy(_cip,inet_ntoa(in));
}
void decode_tcp(char *_packet)
{
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = (ntohs(tcp_header->info_ctrl) & 0x003f);
printf("\n source port : %ld",htons(tcp_header->source_port));
printf("\n destination port : %ld",htons(tcp_header->destination_port));
printf("\n control bits :");
if(flags & 0x01)
printf("FIN");
if(flags & 0x02)
printf("SYN");
if( flags & 0x04)
printf("RST");
if(flags & 0x08)
printf("PSH");
if(flags & 0x10)
printf("ACK");
if(flags & 0x20)
printf("URG");
printf("\n sequence nubmer : %lu",ntohl(tcp_header->seq_number));
}
void decode_icmp(char *_packet)
{
ICMPHEADER *icmp_header = (ICMPHEADER *)_packet;
printf("\n type: %d",icmp_header->type);
switch (icmp_header->type)
{
case 0:
printf("echo reply");
break;
case 8:
printf("echo request");
break;
default:
break;
}
printf("\n code %d",icmp_header->code);
printf("\n code: %ld",icmp_header->checksum);
}
il problema è che mi da svariati errori di sintassi sulla linea 103,ma io non riesco a capire dove effettivamente sia quest'errore .
Con occhi esperti magari l'errore salta fuori.
Grazie in anticipo per l'aiuto.