summaryrefslogtreecommitdiff
path: root/general/pcidrv/test/myping.c
blob: b0c2d22cd0dfd3b8b40da2c022326d401fea9eac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    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.

Module Name:

    MYPING.C

Abstract:



Environment:

    usermode console application

--*/
#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include "testapp.h"
#include "nuiouser.h"
#include <intsafe.h>

#define MAX_HEADER_SIZE (sizeof(ETH_HEADER) + sizeof(IpHeader) + sizeof(IcmpHeader))
#define MAX_ECHO_PAY_LOAD (ETH_MAX_PACKET_SIZE - MAX_HEADER_SIZE)


#define ETH_HEADER_SIZE             14
#define ETH_MAX_DATA_SIZE           1500
#define ETH_MAX_PACKET_SIZE         ETH_HEADER_SIZE + ETH_MAX_DATA_SIZE
#define ETH_MIN_PACKET_SIZE         60

#define ARP_ETYPE_ARP   0x806
#define IP_PROT_TYPE   0x800
#define ARP_REQUEST     1
#define ARP_RESPONSE    2
#define ARP_HW_ENET     1

#define PROTOCOL_ICMP   1

// 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

#define DEFAULT_DATA_SIZE      32       // default data size

#define DEFAULT_SEND_COUNT     32767   // number of ICMP requests to send

#define DEFAULT_RECV_TIMEOUT   6000     // six second

#define DEFAULT_TTL            128

#define IP_ADDR_LEN             4

#include <pshpack1.h>

// ========================================================================
// The IP header
//

typedef struct iphdr {
    unsigned char  verlen;
    unsigned char  tos;            // Type of service
    unsigned short total_len;      // total length of the packet
    unsigned short ident;          // unique identifier
    unsigned short frag_and_flags; // flags
    unsigned char  ttl;            // time to live
    unsigned char  proto;          // protocol (TCP, UDP etc)
    unsigned char  checksumHigh;
    unsigned char  checksumLow;
    unsigned int sourceIP;         // source      ip address
    unsigned int destIP;           // destination ip address

} IpHeader;

// ========================================================================
// ICMP header
//

typedef struct _ihdr {
    BYTE   i_type;
    BYTE   i_code;    /* type sub code */
    USHORT i_cksum;
    USHORT i_id;
    USHORT i_seq;
    ULONG  timestamp; /* non standard, reserve space for time */
} IcmpHeader;

typedef struct _ETH_HEADER
{
    UCHAR       DstAddr[MAC_ADDR_LEN];
    UCHAR       SrcAddr[MAC_ADDR_LEN];
    USHORT      EthType;
} ETH_HEADER, *PETH_HEADER;

// Structure of an ARP header.
typedef struct _ARP_BODY {
    USHORT      hw;                      // Hardware address space. = 00 01
    USHORT      pro;                     // Protocol address space. = 08 00
    UCHAR       hlen;                    // Hardware address length. = 06
    UCHAR       plen;                    // Protocol address length.  = 04
    USHORT      opcode;                  // Opcode.
    UCHAR       SenderHwAddr[MAC_ADDR_LEN]; // Source HW address.
    UINT        SenderIpAddr;                  // Source protocol address.
    UCHAR       DestHwAddr[MAC_ADDR_LEN]; // Destination HW address.
    UINT        DestIPAddr;                  // Destination protocol address.
} _ARP_BODY, *PARP_BODY;

#include <poppack.h>

//
// For Read operation.
//
typedef struct _RCB {
    OVERLAPPED      Overlapped;
    char            Buffer[ETH_MAX_PACKET_SIZE];
    PDEVICE_INFO    DeviceInfo;
}RCB, *PRCB;

//
// For Write operation.
//
typedef struct _TCB {
    OVERLAPPED      Overlapped;
    char            *Buffer; // packet length is user specified.
    ULONG           BufferLength;
    PDEVICE_INFO    DeviceInfo;

}TCB, *PTCB;

unsigned short PacketId;

VOID
PostNextRead(
    RCB *pRCB
    );


//
// Function: SetIcmpSequence
//
// Description:
//    This routine sets the sequence number of the ICMP request packet.
//
VOID
SetIcmpSequence(
    _At_((IcmpHeader*)buf, _Out_writes_bytes_all_(bufSize)) char *buf,
    _In_ _In_range_(==, sizeof(IcmpHeader)) ULONG bufSize
    )
{
    ULONG    sequence=0;
    IcmpHeader    *icmpv4=NULL;

    #pragma prefast(suppress:__WARNING_USE_OTHER_FUNCTION, "The recommended function GeTickCount64 is available only on Windows Vista/Server 2008 and above")
    sequence = GetTickCount();

    icmpv4 = (IcmpHeader *)buf;

    icmpv4->i_seq = (USHORT)sequence;
}

