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 #include <stdio.h>
250e552da7Schristos #include <stdlib.h>
260e552da7Schristos #include <string.h>
270e552da7Schristos
280e552da7Schristos #ifndef _WIN32
290e552da7Schristos # include <unistd.h>
300e552da7Schristos # include <sys/socket.h>
310e552da7Schristos # include <sys/un.h>
320e552da7Schristos #endif
330e552da7Schristos
340e552da7Schristos static int send_cb_called = 0;
350e552da7Schristos static int close_cb_called = 0;
360e552da7Schristos
370e552da7Schristos static uv_udp_send_t send_req;
380e552da7Schristos
390e552da7Schristos
startup(void)400e552da7Schristos static void startup(void) {
410e552da7Schristos #ifdef _WIN32
420e552da7Schristos struct WSAData wsa_data;
430e552da7Schristos int r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
440e552da7Schristos ASSERT(r == 0);
450e552da7Schristos #endif
460e552da7Schristos }
470e552da7Schristos
480e552da7Schristos
create_udp_socket(void)490e552da7Schristos static uv_os_sock_t create_udp_socket(void) {
500e552da7Schristos uv_os_sock_t sock;
510e552da7Schristos
520e552da7Schristos sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
530e552da7Schristos #ifdef _WIN32
540e552da7Schristos ASSERT(sock != INVALID_SOCKET);
550e552da7Schristos #else
560e552da7Schristos ASSERT(sock >= 0);
570e552da7Schristos #endif
580e552da7Schristos
590e552da7Schristos #ifndef _WIN32
600e552da7Schristos {
610e552da7Schristos /* Allow reuse of the port. */
620e552da7Schristos int yes = 1;
630e552da7Schristos int r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
640e552da7Schristos ASSERT(r == 0);
650e552da7Schristos }
660e552da7Schristos #endif
670e552da7Schristos
680e552da7Schristos return sock;
690e552da7Schristos }
700e552da7Schristos
710e552da7Schristos
close_socket(uv_os_sock_t sock)720e552da7Schristos static void close_socket(uv_os_sock_t sock) {
730e552da7Schristos int r;
740e552da7Schristos #ifdef _WIN32
750e552da7Schristos r = closesocket(sock);
760e552da7Schristos #else
770e552da7Schristos r = close(sock);
780e552da7Schristos #endif
790e552da7Schristos ASSERT(r == 0);
800e552da7Schristos }
810e552da7Schristos
820e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)830e552da7Schristos static void alloc_cb(uv_handle_t* handle,
840e552da7Schristos size_t suggested_size,
850e552da7Schristos uv_buf_t* buf) {
860e552da7Schristos static char slab[65536];
870e552da7Schristos ASSERT(suggested_size <= sizeof(slab));
880e552da7Schristos buf->base = slab;
890e552da7Schristos buf->len = sizeof(slab);
900e552da7Schristos }
910e552da7Schristos
920e552da7Schristos
close_cb(uv_handle_t * handle)930e552da7Schristos static void close_cb(uv_handle_t* handle) {
94*5f2f4271Schristos ASSERT_NOT_NULL(handle);
950e552da7Schristos close_cb_called++;
960e552da7Schristos }
970e552da7Schristos
980e552da7Schristos
recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)990e552da7Schristos static void recv_cb(uv_udp_t* handle,
1000e552da7Schristos ssize_t nread,
1010e552da7Schristos const uv_buf_t* buf,
1020e552da7Schristos const struct sockaddr* addr,
1030e552da7Schristos unsigned flags) {
1040e552da7Schristos int r;
1050e552da7Schristos
1060e552da7Schristos if (nread < 0) {
1070e552da7Schristos ASSERT(0 && "unexpected error");
1080e552da7Schristos }
1090e552da7Schristos
1100e552da7Schristos if (nread == 0) {
1110e552da7Schristos /* Returning unused buffer. Don't count towards sv_recv_cb_called */
112*5f2f4271Schristos ASSERT_NULL(addr);
1130e552da7Schristos return;
1140e552da7Schristos }
1150e552da7Schristos
1160e552da7Schristos ASSERT(flags == 0);
1170e552da7Schristos
118*5f2f4271Schristos ASSERT_NOT_NULL(addr);
1190e552da7Schristos ASSERT(nread == 4);
1200e552da7Schristos ASSERT(memcmp("PING", buf->base, nread) == 0);
1210e552da7Schristos
1220e552da7Schristos r = uv_udp_recv_stop(handle);
1230e552da7Schristos ASSERT(r == 0);
1240e552da7Schristos
1250e552da7Schristos uv_close((uv_handle_t*) handle, close_cb);
1260e552da7Schristos }
1270e552da7Schristos
1280e552da7Schristos
send_cb(uv_udp_send_t * req,int status)1290e552da7Schristos static void send_cb(uv_udp_send_t* req, int status) {
130*5f2f4271Schristos ASSERT_NOT_NULL(req);
1310e552da7Schristos ASSERT(status == 0);
1320e552da7Schristos
1330e552da7Schristos send_cb_called++;
1340e552da7Schristos uv_close((uv_handle_t*)req->handle, close_cb);
1350e552da7Schristos }
1360e552da7Schristos
1370e552da7Schristos
TEST_IMPL(udp_open)1380e552da7Schristos TEST_IMPL(udp_open) {
1390e552da7Schristos struct sockaddr_in addr;
1400e552da7Schristos uv_buf_t buf = uv_buf_init("PING", 4);
141*5f2f4271Schristos uv_udp_t client, client2;
1420e552da7Schristos uv_os_sock_t sock;
1430e552da7Schristos int r;
1440e552da7Schristos
1450e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1460e552da7Schristos
1470e552da7Schristos startup();
1480e552da7Schristos sock = create_udp_socket();
1490e552da7Schristos
1500e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
1510e552da7Schristos ASSERT(r == 0);
1520e552da7Schristos
1530e552da7Schristos r = uv_udp_open(&client, sock);
1540e552da7Schristos ASSERT(r == 0);
1550e552da7Schristos
1560e552da7Schristos r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
1570e552da7Schristos ASSERT(r == 0);
1580e552da7Schristos
1590e552da7Schristos r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
1600e552da7Schristos ASSERT(r == 0);
1610e552da7Schristos
1620e552da7Schristos r = uv_udp_send(&send_req,
1630e552da7Schristos &client,
1640e552da7Schristos &buf,
1650e552da7Schristos 1,
1660e552da7Schristos (const struct sockaddr*) &addr,
1670e552da7Schristos send_cb);
1680e552da7Schristos ASSERT(r == 0);
1690e552da7Schristos
1700e552da7Schristos #ifndef _WIN32
1710e552da7Schristos {
1720e552da7Schristos r = uv_udp_init(uv_default_loop(), &client2);
1730e552da7Schristos ASSERT(r == 0);
1740e552da7Schristos
1750e552da7Schristos r = uv_udp_open(&client2, sock);
1760e552da7Schristos ASSERT(r == UV_EEXIST);
1770e552da7Schristos
1780e552da7Schristos uv_close((uv_handle_t*) &client2, NULL);
1790e552da7Schristos }
180*5f2f4271Schristos #else /* _WIN32 */
181*5f2f4271Schristos (void)client2;
182*5f2f4271Schristos #endif
1830e552da7Schristos
1840e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1850e552da7Schristos
1860e552da7Schristos ASSERT(send_cb_called == 1);
1870e552da7Schristos ASSERT(close_cb_called == 1);
1880e552da7Schristos
1890e552da7Schristos ASSERT(client.send_queue_size == 0);
1900e552da7Schristos
1910e552da7Schristos MAKE_VALGRIND_HAPPY();
1920e552da7Schristos return 0;
1930e552da7Schristos }
1940e552da7Schristos
1950e552da7Schristos
TEST_IMPL(udp_open_twice)1960e552da7Schristos TEST_IMPL(udp_open_twice) {
1970e552da7Schristos uv_udp_t client;
1980e552da7Schristos uv_os_sock_t sock1, sock2;
1990e552da7Schristos int r;
2000e552da7Schristos
2010e552da7Schristos startup();
2020e552da7Schristos sock1 = create_udp_socket();
2030e552da7Schristos sock2 = create_udp_socket();
2040e552da7Schristos
2050e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
2060e552da7Schristos ASSERT(r == 0);
2070e552da7Schristos
2080e552da7Schristos r = uv_udp_open(&client, sock1);
2090e552da7Schristos ASSERT(r == 0);
2100e552da7Schristos
2110e552da7Schristos r = uv_udp_open(&client, sock2);
2120e552da7Schristos ASSERT(r == UV_EBUSY);
2130e552da7Schristos close_socket(sock2);
2140e552da7Schristos
2150e552da7Schristos uv_close((uv_handle_t*) &client, NULL);
2160e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2170e552da7Schristos
2180e552da7Schristos MAKE_VALGRIND_HAPPY();
2190e552da7Schristos return 0;
2200e552da7Schristos }
2210e552da7Schristos
TEST_IMPL(udp_open_bound)2220e552da7Schristos TEST_IMPL(udp_open_bound) {
2230e552da7Schristos struct sockaddr_in addr;
2240e552da7Schristos uv_udp_t client;
2250e552da7Schristos uv_os_sock_t sock;
2260e552da7Schristos int r;
2270e552da7Schristos
2280e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
2290e552da7Schristos
2300e552da7Schristos startup();
2310e552da7Schristos sock = create_udp_socket();
2320e552da7Schristos
2330e552da7Schristos r = bind(sock, (struct sockaddr*) &addr, sizeof(addr));
2340e552da7Schristos ASSERT(r == 0);
2350e552da7Schristos
2360e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
2370e552da7Schristos ASSERT(r == 0);
2380e552da7Schristos
2390e552da7Schristos r = uv_udp_open(&client, sock);
2400e552da7Schristos ASSERT(r == 0);
2410e552da7Schristos
2420e552da7Schristos r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
2430e552da7Schristos ASSERT(r == 0);
2440e552da7Schristos
2450e552da7Schristos uv_close((uv_handle_t*) &client, NULL);
2460e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2470e552da7Schristos
2480e552da7Schristos MAKE_VALGRIND_HAPPY();
2490e552da7Schristos return 0;
2500e552da7Schristos }
2510e552da7Schristos
TEST_IMPL(udp_open_connect)2520e552da7Schristos TEST_IMPL(udp_open_connect) {
2530e552da7Schristos struct sockaddr_in addr;
2540e552da7Schristos uv_buf_t buf = uv_buf_init("PING", 4);
2550e552da7Schristos uv_udp_t client;
2560e552da7Schristos uv_udp_t server;
2570e552da7Schristos uv_os_sock_t sock;
2580e552da7Schristos int r;
2590e552da7Schristos
2600e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
2610e552da7Schristos
2620e552da7Schristos startup();
2630e552da7Schristos sock = create_udp_socket();
2640e552da7Schristos
2650e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
2660e552da7Schristos ASSERT(r == 0);
2670e552da7Schristos
2680e552da7Schristos r = connect(sock, (const struct sockaddr*) &addr, sizeof(addr));
2690e552da7Schristos ASSERT(r == 0);
2700e552da7Schristos
2710e552da7Schristos r = uv_udp_open(&client, sock);
2720e552da7Schristos ASSERT(r == 0);
2730e552da7Schristos
2740e552da7Schristos r = uv_udp_init(uv_default_loop(), &server);
2750e552da7Schristos ASSERT(r == 0);
2760e552da7Schristos
2770e552da7Schristos r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
2780e552da7Schristos ASSERT(r == 0);
2790e552da7Schristos
2800e552da7Schristos r = uv_udp_recv_start(&server, alloc_cb, recv_cb);
2810e552da7Schristos ASSERT(r == 0);
2820e552da7Schristos
2830e552da7Schristos r = uv_udp_send(&send_req,
2840e552da7Schristos &client,
2850e552da7Schristos &buf,
2860e552da7Schristos 1,
2870e552da7Schristos NULL,
2880e552da7Schristos send_cb);
2890e552da7Schristos ASSERT(r == 0);
2900e552da7Schristos
2910e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2920e552da7Schristos
2930e552da7Schristos ASSERT(send_cb_called == 1);
2940e552da7Schristos ASSERT(close_cb_called == 2);
2950e552da7Schristos
2960e552da7Schristos ASSERT(client.send_queue_size == 0);
2970e552da7Schristos
2980e552da7Schristos MAKE_VALGRIND_HAPPY();
2990e552da7Schristos return 0;
3000e552da7Schristos }
3010e552da7Schristos
3020e552da7Schristos #ifndef _WIN32
TEST_IMPL(udp_send_unix)3030e552da7Schristos TEST_IMPL(udp_send_unix) {
3040e552da7Schristos /* Test that "uv_udp_send()" supports sending over
3050e552da7Schristos a "sockaddr_un" address. */
3060e552da7Schristos struct sockaddr_un addr;
3070e552da7Schristos uv_udp_t handle;
3080e552da7Schristos uv_udp_send_t req;
3090e552da7Schristos uv_loop_t* loop;
3100e552da7Schristos uv_buf_t buf = uv_buf_init("PING", 4);
3110e552da7Schristos int fd;
3120e552da7Schristos int r;
3130e552da7Schristos
3140e552da7Schristos loop = uv_default_loop();
3150e552da7Schristos
3160e552da7Schristos memset(&addr, 0, sizeof addr);
3170e552da7Schristos addr.sun_family = AF_UNIX;
3180e552da7Schristos ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
3190e552da7Schristos memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
3200e552da7Schristos
3210e552da7Schristos fd = socket(AF_UNIX, SOCK_STREAM, 0);
3220e552da7Schristos ASSERT(fd >= 0);
3230e552da7Schristos
3240e552da7Schristos unlink(TEST_PIPENAME);
3250e552da7Schristos ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
3260e552da7Schristos ASSERT(0 == listen(fd, 1));
3270e552da7Schristos
3280e552da7Schristos r = uv_udp_init(loop, &handle);
3290e552da7Schristos ASSERT(r == 0);
3300e552da7Schristos r = uv_udp_open(&handle, fd);
3310e552da7Schristos ASSERT(r == 0);
3320e552da7Schristos uv_run(loop, UV_RUN_DEFAULT);
3330e552da7Schristos
3340e552da7Schristos r = uv_udp_send(&req,
3350e552da7Schristos &handle,
3360e552da7Schristos &buf,
3370e552da7Schristos 1,
3380e552da7Schristos (const struct sockaddr*) &addr,
3390e552da7Schristos NULL);
3400e552da7Schristos ASSERT(r == 0);
3410e552da7Schristos
3420e552da7Schristos uv_close((uv_handle_t*)&handle, NULL);
3430e552da7Schristos uv_run(loop, UV_RUN_DEFAULT);
3440e552da7Schristos close(fd);
3450e552da7Schristos unlink(TEST_PIPENAME);
3460e552da7Schristos
3470e552da7Schristos MAKE_VALGRIND_HAPPY();
3480e552da7Schristos return 0;
3490e552da7Schristos }
3500e552da7Schristos #endif
351