Ping program implementation in Windows

Source: Internet
Author: User

Implementation of the Ping program in Windows: the original socket sock_raw is used based on the ICMP protocol on the IP protocol. The type of the ICMP packet sent is 8 (Request Reply ), the type of the ICMP packet received by the reply is 0. To determine whether the received packet is sent by the current process. Set the ICMP packet ID as the process ID. If the-r option is used, the intermediate route is recorded.

Reference code:

// Description://    This sample illustrates how an ICMP ping app can be written//    using the SOCK_RAW socket type and IPPROTO_ICMP protocol.//    By creating a raw socket, the underlying layer does not change//    the protocol header so that when we submit the ICMP header//    nothing is changed so that the receiving end will see an //    ICMP packet. Additionally, we use the record route IP option//    to get a round trip path to the endpoint. Note that the size//    of the IP option header that records the route is limited to//    nine IP addresses.//// Compile://     cl -o Ping Ping.c ws2_32.lib /Zp1//// Command Line Options/Parameters://     Ping [host] [packet-size]//     //     host         String name of host to ping//     packet-size  Integer size of packet to send //                      (smaller than 1024 bytes)////#pragma pack(1)#include "stdafx.h"#include <winsock2.h>#include <ws2tcpip.h>#include <stdio.h>#include <stdlib.h>#define WIN32_LEAN_AND_MEAN#pragma comment(lib,"ws2_32.lib")#define IP_RECORD_ROUTE  0x7// IP header structuretypedef struct _iphdr {    //Suppose the BYTE_ORDER is LITTLE_ENDIAN    unsigned int   h_len:4;        // Length of the header    unsigned int   version:4;      // Version of IP    unsigned char  tos;            // Type of service    unsigned short total_len;      // Total length of the packet    unsigned short id;             // Unique identification    unsigned short frag_offset;    // Fragment offset    unsigned char  ttl;            // Time to live    unsigned char  protocol;       // Protocol (TCP, UDP etc)    unsigned short checksum;       // IP checksum    unsigned int   sourceIP;       // Source IP    unsigned int   destIP;         // Destination IP} IpHeader;#define ICMP_ECHO        8#define ICMP_ECHOREPLY   0#define ICMP_MIN         8 // Minimum 8-byte ICMP packet (header)// ICMP header structure// This is not the standard header, but we reserve space for timetypedef struct _icmphdr {    BYTE   i_type;    BYTE   i_code;                 // Type sub code    USHORT i_cksum;    USHORT i_id;    USHORT i_seq;    ULONG  timestamp;} IcmpHeader;// IP option header - use with socket option IP_OPTIONStypedef struct _ipoptionhdr{    unsigned char code;        // Option type    unsigned char len;         // Length of option hdr    unsigned char ptr;         // Offset into options    unsigned long addr[9];     // List of IP addrs} IpOptionHeader;#define DEF_PACKET_SIZE  32        // Default packet size#define MAX_PACKET       1024      // Max ICMP packet size#define MAX_IP_HDR_SIZE  60        // Max IP header size w/optionsBOOL  bRecordRoute;int   datasize;char *lpdest;// Function: usage// Description://    Print usage informationvoid usage(char *progname){    printf("usage: ping -r 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.