diff --git a/src/SocketCANSharp/LibcNativeMethods.cs b/src/SocketCANSharp/LibcNativeMethods.cs index 6350b98..00474d1 100644 --- a/src/SocketCANSharp/LibcNativeMethods.cs +++ b/src/SocketCANSharp/LibcNativeMethods.cs @@ -38,886 +38,886 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE namespace SocketCANSharp { - /// - /// C Standard Library Native Methods - /// - public static class LibcNativeMethods - { - /// - /// Number of the last error which indicates what went wrong. Set by system calls and some library functions when an error occurs. - /// - public static int Errno { get { return Marshal.GetLastWin32Error(); } } - - /// - /// Creates a CAN socket. - /// - /// Address Family - /// Type of socket - /// CAN Protocol Type - /// Socket Handle Wrapper Instance - [DllImport("libc", EntryPoint="socket", SetLastError=true)] - public static extern SafeFileDescriptorHandle Socket(int addressFamily, SocketType socketType, SocketCanProtocolType protocolType); - - /// - /// Manipulates the underlying device parameters of special files. - /// - /// Socket Handle Wrapper Instance - /// Request Code - /// Integer Argument - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="ioctl", SetLastError=true)] - public static extern int Ioctl(SafeFileDescriptorHandle socketHandle, int request, ref int arg); - - /// - /// Manipulates the underlying device parameters of special files. - /// - /// Socket Handle Wrapper Instance - /// Request Code - /// Interface Request structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="ioctl", SetLastError=true)] - public static extern int Ioctl(SafeFileDescriptorHandle socketHandle, int request, [In][Out] Ifreq ifreq); - - /// - /// Manipulates the underlying device parameters of special files. - /// - /// Socket Handle Wrapper Instance - /// Request Code - /// Interface Request structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] - public static extern int Ioctl(SafeFileDescriptorHandle socketHandle, int request, [In][Out] IfreqMtu ifreq); - - /// - /// Used to obtain a timeval struct with the receive timestamp of the last packet passed to the user. - /// - /// Socket Handle Wrapper Instance - /// Request Code - /// Timeval structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] - public static extern int Ioctl(SafeFileDescriptorHandle socketHandle, int request, [In][Out] Timeval timeval); - - /// - /// Assigns the specified SocketCAN base address to the socket. - /// - /// Socket Handle Wrapper Instance - /// SocketCAN base address structure - /// Size of address structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="bind", SetLastError=true)] - public static extern int Bind(SafeFileDescriptorHandle socketHandle, SockAddrCan addr, int addrSize); - - /// - /// Assigns the specified SocketCAN ISO-TP address to the socket. - /// - /// Socket Handle Wrapper Instance - /// SocketCAN ISO-TP address structure - /// Size of address structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="bind", SetLastError=true)] - public static extern int Bind(SafeFileDescriptorHandle socketHandle, SockAddrCanIsoTp addr, int addrSize); - - /// - /// Assigns the specified SocketCAN J1939 address to the socket. - /// - /// Socket Handle Wrapper Instance - /// SocketCAN J1939 address structure - /// Size of address structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="bind", SetLastError=true)] - public static extern int Bind(SafeFileDescriptorHandle socketHandle, SockAddrCanJ1939 addr, int addrSize); - - /// - /// Establishes a connection on the socket to the specified SocketCAN base address. - /// - /// Socket Handle Wrapper Instance - /// SocketCAN base address structure containing the peer address - /// Size of address structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="connect", SetLastError=true)] - public static extern int Connect(SafeFileDescriptorHandle socketHandle, SockAddrCan addr, int addrSize); - - /// - /// Establishes a connection on the socket to the specified SocketCAN base address. - /// - /// Socket Handle Wrapper Instance - /// SocketCAN J1939 address structure containing the peer address - /// Size of address structure - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="connect", SetLastError=true)] - public static extern int Connect(SafeFileDescriptorHandle socketHandle, SockAddrCanJ1939 addr, int addrSize); - - /// - /// Write the CanFrame to the socket. - /// - /// Socket Handle Wrappper Instance - /// CAN Frame to write - /// Size of CAN Frame in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, ref CanFrame frame, int frameSize); - - /// - /// Write the CanFdFrame to the socket. - /// - /// Socket Handle Wrapper Instance - /// CAN FD Frame to write - /// Size of CAN FD Frame in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, ref CanFdFrame frame, int frameSize); - - /// - /// Write the CanXlFrame to the socket. - /// - /// Socket Handle Wrapper Instance - /// CAN XL Frame to write - /// Size of CAN XL Frame in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, ref CanXlFrame frame, int frameSize); - - /// - /// Write the BcmCanMessage to the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanMessage message, int msgSize); - - /// - /// Write the BcmCanFdMessage to the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanFdMessage message, int msgSize); - - /// - /// Write the BcmCanSingleMessage to the socket. - /// - /// Socket Handle Wrapper Instance - /// Special Single Frame BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanSingleMessage message, int msgSize); - - /// - /// Write the BcmCanFdSingleMessage to the socket. - /// - /// Socket Handle Wrapper Instance - /// Special Single Frame BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanFdSingleMessage message, int msgSize); - - /// - /// Write the BcmMessageHeader to the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM Message Header to write - /// Size of BCM Message Header in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmMessageHeader header, int headerSize); - - /// - /// Write the BcmCanMessage to the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanMessage32 message, int msgSize); - - /// - /// Write the BcmCanFdMessage to the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanFdMessage32 message, int msgSize); - - /// - /// Write the BcmCanSingleMessage to the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// Special Single Frame BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanSingleMessage32 message, int msgSize); - - /// - /// Write the BcmCanFdSingleMessage to the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// Special Single Frame BCM Message to write - /// Size of BCM Message in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmCanFdSingleMessage32 message, int msgSize); - - /// - /// Write the BcmMessageHeader to the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM Message Header to write - /// Size of BCM Message Header in bytes - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, BcmMessageHeader32 header, int headerSize); - - /// - /// Write the byte array to the socket. - /// - /// Socket Handle Wrapper Instance - /// Byte Array to write - /// Size of Byte Array - /// The number of bytes written on success, -1 on error - [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, byte[] data, int dataSize); - - /// - /// Read a CanFrame from the socket. - /// - /// Socket Handle Wrapper Instance - /// CAN Frame structure to populate - /// Size of CAN Frame structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, ref CanFrame frame, int frameSize); - - /// - /// Read a CanFdFrame from the socket. - /// - /// Socket Handle Wrapper Instance - /// CAN FD Frame structure to populate - /// Size of CAN FD Frame structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, ref CanFdFrame frame, int frameSize); - - /// - /// Read a CanXlFrame from the socket. - /// - /// Socket Handle Wrapper Instance - /// CAN XL Frame structure to populate - /// Size of CAN XL Frame structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, ref CanXlFrame frame, int frameSize); - - /// - /// Read a BcmCanMessage from the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM CAN Message structure to populate - /// Size of BCM CAN Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmCanMessage message, int msgSize); - - /// - /// Read a BcmGenericMessage from the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM Generic Message structure to populate - /// Size of BCM Generic Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmGenericMessage message, int msgSize); - - /// - /// Read a BcmGenericMessage from the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM Generic Message structure to populate - /// Size of BCM Generic Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmGenericMessage32 message, int msgSize); - - /// - /// Read a BcmCanFdMessage from the socket. - /// - /// Socket Handle Wrapper Instance - /// BCM CAN Message structure to populate - /// Size of BCM CAN FD Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmCanFdMessage message, int msgSize); - - /// - /// Read a BcmCanMessage from the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM CAN Message structure to populate - /// Size of BCM CAN Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmCanMessage32 message, int msgSize); - - /// - /// Read a BcmCanFdMessage from the socket. Variant for 32-bit. - /// - /// Socket Handle Wrapper Instance - /// BCM CAN Message structure to populate - /// Size of BCM CAN FD Message structure - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] BcmCanFdMessage32 message, int msgSize); - - /// - /// Read a byte array from the socket. - /// - /// Socket Handle Wrapper Instance - /// Byte array to populate - /// Size of byte array - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="read", SetLastError=true)] - public static extern int Read(SafeFileDescriptorHandle socketHandle, [Out] byte[] data, int dataSize); - - /// - /// Receive a CanFrame from a socket. - /// - /// Socket Handle Wrapper Instance - /// CAN Frame structure - /// Size of CAN Frame structure - /// Message Flags - /// The SocketCAN base source address - /// The size of the SocketCAN base address - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="recvfrom", SetLastError=true)] - public static extern int RecvFrom(SafeFileDescriptorHandle socketHandle, ref CanFrame frame, int frameSize, MessageFlags flags, [Out] SockAddrCan addr, ref int addrSize); - - /// - /// Receive a CanFdFrame from a socket. - /// - /// Socket Handle Wrapper Instance - /// CAN FD Frame structure - /// Size of CAN FD Frame structure - /// Message Flags - /// The SocketCAN base source address - /// The size of the SocketCAN base address - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="recvfrom", SetLastError=true)] - public static extern int RecvFrom(SafeFileDescriptorHandle socketHandle, ref CanFdFrame frame, int frameSize, MessageFlags flags, [Out] SockAddrCan addr, ref int addrSize); - - /// - /// Receive a byte array from a socket. - /// - /// Socket Handle Wrapper Instance - /// Byte array - /// Size of byte array - /// Message Flags - /// The SocketCAN J1939 source address - /// The size of the SocketCAN J1939 address - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint = "recvfrom", SetLastError = true)] - public static extern int RecvFrom(SafeFileDescriptorHandle socketHandle, [Out] byte[] data, int dataSize, MessageFlags flags, [Out] SockAddrCanJ1939 addr, ref int addrSize); - - /// - /// Receive a byte array on a connected socket. - /// - /// Socket Handle Wrapper Instance - /// Byte array - /// Size of byte array - /// Message Flags - /// The number of bytes read on success, -1 on error - [DllImport("libc", EntryPoint="recv", SetLastError=true)] - public static extern int Recv(SafeFileDescriptorHandle socketHandle, [Out] byte[] data, int dataSize, MessageFlags flags); - - /// - /// Transmit a CanFrame to another socket. - /// - /// Socket Handle Wrapper Instance - /// CAN Frame structure - /// Size of CAN Frame structure - /// Message Flags - /// The SocketCAN base destination address - /// The size of the SocketCAN base address - /// The number of bytes sent on success, -1 on error - [DllImport("libc", EntryPoint="sendto", SetLastError=true)] - public static extern int SendTo(SafeFileDescriptorHandle socketHandle, ref CanFrame frame, int frameSize, MessageFlags flags, SockAddrCan addr, int addrSize); - - /// - /// Transmit a CanFdFrame to another socket. - /// - /// Socket Handle Wrapper Instance - /// CAN FD Frame structure - /// Size of CAN FD Frame structure - /// Message Flags - /// The SocketCAN base destination address - /// The size of the SocketCAN base address - /// The number of bytes sent on success, -1 on error - [DllImport("libc", EntryPoint="sendto", SetLastError=true)] - public static extern int SendTo(SafeFileDescriptorHandle socketHandle, ref CanFdFrame frame, int frameSize, MessageFlags flags, SockAddrCan addr, int addrSize); - - /// - /// Transmit a byte array to another socket. - /// - /// Socket Handle Wrapper Instance - /// Byte array - /// Size of byte array - /// Message Flags - /// The SocketCAN J1939 destination address - /// The size of the SocketCAN J1939 address - /// The number of bytes sent on success, -1 on error - [DllImport("libc", EntryPoint="sendto", SetLastError=true)] - public static extern int SendTo(SafeFileDescriptorHandle socketHandle, byte[] data, int dataSize, MessageFlags flags, SockAddrCanJ1939 addr, int addrSize); - - /// - /// Transmit a byte array to another socket. - /// - /// Socket Handle Wrapper Instance - /// Byte array - /// Size of byte array - /// Message Flags - /// The number of bytes sent on success, -1 on error - [DllImport("libc", EntryPoint="send", SetLastError=true)] - public static extern int Send(SafeFileDescriptorHandle socketHandle, byte[] data, int dataSize, MessageFlags flags); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Unsigned integer value - /// Size of unsigned interger - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref uint optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Unsigned integer value - /// Size of an unsigned integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref uint optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Array of CAN Filters - /// Size of CAN Filter Array in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanFilter[] filters, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Array of CAN Filters - /// Size of CAN Filter Array in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, [In, Out] CanFilter[] filters, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref int optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref int optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// SOL_SOCKET - /// SOL_SOCKET socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, ref int optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// SOL_SOCKET - /// SOL_SOCKET socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, ref int optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// SOL_SOCKET - /// SOL_SOCKET socket option - /// Time interval object - /// Size of Time interval object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, Timeval timeval, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// SOL_SOCKET - /// SOL_SOCKET socket option - /// Time interval object - /// Size of Time interval object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, [In, Out] Timeval timeval, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// Unsigned Integer Value - /// Size of unsigned integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, ref uint optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// Unsigned Integer Value - /// Size of unsigned integer - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, ref uint optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Flow Control Options object - /// Size of ISO-TP Flow Control Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpFlowControlOptions fcOptions, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Flow Control Options object - /// Size of ISO-TP Flow Control Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpFlowControlOptions fcOptions, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Link Layer Options object - /// Size of ISO-TP Link Layer Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpLinkLayerOptions llOptions, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Link Layer Options object - /// Size of ISO-TP Link Layer Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpLinkLayerOptions llOptions, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Options object - /// Size of ISO-TP Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpOptions tpOptions, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_ISOTP Socket handle - /// SOL_CAN_ISOTP - /// SOL_CAN_ISOTP socket option - /// ISO-TP Options object - /// Size of ISO-TP Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpOptions tpOptions, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_J1939 Socket handle - /// SOL_CAN_J1939 - /// SOL_CAN_J1939 socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 or 1 on success depending on option name and value, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, ref int optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_J1939 Socket handle - /// SOL_CAN_J1939 - /// SOL_CAN_J1939 socket option - /// Signed Integer Value - /// Size of signed integer - /// 0 or 1 on success depending on option name and value, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, ref int optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_J1939 Socket handle - /// SOL_CAN_J1939 - /// SOL_CAN_J1939 socket option - /// Array of J1939 Filters - /// Size of array of J1939 Filters in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, J1939Filter[] filters, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_J1939 Socket handle - /// SOL_CAN_J1939 - /// SOL_CAN_J1939 socket option - /// Array of J1939 Filters - /// Size of array of J1939 Filters in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, [In, Out] J1939Filter[] filters, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// Socket level - /// Socket option - /// Pointer to option value - /// Size of the buffer pointed to by optionValue - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, int optionName, IntPtr optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// Socket handle - /// Socket level - /// Socket option - /// Pointer to option value - /// Size of the buffer pointed to by optionValue - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, int optionName, IntPtr optionValue, ref int optionValueSize); - - /// - /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// VCID Options object - /// Size of VCID Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="setsockopt", SetLastError=true)] - public static extern int SetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanRawVcidOptions optionValue, int optionValueSize); - - /// - /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. - /// - /// CAN_RAW Socket handle - /// SOL_CAN_RAW - /// SOL_CAN_RAW socket option - /// VCID Options object - /// Size of VCID Options object in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockopt", SetLastError=true)] - public static extern int GetSockOpt(SafeFileDescriptorHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanRawVcidOptions optionValue, ref int optionValueSize); - - /// - /// Returns a pointer to an array of IfNameIndex objects. Each IfNameIndex object includes information about one of the network interfaces on the local system. - /// - /// Pointer to an array of IfNameIndex objects - [DllImport("libc", EntryPoint="if_nameindex", SetLastError=true)] - public static extern IntPtr IfNameIndex(); - - /// - /// Frees the dynamically allocated data structure returned by IfNameIndex(). - /// - /// Pointer to an array of IfNameIndex objects - [DllImport("libc", EntryPoint="if_freenameindex", SetLastError=true)] - public static extern void IfFreeNameIndex(IntPtr ptr); - - /// - /// Opens an epoll file descriptor. - /// - /// The size argument is ignored since Linux 2.6.8, but must be greater than zero for backwards compatibility. - /// Originally, this argument was intended as a hint to the kernel as to the number of file descriptors that the caller expected to add to the epoll instance. - /// On success, returns a valid file descriptor handle. On failure, returns an invalid file descriptor handle. - [DllImport("libc", EntryPoint="epoll_create", SetLastError=true)] - public static extern SafeFileDescriptorHandle EpollCreate(int size); - - /// - /// Control interface used to add, modify, and delete entries from the interest list of an epoll file descriptor. - /// - /// Epoll File Descriptor. - /// Operation to be performed. - /// Target file descriptor. - /// Event object linked to the targeted file descriptor. - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="epoll_ctl", SetLastError=true)] - public static extern int EpollControl(SafeFileDescriptorHandle epfd, EpollOperation op, SafeFileDescriptorHandle fd, ref EpollEvent evnt); - - /// - /// Waits for an I/O event on an epoll file descriptor. - /// - /// Epoll File Descriptor. - /// A buffer that contains information from the ready lists about file descriptors in the interest list that have some event(s) available. - /// Maximum number of events to wait for. - /// The maximum number of milliseconds that the function call will block for. Set to 0 to return immediately, set to -1 to wait indefinitely. - /// Returns the number of file descriptors ready for the requested I/O. Returns -1 on failure. - [DllImport("libc", EntryPoint="epoll_wait", SetLastError=true)] - public static extern int EpollWait(SafeFileDescriptorHandle epfd, [Out] EpollEvent[] events, int maxEvents, int timeout); - - /// - /// Closes a file descriptor. - /// - /// File descriptor to close. - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="close", SetLastError=true)] - public static extern int Close(IntPtr fd); - - /// - /// Returns the current address to which the socket is bound to. - /// - /// Socket handle - /// Address structure - /// The size of the the socket address structure in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockname", SetLastError=true)] - public static extern int GetSockName(SafeFileDescriptorHandle socketHandle, SockAddrCan sockAddr, ref int sockAddrLen); - - /// - /// Returns the current address to which the socket is bound to. - /// - /// Socket handle - /// Address structure - /// The size of the the socket address structure in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockname", SetLastError=true)] - public static extern int GetSockName(SafeFileDescriptorHandle socketHandle, SockAddrCanIsoTp sockAddr, ref int sockAddrLen); - - /// - /// Returns the current address to which the socket is bound to. - /// - /// Socket handle - /// Address structure - /// The size of the the socket address structure in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getsockname", SetLastError=true)] - public static extern int GetSockName(SafeFileDescriptorHandle socketHandle, SockAddrCanJ1939 sockAddr, ref int sockAddrLen); - - /// - /// Returns the address of the peer to which the socket is connected to. - /// - /// Socket handle - /// Address structure - /// The size of the the socket address structure in bytes - /// 0 on success, -1 on error - [DllImport("libc", EntryPoint="getpeername", SetLastError=true)] - public static extern int GetPeerName(SafeFileDescriptorHandle socketHandle, SockAddrCanJ1939 sockAddr, ref int sockAddrLen); - - /// - /// Receive a MessageHeader from a socket. - /// - /// Socket Handle Wrapper Instance - /// Message Header - /// Message Flags - /// The number of bytes received on success, -1 on error - [DllImport("libc", EntryPoint="recvmsg", SetLastError=true)] - public static extern int RecvMsg(SafeFileDescriptorHandle socketHandle, ref MessageHeader canMessage, MessageFlags flags); - - /// - /// Retrieves the index of the network interface corresponding to the specified name. - /// - /// Interface Name - /// Interface Index on success, 0 on failure - [DllImport("libc", EntryPoint="if_nametoindex", SetLastError=true)] - public static extern uint IfNameToIndex(string name); - - /// - /// Retrieves the name of the network interface corresponding to the specified index. - /// - /// Interface Index - /// Pointer to the buffer where the Interface Name is set - /// Valid IntPtr pointing to a buffer containing the Interface Name on success, IntPtr.Zero (null) on failure. - [DllImport("libc", EntryPoint="if_indextoname", SetLastError=true)] - public static extern IntPtr IfIndexToName(uint index, IntPtr namePtr); - } + /// + /// C Standard Library Native Methods + /// + public static class LibcNativeMethods + { + /// + /// Number of the last error which indicates what went wrong. Set by system calls and some library functions when an error occurs. + /// + public static int Errno { get { return Marshal.GetLastWin32Error(); } } + + /// + /// Creates a CAN socket. + /// + /// Address Family + /// Type of socket + /// CAN Protocol Type + /// Socket Handle Wrapper Instance + [DllImport("libc", EntryPoint = "socket", SetLastError = true)] + public static extern SafeFileDescriptorHandle Socket(int addressFamily, SocketType socketType, SocketCanProtocolType protocolType); + + /// + /// Manipulates the underlying device parameters of special files. + /// + /// Socket Handle Wrapper Instance + /// Request Code + /// Integer Argument + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + public static extern int Ioctl(SafeHandle socketHandle, int request, ref int arg); + + /// + /// Manipulates the underlying device parameters of special files. + /// + /// Socket Handle Wrapper Instance + /// Request Code + /// Interface Request structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + public static extern int Ioctl(SafeHandle socketHandle, int request, [In][Out] Ifreq ifreq); + + /// + /// Manipulates the underlying device parameters of special files. + /// + /// Socket Handle Wrapper Instance + /// Request Code + /// Interface Request structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + public static extern int Ioctl(SafeHandle socketHandle, int request, [In][Out] IfreqMtu ifreq); + + /// + /// Used to obtain a timeval struct with the receive timestamp of the last packet passed to the user. + /// + /// Socket Handle Wrapper Instance + /// Request Code + /// Timeval structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + public static extern int Ioctl(SafeHandle socketHandle, int request, [In][Out] Timeval timeval); + + /// + /// Assigns the specified SocketCAN base address to the socket. + /// + /// Socket Handle Wrapper Instance + /// SocketCAN base address structure + /// Size of address structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "bind", SetLastError = true)] + public static extern int Bind(SafeHandle socketHandle, SockAddrCan addr, int addrSize); + + /// + /// Assigns the specified SocketCAN ISO-TP address to the socket. + /// + /// Socket Handle Wrapper Instance + /// SocketCAN ISO-TP address structure + /// Size of address structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "bind", SetLastError = true)] + public static extern int Bind(SafeHandle socketHandle, SockAddrCanIsoTp addr, int addrSize); + + /// + /// Assigns the specified SocketCAN J1939 address to the socket. + /// + /// Socket Handle Wrapper Instance + /// SocketCAN J1939 address structure + /// Size of address structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "bind", SetLastError = true)] + public static extern int Bind(SafeHandle socketHandle, SockAddrCanJ1939 addr, int addrSize); + + /// + /// Establishes a connection on the socket to the specified SocketCAN base address. + /// + /// Socket Handle Wrapper Instance + /// SocketCAN base address structure containing the peer address + /// Size of address structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "connect", SetLastError = true)] + public static extern int Connect(SafeHandle socketHandle, SockAddrCan addr, int addrSize); + + /// + /// Establishes a connection on the socket to the specified SocketCAN base address. + /// + /// Socket Handle Wrapper Instance + /// SocketCAN J1939 address structure containing the peer address + /// Size of address structure + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "connect", SetLastError = true)] + public static extern int Connect(SafeHandle socketHandle, SockAddrCanJ1939 addr, int addrSize); + + /// + /// Write the CanFrame to the socket. + /// + /// Socket Handle Wrappper Instance + /// CAN Frame to write + /// Size of CAN Frame in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, ref CanFrame frame, int frameSize); + + /// + /// Write the CanFdFrame to the socket. + /// + /// Socket Handle Wrapper Instance + /// CAN FD Frame to write + /// Size of CAN FD Frame in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, ref CanFdFrame frame, int frameSize); + + /// + /// Write the CanXlFrame to the socket. + /// + /// Socket Handle Wrapper Instance + /// CAN XL Frame to write + /// Size of CAN XL Frame in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, ref CanXlFrame frame, int frameSize); + + /// + /// Write the BcmCanMessage to the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanMessage message, int msgSize); + + /// + /// Write the BcmCanFdMessage to the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanFdMessage message, int msgSize); + + /// + /// Write the BcmCanSingleMessage to the socket. + /// + /// Socket Handle Wrapper Instance + /// Special Single Frame BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanSingleMessage message, int msgSize); + + /// + /// Write the BcmCanFdSingleMessage to the socket. + /// + /// Socket Handle Wrapper Instance + /// Special Single Frame BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanFdSingleMessage message, int msgSize); + + /// + /// Write the BcmMessageHeader to the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM Message Header to write + /// Size of BCM Message Header in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmMessageHeader header, int headerSize); + + /// + /// Write the BcmCanMessage to the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanMessage32 message, int msgSize); + + /// + /// Write the BcmCanFdMessage to the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanFdMessage32 message, int msgSize); + + /// + /// Write the BcmCanSingleMessage to the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// Special Single Frame BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanSingleMessage32 message, int msgSize); + + /// + /// Write the BcmCanFdSingleMessage to the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// Special Single Frame BCM Message to write + /// Size of BCM Message in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmCanFdSingleMessage32 message, int msgSize); + + /// + /// Write the BcmMessageHeader to the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM Message Header to write + /// Size of BCM Message Header in bytes + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, BcmMessageHeader32 header, int headerSize); + + /// + /// Write the byte array to the socket. + /// + /// Socket Handle Wrapper Instance + /// Byte Array to write + /// Size of Byte Array + /// The number of bytes written on success, -1 on error + [DllImport("libc", EntryPoint = "write", SetLastError = true)] + public static extern int Write(SafeHandle socketHandle, byte[] data, int dataSize); + + /// + /// Read a CanFrame from the socket. + /// + /// Socket Handle Wrapper Instance + /// CAN Frame structure to populate + /// Size of CAN Frame structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, ref CanFrame frame, int frameSize); + + /// + /// Read a CanFdFrame from the socket. + /// + /// Socket Handle Wrapper Instance + /// CAN FD Frame structure to populate + /// Size of CAN FD Frame structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, ref CanFdFrame frame, int frameSize); + + /// + /// Read a CanXlFrame from the socket. + /// + /// Socket Handle Wrapper Instance + /// CAN XL Frame structure to populate + /// Size of CAN XL Frame structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, ref CanXlFrame frame, int frameSize); + + /// + /// Read a BcmCanMessage from the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM CAN Message structure to populate + /// Size of BCM CAN Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmCanMessage message, int msgSize); + + /// + /// Read a BcmGenericMessage from the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM Generic Message structure to populate + /// Size of BCM Generic Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmGenericMessage message, int msgSize); + + /// + /// Read a BcmGenericMessage from the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM Generic Message structure to populate + /// Size of BCM Generic Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmGenericMessage32 message, int msgSize); + + /// + /// Read a BcmCanFdMessage from the socket. + /// + /// Socket Handle Wrapper Instance + /// BCM CAN Message structure to populate + /// Size of BCM CAN FD Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmCanFdMessage message, int msgSize); + + /// + /// Read a BcmCanMessage from the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM CAN Message structure to populate + /// Size of BCM CAN Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmCanMessage32 message, int msgSize); + + /// + /// Read a BcmCanFdMessage from the socket. Variant for 32-bit. + /// + /// Socket Handle Wrapper Instance + /// BCM CAN Message structure to populate + /// Size of BCM CAN FD Message structure + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] BcmCanFdMessage32 message, int msgSize); + + /// + /// Read a byte array from the socket. + /// + /// Socket Handle Wrapper Instance + /// Byte array to populate + /// Size of byte array + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "read", SetLastError = true)] + public static extern int Read(SafeHandle socketHandle, [Out] byte[] data, int dataSize); + + /// + /// Receive a CanFrame from a socket. + /// + /// Socket Handle Wrapper Instance + /// CAN Frame structure + /// Size of CAN Frame structure + /// Message Flags + /// The SocketCAN base source address + /// The size of the SocketCAN base address + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "recvfrom", SetLastError = true)] + public static extern int RecvFrom(SafeHandle socketHandle, ref CanFrame frame, int frameSize, MessageFlags flags, [Out] SockAddrCan addr, ref int addrSize); + + /// + /// Receive a CanFdFrame from a socket. + /// + /// Socket Handle Wrapper Instance + /// CAN FD Frame structure + /// Size of CAN FD Frame structure + /// Message Flags + /// The SocketCAN base source address + /// The size of the SocketCAN base address + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "recvfrom", SetLastError = true)] + public static extern int RecvFrom(SafeHandle socketHandle, ref CanFdFrame frame, int frameSize, MessageFlags flags, [Out] SockAddrCan addr, ref int addrSize); + + /// + /// Receive a byte array from a socket. + /// + /// Socket Handle Wrapper Instance + /// Byte array + /// Size of byte array + /// Message Flags + /// The SocketCAN J1939 source address + /// The size of the SocketCAN J1939 address + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "recvfrom", SetLastError = true)] + public static extern int RecvFrom(SafeHandle socketHandle, [Out] byte[] data, int dataSize, MessageFlags flags, [Out] SockAddrCanJ1939 addr, ref int addrSize); + + /// + /// Receive a byte array on a connected socket. + /// + /// Socket Handle Wrapper Instance + /// Byte array + /// Size of byte array + /// Message Flags + /// The number of bytes read on success, -1 on error + [DllImport("libc", EntryPoint = "recv", SetLastError = true)] + public static extern int Recv(SafeHandle socketHandle, [Out] byte[] data, int dataSize, MessageFlags flags); + + /// + /// Transmit a CanFrame to another socket. + /// + /// Socket Handle Wrapper Instance + /// CAN Frame structure + /// Size of CAN Frame structure + /// Message Flags + /// The SocketCAN base destination address + /// The size of the SocketCAN base address + /// The number of bytes sent on success, -1 on error + [DllImport("libc", EntryPoint = "sendto", SetLastError = true)] + public static extern int SendTo(SafeHandle socketHandle, ref CanFrame frame, int frameSize, MessageFlags flags, SockAddrCan addr, int addrSize); + + /// + /// Transmit a CanFdFrame to another socket. + /// + /// Socket Handle Wrapper Instance + /// CAN FD Frame structure + /// Size of CAN FD Frame structure + /// Message Flags + /// The SocketCAN base destination address + /// The size of the SocketCAN base address + /// The number of bytes sent on success, -1 on error + [DllImport("libc", EntryPoint = "sendto", SetLastError = true)] + public static extern int SendTo(SafeHandle socketHandle, ref CanFdFrame frame, int frameSize, MessageFlags flags, SockAddrCan addr, int addrSize); + + /// + /// Transmit a byte array to another socket. + /// + /// Socket Handle Wrapper Instance + /// Byte array + /// Size of byte array + /// Message Flags + /// The SocketCAN J1939 destination address + /// The size of the SocketCAN J1939 address + /// The number of bytes sent on success, -1 on error + [DllImport("libc", EntryPoint = "sendto", SetLastError = true)] + public static extern int SendTo(SafeHandle socketHandle, byte[] data, int dataSize, MessageFlags flags, SockAddrCanJ1939 addr, int addrSize); + + /// + /// Transmit a byte array to another socket. + /// + /// Socket Handle Wrapper Instance + /// Byte array + /// Size of byte array + /// Message Flags + /// The number of bytes sent on success, -1 on error + [DllImport("libc", EntryPoint = "send", SetLastError = true)] + public static extern int Send(SafeHandle socketHandle, byte[] data, int dataSize, MessageFlags flags); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Unsigned integer value + /// Size of unsigned interger + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref uint optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Unsigned integer value + /// Size of an unsigned integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref uint optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Array of CAN Filters + /// Size of CAN Filter Array in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanFilter[] filters, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Array of CAN Filters + /// Size of CAN Filter Array in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, [In, Out] CanFilter[] filters, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref int optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, ref int optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// SOL_SOCKET + /// SOL_SOCKET socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, ref int optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// SOL_SOCKET + /// SOL_SOCKET socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, ref int optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// SOL_SOCKET + /// SOL_SOCKET socket option + /// Time interval object + /// Size of Time interval object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, Timeval timeval, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// SOL_SOCKET + /// SOL_SOCKET socket option + /// Time interval object + /// Size of Time interval object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, SocketLevelOptions optionName, [In, Out] Timeval timeval, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// Unsigned Integer Value + /// Size of unsigned integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, ref uint optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// Unsigned Integer Value + /// Size of unsigned integer + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, ref uint optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Flow Control Options object + /// Size of ISO-TP Flow Control Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpFlowControlOptions fcOptions, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Flow Control Options object + /// Size of ISO-TP Flow Control Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpFlowControlOptions fcOptions, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Link Layer Options object + /// Size of ISO-TP Link Layer Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpLinkLayerOptions llOptions, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Link Layer Options object + /// Size of ISO-TP Link Layer Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpLinkLayerOptions llOptions, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Options object + /// Size of ISO-TP Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpOptions tpOptions, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_ISOTP Socket handle + /// SOL_CAN_ISOTP + /// SOL_CAN_ISOTP socket option + /// ISO-TP Options object + /// Size of ISO-TP Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanIsoTpSocketOptions optionName, CanIsoTpOptions tpOptions, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_J1939 Socket handle + /// SOL_CAN_J1939 + /// SOL_CAN_J1939 socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 or 1 on success depending on option name and value, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, ref int optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_J1939 Socket handle + /// SOL_CAN_J1939 + /// SOL_CAN_J1939 socket option + /// Signed Integer Value + /// Size of signed integer + /// 0 or 1 on success depending on option name and value, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, ref int optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_J1939 Socket handle + /// SOL_CAN_J1939 + /// SOL_CAN_J1939 socket option + /// Array of J1939 Filters + /// Size of array of J1939 Filters in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, J1939Filter[] filters, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_J1939 Socket handle + /// SOL_CAN_J1939 + /// SOL_CAN_J1939 socket option + /// Array of J1939 Filters + /// Size of array of J1939 Filters in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, J1939SocketOptions optionName, [In, Out] J1939Filter[] filters, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// Socket level + /// Socket option + /// Pointer to option value + /// Size of the buffer pointed to by optionValue + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, int optionName, IntPtr optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// Socket handle + /// Socket level + /// Socket option + /// Pointer to option value + /// Size of the buffer pointed to by optionValue + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, int optionName, IntPtr optionValue, ref int optionValueSize); + + /// + /// Set the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// VCID Options object + /// Size of VCID Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "setsockopt", SetLastError = true)] + public static extern int SetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanRawVcidOptions optionValue, int optionValueSize); + + /// + /// Get the socket option specified by the option name and socket level to the provided option value for the supplied socket. + /// + /// CAN_RAW Socket handle + /// SOL_CAN_RAW + /// SOL_CAN_RAW socket option + /// VCID Options object + /// Size of VCID Options object in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockopt", SetLastError = true)] + public static extern int GetSockOpt(SafeHandle socketHandle, SocketLevel socketLevel, CanSocketOptions optionName, CanRawVcidOptions optionValue, ref int optionValueSize); + + /// + /// Returns a pointer to an array of IfNameIndex objects. Each IfNameIndex object includes information about one of the network interfaces on the local system. + /// + /// Pointer to an array of IfNameIndex objects + [DllImport("libc", EntryPoint = "if_nameindex", SetLastError = true)] + public static extern IntPtr IfNameIndex(); + + /// + /// Frees the dynamically allocated data structure returned by IfNameIndex(). + /// + /// Pointer to an array of IfNameIndex objects + [DllImport("libc", EntryPoint = "if_freenameindex", SetLastError = true)] + public static extern void IfFreeNameIndex(IntPtr ptr); + + /// + /// Opens an epoll file descriptor. + /// + /// The size argument is ignored since Linux 2.6.8, but must be greater than zero for backwards compatibility. + /// Originally, this argument was intended as a hint to the kernel as to the number of file descriptors that the caller expected to add to the epoll instance. + /// On success, returns a valid file descriptor handle. On failure, returns an invalid file descriptor handle. + [DllImport("libc", EntryPoint = "epoll_create", SetLastError = true)] + public static extern SafeFileDescriptorHandle EpollCreate(int size); + + /// + /// Control interface used to add, modify, and delete entries from the interest list of an epoll file descriptor. + /// + /// Epoll File Descriptor. + /// Operation to be performed. + /// Target file descriptor. + /// Event object linked to the targeted file descriptor. + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "epoll_ctl", SetLastError = true)] + public static extern int EpollControl(SafeHandle epfd, EpollOperation op, SafeHandle fd, ref EpollEvent evnt); + + /// + /// Waits for an I/O event on an epoll file descriptor. + /// + /// Epoll File Descriptor. + /// A buffer that contains information from the ready lists about file descriptors in the interest list that have some event(s) available. + /// Maximum number of events to wait for. + /// The maximum number of milliseconds that the function call will block for. Set to 0 to return immediately, set to -1 to wait indefinitely. + /// Returns the number of file descriptors ready for the requested I/O. Returns -1 on failure. + [DllImport("libc", EntryPoint = "epoll_wait", SetLastError = true)] + public static extern int EpollWait(SafeHandle epfd, [Out] EpollEvent[] events, int maxEvents, int timeout); + + /// + /// Closes a file descriptor. + /// + /// File descriptor to close. + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "close", SetLastError = true)] + public static extern int Close(IntPtr fd); + + /// + /// Returns the current address to which the socket is bound to. + /// + /// Socket handle + /// Address structure + /// The size of the the socket address structure in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockname", SetLastError = true)] + public static extern int GetSockName(SafeHandle socketHandle, SockAddrCan sockAddr, ref int sockAddrLen); + + /// + /// Returns the current address to which the socket is bound to. + /// + /// Socket handle + /// Address structure + /// The size of the the socket address structure in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockname", SetLastError = true)] + public static extern int GetSockName(SafeHandle socketHandle, SockAddrCanIsoTp sockAddr, ref int sockAddrLen); + + /// + /// Returns the current address to which the socket is bound to. + /// + /// Socket handle + /// Address structure + /// The size of the the socket address structure in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getsockname", SetLastError = true)] + public static extern int GetSockName(SafeHandle socketHandle, SockAddrCanJ1939 sockAddr, ref int sockAddrLen); + + /// + /// Returns the address of the peer to which the socket is connected to. + /// + /// Socket handle + /// Address structure + /// The size of the the socket address structure in bytes + /// 0 on success, -1 on error + [DllImport("libc", EntryPoint = "getpeername", SetLastError = true)] + public static extern int GetPeerName(SafeHandle socketHandle, SockAddrCanJ1939 sockAddr, ref int sockAddrLen); + + /// + /// Receive a MessageHeader from a socket. + /// + /// Socket Handle Wrapper Instance + /// Message Header + /// Message Flags + /// The number of bytes received on success, -1 on error + [DllImport("libc", EntryPoint = "recvmsg", SetLastError = true)] + public static extern int RecvMsg(SafeHandle socketHandle, ref MessageHeader canMessage, MessageFlags flags); + + /// + /// Retrieves the index of the network interface corresponding to the specified name. + /// + /// Interface Name + /// Interface Index on success, 0 on failure + [DllImport("libc", EntryPoint = "if_nametoindex", SetLastError = true)] + public static extern uint IfNameToIndex(string name); + + /// + /// Retrieves the name of the network interface corresponding to the specified index. + /// + /// Interface Index + /// Pointer to the buffer where the Interface Name is set + /// Valid IntPtr pointing to a buffer containing the Interface Name on success, IntPtr.Zero (null) on failure. + [DllImport("libc", EntryPoint = "if_indextoname", SetLastError = true)] + public static extern IntPtr IfIndexToName(uint index, IntPtr namePtr); + } } \ No newline at end of file diff --git a/src/SocketCANSharp/Network/AbstractSocket.cs b/src/SocketCANSharp/Network/AbstractSocket.cs index dbc75bd..1df22e5 100644 --- a/src/SocketCANSharp/Network/AbstractSocket.cs +++ b/src/SocketCANSharp/Network/AbstractSocket.cs @@ -53,7 +53,7 @@ public abstract class AbstractSocket : IDisposable /// /// Represents the operating system handle exposed in a safe manner for the socket that the current AbstractSocket object encapsulates. /// - public SafeFileDescriptorHandle SafeHandle { get; protected set; } + public SafeHandle SafeHandle { get; protected set; } /// /// Represents the operating system handle for the socket that is encapsulated by this object. diff --git a/src/SocketCANSharp/Network/CanNetworkInterface.cs b/src/SocketCANSharp/Network/CanNetworkInterface.cs index 62ae534..41a0d55 100644 --- a/src/SocketCANSharp/Network/CanNetworkInterface.cs +++ b/src/SocketCANSharp/Network/CanNetworkInterface.cs @@ -463,7 +463,7 @@ public static IEnumerable GetAllInterfaces(bool includeVirt /// Socket Handle is closed or invalid. /// Interface Name is null, empty, or only whitespace characters. /// Failed to look up interface by name. - public static CanNetworkInterface GetInterfaceByName(SafeFileDescriptorHandle socketHandle, string interfaceName) + public static CanNetworkInterface GetInterfaceByName(SafeHandle socketHandle, string interfaceName) { if (socketHandle == null) throw new ArgumentNullException(nameof(socketHandle)); @@ -491,7 +491,7 @@ public static CanNetworkInterface GetInterfaceByName(SafeFileDescriptorHandle so /// Socket Handle is null. /// Socket Handle is closed or invalid. /// Failed to look up interface by index. - public static CanNetworkInterface GetInterfaceByIndex(SafeFileDescriptorHandle socketHandle, int interfaceIndex) + public static CanNetworkInterface GetInterfaceByIndex(SafeHandle socketHandle, int interfaceIndex) { if (socketHandle == null) throw new ArgumentNullException(nameof(socketHandle)); @@ -514,7 +514,7 @@ public static CanNetworkInterface GetInterfaceByIndex(SafeFileDescriptorHandle s /// Maximum Transmission Unit of the interface. /// Unable to retreive MTU size information for the interface. [Obsolete("ReadSupportedMtu method is deprecated, please use MaximumTransmissionUnit property instead.")] - public int ReadSupportedMtu(SafeFileDescriptorHandle socketHandle) + public int ReadSupportedMtu(SafeHandle socketHandle) { if (socketHandle == null) throw new ArgumentNullException(nameof(socketHandle)); diff --git a/src/SocketCANSharp/Network/Epoll.cs b/src/SocketCANSharp/Network/Epoll.cs index bb97656..d4986fb 100644 --- a/src/SocketCANSharp/Network/Epoll.cs +++ b/src/SocketCANSharp/Network/Epoll.cs @@ -35,6 +35,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE using System; using System.Net.Sockets; using System.Linq; +using System.Runtime.InteropServices; namespace SocketCANSharp.Network { @@ -51,7 +52,7 @@ public class Epoll : IDisposable /// /// Represents the operating system handle exposed in a safe manner for the epoll file descriptor that the current Epoll object encapsulates. /// - public SafeFileDescriptorHandle SafeHandle { get; } + public SafeHandle SafeHandle { get; } /// /// Represents the operating system handle for the epoll file descriptor that is encapsulated by this object. @@ -78,7 +79,7 @@ public Epoll() /// Epoll file descriptor is closed. /// Socket handle is null. /// The epoll_ctl call failed. - public void Add(SafeFileDescriptorHandle socketHandle, EpollEvent eventSettings) + public void Add(SafeHandle socketHandle, EpollEvent eventSettings) { if (_disposed) throw new ObjectDisposedException(GetType().FullName); @@ -100,7 +101,7 @@ public void Add(SafeFileDescriptorHandle socketHandle, EpollEvent eventSettings) /// Epoll file descriptor is closed. /// Socket handle is null. /// The epoll_ctl call failed. - public void Modify(SafeFileDescriptorHandle socketHandle, EpollEvent eventSettings) + public void Modify(SafeHandle socketHandle, EpollEvent eventSettings) { if (_disposed) throw new ObjectDisposedException(GetType().FullName); @@ -121,7 +122,7 @@ public void Modify(SafeFileDescriptorHandle socketHandle, EpollEvent eventSettin /// Epoll file descriptor is closed. /// Socket handle is null. /// The epoll_ctl call failed. - public void Remove(SafeFileDescriptorHandle socketHandle) + public void Remove(SafeHandle socketHandle) { if (_disposed) throw new ObjectDisposedException(GetType().FullName); diff --git a/src/SocketCANSharp/Network/Netlink/Gateway/CanGatewayNativeMethods.cs b/src/SocketCANSharp/Network/Netlink/Gateway/CanGatewayNativeMethods.cs index 9e3471e..edf6614 100644 --- a/src/SocketCANSharp/Network/Netlink/Gateway/CanGatewayNativeMethods.cs +++ b/src/SocketCANSharp/Network/Netlink/Gateway/CanGatewayNativeMethods.cs @@ -49,6 +49,6 @@ public static class CanGatewayNativeMethods /// Size of CAN Gateway Request in bytes /// The number of bytes written on success, -1 on error [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, CanGatewayRequest req, int reqSize); + public static extern int Write(SafeHandle socketHandle, CanGatewayRequest req, int reqSize); } } \ No newline at end of file diff --git a/src/SocketCANSharp/Network/Netlink/NetlinkNativeMethods.cs b/src/SocketCANSharp/Network/Netlink/NetlinkNativeMethods.cs index f662ae7..a2beec1 100644 --- a/src/SocketCANSharp/Network/Netlink/NetlinkNativeMethods.cs +++ b/src/SocketCANSharp/Network/Netlink/NetlinkNativeMethods.cs @@ -60,7 +60,7 @@ public static class NetlinkNativeMethods /// Size of address structure /// 0 on success, -1 on error [DllImport("libc", EntryPoint="bind", SetLastError=true)] - public static extern int Bind(SafeFileDescriptorHandle socketHandle, SockAddrNetlink addr, int addrSize); + public static extern int Bind(SafeHandle socketHandle, SockAddrNetlink addr, int addrSize); /// /// Returns the current address to which the socket is bound to. @@ -70,7 +70,7 @@ public static class NetlinkNativeMethods /// The size of the the socket address structure in bytes /// 0 on success, -1 on error [DllImport("libc", EntryPoint="getsockname", SetLastError=true)] - public static extern int GetSockName(SafeFileDescriptorHandle socketHandle, SockAddrNetlink sockAddr, ref int sockAddrLen); + public static extern int GetSockName(SafeHandle socketHandle, SockAddrNetlink sockAddr, ref int sockAddrLen); /// /// Write the Network Interface Information Request to the socket. @@ -80,7 +80,7 @@ public static class NetlinkNativeMethods /// Size of Network Interface Information Request in bytes /// The number of bytes written on success, -1 on error [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, NetworkInterfaceInfoRequest req, int reqSize); + public static extern int Write(SafeHandle socketHandle, NetworkInterfaceInfoRequest req, int reqSize); /// /// Write the Network Interface Modifier Request to the socket. @@ -90,6 +90,6 @@ public static class NetlinkNativeMethods /// Size of Network Interface Modifier Request in bytes /// The number of bytes written on success, -1 on error [DllImport("libc", EntryPoint="write", SetLastError=true)] - public static extern int Write(SafeFileDescriptorHandle socketHandle, NetworkInterfaceModifierRequest req, int reqSize); + public static extern int Write(SafeHandle socketHandle, NetworkInterfaceModifierRequest req, int reqSize); } } \ No newline at end of file