I'm trying to port some netlink socket code from C to C#. The code is the same as in the link:
First, we port sockaddr_nl
structure (c source):
[StructLayout(LayoutKind.Sequential)]
public struct sockaddr_nl
{
public ushort nl_family;
public ushort nl_pad;
public uint nl_pid;
public uint nl_groups;
}
Then, get a socket fd using
var fd = -1;
fd = UnixFiller.socket(
(int) UnixAddressFamily.AF_NETLINK,
(int) UnixSocketType.SOCK_RAW | 0x80000,
0 /* NETLINK_ROUTE */
);
if(fd < 0)
FatalError("Failed to create routing socket.");
UnixFiller.socket
is just a DLLImport from libc. This call succeeds as expected.
However, the next step would be to cast the expressive sockaddr_nl
structure to the family + byte array structure sockaddr
consumed by bind:
bind(fd, (struct sockaddr*)&address, sizeof(address));
Where sockaddr
is
struct sockaddr {
sa_family_t sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};
Which, as far as I know, would be this in C#:
[StructLayout(LayoutKind.Sequential)]
public struct sockaddr
{
public ushort sa_familiy;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] byte[] sa_data;
}
Question
First, how should the bind
import look like?
[DllImport("libc", SetLastError = true, EntryPoint = "bind")]
public static extern int bind(int sockfd, ??? addr, int addrlen);
??? = unknown.
Next, how can I port the cast from sockaddr_nl
to sockaddr
in order to pass the right format to bind
?