Hvad er det for en meddelse du vil have? EN du selv sender?
Jeg modtager de meddelelser som andre programmer modtager, hvorefter jeg gerne vil fjerne dem så de pågældende programmer ikke modtager dem, dvs. at jeg er kun opsnapper, hverken modtager eller afsender.
Du kan måske bedre forstå det ved at se mit program:
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
#define MAX_HOSTNAME_LAN 255
FILE *fp;
typedef struct _iphdr
{
unsigned char h_lenver;
unsigned char tos;
unsigned short total_len;
unsigned short ident;
unsigned short frag_and_flags;
unsigned char ttl;
unsigned char proto;
unsigned short checksum;
unsigned int sourceIP;
unsigned int destIP;
}IP_HDR;
void RecvPacket();
int filterpacket(char *buf);
char output[500];
void main()
{
fp = fopen("Sniffer1.txt","a+");
RecvPacket();
fclose(fp);
}
void RecvPacket()
{
SOCKET sock;
WSADATA wsd;
char RecvBuf[65535] = {0};
DWORD dwBytesRet;
unsigned int optval = 1;
int i;
WSAStartup(MAKEWORD(2,1),&wsd);
sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
char FAR name[MAX_HOSTNAME_LAN];
gethostname(name, MAX_HOSTNAME_LAN);
struct hostent FAR * pHostent;
pHostent = (struct hostent * )malloc(sizeof(struct hostent));
pHostent = gethostbyname(name);
SOCKADDR_IN sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(6000);
memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
bind(sock, (SOCKADDR *)&sa, sizeof(sa));
WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
for(i=0;i<100;i++)
{
memset(RecvBuf, 0, sizeof(RecvBuf));
recv(sock, RecvBuf, sizeof(RecvBuf), 0);
filterpacket(RecvBuf);
}
}
// Filter the Packet
int filterpacket(char *buf)
{
IP_HDR *pIpheader;
char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
SOCKADDR_IN saSource, saDest;
int iProtocol, iTTL;
pIpheader = (IP_HDR *)buf;
//Check Proto
iProtocol = pIpheader->proto;
if(iProtocol==IPPROTO_TCP)
{
fprintf(fp, "Protocol is TCP");
}
if(iProtocol==IPPROTO_UDP)
{
fprintf(fp, "Protocol is UDP");
}
if(iProtocol==IPPROTO_ICMP)
{
fprintf(fp, "Protocol is ICMP");
}
//Check Source IP
saSource.sin_addr.s_addr = pIpheader->sourceIP;
strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
//Check Dest IP
saDest.sin_addr.s_addr = pIpheader->destIP;
strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
iTTL = pIpheader->ttl;
fprintf(fp, "%s->%s", (char *)szSourceIP, (char *)szDestIP);
fprintf(fp, "TTL=%d\\n", (char *)iTTL);
return true;
}