From c4122907a34b75445f6f926337d21996603adf92 Mon Sep 17 00:00:00 2001 From: Sepherosa Ziehau Date: Tue, 23 Sep 2014 20:51:14 +0800 Subject: [PATCH] test/udp: Add SO_REUSEPORT test --- test/udp/bindconnsend/udp_bindconnsend.c | 18 +++++++++++++++--- test/udp/bindsend/udp_bindsend.c | 18 +++++++++++++++--- test/udp/conn2send/udp_conn2send.c | 18 +++++++++++++++--- test/udp/sendwildcard/udp_sendwildcard.c | 18 +++++++++++++++--- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/test/udp/bindconnsend/udp_bindconnsend.c b/test/udp/bindconnsend/udp_bindconnsend.c index 09ef6d250e..ede4d0536d 100644 --- a/test/udp/bindconnsend/udp_bindconnsend.c +++ b/test/udp/bindconnsend/udp_bindconnsend.c @@ -14,7 +14,7 @@ static void usage(const char *cmd) { - fprintf(stderr, "%s -4 ip4 -p port [-b ip4] -P bind_port\n", cmd); + fprintf(stderr, "%s -4 ip4 -p port [-b ip4] -P bind_port [-r]\n", cmd); exit(1); } @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { struct sockaddr_in in, local_in; - int s, opt, n; + int s, opt, n, reuseport; uint8_t buf[18]; memset(&in, 0, sizeof(in)); @@ -31,7 +31,9 @@ main(int argc, char *argv[]) memset(&local_in, 0, sizeof(local_in)); local_in.sin_family = AF_INET; - while ((opt = getopt(argc, argv, "4:P:b:p:")) != -1) { + reuseport = 0; + + while ((opt = getopt(argc, argv, "4:P:b:p:r")) != -1) { switch (opt) { case '4': if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0) @@ -53,6 +55,10 @@ main(int argc, char *argv[]) in.sin_port = htons(in.sin_port); break; + case 'r': + reuseport = 1; + break; + default: usage(argv[0]); } @@ -66,6 +72,12 @@ main(int argc, char *argv[]) if (s < 0) err(2, "socket failed"); + if (reuseport) { + if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, + &reuseport, sizeof(reuseport)) < 0) + err(2, "setsockopt SO_REUSEPORT failed"); + } + if (bind(s, (const struct sockaddr *)&local_in, sizeof(local_in)) < 0) err(2, "bind failed"); diff --git a/test/udp/bindsend/udp_bindsend.c b/test/udp/bindsend/udp_bindsend.c index 6c97d5d73b..3b3abf2b7d 100644 --- a/test/udp/bindsend/udp_bindsend.c +++ b/test/udp/bindsend/udp_bindsend.c @@ -14,7 +14,7 @@ static void usage(const char *cmd) { - fprintf(stderr, "%s -4 ip4 -p port [-b ip4] -P bind_port\n", cmd); + fprintf(stderr, "%s -4 ip4 -p port [-b ip4] -P bind_port [-r]\n", cmd); exit(1); } @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { struct sockaddr_in in, local_in; - int s, opt, n; + int s, opt, n, reuseport; uint8_t buf[18]; memset(&in, 0, sizeof(in)); @@ -31,7 +31,9 @@ main(int argc, char *argv[]) memset(&local_in, 0, sizeof(local_in)); local_in.sin_family = AF_INET; - while ((opt = getopt(argc, argv, "4:P:b:p:")) != -1) { + reuseport = 0; + + while ((opt = getopt(argc, argv, "4:P:b:p:r")) != -1) { switch (opt) { case '4': if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0) @@ -53,6 +55,10 @@ main(int argc, char *argv[]) in.sin_port = htons(in.sin_port); break; + case 'r': + reuseport = 1; + break; + default: usage(argv[0]); } @@ -66,6 +72,12 @@ main(int argc, char *argv[]) if (s < 0) err(2, "socket failed"); + if (reuseport) { + if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, + &reuseport, sizeof(reuseport)) < 0) + err(2, "setsockopt SO_REUSEPORT failed"); + } + if (bind(s, (const struct sockaddr *)&local_in, sizeof(local_in)) < 0) err(2, "bind failed"); diff --git a/test/udp/conn2send/udp_conn2send.c b/test/udp/conn2send/udp_conn2send.c index 1073eeb135..c1ea2b8918 100644 --- a/test/udp/conn2send/udp_conn2send.c +++ b/test/udp/conn2send/udp_conn2send.c @@ -15,7 +15,7 @@ static void usage(const char *cmd) { - fprintf(stderr, "%s -4 ip4 -p port\n", cmd); + fprintf(stderr, "%s -4 ip4 -p port [-r]\n", cmd); exit(1); } @@ -23,13 +23,15 @@ int main(int argc, char *argv[]) { struct sockaddr_in in, in2; - int s, opt, n; + int s, opt, n, reuseport; uint8_t buf[18]; memset(&in, 0, sizeof(in)); in.sin_family = AF_INET; - while ((opt = getopt(argc, argv, "4:p:")) != -1) { + reuseport = 0; + + while ((opt = getopt(argc, argv, "4:p:r")) != -1) { switch (opt) { case '4': if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0) @@ -41,6 +43,10 @@ main(int argc, char *argv[]) in.sin_port = htons(in.sin_port); break; + case 'r': + reuseport = 1; + break; + default: usage(argv[0]); } @@ -53,6 +59,12 @@ main(int argc, char *argv[]) if (s < 0) err(2, "socket failed"); + if (reuseport) { + if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, + &reuseport, sizeof(reuseport)) < 0) + err(2, "setsockopt SO_REUSEPORT failed"); + } + if (connect(s, (const struct sockaddr *)&in, sizeof(in)) < 0) err(2, "connect failed"); diff --git a/test/udp/sendwildcard/udp_sendwildcard.c b/test/udp/sendwildcard/udp_sendwildcard.c index b9a0ea3f2d..e45995ca9d 100644 --- a/test/udp/sendwildcard/udp_sendwildcard.c +++ b/test/udp/sendwildcard/udp_sendwildcard.c @@ -14,7 +14,7 @@ static void usage(const char *cmd) { - fprintf(stderr, "%s -4 ip4 -p port\n", cmd); + fprintf(stderr, "%s -4 ip4 -p port [-r]\n", cmd); exit(1); } @@ -22,13 +22,15 @@ int main(int argc, char *argv[]) { struct sockaddr_in in; - int s, opt, n; + int s, opt, n, reuseport; uint8_t buf[18]; memset(&in, 0, sizeof(in)); in.sin_family = AF_INET; - while ((opt = getopt(argc, argv, "4:p:")) != -1) { + reuseport = 0; + + while ((opt = getopt(argc, argv, "4:p:r")) != -1) { switch (opt) { case '4': if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0) @@ -40,6 +42,10 @@ main(int argc, char *argv[]) in.sin_port = htons(in.sin_port); break; + case 'r': + reuseport = 1; + break; + default: usage(argv[0]); } @@ -52,6 +58,12 @@ main(int argc, char *argv[]) if (s < 0) err(2, "socket failed"); + if (reuseport) { + if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, + &reuseport, sizeof(reuseport)) < 0) + err(2, "setsockopt SO_REUSEPORT failed"); + } + n = sendto(s, buf, sizeof(buf), 0, (const struct sockaddr *)&in, sizeof(in)); if (n < 0) -- 2.41.0