10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos #include <string.h>
280e552da7Schristos
290e552da7Schristos #define CHECK_HANDLE(handle) \
300e552da7Schristos ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
310e552da7Schristos
320e552da7Schristos static uv_udp_t server;
330e552da7Schristos static uv_udp_t client;
340e552da7Schristos
350e552da7Schristos static int cl_send_cb_called;
360e552da7Schristos static int cl_recv_cb_called;
370e552da7Schristos
380e552da7Schristos static int sv_send_cb_called;
390e552da7Schristos static int sv_recv_cb_called;
400e552da7Schristos
410e552da7Schristos static int close_cb_called;
420e552da7Schristos
430e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)440e552da7Schristos static void alloc_cb(uv_handle_t* handle,
450e552da7Schristos size_t suggested_size,
460e552da7Schristos uv_buf_t* buf) {
470e552da7Schristos static char slab[65536];
480e552da7Schristos CHECK_HANDLE(handle);
490e552da7Schristos ASSERT(suggested_size <= sizeof(slab));
500e552da7Schristos buf->base = slab;
510e552da7Schristos buf->len = sizeof(slab);
520e552da7Schristos }
530e552da7Schristos
540e552da7Schristos
close_cb(uv_handle_t * handle)550e552da7Schristos static void close_cb(uv_handle_t* handle) {
560e552da7Schristos CHECK_HANDLE(handle);
570e552da7Schristos ASSERT(1 == uv_is_closing(handle));
580e552da7Schristos close_cb_called++;
590e552da7Schristos }
600e552da7Schristos
610e552da7Schristos
cl_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)620e552da7Schristos static void cl_recv_cb(uv_udp_t* handle,
630e552da7Schristos ssize_t nread,
640e552da7Schristos const uv_buf_t* buf,
650e552da7Schristos const struct sockaddr* addr,
660e552da7Schristos unsigned flags) {
670e552da7Schristos CHECK_HANDLE(handle);
680e552da7Schristos ASSERT(flags == 0);
690e552da7Schristos
700e552da7Schristos if (nread < 0) {
710e552da7Schristos ASSERT(0 && "unexpected error");
720e552da7Schristos }
730e552da7Schristos
740e552da7Schristos if (nread == 0) {
750e552da7Schristos /* Returning unused buffer. Don't count towards cl_recv_cb_called */
76*5f2f4271Schristos ASSERT_NULL(addr);
770e552da7Schristos return;
780e552da7Schristos }
790e552da7Schristos
80*5f2f4271Schristos ASSERT_NOT_NULL(addr);
810e552da7Schristos ASSERT(nread == 4);
820e552da7Schristos ASSERT(!memcmp("PONG", buf->base, nread));
830e552da7Schristos
840e552da7Schristos cl_recv_cb_called++;
850e552da7Schristos
860e552da7Schristos uv_close((uv_handle_t*) handle, close_cb);
870e552da7Schristos }
880e552da7Schristos
890e552da7Schristos
cl_send_cb(uv_udp_send_t * req,int status)900e552da7Schristos static void cl_send_cb(uv_udp_send_t* req, int status) {
910e552da7Schristos int r;
920e552da7Schristos
93*5f2f4271Schristos ASSERT_NOT_NULL(req);
940e552da7Schristos ASSERT(status == 0);
950e552da7Schristos CHECK_HANDLE(req->handle);
960e552da7Schristos
970e552da7Schristos r = uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb);
980e552da7Schristos ASSERT(r == 0);
990e552da7Schristos
1000e552da7Schristos cl_send_cb_called++;
1010e552da7Schristos }
1020e552da7Schristos
1030e552da7Schristos
sv_send_cb(uv_udp_send_t * req,int status)1040e552da7Schristos static void sv_send_cb(uv_udp_send_t* req, int status) {
105*5f2f4271Schristos ASSERT_NOT_NULL(req);
1060e552da7Schristos ASSERT(status == 0);
1070e552da7Schristos CHECK_HANDLE(req->handle);
1080e552da7Schristos
1090e552da7Schristos uv_close((uv_handle_t*) req->handle, close_cb);
1100e552da7Schristos free(req);
1110e552da7Schristos
1120e552da7Schristos sv_send_cb_called++;
1130e552da7Schristos }
1140e552da7Schristos
1150e552da7Schristos
sv_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)1160e552da7Schristos static void sv_recv_cb(uv_udp_t* handle,
1170e552da7Schristos ssize_t nread,
1180e552da7Schristos const uv_buf_t* rcvbuf,
1190e552da7Schristos const struct sockaddr* addr,
1200e552da7Schristos unsigned flags) {
1210e552da7Schristos uv_udp_send_t* req;
1220e552da7Schristos uv_buf_t sndbuf;
1230e552da7Schristos int r;
1240e552da7Schristos
1250e552da7Schristos if (nread < 0) {
1260e552da7Schristos ASSERT(0 && "unexpected error");
1270e552da7Schristos }
1280e552da7Schristos
1290e552da7Schristos if (nread == 0) {
1300e552da7Schristos /* Returning unused buffer. Don't count towards sv_recv_cb_called */
131*5f2f4271Schristos ASSERT_NULL(addr);
1320e552da7Schristos return;
1330e552da7Schristos }
1340e552da7Schristos
1350e552da7Schristos CHECK_HANDLE(handle);
1360e552da7Schristos ASSERT(flags == 0);
1370e552da7Schristos
138*5f2f4271Schristos ASSERT_NOT_NULL(addr);
1390e552da7Schristos ASSERT(nread == 4);
1400e552da7Schristos ASSERT(!memcmp("PING", rcvbuf->base, nread));
1410e552da7Schristos
1420e552da7Schristos /* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
1430e552da7Schristos * anymore. That's problematic because the read buffer won't be returned
1440e552da7Schristos * either... Not sure I like that but it's consistent with `uv_read_stop`.
1450e552da7Schristos */
1460e552da7Schristos r = uv_udp_recv_stop(handle);
1470e552da7Schristos ASSERT(r == 0);
1480e552da7Schristos
1490e552da7Schristos req = malloc(sizeof *req);
150*5f2f4271Schristos ASSERT_NOT_NULL(req);
1510e552da7Schristos
1520e552da7Schristos sndbuf = uv_buf_init("PONG", 4);
1530e552da7Schristos r = uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb);
1540e552da7Schristos ASSERT(r == 0);
1550e552da7Schristos
1560e552da7Schristos sv_recv_cb_called++;
1570e552da7Schristos }
1580e552da7Schristos
1590e552da7Schristos
TEST_IMPL(udp_send_and_recv)1600e552da7Schristos TEST_IMPL(udp_send_and_recv) {
1610e552da7Schristos struct sockaddr_in addr;
1620e552da7Schristos uv_udp_send_t req;
1630e552da7Schristos uv_buf_t buf;
1640e552da7Schristos int r;
1650e552da7Schristos
1660e552da7Schristos ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
1670e552da7Schristos
1680e552da7Schristos r = uv_udp_init(uv_default_loop(), &server);
1690e552da7Schristos ASSERT(r == 0);
1700e552da7Schristos
1710e552da7Schristos r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
1720e552da7Schristos ASSERT(r == 0);
1730e552da7Schristos
1740e552da7Schristos r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
1750e552da7Schristos ASSERT(r == 0);
1760e552da7Schristos
1770e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1780e552da7Schristos
1790e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
1800e552da7Schristos ASSERT(r == 0);
1810e552da7Schristos
1820e552da7Schristos /* client sends "PING", expects "PONG" */
1830e552da7Schristos buf = uv_buf_init("PING", 4);
1840e552da7Schristos
1850e552da7Schristos r = uv_udp_send(&req,
1860e552da7Schristos &client,
1870e552da7Schristos &buf,
1880e552da7Schristos 1,
1890e552da7Schristos (const struct sockaddr*) &addr,
1900e552da7Schristos cl_send_cb);
1910e552da7Schristos ASSERT(r == 0);
1920e552da7Schristos
1930e552da7Schristos ASSERT(close_cb_called == 0);
1940e552da7Schristos ASSERT(cl_send_cb_called == 0);
1950e552da7Schristos ASSERT(cl_recv_cb_called == 0);
1960e552da7Schristos ASSERT(sv_send_cb_called == 0);
1970e552da7Schristos ASSERT(sv_recv_cb_called == 0);
1980e552da7Schristos
1990e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2000e552da7Schristos
2010e552da7Schristos ASSERT(cl_send_cb_called == 1);
2020e552da7Schristos ASSERT(cl_recv_cb_called == 1);
2030e552da7Schristos ASSERT(sv_send_cb_called == 1);
2040e552da7Schristos ASSERT(sv_recv_cb_called == 1);
2050e552da7Schristos ASSERT(close_cb_called == 2);
2060e552da7Schristos
2070e552da7Schristos ASSERT(client.send_queue_size == 0);
2080e552da7Schristos ASSERT(server.send_queue_size == 0);
2090e552da7Schristos
2100e552da7Schristos MAKE_VALGRIND_HAPPY();
2110e552da7Schristos return 0;
2120e552da7Schristos }
213