ICMP 協議用於ping 網路狀態的原理與實現

2021-08-22 10:27:42 字數 3643 閱讀 1173

具體**實現:

ping.h

#pragma once

//在預設windows.h會包含winsock.h,當你包含winsock2.h就會衝突,因此在包含windows.h前需要定義乙個巨集,#define win32_lean_and_mean ;去除winsock.h

//要麼將#include 放在#include前面或者直接去掉#include

/*system32中有icmp.dll檔案,其中包含函式:

icmpcreatefile()

icmpsendecho(8個引數)

icmpclosehandle

*/#include

#include

#pragma comment(lib, "ws2_32") // 鏈結到ws2_32.lib

#define def_packet_size 32

#define echo_request 8

#define echo_reply 0

struct ipheader

;struct icmpheader

;struct pingreply

;class cping

;

ping.cpp

#include "ping.h"

#include

ushort cping::s_uspacketseq = 0;

cping::cping() :m_szicmpdata(null),m_bisinitsucc(false)

m_event = wsacreateevent();

m_uscurrentprocid = (ushort)getcurrentprocessid();

//setsockopt(m_sockraw);

/*if ((m_sockraw = wsasocket(af_inet, sock_raw, ipproto_icmp, null, 0, 0)) != socket_error)

}*/m_sockraw = wsasocket(af_inet, sock_raw, ipproto_icmp, null, 0, 0);

if (m_sockraw == invalid_socket)

else

}}cping::~cping()

}bool cping::ping(dword dwdestip, pingreply *ppingreply, dword dwtimeout)

bool cping::ping(const

char *szdestip, pingreply *ppingreply, dword dwtimeout)

return pingcore(/*inet_addr(szdestip)*/dwip, ppingreply, dwtimeout);

}return

false;

}bool cping::pingcore(dword dwdestip, pingreply *ppingreply, dword dwtimeout)

//配置socket

sockaddr_in sockaddrdest;

sockaddrdest.sin_family = af_inet;

sockaddrdest.sin_addr

.s_addr = dwdestip;

int nsockaddrdestsize = sizeof(sockaddrdest);

//構建icmp包

int nicmpdatasize = def_packet_size + sizeof(icmpheader);

ulong ulsendtimestamp = gettickcountcalibrate();

ushort usseq = ++s_uspacketseq;

memset(m_szicmpdata, 0, nicmpdatasize);

icmpheader *picmpheader = (icmpheader*)m_szicmpdata;

picmpheader->m_bytype = echo_request;

picmpheader->m_bycode = 0;

picmpheader->m_usid = m_uscurrentprocid;

picmpheader->m_usseq = usseq;

picmpheader->m_ultimestamp = ulsendtimestamp;

picmpheader->m_uschecksum = calchecksum((ushort*)m_szicmpdata, nicmpdatasize);

//傳送icmp報文

if (sendto(m_sockraw, m_szicmpdata, nicmpdatasize, 0, (struct sockaddr*)&sockaddrdest, nsockaddrdestsize) == socket_error)

//判斷是否需要接收相應報文

if (ppingreply == null)

char recvbuf[256] = ;

while (true)}}

}//超時

if (gettickcountcalibrate() - ulsendtimestamp >= dwtimeout)

}}ushort cping::calchecksum(ushort *pbuffer, int nsize)

if (nsize)

ulchecksum = (ulchecksum >> 16) + (ulchecksum & 0xffff);

ulchecksum += (ulchecksum >> 16);

return (ushort)(~ulchecksum);

}ulong cping::gettickcountcalibrate()

if (s_ullfirstcalltickms == 0)

return s_ulfirstcalltick + (ulong)(llcurrenttimems - s_ullfirstcalltickms);

}

main.cpp

/*

icmp是(internet control message protocol)internet控制報文協議。它是tcp/ip協議族的乙個子協議,屬於網路層協議, 用於在ip主機、路由器之間傳遞控制訊息。控制訊息是指網路通不通、主機是否可達、路由是否可用等網路本身的訊息。這些控制訊息雖然並不傳輸使用者資料,但是對於使用者資料的傳遞起著重要的作用

*/#include

#include

#include "ping.h"

#include "commdef.h"

int main(void)

; switch (reply.m_dwttl)

printf("reply from %s: bytes=%d time=%ldms ttl=%ld ostype=%s\n", szdestip, reply.m_dwbytes, reply.m_dwroundtriptime, reply.m_dwttl, szostype);

sleep(500);

}return

0;}

使用ICMP協議Ping網路主機

coding utf 8 usr bin env python import osimport argparse import socket import struct import select import time icmp echo request 8 platform specific 超...

ICMP協議之ping實現

1.相關icmp協議概述 這裡只講解與ping有關的icmp訊息型別,主機傳送回送訊息 type 8 被請求主機回送響應訊息 type 0 基本格式如下 回送訊息 echo 回送響應訊息 echo reply 其中 code 0,checksum為校驗和,重點注意從icmp的頭部 即type開始 到...

ping 原理與ICMP協議

ping 原理與icmp協議 ping的原理 ping程式是用來探測主機到主機之間是否可通訊,如果不能ping到某台主機,表明不能和這台主機建立連線。ping使用的是icmp協議,它傳送icmp回送請求訊息給目的主機。icmp協議規定 目的主機必須返回icmp回送應答訊息給源主機。如果源主機在一定時...