It is currently Sat May 25, 2013 8:34 pm



Post new topic Reply to topic  [ 39 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Developping a forwarding application
PostPosted: Mon Jan 11, 2010 6:06 pm 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
Hello,

I have to develop an application that could work the same way as netfilter does to forward packets. It seems that I could use WinpkFilter in order to do that. I looked into the code of the internet gateway which is provided as an advanced sample ; I can't use it as is because I have to be able to set rules like this :

if (packet.source == x.x.x.x || packet.destination == y.y.y.y) then forward(packet, ipdest).

Is that possible to define this kind of rules with the WinpkFilter framework ? I noticed that some virtual interfaces don't appear in the adapters list (such as ms loopback adapters). In spite of it, could I be able to forward packets to a virtual interface ?

Finally, I already have an application that should integrate this new functionnality. Is there a difference in terms of performance between kernel or user mode for this kind of application ?

Thank you for answering me !


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 12, 2010 8:43 am 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
Quote:
if (packet.source == x.x.x.x || packet.destination == y.y.y.y) then forward(packet, ipdest).

Is that possible to define this kind of rules with the WinpkFilter framework ?


Yes, this is possible. However you have to write the forwarding code by yourself.

Quote:
I noticed that some virtual interfaces don't appear in the adapters list (such as ms loopback adapters). In spite of it, could I be able to forward packets to a virtual interface ?


WInpkFilter NDIS IM driver binds only to ethernet and wan media (defined by FilterMediaTypse in INF files). Probably Microsoft Loopback adapter specifies his media as nolower. It is possible to change WInpkFilter INF files to bind to this type of network interfaces, although I find this unsafe (as you may bind to intarfaces you don't need at all). The safer way is using different virtual network interface like VirtNet available on this web-site. VirtNet declares itself as ethernet interface.

Quote:
Finally, I already have an application that should integrate this new functionnality. Is there a difference in terms of performance between kernel or user mode for this kind of application ?


Of course kernel mode implementation is faster. You don't have to waste processor cycles for transition packets from user to kernel and back. However, CPU on desktop computers these days are powerful enough to smooth the difference.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 12, 2010 11:14 am 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
Thank you very much for your answer.
SerpentFly wrote:
Yes, this is possible. However you have to write the forwarding code by yourself.


Is there a documentation more detailed than the one provided with the samples ? I don't know how to start...


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Wed Jan 13, 2010 10:45 am 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
I installed the VirtNet interface and it should be easier to do the forwarding : if I'm able to check the packets' source and destination, I'll "just" have to call SendPacketsToAdapter() where ETH_M_REQUEST.hAdapterHandle is set to the handle of the virtnet interface.

I'm trying to build an example in order to learn how to use the API. I want to use the C++ class to integrate it into an existing program. But I use MinGW and the build fails :

C:\Users\Eldred\Documents\src_test\ip_forward>g++ -o ip_forward main.cc -lndisapi
C:\Users\Eldred\AppData\Local\Temp/ccO5lqAd.o:main.cc:(.text+0x19e): undefined r
eference to `_imp___ZN8CNdisApiC1EPKc'
C:\Users\Eldred\AppData\Local\Temp/ccO5lqAd.o:main.cc:(.text+0x1bb): undefined r
eference to `CNdisApi::~CNdisApi()'

The link seems to be correct however, because if I don't do this I have one more error. Any idea to fix this error ?

I also have another question : I ran listadapters.exe and it shew me a list of interfaces. Then, I plugged a 3G key and ran again the program : it shew me the same list as before. Is this normal ?


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Mon Jan 18, 2010 1:53 pm 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
Quote:
The link seems to be correct however, because if I don't do this I have one more error. Any idea to fix this error ?


Try to rebuild ndisapi.dll with your compiler and then link to your application. Different C++ compilers use different name mangling for C++.
Quote:
I also have another question : I ran listadapters.exe and it shew me a list of interfaces. Then, I plugged a 3G key and ran again the program : it shew me the same list as before. Is this normal ?


In Windows all WAN devices are visible as NDISWANIP virtual Ethernet interface, so adding another modem does not change the list of adapters. If you establish the connection using 3G modem you will see it in the list of the active WAN links.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 19, 2010 11:08 am 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
About the 3g modem I can't test it now but I remember I established the connection before to test and it didn't appear in the list... It's a particular modem that works in VPN mode only, does that change a thing ?


About the build, I tried to build the library with my own compiler :

g++ -c *.cpp
ar -rc ndisapi.a *.o
ar s ndisapi.a
ren ndisapi.a ndisapi.lib

Got the following errors when trying to build my program using this library :

g++ -o ip_forward *.o -lndisapi
main.o(.text+0x9c):main.cc: undefined reference to `_imp___ZN8CNdisApiC1EPKc'
D:/Utilitaires/Programmation/MinGW/bin/../lib/gcc-lib/mingw32/3.3.1/../../..\ndi
sapi.lib(ndisapi.o.b)(.text+0x3c):ndisapi.cpp: undefined reference to `_imp___ZT
V8CNdisApi'
D:/Utilitaires/Programmation/MinGW/bin/../lib/gcc-lib/mingw32/3.3.1/../../..\ndi
sapi.lib(ndisapi.o.b)(.text+0x1e8):ndisapi.cpp: undefined reference to `_imp___Z
TV8CNdisApi'
D:/Utilitaires/Programmation/MinGW/bin/../lib/gcc-lib/mingw32/3.3.1/../../..\ndi
sapi.lib(ndisapi.o.b)(.text+0x391):ndisapi.cpp: undefined reference to `_imp___Z
TV8CNdisApi'
D:/Utilitaires/Programmation/MinGW/bin/../lib/gcc-lib/mingw32/3.3.1/../../..\ndi
sapi.lib(ndisapi.o.b)(.text+0x3f3):ndisapi.cpp: undefined reference to `_imp___Z
TV8CNdisApi'
D:/Utilitaires/Programmation/MinGW/bin/../lib/gcc-lib/mingw32/3.3.1/../../..\ndi
sapi.lib(ndisapi.o.b)(.text+0x455):ndisapi.cpp: undefined reference to `_imp___Z
TV8CNdisApi'

If I copy the dll file already build into the lib folder, it gives me :

main.o(.text+0x26):main.cc: undefined reference to `GetDriverVersion@4'
main.o(.text+0x9c):main.cc: undefined reference to `_imp___ZN8CNdisApiC1EPKc'
main.o(.text+0xb9):main.cc: undefined reference to `CNdisApi::~CNdisApi()'

Now I try to build the dll on my own, but same errors again :

g++ -shared ndisapi.o -o ndisapi.dll
ndisapi.o(.text+0x3c):ndisapi.cpp: undefined reference to `_imp___ZTV8CNdisApi'
ndisapi.o(.text+0x1e8):ndisapi.cpp: undefined reference to `_imp___ZTV8CNdisApi'

ndisapi.o(.text+0x391):ndisapi.cpp: undefined reference to `_imp___ZTV8CNdisApi'

ndisapi.o(.text+0x3f3):ndisapi.cpp: undefined reference to `_imp___ZTV8CNdisApi'

ndisapi.o(.text+0x455):ndisapi.cpp: undefined reference to `_imp___ZTV8CNdisApi'

What am I missing ?


Last edited by Eldred on Tue Jan 19, 2010 12:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 19, 2010 11:40 am 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
3G modem won't appear in the list as dedicated network adapter, but you will see an active WAN link under NDISWANIP network interface when connection is established.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 19, 2010 12:35 pm 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
OK I get it, thank you.

And I'm sorry, I edited my last post because I hadn't see you had answered.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 19, 2010 2:18 pm 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
Hmm, link errors look like you have different name mangling in DLL and application. Strange issue if you have rebuilt both projects with the same compiler. May be you have to do some changes to NDISAPI.DLL to build properly with MinGW. Regretfully,I have never used MinGW and I can hardly advise how to persuade it to make proper linkages.

You may consider using C interface instead C++ one, it won't have problems like this.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Tue Jan 19, 2010 4:16 pm 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
It still doesn't work... I'm just trying to build this simple program that should display "0" :

#include <windows.h>
#include <tchar.h>
#include "Common.h"
#include "ndisapi.h"
#include <stdio.h>

int main()
{
printf("%s\n",GetDriverVersion(NULL));
return 0;
}

with g++ -o test main.cc -lndisapi

I tried to link with the provided library, then mine ; and I still have a :

D:\DOCUME~1\Eldred\LOCALS~1\Temp/ccE1baaa.o(.text+0x2a):main.cc: undefined reference to `GetDriverVersion@4'

I don't understand...


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Wed Jan 20, 2010 2:32 pm 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
I would advise you to check if ndisapi.dll built with MinGW really exports required functions. May be project needs some rework to be properly used with MinGW.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Wed Jan 20, 2010 2:44 pm 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
Probably. How can I check this ?


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Mon Jan 25, 2010 2:49 pm 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
There are ways. I use dumpbin tool for this
http://msdn.microsoft.com/en-us/library ... 71%29.aspx


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Fri Jan 29, 2010 6:51 pm 
Offline

Joined: Mon Jan 11, 2010 5:54 pm
Posts: 25
Thanks for the link. I managed to compile my own program on another system, using the C interface. I can display the adapter list ; but I can't ReadPacket(..) : it returns false.

I don't know what I am missing :

TCP_AdapterList AdList;
INTERMEDIATE_BUFFER PacketBuffer;
ETH_REQUEST ReadRequest;
ADAPTER_MODE Mode;

ZeroMemory ( &ReadRequest, sizeof(ETH_REQUEST) );

ZeroMemory ( &PacketBuffer, sizeof(INTERMEDIATE_BUFFER) );
ReadRequest.hAdapterHandle = (HANDLE)AdList.m_nAdapterHandle[IFACE];

ReadRequest.EthPacket.Buffer = &PacketBuffer;

Mode.dwFlags = MSTCP_FLAG_SENT_LISTEN|MSTCP_FLAG_RECV_LISTEN;

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

SetAdapterMode(hDrv,&Mode);

int count = 0;
while(ReadPacket(hDrv,&ReadRequest) && count<10)
{
cout<< PacketBuffer.m_Length<<endl;
count++;
}

Is there a problem of initialization, or someting like that ? This code looks like the one from the sample, but... it can't read anything although the sample can.


Top
 Profile  
 
 Post subject: Re: Developping a forwarding application
PostPosted: Mon Feb 01, 2010 6:18 pm 
Offline
Site Admin

Joined: Wed Jul 26, 2006 12:22 pm
Posts: 508
May be there are no packets to read from the adapter at the moment when you do read and ReadPacket returns FALSE.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 39 posts ]  Go to page 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Theme designed by stylerbb.net © 2008
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All times are UTC + 2 hours