Click here to Skip to main content
15,922,630 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCDialog vs CRecordView Pin
gteodora22-Apr-06 4:52
gteodora22-Apr-06 4:52 
AnswerRe: CDialog vs CRecordView Pin
includeh1022-Apr-06 6:15
includeh1022-Apr-06 6:15 
AnswerRe: CDialog vs CRecordView Pin
Aqueel22-Apr-06 10:02
Aqueel22-Apr-06 10:02 
GeneralRe: CDialog vs CRecordView Pin
David Crow24-Apr-06 3:20
David Crow24-Apr-06 3:20 
GeneralRe: CDialog vs CRecordView Pin
Aqueel24-Apr-06 17:16
Aqueel24-Apr-06 17:16 
QuestionStartDoc not being called by word Pin
pgibson00770022-Apr-06 4:32
pgibson00770022-Apr-06 4:32 
AnswerRe: StartDoc not being called by word Pin
P Gibson26-Apr-06 0:08
P Gibson26-Apr-06 0:08 
QuestionThose who know how to use raw socket to do a sniff , can help me ? Pin
fyr00000022-Apr-06 3:29
fyr00000022-Apr-06 3:29 
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>



#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define BUFFER_SIZE 65535
#pragma comment(lib, "ws2_32.lib")












#define PROTOCOL_STRING_ICMP_TXT "ICMP"
#define PROTOCOL_STRING_TCP_TXT "TCP"
#define PROTOCOL_STRING_UDP_TXT "UDP"
#define PROTOCOL_STRING_SPX_TXT "SPX"
#define PROTOCOL_STRING_NCP_TXT "NCP"
#define PROTOCOL_STRING_UNKNOW_TXT "UNKNOW"
//……
char* GetProtocolTxt(int Protocol)
{
switch (Protocol){
case IPPROTO_ICMP : //1 /* control message protocol */
return PROTOCOL_STRING_ICMP_TXT;
case IPPROTO_TCP : //6 /* tcp */
return PROTOCOL_STRING_TCP_TXT;
case IPPROTO_UDP : //17 /* user datagram protocol */
return PROTOCOL_STRING_UDP_TXT;
default:
return PROTOCOL_STRING_UNKNOW_TXT;
}
}















typedef struct _TCP{ WORD SrcPort; // 源端口
WORD DstPort; // 目的端口
DWORD SeqNum; // 顺序号
DWORD AckNum; // 确认号
BYTE DataOff; // TCP头长
BYTE Flags; // 标志(URG、ACK等)
WORD Window; // 窗口大小
WORD Chksum; // 校验和
WORD UrgPtr; // 紧急指针
} TCP;
typedef TCP *LPTCP;
typedef TCP UNALIGNED * ULPTCP;




typedef struct _IP{
union{ BYTE Version; // 版本
BYTE HdrLen; // IHL
};
BYTE ServiceType; // 服务类型
WORD TotalLen; // 总长
WORD ID; // 标识
union{ WORD Flags; // 标志
WORD FragOff; // 分段偏移
};
BYTE TimeToLive; // 生命期
BYTE Protocol; // 协议
WORD HdrChksum; // 头校验和
DWORD SrcAddr; // 源地址
DWORD DstAddr; // 目的地址
BYTE Options; // 选项
} IP;
typedef IP * LPIP;
typedef IP UNALIGNED * ULPIP;



void main()
{
WSADATA WSAData;
SOCKET sock;
BOOL flag=true;
char LocalName[16];
struct hostent *pHost;
struct sockaddr_in addr_in;
char RecvBuf[BUFFER_SIZE];
IP ip;
TCP tcp;
/////////////////////////////////////////////////////////////有问题!!!!!!!!!
// 检查 Winsock 版本号,WSAData为WSADATA结构对象
WSAStartup(MAKEWORD(2, 2), &WSAData);
// 创建原始套接字
sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
// 设置IP头操作选项,其中flag 设置为ture,亲自对IP头进行处理
setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag));
// 获取本机名
gethostname((char*)LocalName, sizeof(LocalName)-1);
// 获取本地 IP 地址
pHost = gethostbyname((char*)LocalName);
printf("我的主机 %s\n",pHost->h_name);
// 填充SOCKADDR_IN结构
addr_in.sin_addr = *(in_addr *)pHost-> h_addr_list[0]; //IP
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(57274);
// 把原始套接字sock 绑定到本地网卡地址上
bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in));
// dwValue为输入输出参数,为1时执行,0时取消
DWORD dwValue = 1;
// 设置 SOCK_RAW 为SIO_RCVALL,以便接收所有的IP包。其中SIO_RCVALL
// 的定义为: #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
ioctlsocket(sock, SIO_RCVALL, &dwValue);


