IP packet filtering

Source: Internet
Author: User
Tags remove filter
/*
Use the APIS provided by the operating system to write a firewall.
For more information about the APIS involved in this program, visit the Microsoft msdn library.
The code is compiled in C ++ Builder 5
If you want to communicate with me, please email: zzwinner@163.com
*/

# Pragma hdrstop
# Include "windows. H"
# Include "fltdefs. H"
// You need to load "iphlpapi. lib"

# Pragma argsused
Int main (INT argc, char * argv [])
{
// An interface for filtering network packets
Interface_handle hinterface;
Pfcreateinterface (0,
Pf_action_drop, // pf_action_forward,
Pf_action_drop, // pf_action_forward,
False,
True,
& Hinterface );


// Bind an IP address that requires network packet filtering
Byte localip [] = {192,168 };
Pfbindinterfacetoipaddress (hinterface, pf_ipv4, localip );

// Now let's filter the HTTP interface
Filter_handle fhandle;

// Fill in the Rule structure of the filter package
Pf_filter_descriptor infilter;
Infilter. dwfilterflags = fd_flags_nosyn; // always add this value
Infilter. dwrule = 0; // always add this value
Infilter. pfattype = pf_ipv4; // use an IPv4 address.
Infilter. srcaddr = localip; // set the local IP Address
Infilter. srcmask = "/xFF"; // sets the local subnet mask.
Infilter. wsrcport = filter_tcpudp_port_any; // any source port
Infilter. wsrcporthighrange = filter_tcpudp_port_any;
Infilter. dstaddr = 0; // any target address
Infilter. dstmask = 0;
Infilter. wdstport = 80; // target port 80 (HTTP Service)
Infilter. wdstporthighrange = 80;
Infilter. dwprotocol = filter_proto_tcp; // The filtering protocol.

// Add a filter Interface
Pfaddfilterstointerface (hinterface, 1, & infilter, 0, null, & fhandle );
// Set a debugging breakpoint here and check whether your IE cannot access the Web page.

// Remove Filter
Pfremovefilterhandles (hinterface, 1, & fhandle );

Pfunbindinterface (hinterface );
Pfdeleteinterface (hinterface );

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.