//
// Using _Inexpressible_ to suppress prefast warning 2014 : "Potential overflow
// using expression at icmp_hdr->i_code"
//
VOID
InitIcmpHeader(
    _Out_writes_bytes_(_Inexpressible_(bufSize + datasize)) char *buf,
    ULONG bufSize,
    int datasize)
{
    IcmpHeader   *icmp_hdr=NULL;
    char       *datapart=NULL;

    icmp_hdr = (IcmpHeader *)buf;
    icmp_hdr->i_type     = ICMPV4_ECHO_REQUEST_TYPE;        // request an ICMP echo
    icmp_hdr->i_code     = ICMPV4_ECHO_REQUEST_CODE;
    icmp_hdr->i_id       = htons((USHORT)PacketId++);
    icmp_hdr->i_cksum = 0;
    icmp_hdr->i_seq = 0;
    #pragma prefast(suppress:__WARNING_USE_OTHER_FUNCTION, "The recommended function GeTickCount64 is available only on Windows Vista/Server 2008 and above")
    icmp_hdr->timestamp= htonl(GetTickCount());

    datapart = buf + sizeof(IcmpHeader);
    //
    // Place some junk in the buffer.
    //
    memset(datapart, 'X', datasize);
}

VOID
ComputeIPChecksum (
    IpHeader    *pIPHeader
    )
//////////////////////////////////////////////////////////////////////////////
{
    ULONG           Checksum;
    PUCHAR          NextChar;

    pIPHeader->checksumHigh = pIPHeader->checksumLow = 0;
    Checksum = 0;
    for ( NextChar = (PUCHAR) pIPHeader
        ; (NextChar - (PUCHAR) pIPHeader) <= (sizeof(IpHeader) - 2)
        ; NextChar += 2)
    {
        Checksum += ((ULONG) (NextChar[0]) << 8) + (ULONG) (NextChar[1]);
    }

    Checksum = (Checksum >> 16) + (Checksum & 0xffff);
    Checksum += (Checksum >> 16);
    Checksum = ~Checksum;

    pIPHeader->checksumHigh = (UCHAR) ((Checksum >> 8) & 0xff);
    pIPHeader->checksumLow = (UCHAR) (Checksum & 0xff);
}

//
// Function: checksum
//
// Description:
//    This function calculates the 16-bit one's complement sum
//    of the supplied buffer (ICMP) header.
//
USHORT
checksum(
    USHORT *buffer,
    int size
    )
{
    unsigned long cksum=0;

    while (size > 1)
    {
        cksum += *buffer++;
        size -= sizeof(USHORT);
    }
    if (size)
    {
        cksum += *(UCHAR*)buffer;
    }
    cksum = (cksum >> 16) + (cksum & 0xffff);
    cksum += (cksum >>16);
    return (USHORT)(~cksum);
}


VOID
ComputeIcmpChecksum(
    _At_((IcmpHeader*) buf, _Inout_updates_bytes_all_(bufSize)) char *buf,
    _In_ _In_range_(==, sizeof(IcmpHeader)) ULONG bufSize,
    int packetlen
    )
{
        IcmpHeader *icmpv4;

        icmpv4 = (IcmpHeader *)buf;
        icmpv4->i_cksum = 0;
        icmpv4->i_cksum = checksum((USHORT *)buf, packetlen);
}

void
InitEtherHeader(
    PDEVICE_INFO DeviceInfo,
    PETH_HEADER pEthHeader
    )
{

    pEthHeader->EthType = htons(IP_PROT_TYPE);
    memcpy(pEthHeader->DstAddr, DeviceInfo->TargetMacAddr, MAC_ADDR_LEN);
    memcpy(pEthHeader->SrcAddr, DeviceInfo->SrcMacAddr, MAC_ADDR_LEN);


}

