diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2015-01-06 21:27:14 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2015-01-06 21:30:19 +0100 |
commit | fc7bf0059415757ff67f26c16e5a9f9d55f658a0 (patch) | |
tree | b3a7556255d65163beeddd1f564cd0bfb3f32929 /tests/utils.c | |
parent | 066db0b051a048e005f7bb344621b5ea931aeea3 (diff) | |
download | gnutls-fc7bf0059415757ff67f26c16e5a9f9d55f658a0.tar.gz |
tests: moved udp_socketpair to utils
Diffstat (limited to 'tests/utils.c')
-rw-r--r-- | tests/utils.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/utils.c b/tests/utils.c index b61321d39a..516cc9d60a 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -27,6 +27,9 @@ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> +#include <netinet/in.h> +#include <sys/types.h> +#include <sys/socket.h> #include "utils.h" @@ -151,3 +154,54 @@ int main(int argc, char *argv[]) return error_count ? 1 : 0; } + +static int udp_socket(void) +{ + int on = 1; + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_addr = {htonl(INADDR_LOOPBACK)}, + .sin_port = 0 + }; + int fd; + + fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); +#if defined(SO_REUSEPORT) + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char*)&on, sizeof(on)); +#endif + + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + return fd; +} + +static void udp_connect(int fd1, int fd2) +{ + struct sockaddr_in addr; + socklen_t addrlen = sizeof(addr); + + if (getsockname(fd1, &addr, &addrlen) < 0) { + perror("getsockname"); + exit(EXIT_FAILURE); + } + + if (connect(fd2, &addr, addrlen) < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } +} + +int udp_socketpair(int *fd) +{ + fd[0] = udp_socket(); + fd[1] = udp_socket(); + + udp_connect(fd[0], fd[1]); + udp_connect(fd[1], fd[0]); + + return 0; +} |