write a firewall using the APIS provided by the operating system
/*
write a firewall using the APIS provided by the operating system.
for more information about the APIS involved in the Program , 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 the" lplpapi. 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 the filter Interface
Pfremovefilterhandles (hinterface, 1, & fhandle );
Pfunbindinterface (hinterface );
Pfdeleteinterface (hinterface );
Return 0;
}