BOOL
InitIpHeader(
    PDEVICE_INFO DeviceInfo,
    IpHeader *pIpHeader,
    unsigned short IpLength)
{
    pIpHeader->verlen = 0x45;
    pIpHeader->tos = 0; //Normal service
    pIpHeader->total_len = htons(IpLength);
    pIpHeader->ident = htons(0xABBA);
    pIpHeader->frag_and_flags =  0;
    pIpHeader->ttl = DEFAULT_TTL;
    pIpHeader->proto = PROTOCOL_ICMP;
    #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
    pIpHeader->sourceIP = inet_addr(DeviceInfo->SourceIp);
    #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
    pIpHeader->destIP = inet_addr(DeviceInfo->DestIp);

    return TRUE;
}
VOID WriteComplete(DWORD dwError, DWORD dwBytesTransferred, LPOVERLAPPED pOvl)
{
   TCB* pTCB = (TCB *)pOvl;

    if (dwError) {
        if(dwError == ERROR_DEVICE_NOT_CONNECTED) {
            Display(TEXT("WriteComplete: Device not connected"));
        }
        else {
            Display(TEXT("WriteComplete: Error %x"), dwError);

        }
    }

    DisplayV(TEXT("Write Complete: %x"), dwBytesTransferred);
    HeapFree (GetProcessHeap(), 0, pTCB->Buffer);
    HeapFree (GetProcessHeap(), 0, pTCB);
}

VOID WriteCompleteArp(DWORD dwError, DWORD dwBytesTransferred, LPOVERLAPPED pOvl)
{
   if (dwError) {
        if(dwError == ERROR_DEVICE_NOT_CONNECTED) {
            Display(TEXT("WriteCompleteArp: Device not connected"));
        }
        else {
            Display(TEXT("WriteCompleteArp: Error %x"), dwError);

        }
    }

    DisplayV(TEXT("Write Complete ARP: %x"), dwBytesTransferred);
}

VOID
ReadMacAddrComplete(
    DWORD dwError,
    DWORD dwBytesTransferred,
    LPOVERLAPPED pOvl
    )
{
    if (ERROR_OPERATION_ABORTED != dwError)
    {
        RCB* pRCB = (RCB *)pOvl;

        char *Buffer = pRCB->Buffer;
        PETH_HEADER ethHeader = (PETH_HEADER) Buffer;
        PARP_BODY   pBody;

        PDEVICE_INFO deviceInfo = pRCB->DeviceInfo;
        WCHAR unicodeIpAddr[MAX_LEN];
        char *ipAddr;

        DisplayV(TEXT("ReadMacAddrComplete: %x"), dwBytesTransferred);

         if(ntohs(ethHeader->EthType) == ARP_ETYPE_ARP){

             pBody = (PARP_BODY)(Buffer + sizeof(ETH_HEADER));

             if(ntohs(pBody->opcode) == ARP_RESPONSE){
                #pragma prefast(suppress:__WARNING_IPV6_ADDRESS_STRUCTURE_IPV4_SPECIFIC, "This test app doesn't support IPv6")
                struct in_addr IPAddr = {0};

                memcpy(&IPAddr, &pBody->SenderIpAddr, IP_ADDR_LEN);

                if(memcmp(pBody->DestHwAddr, deviceInfo->SrcMacAddr, MAC_ADDR_LEN) == 0){

                   #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
                   ipAddr = inet_ntoa(IPAddr);

                   if (!MultiByteToWideChar (
                                CP_ACP,
                                0,
                                ipAddr,
                                -1, // string is null terminated
                                unicodeIpAddr,
                                sizeof(unicodeIpAddr)/sizeof(WCHAR)
                                )) {
                        Display(TEXT("AnsitoUnicode conversion failed"));

                   }

                   DisplayV(TEXT("Target IP Address: %ws"), unicodeIpAddr);
                   
                   DisplayV(TEXT("Target Mac Address: %02X-%02X-%02X-%02X-%02X-%02X-"),
                                        pBody->SenderHwAddr[0],
                                        pBody->SenderHwAddr[1],
                                        pBody->SenderHwAddr[2],
                                        pBody->SenderHwAddr[3],
                                        pBody->SenderHwAddr[4],
                                        pBody->SenderHwAddr[5]
                                        );
                   memcpy(deviceInfo->TargetMacAddr, pBody->SenderHwAddr, MAC_ADDR_LEN);

                   SetEvent(deviceInfo->PingEvent);
                   return;
                }
            }
        }

        if(!ReadFileEx(pRCB->DeviceInfo->hDevice, pRCB->Buffer, sizeof(pRCB->Buffer),
            &pRCB->Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE) ReadMacAddrComplete)){
            Display(TEXT("ReadMacAddrComplete: ReadFileEx failed %x"), GetLastError());
        }

    }
}