while (true)
{
// 接收原始数据包信息
int ret = recv(sock, RecvBuf, BUFFER_SIZE, 0);
printf("haha\n");
if (ret > 0)
{
// 对数据包进行分析,并输出分析结果
ip = *(IP*)RecvBuf;
tcp = *(TCP*)(RecvBuf + ip.HdrLen);
printf("协议: %s\r\n",GetProtocolTxt(ip.Protocol));
printf("IP源地址: %s\r\n",inet_ntoa(*(in_addr*)&ip.SrcAddr));
printf("IP目标地址: %s\r\n",inet_ntoa(*(in_addr*)&ip.DstAddr));
printf("TCP源端口号: %d\r\n",tcp.SrcPort);
printf("TCP目标端口号:%d\r\n",tcp.DstPort);
printf("数据包长度: %d\r\n\r\n\r\n",ntohs(ip.TotalLen));
}
}

}

Laugh | :laugh:
The above is the program coding .

I write the sentence "printf("haha\n");" to debug the program !!!
But the recv method can't return an value , and nothing to do but wait.
How to solve it ? And why it will perform like that???
Thank you very much!!!






Jeff Fan
No change is the worst!!!
The principal of mine
msn:fyr000000@hotmail.com
If you like coding , add me.

-- modified at 9:35 Saturday 22nd April, 2006
AnswerRe: Those who know how to use raw socket to do a sniff , can help me ? Pin
fyr00000022-Apr-06 3:34
fyr00000022-Apr-06 3:34 
GeneralRe: Those who know how to use raw socket to do a sniff , can help me ? Pin
Sandeep. Vaidya25-Apr-06 2:31
Sandeep. Vaidya25-Apr-06 2:31 
AnswerRe: Those who know how to use raw socket to do a sniff , can help me ? Pin
fyr00000025-Apr-06 15:54
fyr00000025-Apr-06 15:54 
QuestionGlobals In MFC App Wizard Pin
jinbabaj22-Apr-06 3:12
jinbabaj22-Apr-06 3:12 
AnswerRe: Globals In MFC App Wizard Pin
includeh1022-Apr-06 4:58
includeh1022-Apr-06 4:58 
AnswerRe: Globals In MFC App Wizard Pin
Michael Dunn22-Apr-06 9:10
sitebuilderMichael Dunn22-Apr-06 9:10 
AnswerRe: Globals In MFC App Wizard Pin
Aqueel22-Apr-06 9:55
Aqueel22-Apr-06 9:55 
AnswerRe: Globals In MFC App Wizard Pin
John R. Shaw22-Apr-06 18:22
John R. Shaw22-Apr-06 18:22 
QuestionCan MSVS 6 leverage .net (more precisely C# forms)? Pin
-e22-Apr-06 2:30
-e22-Apr-06 2:30 
AnswerRe: Can MSVS 6 leverage .net (more precisely C# forms)? Pin
toxcct22-Apr-06 7:07
toxcct22-Apr-06 7:07 
GeneralRe: Can MSVS 6 leverage .net (more precisely C# forms)? Pin
-e22-Apr-06 12:42
-e22-Apr-06 12:42 
AnswerRe: Can MSVS 6 leverage .net (more precisely C# forms)? Pin
Waldermort22-Apr-06 8:17
Waldermort22-Apr-06 8:17 
GeneralRe: Can MSVS 6 leverage .net (more precisely C# forms)? Pin
-e22-Apr-06 13:02
-e22-Apr-06 13:02 
AnswerRe: Can MSVS 6 leverage .net (more precisely C# forms)? Pin
Michael P Butler23-Apr-06 7:57
Michael P Butler23-Apr-06 7:57 
QuestionScroll Window without document/view Pin
mwybranczyk22-Apr-06 1:34
mwybranczyk22-Apr-06 1:34 
AnswerRe: Scroll Window without document/view Pin
includeh1022-Apr-06 4:08
includeh1022-Apr-06 4:08 
QuestionListbox control Pin
srija22-Apr-06 1:26
srija22-Apr-06 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.