Fix endianess issue and add srand()
cover Fix endianess issue and add srand()
Hi Jens,
2 patches for liburing:
- Fix endiness issue (from me, suggested by Louvian).
- Add srand() before calling rand() (from me).
----------------------------------------------------------------
Ammar Faizi (2):
test: Fix endianess issue on bind() and connect() test/accept-link: Add srand() for better randomness
test/232c93d07b74-test.c | 9 +++++---- test/accept-link.c | 14 ++++++++++---- test/accept.c | 5 +++-- test/poll-link.c | 11 +++++++---- test/shutdown.c | 5 +++-- test/socket-rw-eagain.c | 5 +++-- test/socket-rw.c | 5 +++-- 7 files changed, 34 insertions(+), 20 deletions(-)
---
Ammar Faizi
PATCH 1/2 test: Fix endianess issue on `bind()` and `connect()`
When we call bind() or connect(), the addr and port should be in big endian value representation.
Portability notes:
- Do not hard code the address with integer like this 0x0100007fU. Instead, use inet_addr("127.0.0.1") to ensure portability for the machine with different endianess. It's also cleaner and more readable to use inet_addr() from #include <arpa/inet.h>.
- Use htons(port_number) to make sure the port_number is properly choosen instead directly assign it with integer (still about endianess problem).
This commit fixes endianess issue in these files:
test/232c93d07b74-test.c
test/accept-link.c
test/accept.c
test/poll-link.c
test/shutdown.c
test/socket-rw-eagain.c
test/socket-rw.c
--- test/232c93d07b74-test.c | 9 +++++---- test/accept-link.c | 11 +++++++---- test/accept.c | 5 +++-- test/poll-link.c | 11 +++++++---- test/shutdown.c | 5 +++-- test/socket-rw-eagain.c | 5 +++-- test/socket-rw.c | 5 +++-- 7 files changed, 31 insertions(+), 20 deletions(-)
test/232c93d07b74-test.c +5 -4
@@ -19,6 +19,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -75,8 +76,8 @@ static void *rcv(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_port =PORT ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons (PORT ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); res =bind (s0, (struct sockaddr *) &addr,sizeof (addr));assert (res !=-1 ); }else {
@@ -190,8 +191,8 @@ static void *snd(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_port =PORT ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons (PORT ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =connect (s0, (struct sockaddr *) &addr,sizeof (addr));assert (ret !=-1 ); }else {
test/accept-link.c +7 -4
@@ -11,6 +11,7 @@#include <netinet/tcp.h> #include <netinet/in.h> #include <poll.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,7 +43,8 @@ struct data {unsigned expected[2 ];unsigned just_positive[2 ];unsigned long timeout; -int port; +unsigned short port; +unsigned int addr;int stop; };
@@ -63,7 +65,7 @@ static void *send_thread(void *arg) addr.sin_family =AF_INET ; addr.sin_port = data->port; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr = data->addr; ret =connect (s0, (struct sockaddr *)&addr,sizeof (addr));assert (ret !=-1 );
@@ -95,11 +97,12 @@ void *recv_thread(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + data->addr =inet_addr ("127.0.0.1" ); + addr.sin_addr.s_addr = data->addr; i =0 ; do { - data->port = 1025 + (rand() % 64510); + data->port = htons(1025 + (rand() % 64510)); addr.sin_port = data->port; if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
test/accept.c +3 -2
@@ -17,6 +17,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "helpers.h" #include "liburing.h"
@@ -107,8 +108,8 @@ static int start_accept_listen(struct sockaddr_in *addr, int port_off) addr = &laddr; addr->sin_family =AF_INET ; - addr->sin_port =0x1235 + port_off; - addr->sin_addr.s_addr =0x0100007fU ; + addr->sin_port =htons (0x1235 + port_off); + addr->sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =bind (fd, (struct sockaddr *)addr,sizeof (*addr));assert (ret !=-1 );
test/poll-link.c +7 -4
@@ -11,6 +11,7 @@#include <netinet/tcp.h> #include <netinet/in.h> #include <poll.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,7 +43,8 @@ struct data {unsigned expected[2 ];unsigned is_mask[2 ];unsigned long timeout; -int port; +unsigned short port; +unsigned int addr;int stop; };
@@ -59,7 +61,7 @@ static void *send_thread(void *arg) addr.sin_family =AF_INET ; addr.sin_port = data->port; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr = data->addr;if (connect (s0, (struct sockaddr *)&addr,sizeof (addr)) !=-1 )wait_for_var (&recv_thread_done);
@@ -90,11 +92,12 @@ void *recv_thread(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + data->addr =inet_addr ("127.0.0.1" ); + addr.sin_addr.s_addr = data->addr; i =0 ; do { - data->port = 1025 + (rand() % 64510); + data->port = htons(1025 + (rand() % 64510)); addr.sin_port = data->port; if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
test/shutdown.c +3 -2
@@ -15,6 +15,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,8 +43,8 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_port = (rand () %61440 ) +4096 ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons ((rand () %61440 ) +4096 ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =bind (recv_s0, (struct sockaddr *)&addr,sizeof (addr));assert (ret !=-1 );
test/socket-rw-eagain.c +3 -2
@@ -15,6 +15,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -38,10 +39,10 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); do { - addr.sin_port = (rand() % 61440) + 4096; + addr.sin_port = htons((rand() % 61440) + 4096); ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)); if (!ret) break;
test/socket-rw.c +3 -2
@@ -17,6 +17,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -40,10 +41,10 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); do { - addr.sin_port = (rand() % 61440) + 4096; + addr.sin_port = htons((rand() % 61440) + 4096); ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)); if (!ret) break;
Signature · 1 line
2.30.2
PATCH 2/2 test/accept-link: Add `srand()` for better randomness
In reply to Ammar FaiziThe use of rand() should be accompanied by srand().
--- test/accept-link.c | 3 +++ 1 file changed, 3 insertions(+)
test/accept-link.c +3 -0
@@ -240,6 +240,9 @@ int main(int argc, char *argv[]) {if (argc >1 )return 0 ; + +srand ((unsigned )time (NULL )); +if (test_accept_timeout (0 ,200000000 )) {fprintf (stderr,"accept timeout 0 failed\n" );return 1 ;
Signature · 1 line
2.30.2
Jens Axboe
maintainerRe: [PATCH liburing 2/2] test/accept-link: Add `srand()` for better randomness
Quoted 1 line · Ammar Faizi
The use of
rand()should be accompanied bysrand().
The idiomatic way that I usually do it for these tests is:
srand(getpid());
Shouldn't really matter, but we may as well keep it consistent.
Signature · 1 line
Jens Axboe
v2 cover Fix endianess issue and add srand()
Quoted 9 lines · Jens Axboe
Quoted 1 line · Ammar Faizi
The use of
rand()should be accompanied bysrand().The idiomatic way that I usually do it for these tests is:
srand(getpid());
Shouldn't really matter, but we may as well keep it consistent.
v2: Use getpid() for the random seed.
----------------------------------------------------------------
Ammar Faizi (2):
test: Fix endianess issue on bind() and connect() test/accept-link: Add srand() for better randomness
test/232c93d07b74-test.c | 9 +++++---- test/accept-link.c | 14 ++++++++++---- test/accept.c | 5 +++-- test/poll-link.c | 11 +++++++---- test/shutdown.c | 5 +++-- test/socket-rw-eagain.c | 5 +++-- test/socket-rw.c | 5 +++-- 7 files changed, 34 insertions(+), 20 deletions(-)
---
Ammar Faizi
PATCH v2 1/2 test: Fix endianess issue on `bind()` and `connect()`
When we call bind() or connect(), the addr and port should be in big endian value representation.
Portability notes:
- Do not hard code the address with integer like this 0x0100007fU. Instead, use inet_addr("127.0.0.1") to ensure portability for the machine with different endianess. It's also cleaner and more readable to use inet_addr() from #include <arpa/inet.h>.
- Use htons(port_number) to make sure the port_number is properly choosen instead directly assign it with integer (still about endianess problem).
This commit fixes endianess issue in these files:
test/232c93d07b74-test.c
test/accept-link.c
test/accept.c
test/poll-link.c
test/shutdown.c
test/socket-rw-eagain.c
test/socket-rw.c
--- test/232c93d07b74-test.c | 9 +++++---- test/accept-link.c | 11 +++++++---- test/accept.c | 5 +++-- test/poll-link.c | 11 +++++++---- test/shutdown.c | 5 +++-- test/socket-rw-eagain.c | 5 +++-- test/socket-rw.c | 5 +++-- 7 files changed, 31 insertions(+), 20 deletions(-)
test/232c93d07b74-test.c +5 -4
@@ -19,6 +19,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -75,8 +76,8 @@ static void *rcv(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_port =PORT ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons (PORT ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); res =bind (s0, (struct sockaddr *) &addr,sizeof (addr));assert (res !=-1 ); }else {
@@ -190,8 +191,8 @@ static void *snd(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_port =PORT ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons (PORT ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =connect (s0, (struct sockaddr *) &addr,sizeof (addr));assert (ret !=-1 ); }else {
test/accept-link.c +7 -4
@@ -11,6 +11,7 @@#include <netinet/tcp.h> #include <netinet/in.h> #include <poll.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,7 +43,8 @@ struct data {unsigned expected[2 ];unsigned just_positive[2 ];unsigned long timeout; -int port; +unsigned short port; +unsigned int addr;int stop; };
@@ -63,7 +65,7 @@ static void *send_thread(void *arg) addr.sin_family =AF_INET ; addr.sin_port = data->port; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr = data->addr; ret =connect (s0, (struct sockaddr *)&addr,sizeof (addr));assert (ret !=-1 );
@@ -95,11 +97,12 @@ void *recv_thread(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + data->addr =inet_addr ("127.0.0.1" ); + addr.sin_addr.s_addr = data->addr; i =0 ; do { - data->port = 1025 + (rand() % 64510); + data->port = htons(1025 + (rand() % 64510)); addr.sin_port = data->port; if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
test/accept.c +3 -2
@@ -17,6 +17,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "helpers.h" #include "liburing.h"
@@ -107,8 +108,8 @@ static int start_accept_listen(struct sockaddr_in *addr, int port_off) addr = &laddr; addr->sin_family =AF_INET ; - addr->sin_port =0x1235 + port_off; - addr->sin_addr.s_addr =0x0100007fU ; + addr->sin_port =htons (0x1235 + port_off); + addr->sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =bind (fd, (struct sockaddr *)addr,sizeof (*addr));assert (ret !=-1 );
test/poll-link.c +7 -4
@@ -11,6 +11,7 @@#include <netinet/tcp.h> #include <netinet/in.h> #include <poll.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,7 +43,8 @@ struct data {unsigned expected[2 ];unsigned is_mask[2 ];unsigned long timeout; -int port; +unsigned short port; +unsigned int addr;int stop; };
@@ -59,7 +61,7 @@ static void *send_thread(void *arg) addr.sin_family =AF_INET ; addr.sin_port = data->port; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr = data->addr;if (connect (s0, (struct sockaddr *)&addr,sizeof (addr)) !=-1 )wait_for_var (&recv_thread_done);
@@ -90,11 +92,12 @@ void *recv_thread(void *arg)struct sockaddr_in addr; addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + data->addr =inet_addr ("127.0.0.1" ); + addr.sin_addr.s_addr = data->addr; i =0 ; do { - data->port = 1025 + (rand() % 64510); + data->port = htons(1025 + (rand() % 64510)); addr.sin_port = data->port; if (bind(s0, (struct sockaddr*)&addr, sizeof(addr)) != -1)
test/shutdown.c +3 -2
@@ -15,6 +15,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -42,8 +43,8 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_port = (rand () %61440 ) +4096 ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_port =htons ((rand () %61440 ) +4096 ); + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); ret =bind (recv_s0, (struct sockaddr *)&addr,sizeof (addr));assert (ret !=-1 );
test/socket-rw-eagain.c +3 -2
@@ -15,6 +15,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -38,10 +39,10 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); do { - addr.sin_port = (rand() % 61440) + 4096; + addr.sin_port = htons((rand() % 61440) + 4096); ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)); if (!ret) break;
test/socket-rw.c +3 -2
@@ -17,6 +17,7 @@#include <sys/un.h> #include <netinet/tcp.h> #include <netinet/in.h> +#include <arpa/inet.h> #include "liburing.h"
@@ -40,10 +41,10 @@ int main(int argc, char *argv[])assert (ret !=-1 ); addr.sin_family =AF_INET ; - addr.sin_addr.s_addr =0x0100007fU ; + addr.sin_addr.s_addr =inet_addr ("127.0.0.1" ); do { - addr.sin_port = (rand() % 61440) + 4096; + addr.sin_port = htons((rand() % 61440) + 4096); ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)); if (!ret) break;
Signature · 1 line
2.30.2
PATCH v2 2/2 test/accept-link: Add `srand()` for better randomness
In reply to Ammar FaiziThe use of rand() should be accompanied by srand().
--- test/accept-link.c | 3 +++ 1 file changed, 3 insertions(+)
test/accept-link.c +3 -0
@@ -240,6 +240,9 @@ int main(int argc, char *argv[]) {if (argc >1 )return 0 ; + +srand (getpid ()); +if (test_accept_timeout (0 ,200000000 )) {fprintf (stderr,"accept timeout 0 failed\n" );return 1 ;
Signature · 1 line
2.30.2
Quoted 5 lines · Ammar Faizi
Quoted 4 lines · Jens Axboe
Quoted 1 line · Ammar Faizi
The use of
rand()should be accompanied bysrand().
Quoted 7 lines
Quoted 5 lines
The idiomatic way that I usually do it for these tests is:
srand(getpid());
Shouldn't really matter, but we may as well keep it consistent.
v2: Use
getpid()for the random seed.
Applied, thanks.
Signature · 1 line
Jens Axboe