線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3007
推到 Plurk!
推到 Facebook!

Ping程式,其中request不知道怎麼設成0x08

答題得分者是:RaynorPao
victor-plus
一般會員


發表:4
回覆:2
積分:1
註冊:2008-09-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-09-21 17:43:29 IP:122.116.xxx.xxx 訂閱
我想要寫一隻ping的程式
但是在其中request的裡面有一個type的欄位應當是要設8
用wireshark去看的話在記憶體裡面是0x08
可是我送出去的話都會是0x38
我用IntToHex的涵式都不能用
也有把struct裡面的u_char改成int過
還是不行
[code cpp]
typedef struct tagICMPHDR
{
u_char Type; // Type
u_char Code; // Code
u_short Checksum; // Checksum
u_short ID; // Identification
u_short Seq; // Sequence
char Data; // Data
}ICMPHDR, *PICMPHER;

ICMPHDR.type=8;

S_buff=ICMPHDR.Type =ICMPHDR.Code =ICMPHDR.Checksum =ICMPHDR.ID=ICMPHDR.Seq =ICMPHDR.Data;
sendto(R_Socket,S_buff,sizeof(S_buff),0,(SOCKADDR *)&R_addr,R_addr_size);
請在此區域輸入程式碼

[/code]
type這個欄位就是寫在ICMPHDR.type那邊
可是我不管怎麼設
傳出去後用wireshark抓下來看都是0x38
請問大大們我該如何解決

謝謝






RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-09-21 17:54:23 IP:61.60.xxx.xxx 訂閱
(1)如果改成以下這樣子寫,是否可行?

[code cpp]
ICMPHDR.type=(BYTE)0x08;
[/code]

(2)如果要寫 ping 的功能,可以利用 Indy 元件來完成,這裡有一個現成的範例參考 http://delphi.ktop.com.tw/board.php?cid=168&fid=920&tid=44577
------
-- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
victor-plus
一般會員


發表:4
回覆:2
積分:1
註冊:2008-09-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-09-21 18:35:36 IP:122.116.xxx.xxx 訂閱
還是沒辦法耶...

抓到的值依然是0x38


我是要用winsock2去寫

所以不用lndy@@

謝謝大大的回覆
RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-09-21 21:15:09 IP:61.60.xxx.xxx 訂閱
(1)以下這一行程式碼,好像有點怪怪的

[code cpp]
S_buff=ICMPHDR.Type =ICMPHDR.Code =ICMPHDR.Checksum =ICMPHDR.ID=ICMPHDR.Seq =ICMPHDR.Data;

[/code]

(2)如果只是要把 ICMPHDR 這個 struct 當成封包送出去的話,只需要像以下這樣子寫(不需要寫以上那一行程式碼)

