tst-connect.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Test the connect function.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <arpa/inet.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <stdbool.h>
  20. #include <support/check.h>
  21. #include <support/xsocket.h>
  22. #include <support/xunistd.h>
  23. #include <sys/socket.h>
  24. #include <stdio.h>
  25. static struct sockaddr_in server_address;
  26. int
  27. open_socket_inet_tcp (void)
  28. {
  29. int fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  30. if (fd < 0)
  31. {
  32. if (errno == EAFNOSUPPORT)
  33. FAIL_UNSUPPORTED ("The host does not support IPv4");
  34. else
  35. FAIL_EXIT1 ("socket (AF_INET, SOCK_STREAM, IPPROTO_TCP): %m\n");
  36. }
  37. return fd;
  38. }
  39. static pid_t
  40. start_server (void)
  41. {
  42. server_address.sin_family = AF_INET;
  43. server_address.sin_port = 0;
  44. server_address.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  45. int server_sock = open_socket_inet_tcp ();
  46. xbind (server_sock, (struct sockaddr *) &server_address,
  47. sizeof (server_address));
  48. socklen_t sa_len = sizeof (server_address);
  49. xgetsockname (server_sock, (struct sockaddr *) &server_address, &sa_len);
  50. xlisten (server_sock, 5);
  51. pid_t my_pid = xfork ();
  52. if (my_pid > 0)
  53. {
  54. xclose (server_sock);
  55. return my_pid;
  56. }
  57. struct sockaddr_in client_address;
  58. socklen_t ca_len = sizeof (server_address);
  59. int client_sock = xaccept (server_sock, (struct sockaddr *) &client_address,
  60. &ca_len);
  61. printf ("socket accepted %d\n", client_sock);
  62. _exit (0);
  63. }
  64. static int
  65. do_test (void)
  66. {
  67. pid_t serv_pid;
  68. struct sockaddr_in peer;
  69. socklen_t peer_len;
  70. serv_pid = start_server ();
  71. int client_sock = open_socket_inet_tcp ();
  72. xconnect (client_sock, (const struct sockaddr *) &server_address,
  73. sizeof (server_address));
  74. /* A second connect with same arguments should fail with EISCONN. */
  75. int result = connect (client_sock,
  76. (const struct sockaddr *) &server_address,
  77. sizeof (server_address));
  78. if (result == 0 || errno != EISCONN)
  79. FAIL_EXIT1 ("Second connect (%d), should fail with EISCONN: %m",
  80. client_sock);
  81. peer_len = sizeof (peer);
  82. xgetpeername (client_sock, (struct sockaddr *) &peer, &peer_len);
  83. TEST_COMPARE (peer_len, sizeof (peer));
  84. TEST_COMPARE (peer.sin_port, server_address.sin_port);
  85. TEST_COMPARE_BLOB (&peer.sin_addr, sizeof (peer.sin_addr),
  86. &server_address.sin_addr,
  87. sizeof (server_address.sin_addr));
  88. int status;
  89. xwaitpid (serv_pid, &status, 0);
  90. TEST_COMPARE (status, 0);
  91. return 0;
  92. }
  93. #include <support/test-driver.c>