protected static int IPSecCreateFilterData(IntPtr hStore, IPSEC_FILTER_DATA ipsecFilterData) { IntPtr pExampleFilterData = Marshal.AllocHGlobal(Marshal.SizeOf(ipsecFilterData)); Marshal.StructureToPtr(ipsecFilterData, pExampleFilterData, false); int hr = IPSecCreateFilterData(hStore, pExampleFilterData); Marshal.FreeHGlobal(pExampleFilterData); return(hr); }
protected static int IPSecGetFilterData(IntPtr hStore, Guid filterGuid, out IPSEC_FILTER_DATA ipsecFilterData) { ipsecFilterData = new IPSEC_FILTER_DATA(); //Allocate memory for the struct pointer. IntPtr ppIpsecFilterData = Marshal.AllocHGlobal(Marshal.SizeOf(new IntPtr())); int hr = IPSecGetFilterData(hStore, filterGuid, ppIpsecFilterData); if (hr != 0) { return(hr); } //Dereference the ppIpsecISAKMPData into a pointer. IntPtr pIpsecFilterData = Marshal.ReadIntPtr(ppIpsecFilterData); ipsecFilterData = (IPSEC_FILTER_DATA)Marshal.PtrToStructure(pIpsecFilterData, typeof(IPSEC_FILTER_DATA)); Marshal.FreeHGlobal(ppIpsecFilterData); return(hr); }
/// <summary> /// Creates a filter with the specified ports. /// </summary> /// <param name="name">The name to give the filter .</param> /// <param name="ports">The ports to use in the filter .</param> /// <param name="ipsecFilterData">An out struct to return the data.</param> /// <param name="description">The description to give the filter .</param> /// <returns>>A WinError System Error Code.</returns> private int CreatePortFilter(string name, Port[] ports, out IPSEC_FILTER_DATA ipsecFilterData, string description = "") { //Initialize. ipsecFilterData = new IPSEC_FILTER_DATA() { pszIpsecName = name, pszIpsecDescription = description, dwNumFilterSpecs = ports.Length, ppFilterSpecs = IntPtr.Zero, dwWhenChanged = (int)new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds(), FilterIdentifier = Guid.NewGuid(), }; //Create an array of filter specs for each specified port. IPSEC_FILTER_SPEC[] filterSpecs = new IPSEC_FILTER_SPEC[ports.Length]; //Create an array of pointers to allocation. IntPtr[] pFilterSpecs = new IntPtr[ports.Length]; for (int i = 0; i < ports.Length; i++) { //Initialize a spec. filterSpecs[i] = new() { dwMirrorFlag = 0x0, FilterSpecGUID = Guid.NewGuid(), pszDescription = "", pszSrcDNSName = "", filter = new NativeMethods.Ipsec.IPSEC_FILTER { Flags = 0x0, Pad = '\0', TunnelAddr = 0, TunnelFilter = false } }; if (ports[i].portType == PortType.TCP) { filterSpecs[i].filter.DstPort = ports[i].port; filterSpecs[i].filter.Protocol = (int)PortType.TCP; filterSpecs[i].filter.DstUnknownFlag1 = 0x1; } else { filterSpecs[i].filter.SrcPort = ports[i].port; filterSpecs[i].filter.Protocol = (int)PortType.UDP; filterSpecs[i].dwMirrorFlag = 0x1; filterSpecs[i].filter.SrcUnknownFlag1 = 0x1; } //Marshal the struct to a pointer. pFilterSpecs[i] = Marshal.AllocHGlobal(Marshal.SizeOf(filterSpecs[i])); Marshal.StructureToPtr(filterSpecs[i], pFilterSpecs[i], false); } //Create the 2d pointer and write the struct pointers sequentially next to each other. IntPtr ppFilterSpecs = Marshal.AllocHGlobal(IntPtr.Size * ports.Length); IntPtr ptrCopy = ppFilterSpecs; for (int i = 0; i < ports.Length; i++) { Marshal.WriteIntPtr(ptrCopy, pFilterSpecs[i]); ptrCopy += IntPtr.Size; } ipsecFilterData.ppFilterSpecs = ppFilterSpecs; //Call the FriendlyMethod native. int hr = IPSecCreateFilterData(hStore, ipsecFilterData); //Free unmanaged memory. for (int i = 0; i < ports.Length; i++) { Marshal.FreeHGlobal(pFilterSpecs[i]); } Marshal.FreeHGlobal(ppFilterSpecs); return(hr); }