如何设置setsockopt设置bpf过滤器

时间:2019-06-18 02:53:20

标签: c# sockets pinvoke setsockopt

我正在尝试从.NET Core 3.0预览版6中设置setsockopt来设置bpf过滤器,但是显然不知道如何将其封送,结果总是返回-1 ...

编辑:添加了here的结构定义

struct sock_filter {    /* Filter block */
    __u16   code;   /* Actual filter code */
    __u8    jt; /* Jump true */
    __u8    jf; /* Jump false */
    __u32   k;      /* Generic multiuse field */
};

struct sock_fprog {         /* Required for SO_ATTACH_FILTER. */
    unsigned short         len; /* Number of filter blocks */
    struct sock_filter __user *filter;
};

外部签名...

internal static partial class Sys
{
    [DllImport("libc", SetLastError = true)]
    public static extern int setsockopt(int socketFc, int level, int optname, sock_fprog optval, int optlem);
}

我目前如何在C#中定义结构...

[StructLayout(LayoutKind.Sequential)]
public struct sock_filter
{
    public ushort code;
    public byte jt;
    public byte jf;
    public int k;
}

[StructLayout(LayoutKind.Sequential)]
public struct sock_fprog
{
    public ushort len;
    public IntPtr filter;
}

最后,我尝试构建使用它的东西...

        var filters = new sock_filter[]
        {
            new sock_filter{ code = 0x28, jt = 0, jf = 0, k = 0x0000000c },
            new sock_filter{ code = 0x15, jt = 0, jf = 8, k = 0x000086dd },
            new sock_filter{ code = 0x30, jt = 0, jf = 0, k = 0x00000014 },
            new sock_filter{ code = 0x15, jt = 2, jf = 0, k = 0x00000084 },
            new sock_filter{ code = 0x15, jt = 1, jf = 0, k = 0x00000006 },
            new sock_filter{ code = 0x15, jt = 0, jf = 17, k = 0x00000011 },
            new sock_filter{ code = 0x28, jt = 0, jf = 0, k = 0x00000036 },
            new sock_filter{ code = 0x15, jt = 14, jf = 0, k = 0x00001f97 },
            new sock_filter{ code = 0x28, jt = 0, jf = 0, k = 0x00000038 },
            new sock_filter{ code = 0x15, jt = 12, jf = 13, k = 0x00001f97 },
            new sock_filter{ code = 0x15, jt = 0, jf = 12, k = 0x00000800 },
            new sock_filter{ code = 0x30, jt = 0, jf = 0, k = 0x00000017 },
            new sock_filter{ code = 0x15, jt = 2, jf = 0, k = 0x00000084 },
            new sock_filter{ code = 0x15, jt = 1, jf = 0, k = 0x00000006 },
            new sock_filter{ code = 0x15, jt = 0, jf = 8, k = 0x00000011 },
            new sock_filter{ code = 0x28, jt = 0, jf = 0, k = 0x00000014 },
            new sock_filter{ code = 0x45, jt = 6, jf = 0, k = 0x00001fff },
            new sock_filter{ code = 0xb1, jt = 0, jf = 0, k = 0x0000000e },
            new sock_filter{ code = 0x48, jt = 0, jf = 0, k = 0x0000000e },
            new sock_filter{ code = 0x15, jt = 2, jf = 0, k = 0x00001f97 },
            new sock_filter{ code = 0x48, jt = 0, jf = 0, k = 0x00000010 },
            new sock_filter{ code = 0x15, jt = 0, jf = 1, k = 0x00001f97 },
            new sock_filter{ code = 0x6, jt = 0, jf = 0, k = 0x00040000 },
            new sock_filter{ code = 0x6, jt = 0, jf = 0, k = 0x00000000 }
        };

        bpf = new sock_fprog
        {
            len = (ushort)filters.Length,
            filter = Marshal.AllocHGlobal(filters.Length * Marshal.SizeOf(typeof(sock_filter)))
        };

        var length = Marshal.SizeOf(bpf);

var SO_ATTACH_FILTER = 26;
var result = Sys.setsockopt((int)_socket.Handle, (int)SocketOptionLevel.Socket, SO_ATTACH_FILTER, bpf, length);

0 个答案:

没有答案