BOOL
GetTargetMac(
    PDEVICE_INFO DeviceInfo,
    PRCB pRCB
     )
{
    char        *Buffer = NULL;
    PETH_HEADER ethHeader = NULL;
    PARP_BODY   pBody = NULL;
    unsigned int retries =0;
    DWORD       status;
    HANDLE      hDevice = pRCB->DeviceInfo->hDevice;
    PTCB pTCB  = NULL;

    if(!ReadFileEx(hDevice, pRCB->Buffer, sizeof(pRCB->Buffer),
                &pRCB->Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE) ReadMacAddrComplete))
    {
        Display(TEXT("GetTargetMac: Error in ReadFile %x"), GetLastError());
        return FALSE;
    }

    //
    // We will try asking for mac address ten times. If we don't get valid response,
    // we will bail out.
    //
    while(retries < 10){

        //
        // Allocate memory for the TCB and it's buffer. Writes are overlapped.
        // We don't wait for the write requests to complete before posting
        // another one.
        //
        pTCB = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TCB));
        if(!pTCB){
            Display(TEXT("Ping: HeapAlloc Failed"));
            return FALSE;
        }

        pTCB->Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ETH_MAX_PACKET_SIZE);
        if(!pTCB->Buffer){
            Display(TEXT("Ping: HeapAlloc Failed"));
            HeapFree (GetProcessHeap(), 0, pTCB);
            return FALSE;
        }

        pTCB->BufferLength = ETH_MAX_PACKET_SIZE;
        memset(&pTCB->Overlapped, 0, sizeof(OVERLAPPED));
        pTCB->DeviceInfo = DeviceInfo;

        Buffer = pTCB->Buffer;
        memset(Buffer, 0, ETH_MIN_PACKET_SIZE);

        ethHeader = (PETH_HEADER) Buffer;


        memcpy(ethHeader->SrcAddr, DeviceInfo->SrcMacAddr, MAC_ADDR_LEN);
        memset(ethHeader->DstAddr, 0xff, MAC_ADDR_LEN);
        ethHeader->EthType = htons(ARP_ETYPE_ARP);        //Network byte order

        pBody = (PARP_BODY)(Buffer + sizeof(ETH_HEADER));
        pBody->hw = htons(ARP_REQUEST);    //Network byte order
        pBody->pro = htons(IP_PROT_TYPE);    //Network byte order
        pBody->hlen = MAC_ADDR_LEN;
        pBody->plen = IP_ADDR_LEN;
        pBody->opcode = 0x0100;

        #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
        pBody->SenderIpAddr = inet_addr(DeviceInfo->SourceIp);
        #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
        pBody->DestIPAddr = inet_addr(DeviceInfo->DestIp);

        memcpy(pBody->SenderHwAddr, DeviceInfo->SrcMacAddr, MAC_ADDR_LEN);

        if(!WriteFileEx(hDevice, pTCB->Buffer, ETH_MIN_PACKET_SIZE, &pTCB->Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE) WriteComplete))
        {
                Display(TEXT("GetTargetMac WriteFile failed %x"), GetLastError());
                HeapFree (GetProcessHeap(), 0, pTCB->Buffer);
                HeapFree (GetProcessHeap(), 0, pTCB);
                return FALSE;
        }

        retries++;
        //
        // Wait for the PingEvent to be signalled. This even is signalled when the
        // ReadMacAddrComplete received a valid packet
        //
wait:
        status = WaitForSingleObjectEx(DeviceInfo->PingEvent, 1000, TRUE );

        if ( status == WAIT_OBJECT_0 ) {    // event fired, not timeout
            //
            // Got a valid response. Hurray!
            //
            return TRUE;
        }

        if ( status == WAIT_IO_COMPLETION ) {
            // Either Read/Write completed. Go back to waiting until the completion rotuine
            // processes the response and sigals the event.
            goto wait;
        }
        if (status != WAIT_TIMEOUT){
            //
            // It has to be timeout at this point. Or else something fatal, break out.
            Display(TEXT("WaitForSingleObjectEx returned error %d"), status);
            return FALSE;
        }
    }

    return FALSE;
}

BOOL
GetSrcMac(
    HANDLE  Handle,
    PUCHAR  pSrcMacAddr
    )
{
    DWORD       BytesReturned;
    BOOLEAN     bSuccess;
    UCHAR       QueryBuffer[sizeof(NDISPROT_QUERY_OID) + MAC_ADDR_LEN];
    PNDISPROT_QUERY_OID  pQueryOid;


    DisplayV(TEXT("Trying to get src mac address"), NULL);

    pQueryOid = (PNDISPROT_QUERY_OID)&QueryBuffer[0];
    pQueryOid->Oid = OID_802_3_CURRENT_ADDRESS;

    bSuccess = (BOOLEAN)DeviceIoControl(
                            Handle,
                            IOCTL_NDISPROT_QUERY_OID_VALUE,
                            (LPVOID)&QueryBuffer[0],
                            sizeof(QueryBuffer),
                            (LPVOID)&QueryBuffer[0],
                            sizeof(QueryBuffer),
                            &BytesReturned,
                            NULL);

    if (bSuccess)
    {
        DisplayV(TEXT("GetSrcMac: IoControl success"));
        memcpy(pSrcMacAddr, &pQueryOid->Data[0], MAC_ADDR_LEN);


    }
    else
    {
        Display(TEXT("GetSrcMac: IoControl failed %x\n"), GetLastError());
    }

    return (bSuccess);
}

