⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 < WINDOWS网络编程>>英文版,一本详细讲解WINDOWS平台下网络编程的国外经典书籍,适合英文水平高的牛人
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// 
// Copyright (C) 1999  Microsoft Corporation.  All Rights Reserved.
//
// Module:
//    parser.c
//
// Abstract:
//    This sample illustrates how to use the new Winsock 2 ioctls:
//    SIO_RCVALL, SIO_RCVALL_MCAST, and SIO_RCVALL_IGMPMCAST.
//    
//    This module includes routines for parsing the IP headers read.
//    Not all protocol headers are parsed, just the most common ones
//    (IP, TCP, UDP, and IGMP). You can easily extend this to parse
//    any protocol headers not covered.
// 
// Usage:
//    See rcvall.c
//
// Build:
//    See rcvall.c
//
// Author:
//    Anthony Jones
//

#include <stdio.h>
#include <stdlib.h>

#include "parser.h"
#include "igmphdr.h"

extern BOOL bFilter;
//
// A list of protocol types in the IP protocol header
//
char *szProto[] = {"Reserved",     //  0
                   "ICMP",         //  1
                   "IGMP",         //  2
                   "GGP",          //  3
                   "IP",           //  4
                   "ST",           //  5
                   "TCP",          //  6
                   "UCL",          //  7
                   "EGP",          //  8
                   "IGP",          //  9
                   "BBN-RCC-MON",  // 10
                   "NVP-II",       // 11
                   "PUP",          // 12
                   "ARGUS",        // 13
                   "EMCON",        // 14
                   "XNET",         // 15
                   "CHAOS",        // 16
                   "UDP",          // 17
                   "MUX",          // 18
                   "DCN-MEAS",     // 19
                   "HMP",          // 20
                   "PRM",          // 21
                   "XNS-IDP",      // 22
                   "TRUNK-1",      // 23
                   "TRUNK-2",      // 24
                   "LEAF-1",       // 25
                   "LEAF-2",       // 26
                   "RDP",          // 27
                   "IRTP",         // 28
                   "ISO-TP4",      // 29
                   "NETBLT",       // 30
                   "MFE-NSP",      // 31
                   "MERIT-INP",    // 32
                   "SEP",          // 33
                   "3PC",          // 34
                   "IDPR",         // 35
                   "XTP",          // 36
                   "DDP",          // 37
                   "IDPR-CMTP",    // 38
                   "TP++",         // 39
                   "IL",           // 40
                   "SIP",          // 41
                   "SDRP",         // 42
                   "SIP-SR",       // 43
                   "SIP-FRAG",     // 44
                   "IDRP",         // 45
                   "RSVP",         // 46
                   "GRE",          // 47
                   "MHRP",         // 48
                   "BNA",          // 49
                   "SIPP-ESP",     // 50
                   "SIPP-AH",      // 51
                   "I-NLSP",       // 52
                   "SWIPE",        // 53
                   "NHRP",         // 54
                   "unassigned",   // 55
                   "unassigned",   // 56
                   "unassigned",   // 57
                   "unassigned",   // 58
                   "unassigned",   // 59
                   "unassigned",   // 60
                   "any host internal protocol",  // 61
                   "CFTP",         // 62
                   "any local network",           // 63
                   "SAT-EXPAK",    // 64
                   "KRYPTOLAN",    // 65
                   "RVD",          // 66
                   "IPPC",         // 67
                   "any distributed file system", // 68
                   "SAT-MON",    // 69
                   "VISA",       // 70
                   "IPCV",       // 71
                   "CPNX",       // 72
                   "CPHB",       // 73
                   "WSN",        // 74
                   "PVP",        // 75
                   "BR-SAT-MON", // 76
                   "SUN-ND",     // 77
                   "WB-MON",     // 78
                   "WB-EXPAK",   // 79
                   "ISO-IP",     // 80
                   "VMTP",       // 81
                   "SECURE-VMTP",// 82
                   "VINES",      // 83
                   "TTP",        // 84
                   "NSFNET-IGP", // 85
                   "DGP",        // 86
                   "TCF",        // 87
                   "IGRP",       // 88
                   "OSPFIGP",    // 89
                   "Sprite-RPC", // 90
                   "LARP",       // 91
                   "MTP",        // 92
                   "AX.25",      // 93
                   "IPIP",       // 94
                   "MICP",       // 95
                   "SCC-SP",     // 96
                   "ETHERIP",    // 97
                   "ENCAP",      // 98
                   "any private encryption scheme",    // 98
                   "GMTP"        // 99
                  };
//
// The types of IGMP messages
//
char *szIgmpType[] = {"Unknown",
                      "Host Membership Query",
                      "Host Membership Report",
                      "",
                      "",
                      "",
                      "Version 2 Membership Report",
                      "Leave Group"
                     };

