//○RAWソケットで、パケットを見る
//Win2000以上でしか動作しません
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
char*ProcName(int no){
switch(no){
case 0 : return "IP";
case 1 : return "ICMP Internet control message protocol";
case 3 : return "GGP Gateway-gateway protocol";
case 6 : return "TCP Transmission control protocol";
case 8 : return "EGP Exterior gateway protocol";
case 12 : return "PUP PARC universal packet protocol";
case 17 : return "UDP User datagram protocol";
case 20 : return "HMP Host monitoring protocol";
case 22 : return "XNS-IDP Xerox NS IDP";
case 27 : return "RDP reliable datagram protocol";
case 66 : return "RVD MIT remote virtual disk";
case 255 : return "RAW";
default : return "";
}
}
int main(){
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0) return false;
SOCKET s;
s = WSASocket(AF_INET,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED);
if(s==INVALID_SOCKET) return false;
DWORD d;
char listPos[5000];
if(SOCKET_ERROR==WSAIoctl(s,SIO_ADDRESS_LIST_QUERY,NULL,0,listPos,sizeof(listPos),&d,NULL,NULL)){
return false;
}
SOCKET_ADDRESS_LIST*list=(SOCKET_ADDRESS_LIST*)listPos;
int NicNo=0;
{//NICを列挙して選択させる
for(int i=0;i<list->iAddressCount;i++){
printf("NIC No = %d : %s\n",i,inet_ntoa(((SOCKADDR_IN*)list->Address[i].lpSockaddr)->sin_addr));
}
{
char buff[1024];
printf("NIC No = ");
gets(buff);
NicNo=atoi(buff);//文字を値に変換
}
}
SOCKADDR_IN saddr;
saddr.sin_addr.s_addr = ((SOCKADDR_IN*)list->Address[NicNo].lpSockaddr)->sin_addr.s_addr;
saddr.sin_family =AF_INET;
saddr.sin_port = htons(0);
if (bind(s, (SOCKADDR *)&saddr, sizeof(saddr)) == SOCKET_ERROR) {//バインドエラー
closesocket(s);
WSACleanup();
return false;
}
int op=1;
if(WSAIoctl(s,0x98000001,&op,sizeof(op),NULL,0,&d,NULL,NULL)==SOCKET_ERROR){
closesocket(s);
WSACleanup();
return false;
}
WSABUF wsb;
unsigned char buff[65535]="";
unsigned long len=0;
DWORD flag=0;
while(1){
wsb.buf = (char*)buff;
wsb.len =65535;
memset(wsb.buf,0,65535);
flag=0;
if(SOCKET_ERROR==WSARecv(s,&wsb,1,&len,&flag,NULL,NULL)){
closesocket(s);
WSACleanup();
return false;
}
printf("\n==========================================================\n");
printf("%d.%d.%d.%d : %d ----> %d.%d.%d.%d : %d\n",buff[12],buff[13],buff[14],buff[15],(buff[20]*256+buff[21]),buff[16],buff[17],buff[18],buff[19],(buff[22]*256+buff[23]));
printf("%s\n",ProcName(buff[9]));
{for(unsigned int i=0;i<len;i++) printf("%02x ",buff[i]);}
{for(unsigned int i=0;i<len;i++) if(buff[i]==0) buff[i]=' ';}
{for(unsigned int i=0;i<len;i++) if(buff[i]=='\a') buff[i]=' ';}//ビープ音を取り除く
buff[len]='\0';
printf("\n\n");
{for(unsigned int i=0;i<len;i++) printf("%c",buff[i]);}
}
closesocket(s);
WSACleanup();
return 0;
}
▲トップページ
>
Windows と C++