3164|1

68

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

passthru发送原始帧,NdisFreeBuffer蓝屏 [复制链接]

改写的passthru 在ndis5.1下实现发送原始帧,出现一个奇怪的问题。

PtDispatch中调用发送函数SendRawData,里面给自己的包做了一个标记,

完了以后在PtSendComplete里释放自己申请的内存。结果在调用NdisFreeBuffer的时候蓝屏。

具体描述如下:


  1. NDIS_STATUS SendRawData(PADAPT adapt, PVOID buffer, ULONG length)
  2. {
  3.     NDIS_STATUS     status;
  4.     PNDIS_PACKET    ndisPacket = NULL;
  5.     PNDIS_BUFFER    ndisBuffer = NULL;
  6.     PUCHAR          sendBuffer = NULL;

  7.     PSEND_RSVD      sendRsvd = NULL;
  8.     NDIS_PHYSICAL_ADDRESS highestAcceptableAddress;
  9.     highestAcceptableAddress.QuadPart = -1;

  10.     ASSERT(buffer != NULL);
  11.     ASSERT(length >= ETH_MIN_PACKET_SIZE && length <= ETH_MAX_PACKET_SIZE);

  12.     status = NdisAllocateMemory(&sendBuffer, length, 0, highestAcceptableAddress);
  13.     //status = NdisAllocateMemoryWithTag (&sendBuffer, length, TAG);
  14.     if (status != NDIS_STATUS_SUCCESS)
  15.     {
  16.         return status;
  17.     }

  18.     RtlZeroMemory(sendBuffer, length);
  19.     RtlMoveMemory(sendBuffer, buffer, length);

  20.     NdisAllocatePacket(&status, &ndisPacket, adapt->SendPacketPoolHandle);
  21.     if (status != NDIS_STATUS_SUCCESS)
  22.     {
  23.         NdisFreeMemory(sendBuffer, length, 0);
  24.         return status;
  25.     }

  26.     [color=#FF0000]NdisAllocateBuffer(&status, &ndisBuffer, adapt->SendPacketPoolHandle, &sendBuffer, length); [/color]
  27.     //这里调试结果为SendRawData -> Allocate return: 0x81a2d6d8, 60
  28.     // 马上QuerySafe一下结果返回值也是0xf????,跟下面崩溃前的数字一样

  29.     DBGPRINT(("SendRawData -> Allocate return: 0x%x, %d\n", sendBuffer, length));

  30.     if (status != NDIS_STATUS_SUCCESS)
  31.     {
  32.         NdisFreeMemory(sendBuffer, length, 0);
  33.         NdisFreePacket(ndisPacket);
  34.         return status;
  35.     }

  36.     NdisChainBufferAtFront(ndisPacket, ndisBuffer);

  37.     sendRsvd = (PSEND_RSVD)(ndisPacket->ProtocolReserved);
  38.     sendRsvd->OriginalPkt = (PNDIS_PACKET)1; //标记自己

  39.     ndisPacket->Private.Head->Next = NULL;
  40.     ndisPacket->Private.Tail = NULL;

  41.     NdisSetPacketFlags(ndisPacket, NDIS_FLAGS_DONT_LOOPBACK);
  42.     DBGPRINT(("SendRawData -> Now send!\n"));
  43.     NdisSend(&status, adapt->BindingHandle, ndisPacket);
  44.     if (status != STATUS_PENDING)
  45.     {
  46.         NdisUnchainBufferAtFront(ndisPacket, &ndisBuffer);
  47.         NdisQueryBufferSafe(ndisBuffer, &sendBuffer, &length, NormalPagePriority);
  48.         NdisFreeBuffer(ndisBuffer);
  49.         NdisFreeMemory(sendBuffer, length, 0);
  50.         NdisFreePacket(ndisPacket);
  51.         DBGPRINT(("SendRawData -> Released!\n"));
  52.     }

  53.     DBGPRINT(("SendRawData -> Send succeeded!\n"));
  54.     return status;
  55. }

  56. VOID
  57. PtSendComplete(
  58.     IN  NDIS_HANDLE            ProtocolBindingContext,
  59.     IN  PNDIS_PACKET           Packet,
  60.     IN  NDIS_STATUS            Status
  61.     )
  62. {
  63.     PADAPT            pAdapt = (PADAPT)ProtocolBindingContext;
  64.     PNDIS_PACKET      Pkt;
  65.     NDIS_HANDLE       PoolHandle;

  66.     // -------------------------------------------------------------------------
  67.     PNDIS_BUFFER    ndisBuffer = NULL;
  68.     PUCHAR          sendBuffer = NULL;
  69.     ULONG           length;

  70.     Pkt = ((PSEND_RSVD)(Packet->ProtocolReserved))->OriginalPkt;
  71.     if ((int)Pkt == 1) // 是自己构建的包
  72.     {
  73.         DBGPRINT(("PtSendComplete -> Now free!\n"));
  74.         NdisUnchainBufferAtFront(Packet, &ndisBuffer);

  75.         NdisQueryBufferSafe(ndisBuffer, &sendBuffer, &length, NormalPagePriority);
  76.         DBGPRINT(("PtSendComplete -> Query return: 0x%x, %d\n", sendBuffer, length));
  77.         // 这里很奇怪,结果为PtSendComplete -> Query return: 0xf0f01b38, 60
  78.         // 大小没错,问题是指向0x8???的变成了0xF?????

  79.         NdisFreeBuffer(ndisBuffer);
  80.         [color=#FF0000]NdisFreeMemory(sendBuffer, length, 0); //这里就蓝屏了[/color]
  81.         // 注释掉这里就没问题了,问题是不是意味着内存泄漏了吧?NDIS 5.1

  82.         NdisDprFreePacket(Packet);
  83.         DBGPRINT(("PtSendComplete -> Free successed!\n"));
  84.         return;
  85.     }
  86.     // -------------------------------------------------------------------------

  87. #ifdef NDIS51
  88.     //
  89.     // Packet stacking:
  90.     //
  91.     // Determine if the packet we are completing is the one we allocated. If so, then
  92.     // get the original packet from the reserved area and completed it and free the
  93.     // allocated packet. If this is the packet that was sent down to us, then just
  94.     // complete it
  95.     //
  96.     PoolHandle = NdisGetPoolFromPacket(Packet);
  97.     if (PoolHandle != pAdapt->SendPacketPoolHandle)
  98.     {
  99.         //
  100.         // We had passed down a packet belonging to the protocol above us.
  101.         //
  102.         // DBGPRINT(("PtSendComp: Adapt %p, Stacked Packet %p\n", pAdapt, Packet));

  103.         NdisMSendComplete(pAdapt->MiniportHandle,
  104.                           Packet,
  105.                           Status);
  106.     }
  107.     else
  108. #endif // NDIS51
  109.     {
  110.         PSEND_RSVD        SendRsvd;

  111.         SendRsvd = (PSEND_RSVD)(Packet->ProtocolReserved);
  112.         Pkt = SendRsvd->OriginalPkt;
  113.    
  114. #ifndef WIN9X
  115.         NdisIMCopySendCompletePerPacketInfo (Pkt, Packet);
  116. #endif
  117.    
  118.         NdisDprFreePacket(Packet);

  119.         NdisMSendComplete(pAdapt->MiniportHandle,
  120.                                  Pkt,
  121.                                  Status);
  122.     }
  123.     //
  124.     // Decrease the outstanding send count
  125.     //
  126.     ADAPT_DECR_PENDING_SENDS(pAdapt);
  127. }
复制代码


很伤脑筋。希望各位大侠能指点一下,谢谢。

最新回复

NdisAllocateBuffer(&status, &ndisBuffer, adapt->SendPacketPoolHandle, &sendBuffer, length); &sendBuffer应该是 sendBuffer 手抖了一下。。。调试了2天。郁闷   详情 回复 发表于 2009-2-12 10:33
点赞 关注

回复
举报

84

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
NdisAllocateBuffer(&status, &ndisBuffer, adapt->SendPacketPoolHandle, &sendBuffer, length);

&sendBuffer应该是 sendBuffer 手抖了一下。。。调试了2天。郁闷
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表