VOID
PrintIpHeader(
    IpHeader *pIpHeader
    )
{
    #pragma prefast(suppress:__WARNING_IPV6_ADDRESS_STRUCTURE_IPV4_SPECIFIC, "This test app doesn't support IPv6")
    struct in_addr IPAddr = {0};

    DisplayV(TEXT("Ip Proto %x"), pIpHeader->proto);
    DisplayV(TEXT("Ip VerLen %x"), pIpHeader->verlen);
    DisplayV(TEXT("Ip Total Len %d"), pIpHeader->total_len);
    DisplayV(TEXT("Ip Ident %d"), pIpHeader->ident);
    DisplayV(TEXT("Ip tos %x"), pIpHeader->tos);

    memcpy(&IPAddr, &pIpHeader->sourceIP, IP_ADDR_LEN);

    // Display(TEXT("Source IP %s"), inet_ntoa(IPAddr));

    memcpy(&IPAddr, &pIpHeader->destIP, IP_ADDR_LEN);

    // Display(TEXT("Dest IP %s"), inet_ntoa(IPAddr));

}


BOOL
SetPacketFilter(
    HANDLE  Handle,
    ULONG  PacketFilter
    )
{
    DWORD       BytesReturned;
    BOOLEAN     bSuccess;
    NDISPROT_SET_OID  SetOid;


    DisplayV(TEXT("Trying to SetPacketFilter"), NULL);

    SetOid.Oid = OID_GEN_CURRENT_PACKET_FILTER;

    memcpy(&SetOid.Data[0], &PacketFilter, sizeof(ULONG));

    bSuccess = (BOOLEAN)DeviceIoControl(
                            Handle,
                            IOCTL_NDISPROT_SET_OID_VALUE,
                            (LPVOID)&SetOid,
                            sizeof(NDISPROT_SET_OID),
                            NULL,
                            0,
                            &BytesReturned,
                            NULL);

    if (!bSuccess)
    {
        Display(TEXT("SetPacketFilter: IoControl failed: %d"), GetLastError());
    }

    return (bSuccess);
}


VOID
ProcessReadBuffer(
    PRCB pRCB,
    ULONG BufferLength
    )
{
    char        *Buffer = pRCB->Buffer;
    PETH_HEADER ethHeader = (PETH_HEADER) Buffer;
    PARP_BODY   pBody = (PARP_BODY)(Buffer + sizeof(ETH_HEADER));
    char        ArpResponse[ETH_MIN_PACKET_SIZE] = {0};
    PETH_HEADER pResEthHeader = (PETH_HEADER)ArpResponse;
    PARP_BODY   pRespBody = (PARP_BODY)(ArpResponse + sizeof(ETH_HEADER));
    OVERLAPPED  ov = {0};
    PDEVICE_INFO deviceInfo = pRCB->DeviceInfo;
    HANDLE      hDevice = deviceInfo->hDevice;

    // Check whether its a broadcast ARP request
    if (ethHeader->DstAddr[0] == 0xff &&
            ethHeader->DstAddr[1] == 0xff )
    {

         if(ntohs(ethHeader->EthType) != ARP_ETYPE_ARP){
            deviceInfo->Sleep = FALSE;
            DisplayV(TEXT("Non an arp eth request"));
            goto End;
         }

         if (IP_PROT_TYPE != ntohs(pBody->pro) ||
             MAC_ADDR_LEN != pBody->hlen ||
             IP_ADDR_LEN != pBody->plen )
        {
            //
            // these are just sanity checks
            //
            deviceInfo->Sleep = FALSE;
            DisplayV(TEXT("Non an arp packet"));
            goto End;
        }

        DisplayV(TEXT("This is an arp request"));

        memcpy(pResEthHeader->DstAddr, ethHeader->SrcAddr, MAC_ADDR_LEN);
        memcpy(pResEthHeader->SrcAddr, deviceInfo->SrcMacAddr, MAC_ADDR_LEN);
        pResEthHeader->EthType = ethHeader->EthType;

        pRespBody->hw = pBody->hw;                                       // Hardware address space. = 00 01

        pRespBody->pro = pBody->pro;                                 // Protocol address space. = 08 00

        pRespBody->hlen = MAC_ADDR_LEN; // 6

        pRespBody->plen = IP_ADDR_LEN; // 4

        pRespBody->opcode = htons(ARP_RESPONSE);                        // Opcode.

        memcpy(pRespBody->SenderHwAddr, deviceInfo->SrcMacAddr, MAC_ADDR_LEN);                     // Destination HW address.

        pRespBody->SenderIpAddr = pBody->DestIPAddr ;                    // Source protocol address.

        memcpy(pRespBody->DestHwAddr, pBody->SenderHwAddr, MAC_ADDR_LEN);                     // Destination HW address.

        pRespBody->DestIPAddr = pBody->SenderIpAddr;

        PostNextRead(pRCB);

        DisplayV(TEXT("Writing Arp response\n"));
        if(!WriteFileEx(hDevice, ArpResponse, sizeof(ArpResponse), &ov, (LPOVERLAPPED_COMPLETION_ROUTINE) WriteCompleteArp))
        {
            Display(TEXT("Couldn't write arp response %d"), GetLastError());
            return ;
        }
    }  
    // Check whether its an unicast ARP request
    else if (ethHeader->DstAddr[0] == deviceInfo->SrcMacAddr[0] && 
             ethHeader->DstAddr[1] == deviceInfo->SrcMacAddr[1] && 
             ethHeader->DstAddr[2] == deviceInfo->SrcMacAddr[2] &&
             ethHeader->DstAddr[3] == deviceInfo->SrcMacAddr[3] &&
             ethHeader->DstAddr[4] == deviceInfo->SrcMacAddr[4] &&
             ethHeader->DstAddr[5] == deviceInfo->SrcMacAddr[5] &&
             ntohs(ethHeader->EthType) == ARP_ETYPE_ARP && 
             IP_PROT_TYPE == ntohs(pBody->pro) && 
             MAC_ADDR_LEN == pBody->hlen &&
             IP_ADDR_LEN == pBody->plen) {

        // If it passed all these checks, its an ARP request
        memcpy(pResEthHeader->DstAddr, ethHeader->SrcAddr, MAC_ADDR_LEN);
        memcpy(pResEthHeader->SrcAddr, deviceInfo->SrcMacAddr, MAC_ADDR_LEN);
        pResEthHeader->EthType = ethHeader->EthType;

        pRespBody->hw = pBody->hw;                                       // Hardware address space. = 00 01

        pRespBody->pro = pBody->pro;                                 // Protocol address space. = 08 00

        pRespBody->hlen = MAC_ADDR_LEN; // 6

        pRespBody->plen = IP_ADDR_LEN; // 4

        pRespBody->opcode = htons(ARP_RESPONSE);                        // Opcode.

        memcpy(pRespBody->SenderHwAddr, deviceInfo->SrcMacAddr, MAC_ADDR_LEN);                     // Destination HW address.

        pRespBody->SenderIpAddr = pBody->DestIPAddr ;                    // Source protocol address.

        memcpy(pRespBody->DestHwAddr, pBody->SenderHwAddr, MAC_ADDR_LEN);                     // Destination HW address.

        pRespBody->DestIPAddr = pBody->SenderIpAddr;

        PostNextRead(pRCB);

        DisplayV(TEXT("Writing Arp response\n"));
        if(!WriteFileEx(hDevice, ArpResponse, sizeof(ArpResponse), &ov, (LPOVERLAPPED_COMPLETION_ROUTINE) WriteCompleteArp))
        {
            Display(TEXT("Couldn't write arp response %d"), GetLastError());
            return ;
        }
    }
    else {

        IpHeader *pIpHeader = (IpHeader *)(Buffer + sizeof(ETH_HEADER));
        IcmpHeader *pIcmpHeader = (IcmpHeader *)(Buffer + sizeof(ETH_HEADER)+sizeof(IpHeader));//TODO: Find the iP len from the packet
        ULONG datalen = BufferLength - sizeof(ETH_HEADER) - sizeof(IpHeader) - sizeof(IcmpHeader);
        #pragma prefast(suppress:__WARNING_IPV6_ADDRESS_STRUCTURE_IPV4_SPECIFIC, "This test app doesn't support IPv6")
        struct in_addr IPAddr = {0};
        char *ipAddr;
        WCHAR unicodeIpAddr[MAX_LEN];

        PrintIpHeader(pIpHeader);

        if(pIpHeader->proto == PROTOCOL_ICMP && pIcmpHeader->i_type == ICMPV4_ECHO_REPLY_TYPE &&
                    pIcmpHeader->i_type == ICMPV4_ECHO_REPLY_CODE){

            memcpy(&IPAddr, &pIpHeader->sourceIP, IP_ADDR_LEN);

            #pragma prefast(suppress:__WARNING_IPV6_NAME_RESOLUTION_IPV4_SPECIFIC, "This test app doesn't support IPv6")
            ipAddr = inet_ntoa(IPAddr);

           if (!MultiByteToWideChar (
                        CP_ACP,
                        0,
                        ipAddr,
                        -1,
                        unicodeIpAddr,
                        sizeof(unicodeIpAddr)/sizeof(unicodeIpAddr[0])
                        )) {
                Display(TEXT("AnsitoUnicode conversion failed"));

           }

            #pragma prefast(suppress:__WARNING_USE_OTHER_FUNCTION, "The recommended function GeTickCount64 is available only on Windows Vista/Server 2008 and above")
            Display(TEXT("Reply %d from %ws: bytes=%d time<%dms TTL=%d"),
                ntohs(pIcmpHeader->i_id),
                unicodeIpAddr,
                datalen,
                (GetTickCount()-ntohl(pIcmpHeader->timestamp)),
                pIpHeader->ttl);

            deviceInfo->NumberOfRequestSent++;
            deviceInfo->Sleep = TRUE;

        }

    }

End:
    PostNextRead(pRCB);
    SetEvent(deviceInfo->PingEvent);

}
VOID
ReadComplete(
    DWORD dwError,
    DWORD dwBytesTransferred,
    LPOVERLAPPED pOvl
    )
{
    if (ERROR_OPERATION_ABORTED != dwError)
    {
        RCB* pRCB = (RCB *)pOvl;
        DisplayV(TEXT("ReadComplete: %d"), dwBytesTransferred);
        ProcessReadBuffer(pRCB, dwBytesTransferred);
    }
}

