LincolnII
驱动牛犊
驱动牛犊
  • 注册日期2009-05-14
  • 最后登录2009-06-16
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望81点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1634回复:1

passthru在ptreceive中截获数据包问题。用debugging打印出来是乱码。

楼主#
更多 发布于:2009-05-29 10:28
修改后的ptreceive函数如下:
NDIS_STATUS
PtReceive(
    IN  NDIS_HANDLE         ProtocolBindingContext,
    IN  NDIS_HANDLE         MacReceiveContext,
    IN  PVOID               HeaderBuffer,
    IN  UINT                HeaderBufferSize,
    IN  PVOID               LookAheadBuffer,
    IN  UINT                LookAheadBufferSize,
    IN  UINT                PacketSize
    )
/*++
Routine Description:
    Handle receive data indicated up by the miniport below. We pass
    it along to the protocol above us.
    If the miniport below indicates packets, NDIS would more
    likely call us at our ReceivePacket handler. However we
    might be called here in certain situations even though
    the miniport below has indicated a receive packet, e.g.
    if the miniport had set packet status to NDIS_STATUS_RESOURCES.
        
Arguments:
    <see DDK ref page for ProtocolReceive>
Return Value:
    NDIS_STATUS_SUCCESS if we processed the receive successfully,
    NDIS_STATUS_XXX error code if we discarded it.
--*/
{
    PADAPT            pAdapt = (PADAPT)ProtocolBindingContext;
    PNDIS_PACKET      MyPacket, Packet = NULL;
    NDIS_STATUS       Status = NDIS_STATUS_SUCCESS,Status1;
    
    
    
    
    
UCHAR CONTENT='c';
PUCHAR pPacketContent;
PUCHAR pBuf=NULL;
UINT BufLength;
MDL * pNext;
UINT i;
NDIS_PHYSICAL_ADDRESS HighestAcceptableMax = NDIS_PHYSICAL_ADDRESS_CONST(-1, -1);

    if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
    {
        Status = NDIS_STATUS_FAILURE;
    }
    else do
    {
        //
        // Get at the packet, if any, indicated up by the miniport below.
        //
        Packet = NdisGetReceivedPacket(pAdapt->BindingHandle, MacReceiveContext);
        if (Packet != NULL)
        {
            //
            // The miniport below did indicate up a packet. Use information
            // from that packet to construct a new packet to indicate up.
            //
#ifdef NDIS51
            //
            // NDIS 5.1 NOTE: Do not reuse the original packet in indicating
            // up a receive, even if there is sufficient packet stack space.
            // If we had to do so, we would have had to overwrite the
            // status field in the original packet to NDIS_STATUS_RESOURCES,
            // and it is not allowed for protocols to overwrite this field
            // in received packets.
            //
#endif // NDIS51
            //
            // Get a packet off the pool and indicate that up
            //
            NdisDprAllocatePacket(&Status,
                                &MyPacket,
                                pAdapt->RecvPacketPoolHandle);
            if (Status == NDIS_STATUS_SUCCESS)
            {

                NDIS_PACKET_FIRST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_FIRST_NDIS_BUFFER(Packet);
                NDIS_PACKET_LAST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_LAST_NDIS_BUFFER(Packet);

                NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
                NDIS_SET_PACKET_HEADER_SIZE(MyPacket, HeaderBufferSize);
                //
                // Copy packet flags.
                //
                NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
                //
                // Force protocols above to make a copy if they want to hang
                // on to data in this packet. This is because we are in our
                // Receive handler (not ReceivePacket) and we can't return a
                // ref count from here.
                //
                NDIS_SET_PACKET_STATUS(MyPacket, NDIS_STATUS_RESOURCES);
                
                //    
                NdisQueryPacket( MyPacket,NULL,NULL,NULL,&PacketSize);
                
                Status= NdisAllocateMemory( &pPacketContent, 2000, 0,HighestAcceptableMax);
                if (Status!=NDIS_STATUS_SUCCESS ) return Status;
                NdisZeroMemory (pPacketContent, 2000);
                NdisQueryBufferSafe(MyPacket->Private.Head, &pBuf, &BufLength, 32 );
                if(pBuf==NULL){ DBGPRINT(("获取包内容不成功\n"));}
                else
                {   DBGPRINT(("Head内容是:%s\n",&pBuf));
                    DBGPRINT(("Head大小是:%d\n",BufLength));
                }
                NdisMoveMemory(pPacketContent, pBuf, BufLength);
                i = BufLength;
  
                pNext = MyPacket->Private.Head;
                for(;;)
                {
                    DBGPRINT(("进入内部循环\n"));
                    if(pNext == MyPacket->Private.Tail)
                    {
                        DBGPRINT(("到达尾部,跳出循环\n"));                         //每次运行到这里就跳出for循环了,也就是说Private.Head=Private.Tail。是怎么回事啊?
                        break;
                    }
                    
                    pNext = pNext->Next; //指针后移
                    if(pNext == NULL)
                    {
                        DBGPRINT(("下一缓冲区为空,跳出循环\n"));
                        break;
                    }
                    NdisQueryBufferSafe(pNext,&pBuf,&BufLength,32);
                    NdisMoveMemory(pPacketContent+i,pBuf,BufLength);
                    i+=BufLength;
                    DBGPRINT(("部分包内容是:%s\n",&pBuf));
                    DBGPRINT(("部分包大小是:%d\n",BufLength));
                }
                
                DBGPRINT(("ptreceive\n"));
                DBGPRINT(("包内容是:%s\n",&pPacketContent));
                DBGPRINT(("包大小是:%d\n",i));


                NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
                NdisDprFreePacket(MyPacket);
                break;
            }
        }
        else
        {
            //
            // The miniport below us uses the old-style (not packet)
            // receive indication. Fall through.
            //
        }
        //
        // Fall through if the miniport below us has either not
        // indicated a packet or we could not allocate one
        //
        if (Packet != NULL)
        {
            //
            // We are here because we failed to allocate packet
            //
            PtFlushReceiveQueue(pAdapt);
        }
        if ((pAdapt->MiniportHandle == NULL)
                || (pAdapt->MPDeviceState > NdisDeviceStateD0))
        {
            break;
        }
        
        pAdapt->IndicateRcvComplete = TRUE;
        switch (pAdapt->Medium)
        {
            case NdisMedium802_3:
            case NdisMediumWan:
                NdisMEthIndicateReceive(pAdapt->MiniportHandle,
                                             MacReceiveContext,
                                             HeaderBuffer,
                                             HeaderBufferSize,
                                             LookAheadBuffer,
                                             LookAheadBufferSize,
                                             PacketSize);
                break;
            case NdisMedium802_5:
                NdisMTrIndicateReceive(pAdapt->MiniportHandle,
                                            MacReceiveContext,
                                            HeaderBuffer,
                                            HeaderBufferSize,
                                            LookAheadBuffer,
                                            LookAheadBufferSize,
                                            PacketSize);
                break;
            case NdisMediumFddi:
                NdisMFddiIndicateReceive(pAdapt->MiniportHandle,
                                              MacReceiveContext,
                                              HeaderBuffer,
                                              HeaderBufferSize,
                                              LookAheadBuffer,
                                              LookAheadBufferSize,
                                              PacketSize);
                break;
            default:
                ASSERT(FALSE);
                break;
        }
    } while(FALSE);
    return Status;
}
每个打印的地方用%s打出来都是乱码,是不是包中数据不在pPacketContent里面啊?我添的代码是不是有什么问题哦?从网上很多地方看到的都是这样的啊
LincolnII
驱动牛犊
驱动牛犊
  • 注册日期2009-05-14
  • 最后登录2009-06-16
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望81点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2009-05-31 22:47
回 楼主(LincolnII) 的帖子
知道这么打印出来了,用%x打印出来的。但是又有新的问题,貌似这样只拿到了广播包而且是向外发送的。明明是在ptreceive函数中添加的代码怎么会拿到向外发送的呢?这么久了怎么都没一位牛人来解答一下呢?
游客

返回顶部