Skip to content

Commit

Permalink
添加SDK的ping例子。
Browse files Browse the repository at this point in the history
  • Loading branch information
kouzhudong committed Jan 29, 2024
1 parent cc738b5 commit 43c501c
Show file tree
Hide file tree
Showing 10 changed files with 1,209 additions and 7 deletions.
Binary file modified libnet/IpHelper.h
Binary file not shown.
819 changes: 819 additions & 0 deletions libnet/Ping.cpp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion libnet/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma warning(disable:28251)
#pragma warning(disable:28301)
#pragma warning(disable:26812) //相比于 "enum",首选 "enum class" (Enum.3)


/////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -64,7 +65,7 @@
//#include <ntdef.h>
//#include <netioapi.h>
//#include <LsaLookup.h>
#include <netiodef.h>
//#include <netiodef.h>
#include <comutil.h>
#include <wbemidl.h>
#include <dbt.h>
Expand Down
126 changes: 126 additions & 0 deletions libnet/iphdr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// From Network Programming for Microsoft Windows, Second Edition by
// Anthony Jones and James Ohlund.
// Copyright 2002. Reproduced by permission of Microsoft Press.
// All rights reserved.
//
//
// Sample: Protocol header definitions used by ping (raw sockets)
//
// Files:
// iphdr.h - this file
//
// Description:
// This file contains various protocol header definitions used by
// the raw socket ping sample.
//
// Compile:
// See ping.cpp
//
// Usage:
// See ping.cpp
//

// Align on a 1-byte boundary
#include <pshpack1.h>

// IPv4 header
typedef struct ip_hdr
{
unsigned char ip_verlen; // 4-bit IPv4 version
// 4-bit header length (in 32-bit words)
unsigned char ip_tos; // IP type of service
unsigned short ip_totallength; // Total length
unsigned short ip_id; // Unique identifier
unsigned short ip_offset; // Fragment offset field
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Source address
} IPV4_HDR, *PIPV4_HDR, FAR * LPIPV4_HDR;

// IPv4 option header
typedef struct ipv4_option_hdr
{
unsigned char opt_code; // option type
unsigned char opt_len; // length of the option header
unsigned char opt_ptr; // offset into options
unsigned long opt_addr[9]; // list of IPv4 addresses
} IPV4_OPTION_HDR, *PIPV4_OPTION_HDR, FAR *LPIPV4_OPTION_HDR;

// ICMP header
typedef struct icmp_hdr
{
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short icmp_checksum;
unsigned short icmp_id;
unsigned short icmp_sequence;
} ICMP_HDR, *PICMP_HDR, FAR *LPICMP_HDR;

// IPv6 protocol header
typedef struct ipv6_hdr
{
unsigned long ipv6_vertcflow; // 4-bit IPv6 version
// 8-bit traffic class
// 20-bit flow label
unsigned short ipv6_payloadlen; // payload length
unsigned char ipv6_nexthdr; // next header protocol value
unsigned char ipv6_hoplimit; // TTL
struct in6_addr ipv6_srcaddr; // Source address
struct in6_addr ipv6_destaddr; // Destination address
} IPV6_HDR, *PIPV6_HDR, FAR * LPIPV6_HDR;

// IPv6 fragment header
typedef struct ipv6_fragment_hdr
{
unsigned char ipv6_frag_nexthdr;
unsigned char ipv6_frag_reserved;
unsigned short ipv6_frag_offset;
unsigned long ipv6_frag_id;
} IPV6_FRAGMENT_HDR, *PIPV6_FRAGMENT_HDR, FAR * LPIPV6_FRAGMENT_HDR;

// ICMPv6 header
typedef struct icmpv6_hdr {
unsigned char icmp6_type;
unsigned char icmp6_code;
unsigned short icmp6_checksum;
} ICMPV6_HDR;

// ICMPv6 echo request body
typedef struct icmpv6_echo_request
{
unsigned short icmp6_echo_id;
unsigned short icmp6_echo_sequence;
} ICMPV6_ECHO_REQUEST;

// Define the UDP header
typedef struct udp_hdr
{
unsigned short src_portno; // Source port no.
unsigned short dst_portno; // Dest. port no.
unsigned short udp_length; // Udp packet length
unsigned short udp_checksum; // Udp checksum (optional)
} UDP_HDR, *PUDP_HDR;

// IPv4 option for record route
#define IP_RECORD_ROUTE 0x7

// ICMP6 protocol value (used in the socket call and IPv6 header)
#define IPPROTO_ICMP6 58

// ICMP types and codes
#define ICMPV4_ECHO_REQUEST_TYPE 8
#define ICMPV4_ECHO_REQUEST_CODE 0
#define ICMPV4_ECHO_REPLY_TYPE 0
#define ICMPV4_ECHO_REPLY_CODE 0
#define ICMPV4_MINIMUM_HEADER 8

// ICPM6 types and codes
#define ICMPV6_ECHO_REQUEST_TYPE 128
#define ICMPV6_ECHO_REQUEST_CODE 0
#define ICMPV6_ECHO_REPLY_TYPE 129
#define ICMPV6_ECHO_REPLY_CODE 0

// Restore byte alignment back to default
#include <poppack.h>
4 changes: 4 additions & 0 deletions libnet/libnet.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
<ClInclude Include="html.h" />
<ClInclude Include="IOCTL.h" />
<ClInclude Include="IpAddr.h" />
<ClInclude Include="iphdr.h" />
<ClInclude Include="IpHelper.h" />
<ClInclude Include="log.h" />
<ClInclude Include="NetApi.h" />
Expand All @@ -201,6 +202,7 @@
<ClInclude Include="pathpings.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="raw.h" />
<ClInclude Include="resolve.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="Sock.h" />
<ClInclude Include="spi.h" />
Expand Down Expand Up @@ -234,7 +236,9 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Ping.cpp" />
<ClCompile Include="raw.cpp" />
<ClCompile Include="resolve.cpp" />
<ClCompile Include="Sock.cpp" />
<ClCompile Include="spi.cpp" />
<ClCompile Include="tcp.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions libnet/libnet.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<ClInclude Include="NetworkListManager.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="iphdr.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="resolve.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
Expand Down Expand Up @@ -182,6 +188,12 @@
<ClCompile Include="NetworkListManager.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Ping.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="resolve.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="readme.txt" />
Expand Down
Binary file modified libnet/raw.h
Binary file not shown.
Loading

0 comments on commit 43c501c

Please sign in to comment.