VOID
PostNextRead(
    RCB *pRCB
    )
{

    if(!ReadFileEx(pRCB->DeviceInfo->hDevice, pRCB->Buffer, sizeof(pRCB->Buffer),
                &pRCB->Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE) ReadComplete))
    {
        Display(TEXT("Error in ReadFile: %x"), GetLastError());
    }

}


BOOLEAN
Ping(
    PDEVICE_INFO DeviceInfo
    )
{
    HANDLE hDevice = DeviceInfo->hDevice;
    char *icmpbuf;
    char *ipHeader;
    char *etherHeader;
    unsigned int icmpbuflen, packetlen;
    PTCB pTCB = NULL;

    packetlen = sizeof(IcmpHeader) + sizeof(IpHeader) + sizeof(ETH_HEADER);

    // Add in the data size
    if(FAILED(UIntAdd(packetlen, DeviceInfo->PacketSize, &packetlen))) {
        Display(TEXT("Ping: UIntAdd Failed"));
        goto Error;
    }

    pTCB = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TCB));
    if(!pTCB){
        Display(TEXT("Ping: HeapAlloc Failed"));
        goto Error;
    }

    pTCB->Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, packetlen);
    if(!pTCB->Buffer){
        Display(TEXT("Ping: HeapAlloc Failed"));
        goto Error;
    }

    pTCB->BufferLength = packetlen;
    memset(&pTCB->Overlapped, 0, sizeof(OVERLAPPED));


    // Allocate the buffer that will contain the ICMP request
    etherHeader = pTCB->Buffer;

    ipHeader = etherHeader + sizeof(ETH_HEADER);

    icmpbuf = ipHeader + sizeof(IpHeader);


    icmpbuflen = sizeof(IcmpHeader) + DeviceInfo->PacketSize;

    InitEtherHeader(DeviceInfo, (PETH_HEADER)etherHeader);


    InitIcmpHeader(icmpbuf, icmpbuflen, DeviceInfo->PacketSize);

    // Set the sequence number and compute the checksum
    SetIcmpSequence(icmpbuf, sizeof(IcmpHeader));

    DisplayV(TEXT("Icmp header %d packetlen  %d"), sizeof(IcmpHeader), icmpbuflen);

    ComputeIcmpChecksum(icmpbuf, sizeof(IcmpHeader), icmpbuflen);

    if(!InitIpHeader(DeviceInfo, (IpHeader *)ipHeader, (USHORT)icmpbuflen+sizeof(IpHeader))){
        goto Error;
    }

    ComputeIPChecksum((IpHeader *)ipHeader);

    PrintIpHeader((IpHeader *)ipHeader);

    if(!WriteFileEx(hDevice, etherHeader, packetlen, &pTCB->Overlapped, (LPOVERLAPPED_COMPLETION_ROUTINE) WriteComplete))
    {
            Display(TEXT("Ping: WriteFile failed %x"), GetLastError());
            goto Error;
    }

    return TRUE;

