📄 pgpnetcomm.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: pgpnetComm.cpp,v 1.3 2002/08/06 20:10:27 dallen Exp $
____________________________________________________________________________*/
#include "pgpnetBase.h"
#include "pgpnetVersion.h"
#include "pgpnetPacket.h"
//#include "pgpnetRequest.h"
#include "CPGPnetAdapter.h"
#include "CPGPnetFilterAdapter.h"
#ifndef NDIS40_MINIPORT
//#include "CPGPnetAdapterMac.h"
#endif
#include "pgpNetPM.h"
#include "pgpNetGlobal.h"
#include "CPGPnetDriver.h"
#include "CPGPnetFilterDriver.h"
#ifndef NDIS40_MINIPORT
//#include "CPGPnetFilterDriverMac.h"
#endif
/* PGPnet includes */
//#include "pgpnetOid.h"
#include "pgpNetIPdef.h"
#include "pgpnetIpHelper.h"
#include "pgpnetPacketHelper.h"
#ifndef NDIS40_MINIPORT
//#include "pgpnetNdis3fix.h"
#endif
//#include "pgpnetTrace.h"
#include "pgpnetdebug.h"
//#include "pgpnetoid.h"
#include "pgpnetComm.h"
static PGPError sInjectOutgoingPacket(PGPnetPacket *packet);
static PGPError sInjectIncomingPacket(PGPnetPacket *packet);
VOID PGPCreateUDPPacket(CPGPnetFilterAdapter *adapter,
PGPnetPacketHead* pHead,
PGPUInt16 destPort,
PVOID message,
UINT msgSize)
{
PETHERNET_HEADER ether_header;
USHORT *ether_protocol;
PIP_HEADER ip_header;
PUDP_HEADER udp_header;
PGPnetPacket * pPacket;
PGPUInt32 packetSize, maxDataSize, packetHeaderSize, totalBytesRemaining;
DBG_FUNC("PGPCreateUDPPacket");
DBG_ENTER();
pPacket = pHead->link; // the first link in the chain
// Create the header in the first packet
ether_header = (PETHERNET_HEADER)(pPacket->srcBlock);
ether_protocol = ((PUSHORT)(ðer_header->eth_protocolType[0]));
NdisMoveMemory(ether_header->eth_dstAddress, adapter->mHardwareAddress, 6);
NdisMoveMemory(ether_header->eth_srcAddress, adapter->mHardwareAddress, 6);
*ether_protocol = IPPROT_NET;
ip_header = (PIP_HEADER)((UCHAR*)ether_header + ETHER_HEADER_SIZE);
ip_header->ip_versionandheaderlength = 0x45;
ip_header->ip_tos = 0;
ip_header->ip_len = ntohs(IP_HEADER_SIZE + UDP_HEADER_SIZE + msgSize);
ip_header->ip_id = adapter->mIpId++;
ip_header->ip_foff = 0;
ip_header->ip_ttl = 0x16; // We don't want it loop forever;
ip_header->ip_prot = PROTOCOL_UDP;
ip_header->ip_chksum = 0;
ip_header->ip_src = 0x0100007F;
ip_header->ip_dest = 0x0100007F;
ip_header->ip_chksum = HeaderChecksum((USHORT*)ip_header, TYPE_IP);
udp_header = (PUDP_HEADER)((UCHAR*)ip_header + IP_HEADER_SIZE);
udp_header->source_port = ntohs(destPort);
udp_header->dest_port = ntohs(destPort);
udp_header->msg_len = htons(UDP_HEADER_SIZE + msgSize);
udp_header->checksum = 0;
// Done with headers
// start copying data to the packet chain
maxDataSize = MAX_DATASIZE - UDP_HEADER_SIZE;
packetHeaderSize = ETHER_HEADER_SIZE + IP_HEADER_SIZE + UDP_HEADER_SIZE;
totalBytesRemaining = msgSize;
packetSize = (maxDataSize < totalBytesRemaining)
? maxDataSize : totalBytesRemaining;
while (pPacket != NULL) {
NdisMoveMemory(pPacket->srcBlock + packetHeaderSize,
(PUCHAR)message + (msgSize - totalBytesRemaining),
packetSize);
// set the packet size for both srcBlock and xformBlock
pPacket->srcBlockLen =
pPacket->xformBlockLen =
packetSize + packetHeaderSize;
// copy srcBlock to xformBlock
NdisMoveMemory(pPacket->xformBlock, /*dest*/
pPacket->srcBlock,
pPacket->srcBlockLen);
// set up HeaderBuffer stuff
pPacket->HeaderBuffer = pPacket->srcBlock;
pPacket->HeaderBufferSize = ETHER_HEADER_SIZE;
pPacket->LookaheadBuffer = pPacket->srcBlock + ETHER_HEADER_SIZE;
// calculate remaining bytes
totalBytesRemaining -= packetSize;
packetSize = (maxDataSize < totalBytesRemaining)
? maxDataSize : totalBytesRemaining;
pPacket = pPacket->link;
}
if (totalBytesRemaining != 0) {
// something went wrong here....
DBG_BREAK();
}
DBG_LEAVE(0);
}
VOID PGPKernelRequest(PVOID message, UINT msgSize)
{
// PGPnetPacket * pPacket;
// PGPnetPacketHead * pHead;
// PGPUInt32 numPackets = 0,
// i = 0;
CPGPnetFilterAdapter *adapter = CPGPnetFilterDriver::GetCurrentDriver()->GetActiveFilterAdapter(0);
DBG_FUNC("PGPKernelRequest");
DBG_ENTER();
if (msgSize == 0)
CPGPnetFilterDriver::GetCurrentDriver()->OpenAdapterRequest();
if (msgSize == 1)
CPGPnetFilterDriver::GetCurrentDriver()->HookAdapterRequest();
#if 0
if (!adapter)
return;
// get a packet head
pHead = PGPNdisPacketHeadAlloc();
if (!pHead)
return;
//packet data = 1472? (1514 - 14 - 20 - 8)
numPackets = msgSize / 1472;
if (msgSize % 1472 != 0)
numPackets++;
for (i = 0; i < numPackets; i++) {
pPacket = AllocatePGPnetPacket();
if (!pPacket)
return;
AppendPGPNdisPacket(pHead, pPacket);
}
// set the last packet flags
pPacket->lastSrcBlock = pPacket->lastXformBlock = TRUE;
// set the first packet flags
pHead->link->firstSrcBlock = pHead->link->firstXformBlock = TRUE;
PGPCreateUDPPacket(adapter, pHead, 10000, message, msgSize);
adapter->FragmentPacket(pHead, 0);
// IndicatePackets frees stuff. Hopefully it doesn't screw up anything
adapter->IndicatePackets(pHead->link, FALSE /* not cfg */);
PGPNdisPacketHeadFree(pHead);
DBG_LEAVE(0);
#endif
}
void
NatSAKeepAlive(PGPByte *pDestEthAddr,
PGPUInt32 NboLocalIp,
PGPUInt32 NboPeerNatIp,
PGPUInt32 NboPeerNatPort)
{
PETHERNET_HEADER ether_header;
USHORT *ether_protocol;
PIP_HEADER ip_header;
PUDP_HEADER udp_header;
CPGPnetFilterAdapter *adapter = CPGPnetFilterDriver::GetCurrentDriver()->GetActiveFilterAdapter(0);
PGPnetPacket *packet;
DBG_FUNC("NatSAKeepAlive");
DBG_ENTER();
/* XXX check parameters */
packet = AllocatePGPnetPacket(); /* XXX check return */
ether_header = (PETHERNET_HEADER)(packet->xformBlock);
ether_protocol = ((PUSHORT)(ðer_header->eth_protocolType[0]));
NdisMoveMemory(ether_header->eth_dstAddress, pDestEthAddr, 6);
NdisMoveMemory(ether_header->eth_srcAddress, adapter->mHardwareAddress, 6);
*ether_protocol = IPPROT_NET;
//ip_header = (PIP_HEADER)(packet->srcBlock + ETHER_HEADER_SIZE);
ip_header = (PIP_HEADER)((UCHAR*)ether_header + ETHER_HEADER_SIZE);
ip_header->ip_versionandheaderlength = 0x45;
ip_header->ip_tos = 0;
/* set size to IP hdr, Udp hdr, Ike Hdr */
ip_header->ip_len = ntohs(IP_HEADER_SIZE + UDP_HEADER_SIZE + 0x01);
ip_header->ip_id = adapter->mIpId++;
ip_header->ip_foff = 0;
ip_header->ip_ttl = 0x16; // We don't want it loop forever;
ip_header->ip_prot = PROTOCOL_UDP;
ip_header->ip_chksum = 0;
ip_header->ip_src = NboLocalIp;
ip_header->ip_dest = NboPeerNatIp;
ip_header->ip_chksum = HeaderChecksum((USHORT*)ip_header, TYPE_IP);
udp_header = (PUDP_HEADER)((UCHAR*)ip_header + IP_HEADER_SIZE);
udp_header->source_port = ntohs(500);
udp_header->dest_port = NboPeerNatPort;
udp_header->msg_len = htons(UDP_HEADER_SIZE + 0x01);
udp_header->checksum = 0;
*((UCHAR *)udp_header + UDP_HEADER_SIZE) = 0xFF;
packet->xformBlockLen = ntohs(ip_header->ip_len) + ETHER_HEADER_SIZE;
packet->HeaderBuffer = packet->xformBlock;
packet->HeaderBufferSize = ETHER_HEADER_SIZE;
packet->LookaheadBuffer = (PUCHAR)ip_header;
packet->firstXformBlock = TRUE;
packet->lastXformBlock = TRUE;
packet->bIsPacketLocal = TRUE;
#ifndef NDIS_MINIPORT_DRIVER
packet->Binding = NULL;
#endif
PGPnetAdjustXformPacket(packet);
DBG_LEAVE(0);
}
PGPError
PGPInjectPacket(PGPnetPacket *packet, PGPBoolean bIncoming)
{
if (bIncoming)
return sInjectIncomingPacket(packet);
else
return sInjectOutgoingPacket(packet);
}
PGPError
sInjectOutgoingPacket(PGPnetPacket *packet)
{
PETHERNET_HEADER ether_header;
USHORT *ether_protocol;
CPGPnetFilterAdapter *adapter;
ether_header = (PETHERNET_HEADER)(packet->xformBlock);
ether_protocol = ((PUSHORT)(ðer_header->eth_protocolType[0]));
adapter = FindFilterAdapterByIpAddress(((PIP_HEADER)(packet->xformBlock + ETHER_HEADER_SIZE))->ip_src);
if (adapter) {
NdisMoveMemory(ether_header->eth_srcAddress, adapter->mHardwareAddress, 6);
*ether_protocol = IPPROT_NET;
PGPnetAdjustXformPacket(packet);
adapter->SendPacket(packet);
}
return 0;
}
PGPError
sInjectIncomingPacket(PGPnetPacket *packet)
{
PETHERNET_HEADER ether_header;
USHORT *ether_protocol;
CPGPnetFilterAdapter *adapter;
ether_header = (PETHERNET_HEADER)(packet->xformBlock);
ether_protocol = ((PUSHORT)(ðer_header->eth_protocolType[0]));
adapter = FindFilterAdapterByIpAddress(((PIP_HEADER)(packet->xformBlock + ETHER_HEADER_SIZE))->ip_dest);
if (adapter) {
NdisMoveMemory(ether_header->eth_dstAddress, adapter->mHardwareAddress, 6);
*ether_protocol = IPPROT_NET;
adapter->IndicatePacket(packet);
adapter->IndicateReceiveComplete();
FreePGPnetPacket(packet);
}
return 0;
}
#if 0
NDIS_STATUS ConfigurationIoctl(
IN NDIS_OID Oid,
IN OUT PVOID InformationBuffer,
IN UINT InformationBufferLength,
IN OUT PUINT BytesWritten,
IN OUT PUINT BytesNeeded
)
{
DBG_FUNC("ConfigurationIoctl")
NDIS_STATUS status;
PVOID SourceBuffer;
UINT SourceBufferLength;
UINT GenericULong;
DBG_ENTER();
SourceBuffer = &GenericULong;
SourceBufferLength = sizeof(GenericULong);
switch (Oid)
{
case OID_GEN_SUPPORTED_LIST:
//SourceBuffer = (PVOID)ConfigManagerSupportedOidList;
SourceBufferLength = 0; // sizeof(ConfigManagerSupportedOidList);
status = NDIS_STATUS_SUCCESS;
break;
case OID_GEN_MAC_OPTIONS:
status = NDIS_STATUS_NOT_SUPPORTED;
break;
case OID_GEN_MEDIA_CAPABILITIES:
status = NDIS_STATUS_NOT_SUPPORTED;
break;
case OID_GEN_MEDIA_SUPPORTED:
status = NDIS_STATUS_NOT_SUPPORTED;
break;
case OID_GEN_MEDIA_IN_USE:
status = NDIS_STATUS_NOT_SUPPORTED;
break;
case OID_PGP_WIN32_IOCTL:
SourceBuffer = (PVOID)NULL;
SourceBufferLength = 0;
status = NDIS_STATUS_SUCCESS;
break;
default:
status = NDIS_STATUS_INVALID_OID;
SourceBufferLength = 0;
break;
}
if (status == NDIS_STATUS_SUCCESS)
{
if (SourceBufferLength > InformationBufferLength)
{
*BytesNeeded = SourceBufferLength;
*BytesWritten = 0;
status = NDIS_STATUS_INVALID_LENGTH;
}
else if (SourceBufferLength)
{
NdisMoveMemory(InformationBuffer /*dest*/, SourceBuffer, SourceBufferLength);
*BytesWritten = SourceBufferLength;
}
else
{
*BytesNeeded = *BytesWritten = 0;
}
}
else
{
*BytesNeeded = SourceBufferLength;
*BytesWritten = 0;
}
DBG_LEAVE(status);
return (status);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -