/*
Use the APIS provided by the operating system to write a firewall.
The Program For API descriptions, visit the Microsoft msdn library.
Code 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;
}