Reply To: if i have two or more adapter connect to the Internet

Home Forums Discussions Support Portal if i have two or more adapter connect to the Internet Reply To: if i have two or more adapter connect to the Internet

#5704

Vadim Smirnov
Moderator

An example, it can be done like in the code below (I’m sorry C code only)

TCP_AdapterList		AdList;
CNdisApi api;
ETH_REQUEST Request;
INTERMEDIATE_BUFFER PacketBuffer;
HANDLE hEvent[32];
DWORD dwAdapterCount;

int InitHandles()
{
api.GetTcpipBoundAdaptersInfo ( &AdList );

ADAPTER_MODE Mode;

Mode.dwFlags = MSTCP_FLAG_SENT_TUNNEL|MSTCP_FLAG_RECV_TUNNEL;

dwAdapterCount = AdList.m_nAdapterCount ;

// Create notification events
for(int nCount = 0; nCount < dwAdapterCount; nCount++)
{
hEvent[nCount] = CreateEvent(NULL, TRUE, FALSE, NULL);

Mode.hAdapterHandle = (HANDLE)AdList.m_nAdapterHandle[nCount];

// Set event for helper driver
if ((!hEvent[nCount])||(!api.SetPacketEvent((HANDLE)AdList.m_nAdapterHandle[nCount], hEvent[nCount])))
{
printf ("Failed to create notification event or set it for driver.n");
return 0;
}

api.SetAdapterMode(&Mode);
}

return 1;
}

void ReleaseHandles()
{
// This function releases packets in the adapter queue and stops listening the interface
ADAPTER_MODE Mode;

for(int nCount = 0; nCount < dwAdapterCount; nCount++)
{
Mode.dwFlags = 0;
Mode.hAdapterHandle = (HANDLE)AdList.m_nAdapterHandle[nCount];

// Set NULL event to release previously set event object
api.SetPacketEvent(AdList.m_nAdapterHandle[nCount], NULL);

// Close Event
if (hEvent[nCount])
CloseHandle ( hEvent[nCount+1] );

// Set default adapter mode
api.SetAdapterMode(&Mode);

// Empty adapter packets queue
api.FlushAdapterPacketQueue (AdList.m_nAdapterHandle[nCount]);
}

}

int main(int argc, char* argv[])
{
ether_header* pEthHeader = NULL;
iphdr* pIpHeader = NULL;
DWORD dwEvent;

.............

if(!api.IsDriverLoaded())
{
printf ("Driver not installed on this system of failed to load.n");
return 0;
}

InitHandles();

atexit (ReleaseHandles);


while (TRUE)
{
dwEvent = WaitForMultipleObjects (dwAdapterCount, hEvent, FALSE, INFINITE );

ResetEvent(hEvent[dwEvent]);

// Initialize Request
ZeroMemory ( &Request, sizeof(ETH_REQUEST) );
ZeroMemory ( &PacketBuffer, sizeof(INTERMEDIATE_BUFFER) );
Request.EthPacket.Buffer = &PacketBuffer;
Request.hAdapterHandle = (HANDLE)AdList.m_nAdapterHandle[dwEvent-1];


while(api.ReadPacket(&Request))
{

pEthHeader = (ether_header*)PacketBuffer.m_IBuffer;
pIpHeader = (iphdr*)(PacketBuffer.m_IBuffer + ETHER_HEADER_LENGTH);

if (PacketBuffer.m_dwDeviceFlags == PACKET_FLAG_ON_SEND)
{
// Place packet on the network interface
api.SendPacketToAdapter(&Request);
}
else
{
// Indicate packet to MSTCP
api.SendPacketToMstcp(&Request);
}



}

}

return 0;
}