示例#1
0
    public void C_IPv4_NewFromHost()
    {
        C.IPv4 ipv4 = C.IPv4.New(127, 0, 0, 1);

        /* certainty */
        C.IPv4 ip = default;
        ip.byte_addr[0] = 1;
        ip.byte_addr[1] = 0;
        ip.byte_addr[2] = 0;
        ip.byte_addr[3] = 127;
        Assert.AreEqual(ipv4.int_addr, Util.HtoN(ip.int_addr));

        /* ridiculous certainty */
        Assert.True(IPAddress.TryParse("127.0.0.1", out IPAddress netip));

        fixed(byte *pSrc = netip.GetAddressBytes())
        Assert.AreEqual(ipv4.int_addr, *(uint *)pSrc);

        /* ludicrous certainty */
        uint addr = (127 << 24) | 1;

        Assert.AreEqual(ipv4.int_addr, Util.HtoN(addr));
    }
示例#2
0
    public void UDP_SocketSendRecv()
    {
        Random rand = new Random();

        /* server side endpoints, socket, random payload, send/recv buffers */
        C.IPv4     loopback       = C.IPv4.New(127, 0, 0, 1);
        C.Endpoint ep_server      = C.Endpoint.NewV4(_port, loopback);
        C.Endpoint ep_server_recv = default;
        _sock_server = C.Socket.NewUDP(ep_server);
        C.Buffer buf_server_recv;
        C.Buffer buf_server_send;
        byte[]   pl_server  = new byte[1235];
        byte[]   mem_server = new byte[1408];
        rand.NextBytes(pl_server);

        fixed(byte *bufptr = pl_server) buf_server_send = C.Buffer.New(bufptr, pl_server.Length);

        fixed(byte *bufptr = mem_server) buf_server_recv = C.Buffer.New(bufptr, mem_server.Length);

        /* client side endpoints, socket, random payload, send/recv buffers */
        C.Endpoint ep_client      = C.Endpoint.NewV4(_port + 1, loopback);
        C.Endpoint ep_client_recv = default;
        _sock_client = C.Socket.NewUDP(ep_client);
        C.Buffer buf_client_recv;
        C.Buffer buf_client_send;
        byte[]   pl_client  = new byte[256];
        byte[]   mem_client = new byte[1408];
        rand.NextBytes(pl_client);

        fixed(byte *bufptr = pl_client) buf_client_send = C.Buffer.New(bufptr, pl_client.Length);

        fixed(byte *bufptr = mem_client) buf_client_recv = C.Buffer.New(bufptr, mem_client.Length);

        Assert.True(UDP.Setup());

        fixed(C.Socket *sockptr_server = &_sock_server)
        fixed(C.Socket * sockptr_client = &_sock_client)
        {
            Assert.True(UDP.SocketOpen(sockptr_server));

            Assert.True(UDP.SocketOpen(sockptr_client));
            Assert.True(UDP.SocketNonBlocking(sockptr_client, true));

            Assert.AreEqual(UDP.SocketSend(sockptr_client, &buf_client_send, 1, &ep_server), pl_client.Length);
            Assert.AreEqual(UDP.SocketRecv(sockptr_server, &buf_server_recv, 1, &ep_server_recv), pl_client.Length);

            fixed(byte *bufptr = pl_client) Assert.True(Util.MemCmp(bufptr, 0, buf_server_recv.buf, 0, pl_client.Length));

            Assert.True(Util.MemCmp((byte *)&ep_client, 2, (byte *)&ep_server_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.AreEqual(UDP.SocketSend(sockptr_server, &buf_server_send, 1, &ep_client), pl_server.Length);
            Assert.AreEqual(UDP.SocketRecv(sockptr_client, &buf_client_recv, 1, &ep_client_recv), pl_server.Length);

            fixed(byte *bufptr = pl_server) Assert.True(Util.MemCmp(bufptr, 0, buf_client_recv.buf, 0, pl_server.Length));

            Assert.True(Util.MemCmp((byte *)&ep_server, 2, (byte *)&ep_client_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.True(UDP.SocketClose(sockptr_client));
            Assert.True(UDP.SocketClose(sockptr_server));
        }
    }
示例#3
0
    public void UDP_SocketSendRecv_Blocking()
    {
        Random rand = new Random();

        C.Socket sock_server;
        C.Socket sock_client;

        SL.C.Context ctx = default;
        Assert.True(API.Setup(&ctx));
        try
        {
            /* server side endpoints, socket, random payload, send/recv buffers */
            C.IPv4     loopback       = C.IPv4.New(127, 0, 0, 1);
            C.Endpoint ep_server      = C.Endpoint.NewV4(&ctx, _port, loopback);
            C.Endpoint ep_server_recv = default;
            sock_server = C.Socket.NewUDP(&ctx, ep_server);
            C.Buffer buf_server_recv;
            C.Buffer buf_server_send;
            byte[]   pl_server  = new byte[1235];
            byte[]   mem_server = new byte[1408];
            rand.NextBytes(pl_server);

            fixed(byte *bufptr = pl_server) buf_server_send = C.Buffer.New(bufptr, pl_server.Length);

            fixed(byte *bufptr = mem_server) buf_server_recv = C.Buffer.New(bufptr, mem_server.Length);

            /* client side endpoints, socket, random payload, send/recv buffers */
            C.Endpoint ep_client      = C.Endpoint.NewV4(&ctx, _port + 1, loopback);
            C.Endpoint ep_client_recv = default;
            sock_client = C.Socket.NewUDP(&ctx, ep_client);
            C.Buffer buf_client_recv;
            C.Buffer buf_client_send;
            byte[]   pl_client  = new byte[256];
            byte[]   mem_client = new byte[1408];
            rand.NextBytes(pl_client);

            fixed(byte *bufptr = pl_client) buf_client_send = C.Buffer.New(bufptr, pl_client.Length);

            fixed(byte *bufptr = mem_client) buf_client_recv = C.Buffer.New(bufptr, mem_client.Length);

            Assert.True(API.SocketOpen(&sock_server));
            Assert.True(API.SocketOpen(&sock_client));

            Assert.AreEqual(pl_client.Length, API.SocketSend(&sock_client, &buf_client_send, 1, &ep_server));
            Assert.AreEqual(pl_client.Length, API.SocketRecv(&sock_server, &buf_server_recv, 1, &ep_server_recv));

            fixed(byte *bufptr = pl_client) Assert.True(Util.MemCmp(bufptr, 0, buf_server_recv.buf, 0, pl_client.Length));

            Assert.True(Util.MemCmp((byte *)&ep_client, 2, (byte *)&ep_server_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.AreEqual(pl_server.Length, API.SocketSend(&sock_server, &buf_server_send, 1, &ep_client));
            Assert.AreEqual(pl_server.Length, API.SocketRecv(&sock_client, &buf_client_recv, 1, &ep_client_recv));

            fixed(byte *bufptr = pl_server) Assert.True(Util.MemCmp(bufptr, 0, buf_client_recv.buf, 0, pl_server.Length));

            Assert.True(Util.MemCmp((byte *)&ep_server, 2, (byte *)&ep_client_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.True(API.SocketClose(&sock_server));
            Assert.True(API.SocketClose(&sock_client));
            Assert.True(API.Cleanup(&ctx));
        }
        finally
        {
            API.SocketClose(&sock_server);
            API.SocketClose(&sock_client);
            API.Cleanup(&ctx);
        }
    }