Recently I learned about network filtering and found that there are manyProgramThe filtering function is shown on the surface, but network data cannot be filtered. In my first step, I want to write a simple program to prevent the computer from connecting to the Internet. After searching for the Internet and the help of msdn, I formed the followingCodeIt is reasonable to say that it can achieve the goal, but after running it, you can still open a webpage, watch video chats, and so on. It does not seem to have been affected at all, and no reason can be found. I don't know where it is.
1 # Include <iostream. h> 2 # Include <stdio. h> 3 # Include <winsock2.h> 4 # Include <iphlpapi. h> 5 # Include <fltdefs. h> 6 # Pragma Comment (Lib, "ws2_32 ") 7 # Pragma Comment (Lib, "iphlpapi. lib ") // . Lib extension name may not 8 9 Int Main () 10 { 11 // * ****************************** This part mainly obtains the NIC device information ** ****************************/ 12 Pip_adapter_info padapterinfo; 13 Pip_adapter_info padapter =NULL; 14 DWORD dwretval = 0 ; 15 Padapterinfo = (ip_adapter_info *) malloc ( Sizeof (Ip_adapter_info )); 16 Ulong uloutbuflen = Sizeof (Ip_adapter_info ); 17 18 If (Getadaptersinfo (padapterinfo, & uloutbuflen )! = Error_success) 19 { 20 Globalfree (padapterinfo ); 21 Padapterinfo = (ip_adapter_info * ) Malloc (uloutbuflen ); 22 } 23 If (Dwretval = getadaptersinfo (padapterinfo, & uloutbuflen) = No_error) 24 { 25 Padapter =Padapterinfo; 26 While (Padapter) 27 { 28 Printf ( " ------------------------------------------------------------ \ N " ); 29 Printf ( " Adaptername: \ t % s \ n " , Padapter-> Adaptername ); 30 Printf ( " Adapterdesc: \ t % s \ n " , Padapter-> Description ); 31 Printf ( " Adapteraddr: \ t " ); 32 For (Uint I = 0 ; I <padapter-> addresslength; I ++) 33 { 34 Printf ( " % 02x % C " , Padapter-> Address [I], 35 I = padapter-> addresslength- 1 ? ' \ N ' : ' - ' ); 36 } 37 Printf ( " Adaptertype: \ t % d \ n " , Padapter-> Type ); 38 Printf ( " IPaddress: \ t % s \ n " , Padapter-> Ipaddresslist. IPaddress. String ); 39 Printf ( " Ipmask: \ t % s \ n " , Padapter-> Ipaddresslist. ipmask. String ); 40 41 Padapter = padapter-> Next; 42 } 43 } 44 Else 45 { 46 Printf ( " Call to getadaptersinfo failed \ n " ); 47 } 48 // **************************************** **************************************** **************************************** ***/ 49 50 // **************************************** **************************************** **************************************** ***/ 51 Int Result, _ IFOR, inum; // Inum indicates the device number to be selected. _ IFOR is only used for the following for Loop
52 Pbyte lip; // Save 53 Cout < " Enter the device number to be selected, starting from 1: " ; 54 Cin> Inum; 55 56 Pip_adapter_info selectedadapter; // Extract the local IP address from here 57 58 // Filter_handle fhandle; there is only one filtering rule. You can skip this filter handle. 59 Interface_handle hinterface = NULL; 60 Result = pfcreateinterface ( 0 , Pf_action_forward, pf_action_forward, false, true, & hinterface ); // Create a filter Interface 61 If (Result! = No_error) 62 { 63 Cout < " Fail to call pfcreateinterface " < Endl; 64 Return -1 ; 65 } 66 67 // Attach the filter interface to a local IP Address 68 Ip_addr_string * Localip; 69 For (Selectedadapter = padapterinfo, _ IFOR = 1 ; Selectedadapter! = NULL; selectedadapter = selectedadapter-> next, _ IFOR ++ ) 70 { 71 If (_ IFOR = Inum) 72 { 73 // Each IP of A adapter, one adapter may have multiple IP addresses 74 For (Localip = & selectedadapter-> ipaddresslist; localip! = NULL; localip = localip-> Next) 75 { 76 Lip = (pbyte) & localip-> IPaddress. String; 77 // Cout <"LIP:" <lip <Endl; // If output is available, the local IP address is obtained successfully. This statement is removed when the local IP address is published. 78 Pfbindinterfacetoipaddress (hinterface, pf_ipv4, lip ); // Bind filter interface to local IP Address 79 } 80 Cout < " LIP: " <Lip < Endl; 81 Break ; 82 } 83 } 84 85 // * ********************* Create a filter rule, fill in the Rule structure *********************************/ 86 Pf_filter_descriptor ipflt; 87 Ipflt. dwfilterflags = fd_flags_nosyn; // Always add this value 88 Ipflt. dwrule = 0 ; // Always add this value 89 Ipflt. pfattype = pf_ipv4; // IPv4 address 90 91 Ipflt. srcaddr = lip; // Set local IP Address 92 Ipflt. srcmask = (pbyte) selectedadapter-> ipaddresslist. ipmask. String; // Local subnet mask 93 Ipflt. wsrcport = filter_tcpudp_port_any; // Any source port 94 Ipflt. wsrcporthighrange = Filter_tcpudp_port_any; 95 96 Ipflt. dstaddr = 0 ; // Any target address 97 Ipflt. dstmask = 0 ; 98 Ipflt. wdstport = filter_tcpudp_port_any; // Any target port 99 Ipflt. wdstporthighrange = Filter_tcpudp_port_any; 100 Ipflt. flatebound = 0 ; 101 102 Ipflt. dwprotocol = filter_proto_any; // The filtering protocol. Optional: filter_proto_icmp. 103 // **************************************** **************************************** ***********/ 104 105 Result = pfaddfilterstointerface (hinterface, 1 , & Ipflt, 0 , Null, null ); // Add filter rules to the interface 106 If (Result! = No_error) 107 { 108 Cout < " Fail to call pfaddfilterstointerface " < Endl; 109 Return - 1 ; 110 } 111 Result = pfremovefilterhandles (hinterface, 1 , Null ); // Remove Filter 112 If (Result! = No_error) 113 { 114 Cout < " Fail to call pfremovefiltershandles " < Endl; 115 Return - 1 ; 116 } 117 Pfunbindinterface (hinterface ); 118 Pfdeleteinterface (hinterface ); 119 Return 0 ; 120 }
Run the following command:
That is, an error occurred while adding the filter rule for the first row. If you change the input parameter 1 of this line to 0, this will not fail, but it is unreasonable and cannot block the internet.
the entire structure is simple. It is written in vc6.0. To run this program, you also need to add the iphlpapi. h and fltdefs. h header files to the iphlpapi. Lib library, because these vc6.0 files are not included by default after installation.