Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C

Useful Lite Effective Firewall .NET

Rate me:
Please Sign up or sign in to vote.
3.60/5 (12 votes)
5 Jan 2007 275.4K   1.3K   24   26
Take control of your computer using iphlpapi.dll
Image 1

Image 2

Introduction

This goal of this article is to show how anyone can implement their own firewall using iphlpapi.dll. I have included a BUGGY and not fully functional GUI to show you the way. If you do implement a better GUI, I will be happy to replace the one I included here.

C++
[DllImport("iphlpapi.dll", EntryPoint = "PfCreateInterface")]
        public static extern int PfCreateInterface(
                                        int dwName,
                                        PFFORWARD_ACTION inAction,
                                        PFFORWARD_ACTION outAction,
                                        bool UseLog,
                                        bool MustBeUnique,
                                        ref IntPtr ppInterface
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfDeleteInterface")]
        public static extern int PfDeleteInterface(
                                        IntPtr pInterface
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfBindInterfaceToIndex")]
        public static extern int PfBindInterfaceToIndex(
                                        IntPtr Interface_handle,
                                        int dwIndex,
                                        PFADDRESSTYPE pfatLinkType,
                                        ref int LinkIPAddress
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfBindInterfaceToIPAddress")]
        public static extern int PfBindInterfaceToIPAddress(
                                        IntPtr Interface_handle,
                                        PFADDRESSTYPE pfatType,
                                        ref int ip_address
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfUnBindInterface")]
        public static extern int PfUnBindInterface(
                                        IntPtr pInterface
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfAddFiltersToInterface")]
        public static extern int PfAddFiltersToInterface(
                                        IntPtr interface_handle,
                                        int cInFilters,
                                        [MarshalAsAttribute(UnmanagedType.Struct)] 
                                        ref PPF_FILTER_DESCRIPTOR pfiltIn,
                                        int cOutFilters,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref PPF_FILTER_DESCRIPTOR pfiltOut,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref PPF_FILTER_DESCRIPTOR pfHandle
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfRemoveFiltersFromInterface")]
        public static extern int PfRemoveFiltersFromInterface(
                                        IntPtr ih,
                                        int cInFilters,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref PPF_FILTER_DESCRIPTOR pfiltIn,
                                        int cOutFilters,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref PPF_FILTER_DESCRIPTOR pfiltOut
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfRemoveFilterHandles")]
        public static extern int PfRemoveFilterHandles(
                                        IntPtr interface_handle,
                                        int count_Filters,
                                        [MarshalAsAttribute(UnmanagedType.Struct)] 
                                        ref PPF_FILTER_DESCRIPTOR pvHandles
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfRebindFilters")]
        public static extern int PfRebindFilters(
                                        IntPtr pInterface,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref PPF_LATEBIND_INFO pLateBindInfo
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PPfAddGlobalFilterToInterface")]
        public static extern int PfAddGlobalFilterToInterface(
                                        IntPtr interface_handle,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref GLOBAL_FILTER gfFilter
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "PfRemoveGlobalFilterFromInterface")]
        public static extern int PfRemoveGlobalFilterFromInterface(
                                        IntPtr interface_handle,
                                        [MarshalAsAttribute(UnmanagedType.Struct)]
                                        ref GLOBAL_FILTER gfFilter
                                        );

        [DllImport("iphlpapi.dll", EntryPoint = "SetIpTTL")]
        public static extern int SetIpTTL(uint ttl);

History

  • 5th January, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer TECO GROUP - Jundi Electrical Industry
United States United States
if you have any questions please send an email :

ojundi @ [%s].info

Oz [Jundi]

Comments and Discussions

 
Questionthanks for the api functions Pin
Damian 228-Sep-11 23:50
Damian 228-Sep-11 23:50 
Questionnot work!!!! Pin
salar_master29-Aug-11 3:02
salar_master29-Aug-11 3:02 
QuestionBlock All ports except one for a specified IP Pin
qinta12-Nov-09 9:50
qinta12-Nov-09 9:50 
General测试 Pin
vanery29-Jun-09 16:07
vanery29-Jun-09 16:07 
Generalhello~ Pin
Bok Deuk Song27-May-09 0:49
Bok Deuk Song27-May-09 0:49 
GeneralGood One but.... Pin
jazznewbie11-Mar-09 13:07
jazznewbie11-Mar-09 13:07 
GeneralRe: Good One but.... Pin
jazznewbie11-Mar-09 13:26
jazznewbie11-Mar-09 13:26 
GeneralRe: Good One but.... Pin
jazznewbie19-Mar-09 2:11
jazznewbie19-Mar-09 2:11 
GeneralNo interfaces showing Pin
Paul Rabe9-Feb-09 23:59
Paul Rabe9-Feb-09 23:59 
GeneralI also get the stack exception 6 Pin
blipsnart23-Nov-07 5:52
blipsnart23-Nov-07 5:52 
GeneralYeah! Pin
HankiDesign6-Jul-07 8:32
HankiDesign6-Jul-07 8:32 
QuestionVista: Error 120 [modified] Pin
RichardBPutnam23-May-07 6:06
RichardBPutnam23-May-07 6:06 
AnswerRe: Vista: Error 120 Pin
Jason Kaczor16-Nov-07 14:35
professionalJason Kaczor16-Nov-07 14:35 
GeneralRe: Vista: Error 120 Pin
RichardBPutnam19-Nov-07 5:10
RichardBPutnam19-Nov-07 5:10 
GeneralRe: Vista: Error 120 Pin
powerranger12322-Mar-09 7:53
powerranger12322-Mar-09 7:53 
QuestionCould not add filter 6 Pin
bcodebcode25-Jan-07 10:47
bcodebcode25-Jan-07 10:47 
AnswerRe: Could not add filter 6 Pin
Oz Jundi25-Jan-07 16:09
Oz Jundi25-Jan-07 16:09 
GeneralRe: Could not add filter 6 Pin
bcodebcode26-Jan-07 4:09
bcodebcode26-Jan-07 4:09 
GeneralRe: Could not add filter 6 Pin
yoney mike21-May-07 9:57
yoney mike21-May-07 9:57 
GeneralRe: Could not add filter 6 Pin
Member 402502230-Dec-07 15:18
Member 402502230-Dec-07 15:18 
GeneralProblem About Tho Most Simplest FireWall Pin
umut_8416-Jan-07 2:32
umut_8416-Jan-07 2:32 
GeneralWOW! Pin
Hawk7778-Jan-07 21:00
Hawk7778-Jan-07 21:00 
GeneralFree Firewalls Pin
Vasudevan Deepak Kumar5-Jan-07 21:51
Vasudevan Deepak Kumar5-Jan-07 21:51 
GeneralRe: Free Firewalls Pin
Oz Jundi6-Jan-07 6:16
Oz Jundi6-Jan-07 6:16 
GeneralRe: Free Firewalls Pin
Thomas_fn8-Jan-07 10:57
Thomas_fn8-Jan-07 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.