How to pinvoke setsockopt to set bpf filter

-1

I'm trying to pinvoke setsockopt from .NET core 3.0 preview 6 to set the bpf filter, but apparently don't know how to marshal it and always get back -1 as the result...

EDIT: Added the struct definitions as found 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;
};

The external signature...

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);
}

How I currently have the structs defined in 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;
}

And finally my attempt at constructing things to use it...

        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);
c#
sockets
pinvoke
setsockopt
asked on Stack Overflow Jun 18, 2019 by los93sol • edited Jun 18, 2019 by los93sol

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0