Error:

    if(pTCB) {
        if(pTCB->Buffer){
            HeapFree (GetProcessHeap(), 0, pTCB->Buffer);
        }
        HeapFree (GetProcessHeap(), 0, pTCB);
    }
    return FALSE;
}

DWORD
PingThread (
    PDEVICE_INFO DeviceInfo
    )
{
    RCB                 RCB;
    HANDLE              hDevice = DeviceInfo->hDevice;
    DWORD               status;


    Display(TEXT("Pinging %ws from %ws with %d bytes of data"),
                            DeviceInfo->UnicodeDestIp,
                            DeviceInfo->UnicodeSourceIp,
                            DeviceInfo->PacketSize);
    Sleep(1000);

    //
    // Every time a ping response is recevied, PingEvent will
    // be signalled.
    //
    DeviceInfo->PingEvent = CreateEvent(NULL, FALSE, FALSE, L"PingEvent");
    if (DeviceInfo->PingEvent == NULL) {
        Display(TEXT("CreateEvent failed 0x%x"), GetLastError());
        goto Exit;
    }

    DeviceInfo->NumberOfRequestSent = 0;
    DeviceInfo->Sleep = FALSE;
    DeviceInfo->TimeOut = 0;
    PacketId = 1;

    //
    // Get the MAC address of the local NIC
    //
    if (!GetSrcMac(hDevice, DeviceInfo->SrcMacAddr))
    {
        Display(TEXT("Failed to obtain local MAC address"));
        goto Exit;
    }

    //
    // Set the hardware filter to receive directed on broadcast packets.
    //
    if (!SetPacketFilter(hDevice, NDIS_PACKET_TYPE_DIRECTED|NDIS_PACKET_TYPE_BROADCAST))
    {
        Display(TEXT("Failed to set packet filter"));
        goto Exit;
    }

    //
    // Initialize read control block. Reads requests are serialized.
    // Only one read is outstanding at any time. We allocate memory
    // for the RCB in the stack.
    //
    RCB.DeviceInfo = DeviceInfo;
    memset(&RCB.Overlapped, 0, sizeof(OVERLAPPED));


    //
    // Get MAC address of the target machine by sending an ARP
    // request. We have the target machine's IP address from the user.
    //
    if(!GetTargetMac(DeviceInfo, &RCB)){
        Display(TEXT("Couldn't find the host"));
        goto Exit;
    }

    //
    // Set the hardware filter to receive directed packets only
    //
    if (!SetPacketFilter(hDevice, NDIS_PACKET_TYPE_DIRECTED))
    {
        Display(TEXT("Failed to set packet filter"));
        goto Exit;
    }

    //
    // Post a read buffer and start sending ping packets.
    //
    PostNextRead(&RCB);

    Ping(DeviceInfo);

    //
    // We will exit out of this thread if the number of ping count
    // exceeds the DEFAULT_SEND_COUNT or the main thread requested
    // us to exit by setting ExitThread value to TRUE.
    //
    while(DeviceInfo->NumberOfRequestSent < DEFAULT_SEND_COUNT
            && DeviceInfo->ExitThread == FALSE){

        status = WaitForSingleObjectEx(DeviceInfo->PingEvent, 1000, TRUE );
        if ( status == WAIT_OBJECT_0 ) {    // event fired, not timeout
            //
            // Probably we received a valid ping response from the target.
            //
            if(DeviceInfo->Sleep){
                Sleep(PING_SLEEP_TIME); // sleep for a sec before sending another ECHO
            }
            Ping(DeviceInfo);
            continue;
        }
        //
        // This is just a notification that either Read/Write operation got
        // completed. We will know the result of the acutal operation later
        // when the APC is called.
        //
        if( status == WAIT_IO_COMPLETION ) {
            continue;
        }
        if (status != WAIT_TIMEOUT){

            Display(TEXT("WaitForSingleObjectEx returned error %d"), status);
            break;
        }
        //
        // It seems like the wait timed out. So let us send another ping
        // and see if we get any response.
        //
        DeviceInfo->TimeOut++;
        if(DeviceInfo->TimeOut > MAX_PING_RETRY) {
            Display(TEXT("No response from the target"));
            break;
        }

        Ping(DeviceInfo);
    }

Exit:

    CloseHandle(DeviceInfo->hDevice);
    DeviceInfo->hDevice = INVALID_HANDLE_VALUE;
    DeviceInfo->ThreadHandle = NULL;
    if (DeviceInfo->PingEvent) {
        CloseHandle(DeviceInfo->PingEvent);
        DeviceInfo->PingEvent = NULL;
    }

    Display(TEXT("PingThread is exiting"));
    return 0;
}