// C# binding for TCP/IP services in libsimpleio.so
// Copyright (C)2017-2023, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
using System.Runtime.InteropServices;
namespace IO.Bindings
{
public static partial class libsimpleio
{
///
/// IPv4 address for binding to all network interfaces.
///
public const int INADDR_ANY = 0;
///
/// IPv4 address for binding to the loopback interface (aka
/// localhost).
///
public const int INADDR_LOOPBACK = 0x7F000001;
///
/// IPv4 broadcast address.
///
public const int INADDR_BROADCAST = -1;
///
/// Don't use a gateway to send out the packet, send to hosts only on
/// directly connected networks.
///
public const int MSG_DONTROUTE = 0x0004;
///
/// Enables nonblocking operation; if the operation would block,
/// EAGAIN or EWOULDBLOCK is returned.
///
public const int MSG_DONTWAIT = 0x0040;
///
/// The caller has more data to send. This flag informs the kernel
/// to package all of the data sent in calls with this flag set into
/// a single datagram which is transmitted only when a call is
/// performed that does not specify this flag.
///
public const int MSG_MORE = 0x8000;
///
/// Resolve a domain name to an IPv4 host address.
///
/// Host name to resolve.
/// IPv4 host address.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void IPV4_resolve(string hostname, out int host,
out int error);
///
/// Convert an IPv4 address to a dotted notation string (e.g. 1.2.3.4).
///
/// IPv4 host address
/// Destination buffer.
/// Destination buffer size.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void IPV4_ntoa(int host,
System.Text.StringBuilder buf, int bufsize, out int error);
///
/// Connect to a TCP server.
///
/// IPv4 host address.
/// TCP port number.
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_connect(int host, int port,
out int fd, out int error);
///
/// Start TCP server and wait for a single connection.
///
/// IPv4 address, of the interface to listen on. Use
/// 0.0.0.0 to listen on all interfaces.
/// TCP port number.
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_accept(int host, int port,
out int fd, out int error);
///
/// Start a TCP server and fork for each connection.
///
/// IPv4 address, of the interface to listen on. Use
/// 0.0.0.0 to listen on all interfaces.
/// TCP port number.
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_server(int host, int port,
out int fd, out int error);
///
/// Close a TCP connection.
///
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_close(int fd, out int error);
///
/// Send bytes to TCP peer.
///
/// File descriptor.
/// Source buffer.
/// Source buffer size.
/// Number of bytes actually sent.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_send(int fd, byte[] buf, int bufsize,
out int count, out int error);
///
/// Receive bytes from TCP peer.
///
/// File descriptor.
/// Destination buffer.
/// Destination buffer size.
/// Number of bytes actually received.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void TCP4_receive(int fd, byte[] buf, int bufsize,
out int count, out int error);
///
/// Open a UDP socket.
///
/// IPv4 host address.
/// UDP port number.
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void UDP4_open(int host, int port,
out int fd, out int error);
///
/// Close a UDP socket.
///
/// File descriptor.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void UDP4_close(int fd, out int error);
///
/// Send a UDP datagram.
///
/// File descriptor.
/// Destination IPv4 host address.
/// Destination UDP port number.
/// Source buffer.
/// Source buffer size.
/// Flags for the Linux sendto() system call.
/// Number of bytes actually sent.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void UDP4_send(int fd, int host, int port,
byte[] buf, int bufsize, int flags, out int count, out int error);
///
/// Receive a UDP datagram.
///
/// File descriptor.
/// Source IPv4 host address.
/// Source UDP port number.
/// Destination buffer.
/// Destination buffer size.
/// Flags for the Linux recvfrom() system call.
/// Number of bytes actually received.
/// Error code. Zero upon success or an errno
/// value upon failure.
[DllImport("simpleio")]
public static extern void UDP4_receive(int fd, out int host,
out int port, byte[] buf, int bufsize, int flags, out int count,
out int error);
}
}