pzqxidian
驱动牛犊
驱动牛犊
  • 注册日期2003-12-10
  • 最后登录2004-06-05
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1508回复:2

如何在NDIS中发送数据包?

楼主#
更多 发布于:2004-02-18 14:36
各位老兄,我在这个论坛上看了好多的帖子,想自己发送一个数据包,现在通过PtReceive()接收倒数据,如果为arp请求包,我就调用自定义的函数,回送一个arp应答包,可是总是重启,请各位分析一下原因发送函数源代码:
PADAPT pAdapt = (PADAPT)Handle;
//Handle这个参数是ptreceive()函
//数中IN  NDIS_HANDLE
// ProtocolBindingContext,传递下来的参数

PNDIS_PACKET MyPacket;
PNDIS_BUFFER Insert_Header_buffer;
PNDIS_BUFFER Insert_Data_buffer;
PVOID Insert_Head;
PVOID Insert_Data;
NDIS_STATUS Status;
PARPHDR send_arphdr;
PARPHDR recv_arphdr;
PETHERHDR send_etherhdr;
PETHERHDR recv_etherhdr;
UINT i = 0;

DbgPrint("\nSend_arp_Packet() is runninng\n");

NdisAllocatePacket(&Status,&MyPacket,pAdapt->SendPacketPoolHandle);
if(Status != NDIS_STATUS_SUCCESS)
{
DbgPrint("\nNdisAllocatePacket() failed.\n");
return Status;
}
Status = NdisAllocateMemory(&Insert_Head,14,NDIS_MEMORY_NONCACHED,HighestAcceptableMax);
if(Status != NDIS_STATUS_SUCCESS)
{
DbgPrint("\nNdisAllocateMemory() failed.\n");
NdisFreePacket(MyPacket);
return Status;
}

//插入数据
recv_etherhdr = (PETHERHDR)HeaderBuffer;
send_etherhdr = (PETHERHDR)Insert_Head;
send_etherhdr->type = recv_etherhdr->type;
for(i = 0;i<6;i++)
send_etherhdr->destination_mac = recv_etherhdr->source_mac;
for(i = 0;i<6;i++)
send_etherhdr->source_mac = Self_MAC_Address;

NdisAllocateBuffer(&Status,&Insert_Header_buffer,pAdapt->SendPacketPoolHandle,
Insert_Head,14);
if(Status != NDIS_STATUS_SUCCESS)
{
DbgPrint("\nNdisAllocateBuffer() failed.\n");
NdisFreePacket(MyPacket);
NdisFreeMemory(Insert_Head,14,NDIS_MEMORY_NONCACHED);
return Status;
}
//将数据插入数据包
NdisChainBufferAtBack(MyPacket,Insert_Header_buffer);

Status = NdisAllocateMemory(&Insert_Data,28,NDIS_MEMORY_NONCACHED,HighestAcceptableMax);
if(Status != NDIS_STATUS_SUCCESS)
{
DbgPrint("\nNdisAllocateMemory() failed.\n");
NdisFreePacket(MyPacket);
NdisFreeMemory(Insert_Head,14,NDIS_MEMORY_NONCACHED);
NdisFreeBuffer(Insert_Header_buffer);
return Status;
}

//插入数据
recv_arphdr = (PARPHDR)LookAheadBuffer;
send_arphdr = (PARPHDR)Insert_Data;
send_arphdr->hard_tpye = recv_arphdr->hard_tpye;
send_arphdr->protocol = recv_arphdr->protocol;
send_arphdr->hard_length = recv_arphdr->hard_length;
send_arphdr->protocol_length = recv_arphdr->protocol_length;
send_arphdr->operation_type = type;
send_arphdr->source_ip.S_addr = recv_arphdr->destination_ip.S_addr;
send_arphdr->destination_ip.S_addr = recv_arphdr->source_ip.S_addr;
for(i = 0;i<6;i++)
send_arphdr->destination_mac = recv_arphdr->source_mac;
for(i = 0;i<6;i++)
send_arphdr->source_mac = Self_MAC_Address;


NdisAllocateBuffer(&Status,&Insert_Data_buffer,pAdapt->SendPacketPoolHandle,
Insert_Data,28);
if(Status != NDIS_STATUS_SUCCESS)
{
DbgPrint("\nNdisAllocateBuffer() failed.\n");
NdisFreePacket(MyPacket);
NdisFreeMemory(Insert_Head,14,NDIS_MEMORY_NONCACHED);
NdisFreeBuffer(Insert_Header_buffer);
NdisFreeMemory(Insert_Data,28,NDIS_MEMORY_NONCACHED);
return Status;
}
//将数据插入数据包
NdisChainBufferAtBack(MyPacket,Insert_Data_buffer);

DbgPrint("\nIt is sending self_packet.\n");
Show(MyPacket);
NdisSetPacketFlags(MyPacket,pAdapt->Medium);


Status = MPSendOnePacket(pAdapt->BindingHandle,MyPacket,NdisGetPacketFlags(MyPacket));
if(Status != NDIS_STATUS_PENDING)
{
NdisMSendComplete(
pAdapt->pPrimaryAdapt->MiniportHandle, MyPacket, Status);
Self_Sending_Packet_flag = FALSE;
NdisFreePacket(MyPacket);
NdisFreeMemory(Insert_Head,14,NDIS_MEMORY_NONCACHED);
NdisFreeBuffer(Insert_Header_buffer);
NdisFreeMemory(Insert_Data,28,NDIS_MEMORY_NONCACHED);
NdisFreeBuffer(Insert_Data_buffer);
}
return Status;
其中函数PtSendOnepacket()是xpassthru中定义的函数,
请问还有什么地方不妥的请指点,先谢谢了。如果解决了,给高分。

最新喜欢:

jzyhummeljzyhum...
martinkin
驱动牛犊
驱动牛犊
  • 注册日期2003-02-17
  • 最后登录2011-11-19
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-02-25 16:50
NdisAllocateBuffer(&Status,&Insert_Data_buffer,pAdapt->SendPacketPoolHandle,
Insert_Data,28);

Why use "pAdapt->SendPacketPoolHandle" when you want to allocate a buffer? Buffer should be allocated from BUFFER pool, not packet pool
pzqxidian
驱动牛犊
驱动牛犊
  • 注册日期2003-12-10
  • 最后登录2004-06-05
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-02-25 19:01
因为用NdisAllocateBufferPool()返回的BufferPool总是空的,当然就不能用他来开辟Buffer拉,请问如果用的话,是不是也没什么问题呢?
游客

返回顶部