From 4d05e695ab68a16cc5ed853b50482c443c6e12a9 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 22 Feb 2013 15:37:48 +0100 Subject: [PATCH] Use UDP when using sptps_test in datagram mode. --- src/sptps_test.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/sptps_test.c b/src/sptps_test.c index 9db63ff3..2a9fca0c 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -77,8 +77,8 @@ int main(int argc, char *argv[]) { memset(&hint, 0, sizeof hint); hint.ai_family = AF_UNSPEC; - hint.ai_socktype = SOCK_STREAM; - hint.ai_protocol = IPPROTO_TCP; + hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM; + hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP; hint.ai_flags = initiator ? 0 : AI_PASSIVE; if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) { @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) { return 1; } - int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP); + int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if(sock < 0) { fprintf(stderr, "Could not create socket: %s\n", strerror(errno)); return 1; @@ -106,16 +106,35 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Could not bind socket: %s\n", strerror(errno)); return 1; } - if(listen(sock, 1)) { - fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno)); - return 1; - } - fprintf(stderr, "Listening...\n"); - sock = accept(sock, NULL, NULL); - if(sock < 0) { - fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); - return 1; + if(!datagram) { + if(listen(sock, 1)) { + fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno)); + return 1; + } + fprintf(stderr, "Listening...\n"); + + sock = accept(sock, NULL, NULL); + if(sock < 0) { + fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); + return 1; + } + } else { + fprintf(stderr, "Listening...\n"); + + char buf[65536]; + struct sockaddr addr; + socklen_t addrlen = sizeof addr; + + if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) { + fprintf(stderr, "Could not read from socket: %s\n", strerror(errno)); + return 1; + } + + if(connect(sock, &addr, addrlen)) { + fprintf(stderr, "Could not accept connection: %s\n", strerror(errno)); + return 1; + } } fprintf(stderr, "Connected\n"); -- 2.20.1