資源簡介
RAW模式的SOCKET編程 PING程序是面向用戶的應用程序,該程序使用ICMP的封裝機制,通過IP協議來工作。為了實現直接對IP和ICMP包進行操作,實驗中使用RAW模式的SOCKET編程。熟悉SOCKET的編程,包括基本的系統調用如SOCKET、BIND等; 2、具體內容 1) 定義數據結構需要定義好IP數據報、ICMP包等相關的數據結構; 2) 程序實現在WINDOWS環境下實現PING程序; 3) 程序要求在命令提示符下輸入: PING ΧΧΧ.ΧΧΧ.ΧΧΧ.ΧΧΧ 其中ΧΧΧ為目的主機的IP地址,不要求支持域名,對是否帶有開關變量也不做要求。不帶開關變量時,要求返回4次響應。返回

代碼片段和文件信息
//?ping.cpp?:?定義控制臺應用程序的入口點。
#include
#include
#include
#define?SEND_SIZE?32?
#define?PACKET_SIZE?4096
#define?ICMP_ECHO?8
#define?ICMP_ECHOREPLY?0
#pragma?comment(lib“Ws2_32.lib“)
struct?icmp
{
unsigned?char?icmp_type;????//類型
unsigned?char?icmp_code;????//編碼
unsigned?short?icmp_chksum;?//校驗和
unsigned?short?icmp_id;?????//標示符
unsigned?short?icmp_seq;????//順序號
unsigned?long?icmp_data;????//數據
};
struct?ip
{
unsigned?char?ip_hl:4;???????//報頭長度
unsigned?char?ip_v:4;????????//版本號
unsigned?char?ip_tos;????????//服務類型
unsigned?short?ip_len;???????//總長度
unsigned?short?ip_id;????????//標識
unsigned?short?ip_off;???????//標志
unsigned?char?ip_ttl;????????//生存時間
unsigned?char?ip_p;??????????//協議號
unsigned?short?ip_sum;???????//報頭校驗和
unsigned?long?ip_src;????????//源IP地址
unsigned?long?ip_dst;????????//目的IP地址
};
char?sendpacket[PACKET_SIZE];
char?recvpacket[PACKET_SIZE];
struct?sockaddr_in?dest_addr;???
struct?sockaddr_in?from_addr;
int?sockfd;
int?pid;
unsigned?short?cal_chksum(unsigned?short?*addrint?len);
int?pack(int?pack_no);
int?unpack(unsigned?char?*bufint?len);
void?send_packet(void);
void?recv_packet(void);
int?main(int?argc?CHAR*?argv[])
{
struct?hostent?*host;
struct?protoent?*protocol;
int?timeout=1000;
int?SEND_COUNT=4;
int?i;
char?*par_host;
char?m_Input[100];
printf(“Input?IP:?“);
gets(m_Input);
par_host=m_Input;
WORD?wVersionRequested;
WSADATA?wsaData;
int?err;
wVersionRequested?=?MAKEWORD(?2?2?);
err?=?WSAStartup(?wVersionRequested?&wsaData?);
if?(?err?!=?0?)??return;
if?(?LOBYTE(?wsaData.wVersion?)?!=?2?||
HIBYTE(?wsaData.wVersion?)?!=?2?)?{
WSACleanup(?);
return;?
}?????????????????????????????????????????????????????????????????????
if(?(protocol=getprotobyname(“icmp“)?)==NULL)????//返回對應于給定協議名的包含名字和協議號
{?
printf(“getprotobyname?error\n“);???????????//的protoent結構指針
exit(1);
}
if(?(sockfd=socket(AF_INETSOCK_RAWprotocol->p_proto)?)<0)
{???
printf(“socket?error\n“);
exit(1);
}?????????????????????????????????????????????????????????????????????????
if(setsockopt(sockfdSOL_SOCKETSO_RCVTIMEO(char*)&timeoutsizeof(timeout))<0)??//設置套接口的選項
fprintf(stderr“failed?to?set?recv?timeout:?%d\n“WSAGetLastError());
if(setsockopt(sockfdSOL_SOCKETSO_SNDTIMEO(char*)&timeoutsizeof(timeout))<0)
fprintf(stderr“failed?to?set?send?timeout:?%d\n“WSAGetLastError());???????
memset(&dest_addr0sizeof(dest_addr));
dest_addr.sin_family=AF_INET;?
if(host=gethostbyname(par_host)?)????????//?返回對應于給定主機名的主機信息????????????????????????????
{
?????memcpy(?(char?*)&dest_addr.sin_addrhost->h_addrhost->h_length);
//resolve?address?to?hostname
if(host=gethostbyaddr(host->h_addr4PF_INET))??????????????????????
par_host=host->h_name;??????????
}
else?if(?dest_addr.sin_addr.s_addr=inet_addr(par_host)==INADDR_NONE)
{
printf(“Unkown?host?%s\n“par
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????196695??2009-12-30?10:51??Ping\Debug\Ping.exe
?????文件?????209128??2009-12-30?10:51??Ping\Debug\Ping.ilk
?????文件??????21349??2009-12-30?10:51??Ping\Debug\Ping.obj
?????文件????3644536??2009-12-30?10:51??Ping\Debug\Ping.pch
?????文件?????377856??2009-12-30?10:51??Ping\Debug\Ping.pdb
?????文件?????148480??2009-12-30?10:51??Ping\Debug\vc60.idb
?????文件??????77824??2009-12-30?10:51??Ping\Debug\vc60.pdb
?????文件???????5627??2009-12-30?10:51??Ping\Ping.cpp
?????文件???????3377??2009-12-30?10:51??Ping\Ping.dsp
?????文件????????516??2009-12-30?10:51??Ping\Ping.dsw
?????文件??????33792??2009-12-30?10:51??Ping\Ping.ncb
?????文件??????48640??2009-12-30?10:51??Ping\Ping.opt
?????文件????????736??2009-12-30?10:51??Ping\Ping.plg
?????文件?????275968??2009-12-31?20:25??Ping\報告.doc
?????目錄??????????0??2009-12-30?10:51??Ping\Debug
?????目錄??????????0??2009-12-31?20:33??Ping
-----------?---------??----------?-----??----
??????????????5044524????????????????????16
評論
共有 條評論