[code cpp]
sendto(R_Socket, (LPSTR)&ICMPHDR, sizeof(ICMPHDR), // 後面省略);
[/code]

(3)另外,網路上有一些範例,你可以參考自行修改 http://80diy.com/home/20011028/14/344250.html

[code cpp]
//ping.h
//---------------------------------------------------------------------------
#ifndef PingH
#define PingH
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------

#pragma pack(1)

#define ICMP_ECHOREPLY 0
#define ICMP_ECHOREQ 8

// IP Header -- RFC 791
typedef struct tagIPHDR
{
u_char VIHL; // Version and IHL
u_char TOS; // Type Of Service
short TotLen; // Total Length
short ID; // Identification
short FlagOff; // Flags and Fragment Offset
u_char TTL; // Time To Live
u_char Protocol; // Protocol
u_short Checksum; // Checksum
struct in_addr iaSrc; // Internet Address - Source
struct in_addr iaDst; // Internet Address - Destination
}IPHDR, *PIPHDR;


// ICMP Header - RFC 792
typedef struct tagICMPHDR
{
u_char Type; // Type
u_char Code; // Code
u_short Checksum; // Checksum
u_short ID; // Identification
u_short Seq; // Sequence
char Data; // Data
}ICMPHDR, *PICMPHDR;


#define REQ_DATASIZE 32 // Echo Request Data size

// ICMP Echo Request
typedef struct tagECHOREQUEST
{
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
}ECHOREQUEST, *PECHOREQUEST;


// ICMP Echo Reply
typedef struct tagECHOREPLY
{
IPHDR ipHdr;
ECHOREQUEST echoRequest;
char cFiller[256];
}ECHOREPLY, *PECHOREPLY;
#pragma pack()

//--------------------------------------------------------------------------------
#define INIT_SUCCESS 0
#define TP_ERR_INIT -1
#define ERR_VERSION_NOT_SUPPORT -2
#define TP_ERR_INIT 0x1001

class TPing
{
private:
HWND m_hwnd;
TListBox * m_pReportListBox;
public:
__fastcall TPing(HWND hwnd,TListBox * pReportLst);
DWORD __fastcall Init();
void __fastcall UnLoad();
void __fastcall UserPing(LPCSTR pstrHost);
void __fastcall ReportError(LPCSTR pstrFrom);
int __fastcall WaitForEchoReply(SOCKET s);
u_short __fastcall in_cksum(u_short *addr, int len);

// ICMP Echo Request/Reply functions
int __fastcall SendEchoRequest(SOCKET, LPSOCKADDR_IN);
DWORD __fastcall RecvEchoReply(SOCKET, LPSOCKADDR_IN, u_char *);
};
//--------------------------------------------------------------------------------
#endif
[/code]

[code cpp]
//-------------------------------
//ping.cpp
//---------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Ping.h"

//--------------------------------------------------------------------------------
//
// PING.C -- Ping program using ICMP and RAW Sockets
//
__fastcall TPing::TPing(HWND hwnd,TListBox * pReportLst)
{
m_hwnd=hwnd;
m_pReportListBox=pReportLst;
}
//--------------------------------------------------------------------------------
void __fastcall TPing::UnLoad()
{
WSACleanup();
}
//--------------------------------------------------------------------------------
DWORD __fastcall TPing::Init()
{
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(1,1);
int nRet = WSAStartup(wVersionRequested, &wsaData);
if(nRet != 0)
return TP_ERR_INIT;
// Check version
if(wsaData.wVersion != wVersionRequested)
return ERR_VERSION_NOT_SUPPORT;
}
//--------------------------------------------------------------------------------

// Ping()
// Calls SendEchoRequest() and
// RecvEchoReply() and prints results
void __fastcall TPing::UserPing(LPCSTR pstrHost)
{
SOCKET rawSocket;
LPHOSTENT lpHost;
struct sockaddr_in saDest;
struct sockaddr_in saSrc;
DWORD dwTimeSent;
DWORD dwElapsed;
u_char cTTL;
int nLoop;
int nRet;
char buf[256];

// Create a Raw socket
rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(rawSocket == SOCKET_ERROR)
{
ReportError("socket()");
return;
}

// Lookup host
lpHost = gethostbyname(pstrHost);
if(lpHost == NULL)
{
sprintf(buf,"\nHost not found: %s\n", pstrHost);
m_pReportListBox->Items->Add(buf);
return;
}

// Setup destination socket address
saDest.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
saDest.sin_family = AF_INET;
saDest.sin_port = 0;

// Tell the user what we're doing

sprintf(buf,"\nPinging %s [%s] with %d bytes of data:\n",
pstrHost,
inet_ntoa(saDest.sin_addr),
REQ_DATASIZE);
m_pReportListBox->Items->Add(buf);

// Ping multiple times
for(nLoop = 0; nLoop < 4; nLoop )
{
// Send ICMP echo request
SendEchoRequest(rawSocket, &saDest);
// Use select() to wait for data to be received
nRet = WaitForEchoReply(rawSocket);
if (nRet == SOCKET_ERROR)
{
ReportError("select()");
break;
}
if(!nRet)
{
sprintf(buf,"TimeOut");
m_pReportListBox->Items->Add(buf);
break;
}

// Receive reply
dwTimeSent = RecvEchoReply(rawSocket, &saSrc, &cTTL);

// Calculate elapsed time
dwElapsed = GetTickCount() - dwTimeSent;
sprintf(buf,"Reply from: %s: bytes=%d time=%ldms TTL=%d",
inet_ntoa(saSrc.sin_addr),
REQ_DATASIZE,
dwElapsed,
cTTL);
m_pReportListBox->Items->Add(buf);
}
nRet = closesocket(rawSocket);
if (nRet == SOCKET_ERROR)
ReportError("closesocket()");
}

// SendEchoRequest()
// Fill in echo request header
// and send to destination
int __fastcall TPing::SendEchoRequest(SOCKET s,LPSOCKADDR_IN lpstToAddr)
{
static ECHOREQUEST echoReq;
static nId = 1;
static nSeq = 1;
int nRet;

// Fill in echo request
echoReq.icmpHdr.Type = ICMP_ECHOREQ;
echoReq.icmpHdr.Code = 0;
echoReq.icmpHdr.Checksum = 0;
echoReq.icmpHdr.ID = nId ;
echoReq.icmpHdr.Seq = nSeq ;

// Fill in some data to send
for(nRet = 0; nRet < REQ_DATASIZE; nRet )
echoReq.cData[nRet] = ' ' nRet;

// Save tick count when sent
echoReq.dwTime = GetTickCount();

// Put data in packet and compute checksum
echoReq.icmpHdr.Checksum = in_cksum((u_short *)&echoReq, sizeof(ECHOREQUEST));

// Send the echo request
nRet = sendto(s, // socket
(LPSTR)&echoReq, // buffer
sizeof(ECHOREQUEST),
0, // flags
(LPSOCKADDR)lpstToAddr, // destination
sizeof(SOCKADDR_IN)); // address length

if (nRet == SOCKET_ERROR)
ReportError("sendto()");
return (nRet);
}


// RecvEchoReply()
// Receive incoming data
// and parse out fields
DWORD __fastcall TPing::RecvEchoReply(SOCKET s, LPSOCKADDR_IN lpsaFrom, u_char *pTTL)
{
ECHOREPLY echoReply;
int nRet;
int nAddrLen = sizeof(struct sockaddr_in);

// Receive the echo reply
nRet = recvfrom(s, // socket
(LPSTR)&echoReply, // buffer
sizeof(ECHOREPLY), // size of buffer
0, // flags
(LPSOCKADDR)lpsaFrom, // From address
&nAddrLen); // pointer to address len

// Check return value
if (nRet == SOCKET_ERROR)
ReportError("recvfrom()");

// return time sent and IP TTL
*pTTL = echoReply.ipHdr.TTL;
return(echoReply.echoRequest.dwTime);
}

// What happened?
void __fastcall TPing::ReportError(LPCSTR pWhere)
{
char Buf[256];
sprintf(Buf,"\n%s error: %d\n",pWhere,WSAGetLastError());
m_pReportListBox->Items->Add(Buf);
}


// WaitForEchoReply()
// Use select() to determine when
// data is waiting to be read
int __fastcall TPing::WaitForEchoReply(SOCKET s)
{
struct timeval Timeout;
fd_set readfds;

readfds.fd_count = 1;
readfds.fd_array[0] = s;
Timeout.tv_sec = 5;
Timeout.tv_usec = 0;

return(select(1, &readfds, NULL, NULL, &Timeout));
}


//
// Mike Muuss' in_cksum() function
// and his comments from the original
// ping program
//
// * Author -
// * Mike Muuss
// * U. S. Army Ballistic Research Laboratory
// * December, 1983

//
// I N _ C K S U M
//
// Checksum routine for Internet Protocol family headers (C Version)
//
//
u_short __fastcall TPing::in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;

//
// Our algorithm is simple, using a 32 bit accumulator (sum),
// we add sequential 16 bit words to it, and at the end, fold
// back all the carry bits from the top 16 bits into the lower
// 16 bits.
//
while( nleft > 1 )
{
sum = *w ;
nleft -= 2;
}

// mop up an odd byte, if necessary
if( nleft == 1 )
{
u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ;
sum = u;
}

//
// add back carry outs from top 16 bits to low 16 bits
//
sum = (sum >> 16) (sum & 0xffff); // add hi 16 to low 16
sum = (sum >> 16); // add carry
answer = ~sum; // truncate to 16 bits
return (answer);
}
//---------------------------------------------------------------------------
#pragma package(smart_init)

[/code]
------
-- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
編輯記錄
RaynorPao 重新編輯於 2008-09-21 21:17:52, 註解 無‧
RaynorPao 重新編輯於 2008-09-21 21:21:19, 註解 無‧
RaynorPao 重新編輯於 2008-09-21 21:23:24, 註解 無‧
RaynorPao 重新編輯於 2008-09-21 21:25:39, 註解 無‧
victor-plus
一般會員


發表:4
回覆:2
積分:1
註冊:2008-09-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-09-22 17:46:44 IP:122.116.xxx.xxx 訂閱
謝謝上面大大的細心指導^^

原來是要用LPSTR這個東西呀@@
系統時間:2017-10-20 9:38:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!