Developing a forwarding application

Home Forums Discussions Support Portal Developing a forwarding application

This topic contains 38 replies, has 2 voices, and was last updated by  Eldred 7 years, 7 months ago.

Viewing 15 posts - 1 through 15 (of 39 total)
  • Author
    Posts
  • #5316

    Eldred
    Participant

    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 !

    #6871

    Vadim Smirnov
    Moderator

    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.

    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.

    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.

    #6872

    Eldred
    Participant

    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…

    #6873

    Eldred
    Participant

    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:UsersEldredDocumentssrc_testip_forward>g++ -o ip_forward main.cc -lndisapi
    C:UsersEldredAppDataLocalTemp/ccO5lqAd.o:main.cc:(.text+0x19e): undefined r
    eference to `_imp___ZN8CNdisApiC1EPKc’
    C:UsersEldredAppDataLocalTemp/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 ?

    #6874

    Vadim Smirnov
    Moderator

    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++.

    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.

    #6875

    Eldred
    Participant

    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 ?

    #6876

    Vadim Smirnov
    Moderator

    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.

    #6877

    Eldred
    Participant

    OK I get it, thank you.

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

    #6878

    Vadim Smirnov
    Moderator

    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.

    #6879

    Eldred
    Participant

    It still doesn’t work… I’m just trying to build this simple program that should display “0” :

    #include
    #include
    #include “Common.h”
    #include “ndisapi.h”
    #include

    int main()
    {
    printf(“%sn”,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~1EldredLOCALS~1Temp/ccE1baaa.o(.text+0x2a):main.cc: undefined reference to `GetDriverVersion@4′

    I don’t understand…

    #6880

    Vadim Smirnov
    Moderator

    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.

    #6881

    Eldred
    Participant

    Probably. How can I check this ?

    #6882

    Vadim Smirnov
    Moderator

    There are ways. I use dumpbin tool for this
    http://msdn.microsoft.com/en-us/library/c1h23y6c%28VS.71%29.aspx

    #6883

    Eldred
    Participant

    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< 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.

    #6884

    Vadim Smirnov
    Moderator

    May be there are no packets to read from the adapter at the moment when you do read and ReadPacket returns FALSE.

Viewing 15 posts - 1 through 15 (of 39 total)

You must be logged in to reply to this topic.