您的位置:程序门 -> delphi -> windows sdk/api



哪位大哥,帮帮我啊。把一段c代码翻译成delphi的 


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


哪位大哥,帮帮我啊。把一段c代码翻译成delphi的 [已结贴,结贴人:carmen816]
发表于:2008-01-12 13:35:23 楼主
/*  
利用操作系统提供的api编写防火墙.  
该程序涉及到的api说明请访问微软的msdn   library  
代码在c++   builder   5编译通过  
如果您想和我交流请email:zzwinner@163.com  
*/  
#pragma   hdrstop  
#include   "windows.h"  
#include   "fltdefs.h"  
//   需要加载"iphlpapi.lib"  
//---------------------------------------------------------------------------  

#pragma   argsused  
int   main(int   argc,   char*   argv[])  
{  
        //   一个创建网络包过滤接口  
        interface_handle   hinterface;  
        pfcreateinterface(0,    
                                pf_action_drop,//pf_action_forward,  
                                pf_action_drop,//pf_action_forward,    
                                false,    
                                true,    
                                &hinterface);  

        //   绑定需要网络包过滤的ip地址  
        byte   localip[]   =   {192,168,0,2};  
        pfbindinterfacetoipaddress(hinterface,   pf_ipv4,   localip);  
        //   现在我们开始过滤http协议的的接口  
        filter_handle   fhandle;  
        //   填充过滤包的规则结构  
        pf_filter_descriptor   infilter;  
        infilter.dwfilterflags   =   fd_flags_nosyn;   //一直添这个值  
        infilter.dwrule   =   0;   //一直添这个值  
        infilter.pfattype   =   pf_ipv4;   //用   ipv4   地址  
        infilter.srcaddr   =   localip;   //设置本地ip地址  
        infilter.srcmask   =   "\xff\xff\xff\xff";   //设置本地子网掩码  
        infilter.wsrcport   =   filter_tcpudp_port_any;   //任意来源端口  
        infilter.wsrcporthighrange   =   filter_tcpudp_port_any;  
        infilter.dstaddr   =   0;   //任意目标地址  
        infilter.dstmask   =   0;  
        infilter.wdstport   =   80;   //目标端口   80(http   服务)  
        infilter.wdstporthighrange   =   80;  
        infilter.dwprotocol   =   filter_proto_tcp;   //   过滤的协议  
        //   加入一个过滤接口  
        pfaddfilterstointerface(hinterface,   1,   &infilter,   0,   null,   &fhandle);  
        //   请在这设置一个调试断点,然后看看你的ie是否不能访问web页.   :)  
        //   移除过滤接口  
        pfremovefilterhandles(hinterface,   1,   &fhandle);  
        pfunbindinterface(hinterface);  
        pfdeleteinterface(hinterface);  
        return   0;  
}  
发表于:2008-01-12 14:32:341楼 得分:10
c还不知道什么时候学的了。
up
发表于:2008-01-12 14:56:292楼 得分:40
.c   不难改,   fltdefs.h   要贴出来,这里面都得改
发表于:2008-01-12 15:15:093楼 得分:0
c版的fltdefs   上
c/c++ code
copyright (c) 1995-1999 microsoft corporation module name: fltdefs.h abstract: definitions for the win32 filter apis author: arnold miller (arnoldm) 24-sept-1997 revision history: --*/ #ifndef _fltdefs_h #define _fltdefs_h #if _msc_ver > 1000 #pragma once #endif typedef pvoid filter_handle, *pfilter_handle; typedef pvoid interface_handle, *pinterface_handle; #define pfexport _declspec(dllexport) #ifdef __cplusplus #define externcdecl extern_c #else #define externcdecl #endif #define pfapientry externcdecl dword pfexport winapi typedef enum _globalfilter { gf_fragments = 2, // check consistency of fragments gf_stronghost = 8, // check destination address of input frames gf_fragcache = 9 // check fragments from cache } global_filter, *pglobal_filter; typedef enum _pfforwardaction { pf_action_forward = 0, pf_action_drop } pfforward_action, *ppfforward_action; typedef enum _pfaddrestype { pf_ipv4, pf_ipv6 } pfaddresstype, *ppfaddresstype; ////////////////////////////////////////////////////////////////////////////// // // // the constants that should be used to set up the filter_info_structure // // // ////////////////////////////////////////////////////////////////////////////// #define filter_proto(protoid) makelong(makeword((protoid),0x00),0x00000) #define filter_proto_any filter_proto(0x00) #define filter_proto_icmp filter_proto(0x01) #define filter_proto_tcp filter_proto(0x06) #define filter_proto_udp filter_proto(0x11) #define filter_tcpudp_port_any (word)0x0000 #define filter_icmp_type_any (byte)0xff #define filter_icmp_code_any (byte)0xff typedef struct _pf_filter_descriptor { dword dwfilterflags; // see below dword dwrule; // copied into the log when appropriate pfaddresstype pfattype; pbyte srcaddr; pbyte srcmask; pbyte dstaddr; pbyte dstmask; dword dwprotocol; dword flatebound; word wsrcport; word wdstport; word wsrcporthighrange; word wdstporthighrange; }pf_filter_descriptor, *ppf_filter_descriptor; ////////////////////////////////////////////////////////////////////////////// // // // structure for pfgetinterfacestatistics // // // ////////////////////////////////////////////////////////////////////////////// typedef struct _pf_filter_stats { dword dwnumpacketsfiltered; pf_filter_descriptor info; }pf_filter_stats, *ppf_filter_stats; typedef struct _pf_interface_stats { pvoid pvdrivercontext; dword dwflags; // none as yet (28-sept-1997) dword dwindrops; dword dwoutdrops; pfforward_action eainaction; pfforward_action eaoutaction; dword dwnuminfilters; dword dwnumoutfilters; dword dwfrag; dword dwspoof; dword dwreserved1; dword dwreserved2; large_integer lisyn; large_integer litotallogged; dword dwlostlogentries; pf_filter_stats filterinfo[1]; } pf_interface_stats, *ppf_interface_stats; ////////////////////////////////////////////////////////////////////////////// // // // the number of bytes starting at srcaddr. if you add something to the // // structure make sure this remains valid // // // ////////////////////////////////////////////////////////////////////////////// #define filtersize \sizeof(pf_filter_descriptor) - (dword)(&((ppf_filter_descriptor)0)->srcaddr)) ////////////////////////////////////////////////////////////////////////////// // // // flags for pf_filter_descriptor // // // ////////////////////////////////////////////////////////////////////////////// // // disallows incoming syn // #define fd_flags_nosyn 0x1 // // all legal flags // #define fd_flags_allflags fd_flags_nosyn ////////////////////////////////////////////////////////////////////////////// // // // late bound defs. go in flatebound in a pf_filter_descriptor and // // describe which other fields of the filter are affected by a // // pfrebindfilters call. in general such filters are on wan interfaces // // where one or the other address may change as the connection is // // reconnected. // // the assumption is that such interfaces have only one address. // // // ////////////////////////////////////////////////////////////////////////////// #define lb_src_addr_use_srcaddr_flag 0x00000001 #define lb_src_addr_use_dstaddr_flag 0x00000002 #define lb_dst_addr_use_srcaddr_flag 0x00000004 #define lb_dst_addr_use_dstaddr_flag 0x00000008 #define lb_src_mask_late_flag 0x00000010 #define lb_dst_mask_late_flag 0x00000020 typedef struct _pf_latebind_info { pbyte srcaddr; pbyte dstaddr; pbyte mask; }pf_latebind_info, *ppf_latebind_info; ////////////////////////////////////////////////////////////////////////////// // // // the format of a logged frame and defs for it. // // // ////////////////////////////////////////////////////////////////////////////// typedef enum _pfframetype { pfft_filter = 1, // a filter violation pfft_frag = 2, // bad fragment pfft_spoof = 3 // strong host failure } pfframetype, *ppfframetype; typedef struct _pflogframe { large_integer timestamp; pfframetype pfetypeofframe; dword dwtotalsizeused; // used to find the next frame dword dwfilterrule; // from the filter word wsizeofadditionaldata; word wsizeofipheader; dword dwinterfacename; // the name of the interface dword dwipindex; byte bpacketdata[1]; // the frame. wsizeofipheader // and wsizeofadditionaldata // describe this } pflogframe, *ppflogframe;
发表于:2008-01-12 15:16:194楼 得分:0
c版的fltdefs   下
c/c++ code
////////////////////////////////////////////////////////////////////////////// // // // error codes. these extend the win32 errors by having errors specific to // // these apis. besides these errors, the apis may return any of the win32 // // errors. // // // ////////////////////////////////////////////////////////////////////////////// #define error_base 23000 #define pferror_no_pf_interface (error_base + 0) // never returned. #define pferror_no_filters_given (error_base + 1) #define pferror_buffer_too_small (error_base + 2) #define error_ipv6_not_implemented (error_base + 3) ////////////////////////////////////////////////////////////////////////////// // // // the api prototypes // // // ////////////////////////////////////////////////////////////////////////////// pfapientry pfcreateinterface( dword dwname, pfforward_action inaction, pfforward_action outaction, bool buselog, bool bmustbeunique, interface_handle *ppinterface ); pfapientry pfdeleteinterface( interface_handle pinterface ); pfapientry pfaddfilterstointerface( interface_handle ih, dword cinfilters, ppf_filter_descriptor pfiltin, dword coutfilters, ppf_filter_descriptor pfiltout, pfilter_handle pfhandle ); pfapientry pfremovefiltersfrominterface( interface_handle ih, dword cinfilters, ppf_filter_descriptor pfiltin, dword coutfilters, ppf_filter_descriptor pfiltout ); pfapientry pfremovefilterhandles( interface_handle pinterface, dword cfilters, pfilter_handle pvhandles ); pfapientry pfunbindinterface( interface_handle pinterface ); pfapientry pfbindinterfacetoindex( interface_handle pinterface, dword dwindex, pfaddresstype pfatlinktype, pbyte linkipaddress ); pfapientry pfbindinterfacetoipaddress( interface_handle pinterface, pfaddresstype pfattype, pbyte ipaddress ); pfapientry pfrebindfilters( interface_handle pinterface, ppf_latebind_info platebindinfo ); pfapientry pfaddglobalfiltertointerface( interface_handle pinterface, global_filter gffilter ); pfapientry pfremoveglobalfilterfrominterface( interface_handle pinterface, global_filter gffilter ); ////////////////////////////////////////////////////////////////////////////// // // // log apis. note that there is at most one log and it must be created // // before any interface needing it is created. there is no way to set a // // log onto an existing interface. the log can be applied to any or all of // // the interfaces. // // // ////////////////////////////////////////////////////////////////////////////// pfapientry pfmakelog( handle hevent ); // // provide a buffer, and notification parameters, and get back // the old buffer and status. // pfapientry pfsetlogbuffer( pbyte pbbuffer, dword dwsize, dword dwthreshold, dword dwentries, pdword pdwloggedentries, pdword pdwlostentries, pdword pdwsizeused ); // // doing this will disable the log on any of the interfaces. but if // an interface was created with the log, the actual log will not be // completely deleted until that interface is deleted. this is a small // point, but it might explain a mystery or two. // pfapientry pfdeletelog( void ); ////////////////////////////////////////////////////////////////////////////// // // // get statistics. note pdwbuffersize in an in/out parameter. if // // error_insufficient_buffer is returned, the common statistics are // // available and the correct byte count is in *pdwbuffersize. if only the // // interface statistics are needed, provide a buffer of size // // pf_interface_stats only. // // if the filter descriptions are also needed, then supply a large buffer, // // or use the returned count from the first call to allocate a buffer of // // sufficient size. note that for a shared interface, this second call may // // fail with error_insufficient_buffer. this can happen if the other // // sharers add filters in the interim. this should not happen for a unique // // interface. // // // ////////////////////////////////////////////////////////////////////////////// pfapientry pfgetinterfacestatistics( interface_handle pinterface, ppf_interface_stats ppfstats, pdword pdwbuffersize, bool fresetcounters ); ////////////////////////////////////////////////////////////////////////////// // // // test a packet. // // this call will evaluate the packet against the given interfaces // // and return the filtering action. // // // ////////////////////////////////////////////////////////////////////////////// pfapientry pftestpacket( interface_handle pininterface optional, interface_handle poutinterface optional, dword cbytes, pbyte pbpacket, ppfforward_action ppaction ); #endif
发表于:2008-01-12 15:18:015楼 得分:0
delphi版的fltdefs   (上)
delphi(pascal) code
unit fltdefs; //////////////////////////////////////////////////////////////////////////////// // // delphi conversion of fltdefs.h for use with the iphlpapi.dll // //////////////////////////////////////////////////////////////////////////////// interface uses windows; const iphlpapi = 'iphlpapi.dll'; // byte array type tbytearray = array [0..pred(maxint)] of byte; pbytearray = ^tbytearray; // data types type filter_handle = pointer; pfilter_handle = ^filter_handle; interface_handle = pointer; pinterface_handle = ^interface_handle; // globalfilter enumeration const gf_fragments = 2; gf_stronghost = 8; gf_fragcache = 9; type global_filter = integer; pglobal_filter = ^global_filter; // pfaddresstype enumeration const pf_ipv4 = 0; pf_ipv6 = 1; type pfaddresstype = integer; ppfaddresstype = ^pfaddresstype; // pfforwardaction enumeration const pf_action_forward = 0; pf_action_drop = 1; type pfforward_action = integer; ppfforward_action = ^ppfforward_action; // pfframetype enumeration const pfft_filter = 1; pfft_frag = 2; pfft_spoof = 3; type pfframetype = integer; ppfframetype = ^pfframetype; type _pf_filter_descriptor = packed record dwfilterflags: dword; dwrule: dword; pfattype: pfaddresstype; srcaddr: pbytearray; srcmask: pbytearray; dstaddr: pbytearray; dstmask: pbytearray; dwprotocol: dword; flatebound: dword; wsrcport: word; wdstport: word; wsrcporthighrange: word; wdstporthighrange: word; end; pf_filter_descriptor = _pf_filter_descriptor; ppf_filter_descriptor = ^pf_filter_descriptor; type _pf_filter_stats = packed record dwnumpacketsfiltered:dword; info: pf_filter_descriptor; end; pf_filter_stats = _pf_filter_stats; ppf_filter_stats = ^pf_filter_stats; type _pf_interface_stats = packed record pvdrivercontext: pointer; dwflags: dword; dwindrops: dword; dwoutdrops: dword; eainaction: pfforward_action; eaoutaction: pfforward_action; dwnuminfilters: dword; dwnumoutfilters: dword; dwfrag: dword; dwspoof: dword; dwreserved1: dword; dwreserved2: dword; lisyn: large_integer; litotallogged: large_integer; dwlostlogentries: dword; filterinfo: array [0..0] of pf_filter_stats; end; pf_interface_stats = _pf_interface_stats; ppf_interface_stats = ^pf_interface_stats; type _pf_latebind_info = packed record srcaddr: pbytearray; dstaddr: pbytearray; mask: pbytearray; end; pf_latebind_info = _pf_latebind_info; ppf_latebind_info = ^pf_latebind_info; type _pflogframe = packed record timestamp: large_integer; pfetypeofframe: pfframetype; dwtotalsizeused: dword; dwfilterrule: dword; wsizeofadditionaldata:word; wsizeofipheader: word; dwinterfacename: dword; dwipindex: dword; bpacketdata: array [0..0] of byte; end; pflogframe = _pflogframe; ppflogframe = ^pflogframe; const filter_proto_any = $00; filter_proto_icmp = $01; filter_proto_tcp = $06; filter_proto_udp = $11; filter_tcpudp_port_any = $00; const filter_icmp_type_any = $ff; filter_icmp_code_any = $ff; const fd_flags_nosyn = $01; fd_flags_allflags = fd_flags_nosyn; const lb_src_addr_use_srcaddr_flag = $00000001; lb_src_addr_use_dstaddr_flag = $00000002; lb_dst_addr_use_srcaddr_flag = $00000004; lb_dst_addr_use_dstaddr_flag = $00000008; lb_src_mask_late_flag = $00000010; lb_dst_mask_late_flag = $00000020; const error_base = 23000; pferror_no_pf_interface = (error_base + 0); // never returned. pferror_no_filters_given = (error_base + 1); pferror_buffer_too_small = (error_base + 2); error_ipv6_not_implemented = (error_base + 3); //////////////////////////////////////////////////////////////////////////////// // // filter functions exported by iphlpapi // //////////////////////////////////////////////////////////////////////////////// function pfcreateinterface( dwname: dword; inaction: pfforward_action; outaction: pfforward_action; buselog: bool; bmustbeunique: bool; var ppinterface: interface_handle): dword; stdcall; external iphlpapi name '_pfcreateinterface@24'; function pfdeleteinterface( pinterface: interface_handle): dword; stdcall; external iphlpapi name '_pfdeleteinterface@4'; function pfaddfilterstointerface( ih: interface_handle; cinfilters: dword; pfiltin: ppf_filter_descriptor; coutfilters: dword; pfiltout: ppf_filter_descriptor; pfhandle: pfilter_handle): dword; stdcall; external iphlpapi name '_pfaddfilterstointerface@24'; function pfremovefiltersfrominterface( ih: interface_handle; cinfilters: dword; pfiltin: ppf_filter_descriptor; coutfilters: dword; pfiltout: ppf_filter_descriptor): dword; stdcall; external iphlpapi name '_pfremovefiltersfrominterface@20'; function pfremovefilterhandles( pinterface: interface_handle; cfilters: dword; pvhandles: pfilter_handle): dword; stdcall; external iphlpapi name '_pfremovefilterhandles@12'; function pfunbindinterface( pinterface: interface_handle): dword; stdcall; external iphlpapi name '_pfunbindinterface@4'; function pfbindinterfacetoindex( pinterface: interface_handle; dwindex: dword; pfatlinktype: pfaddresstype; linkipaddress: pbytearray): dword; stdcall; external iphlpapi name '_pfbindinterfacetoindex@16'; function pfbindinterfacetoipaddress( pinterface: interface_handle; pfatlinktype: pfaddresstype; ipaddress: pbytearray): dword; stdcall; external iphlpapi name '_pfbindinterfacetoipaddress@12'; function pfrebindfilters( pinterface: interface_handle; platebindinfo: ppf_latebind_info): dword; stdcall; external iphlpapi name '_pfrebindfilters@8'; function pfaddglobalfiltertointerface( pinterface: interface_handle; gffilter: global_filter): dword; stdcall; external iphlpapi name '_pfaddglobalfiltertointerface@8'; function pfremoveglobalfilterfrominterface( pinterface: interface_handle; gffilter: global_filter): dword; stdcall; external iphlpapi name '_pfremoveglobalfilterfrominterface@8';
发表于:2008-01-12 15:18:586楼 得分:0
delphi版的fltdefs       (下)  
delphi(pascal) code
//////////////////////////////////////////////////////////////////////////////// // // log apis. note that there is at most one log and it must be created // before any interface needing it is created. there is no way to set a // log onto an existing interface. the log can be applied to any or all of // the interfaces. // /////////////////////////////////////////////////////////////////////// function pfmakelog( hevent: thandle): dword; stdcall; external iphlpapi name '_pfmakelog@4'; function pfsetlogbuffer( pbbuffer: pbytearray; dwsize: dword; dwthreshold: dword; dwentries: dword; pdwloggedentries: pdword; pdwlostentries: pdword; pdwsizeused: pdword): dword; stdcall; external iphlpapi name '_pfsetlogbuffer@28'; function pfdeletelog( ): dword; stdcall; external iphlpapi name '_pfdeletelog@0'; //////////////////////////////////////////////////////////////////////////////// // // get statistics. note pdwbuffersize in an in/out parameter. if // error_insufficient_buffer is returned, the common statistics are // available and the correct byte count is in *pdwbuffersize. if only the // interface statistics are needed, provide a buffer of size // pf_interface_stats only. if the filter descriptions are also needed, // then supply a large buffer, or use the returned count from the first call // to allocate a buffer of sufficient size. note that for a shared interface, // this second call may fail with error_insufficient_buffer. this can happen // if the other sharers add filters in the interim. this should not happen for // a unique interface. // //////////////////////////////////////////////////////////////////////////////// function pfgetinterfacestatistics( pinterface: interface_handle; ppfstats: ppf_interface_stats; pdwbuffersize: pdword; fresetcounters: bool): dword; stdcall; external iphlpapi name '_pfgetinterfacestatistics@16'; //////////////////////////////////////////////////////////////////////////////// // // test a packet. this call will evaluate the packet against the given // interfaces and return the filtering action. // //////////////////////////////////////////////////////////////////////////////// function pftestpacket( pininterface: interface_handle; poutinterface: interface_handle; cbytes: dword; pbpacket: pbytearray; ppaction: ppfforward_action): dword; stdcall; external iphlpapi name '_pftestpacket@20'; implementation end. (********) //uses winsock, fltdefs; procedure main(); const localip : array[0..3] of byte =192,168,0,2); filter_tcpudp_port_any : word = $0000; fd_flags_nosyn = $1; var hinterface : interface_handle; fhandle : filter_handle; infilter : pf_filter_descriptor; filter_proto_tcp : dword; dwsrcmask : dword; begin filter_proto_tcp := makelong(makeword(($06),$00),$00000); dwsrcmask := $ffffffff; pfcreateinterface(0, pf_action_drop,//pf_action_forward, pf_action_drop,//pf_action_forward, false, true, hinterface); pfbindinterfacetoipaddress(hinterface, pf_ipv4, @localip); infilter.dwfilterflags := fd_flags_nosyn; infilter.dwrule := 0; infilter.pfattype := pf_ipv4; infilter.srcaddr := @localip; infilter.srcmask := @dwsrcmask; infilter.wsrcport := filter_tcpudp_port_any; infilter.wsrcporthighrange := filter_tcpudp_port_any; infilter.dstaddr := nil; infilter.dstmask := nil; infilter.wdstport := 80; infilter.wdstporthighrange := 80; infilter.dwprotocol := filter_proto_tcp; pfaddfilterstointerface(hinterface, 1, @infilter, 0, nil, @fhandle); pfremovefilterhandles(hinterface, 1, @fhandle); pfunbindinterface(hinterface); pfdeleteinterface(hinterface); end;
发表于:2008-01-12 16:55:437楼 得分:0
发表于:2008-01-12 23:47:378楼 得分:0
comanche   大哥
d版的也有了,代码翻译啊
发表于:2008-01-13 01:29:039楼 得分:50
楼主难道没发现自己贴出来的代码当中就有了?
下面这段不就是了?
delphi(pascal) code
//////////////////////////////////////////////////////////////////////////////// // // test a packet. this call will evaluate the packet against the given // interfaces and return the filtering action. // //////////////////////////////////////////////////////////////////////////////// function pftestpacket( pininterface: interface_handle; poutinterface: interface_handle; cbytes: dword; pbpacket: pbytearray; ppaction: ppfforward_action): dword; stdcall; external iphlpapi name '_pftestpacket@20'; implementation end. (********) //uses winsock, fltdefs; procedure main(); const localip : array[0..3] of byte =192,168,0,2); filter_tcpudp_port_any : word = $0000; fd_flags_nosyn = $1; var hinterface : interface_handle; fhandle : filter_handle; infilter : pf_filter_descriptor; filter_proto_tcp : dword; dwsrcmask : dword; begin filter_proto_tcp := makelong(makeword(($06),$00),$00000); dwsrcmask := $ffffffff; pfcreateinterface(0, pf_action_drop,//pf_action_forward, pf_action_drop,//pf_action_forward, false, true, hinterface); pfbindinterfacetoipaddress(hinterface, pf_ipv4, @localip); infilter.dwfilterflags := fd_flags_nosyn; infilter.dwrule := 0; infilter.pfattype := pf_ipv4; infilter.srcaddr := @localip; infilter.srcmask := @dwsrcmask; infilter.wsrcport := filter_tcpudp_port_any; infilter.wsrcporthighrange := filter_tcpudp_port_any; infilter.dstaddr := nil; infilter.dstmask := nil; infilter.wdstport := 80; infilter.wdstporthighrange := 80; infilter.dwprotocol := filter_proto_tcp; pfaddfilterstointerface(hinterface, 1, @infilter, 0, nil, @fhandle); pfremovefilterhandles(hinterface, 1, @fhandle); pfunbindinterface(hinterface); pfdeleteinterface(hinterface); end;
发表于:2008-01-13 17:42:2810楼 得分:0
超汗。。。


快速检索

最新资讯