//
// Function: PrintRawBytes
//
// Description:
//    This function simply prints out a series of bytes
//    as hexadecimal digits.
//
void PrintRawBytes(BYTE *ptr, DWORD len)
{
    int        i;

    while (len > 0)
    {
        for(i=0; i < 20 ;i++)
        {
            printf("%x%x ", HI_WORD(*ptr), LO_WORD(*ptr));
            len--;
            ptr++;
            if (len == 0)
                break;
        }
        printf("\n");
    }
}

//
// Function: DecodeIGMPHeader
//
// Description:
//    This function takes a pointer to a buffer containing
//    an IGMP packet and prints it out in a readable form.
//
int DecodeIGMPHeader(WSABUF *wsabuf, DWORD iphdrlen, DWORD iptotallen)
{
    BYTE          *hdr = (BYTE *)(wsabuf->buf + iphdrlen);
    unsigned short chksum,
                   version,
                   type,
                   message,
                   maxresptime;
    SOCKADDR_IN    addr;
    int            igmphdrlen;

    igmphdrlen = iptotallen - iphdrlen;

    version = HI_WORD(*hdr);
    type    = LO_WORD(*hdr);
    hdr++;

    maxresptime = *hdr;
    hdr++;

    memcpy(&chksum, hdr, 2);
    chksum = ntohs(chksum);
    hdr += 2;

    printf("   IGMP HEADER:\n");
    if ((version == 1) && (maxresptime == 0) && (igmphdrlen == 8) && (type == 1))
    {
        version = 1;
        message = IGMP_MEMBERSHIP_QUERY;
    }
    else if ((version == 1) && (maxresptime == 0) && (igmphdrlen == 8) && (type == 2))
    {
        version = 1;
        message = IGMP_MEMBERSHIP_REPORT;
    }
    else if ((version == 1) && (maxresptime != 0) && (igmphdrlen == 8) && (type == 1))
    {
        version = 2;
        message = IGMP_MEMBERSHIP_QUERY;
    }
    else if ((version == 1) && (igmphdrlen > 8) && (type == 1))
    {
        version = 3;
        message = IGMP_MEMBERSHIP_QUERY;
    }
    else if ((version == 1) && (maxresptime != 0) && (igmphdrlen == 8) && (type == 6))
    {
        version = 2;
        message = IGMP_MEMBERSHIP_REPORT_V2;
    }
    else if ((version == 1) && (type == 7))
    {
        version = 2;
        message = IGMP_LEAVE_GROUP;
    }
    else if ((version == 2) && (igmphdrlen > 8) && (type == 2))
    {
        version = 3;
        message = IGMP_MEMBERSHIP_REPORT_V3;
    }
    else
        version = 0;           // bad version

    printf("   IGMP Version = %d\n", version);
    printf("   IGMP Type = %s\n", szIgmpType[type]);
    if (version >= 2)
        printf("   Max Resp Time = %d\n", maxresptime);
    if (message == IGMP_MEMBERSHIP_QUERY)
    {
        memcpy(&(addr.sin_addr.s_addr), hdr, 4);
        hdr += 4;

        printf("   IGMP Grp Addr = %s\n", inet_ntoa(addr.sin_addr));
    }
    if ((version == 3) && (message == IGMP_MEMBERSHIP_QUERY))
    {
        ULONG  *source=NULL;
        USHORT  numsources=0, i;
        UCHAR   S, QRV, QQI, RESV;
        int     realsourcecount;

        QRV = S = RESV = (UCHAR)*hdr;
        S = (S >> 3) & 0x7;
        QRV = QRV & 0x7;
        RESV = RESV >> 4;
        hdr++;
         
        QQI = *hdr;
        hdr++;
        
        printf("   Reserve Field:                   %d\n", RESV);
        printf("   Suppress Router Side Processing: %s\n", (S ? "True" : "False"));
        printf("   Querier  Robustness Variable:    %d\n", QRV);
        printf("   Querier  Query Interval:         %d\n", QQI);
        memcpy(&numsources, hdr, 2);
        numsources = ntohs(numsources);
        hdr += 2;
        
        realsourcecount = (igmphdrlen - 12) / 4;
        if (realsourcecount != numsources)
            printf("   ERROR: Calculated source count and num source field don't correspond!\n");

        printf("    Source Count: %d\n", numsources);
        for(i=0; i < realsourcecount ;i++)
        {
            memcpy(&(addr.sin_addr.s_addr), hdr, 4);
            hdr += 4;

            printf("    Source IP: %s\n", inet_ntoa(addr.sin_addr));
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -