socketpair.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019, 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "socketpair.h"
  24. #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
  25. #ifdef WIN32
  26. /*
  27. * This is a socketpair() implementation for Windows.
  28. */
  29. #include <string.h>
  30. #include <winsock2.h>
  31. #include <ws2tcpip.h>
  32. #include <windows.h>
  33. #include <io.h>
  34. #else
  35. #ifdef HAVE_NETDB_H
  36. #include <netdb.h>
  37. #endif
  38. #ifdef HAVE_NETINET_IN_H
  39. #include <netinet/in.h> /* IPPROTO_TCP */
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifndef INADDR_LOOPBACK
  45. #define INADDR_LOOPBACK 0x7f000001
  46. #endif /* !INADDR_LOOPBACK */
  47. #endif /* !WIN32 */
  48. /* The last 3 #include files should be in this order */
  49. #include "curl_printf.h"
  50. #include "curl_memory.h"
  51. #include "memdebug.h"
  52. int Curl_socketpair(int domain, int type, int protocol,
  53. curl_socket_t socks[2])
  54. {
  55. union {
  56. struct sockaddr_in inaddr;
  57. struct sockaddr addr;
  58. } a;
  59. curl_socket_t listener;
  60. curl_socklen_t addrlen = sizeof(a.inaddr);
  61. int reuse = 1;
  62. char data[2][12];
  63. ssize_t dlen;
  64. (void)domain;
  65. (void)type;
  66. (void)protocol;
  67. listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  68. if(listener == CURL_SOCKET_BAD)
  69. return -1;
  70. memset(&a, 0, sizeof(a));
  71. a.inaddr.sin_family = AF_INET;
  72. a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  73. a.inaddr.sin_port = 0;
  74. socks[0] = socks[1] = CURL_SOCKET_BAD;
  75. if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
  76. (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
  77. goto error;
  78. if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
  79. goto error;
  80. if(getsockname(listener, &a.addr, &addrlen) == -1)
  81. goto error;
  82. if(listen(listener, 1) == -1)
  83. goto error;
  84. socks[0] = socket(AF_INET, SOCK_STREAM, 0);
  85. if(socks[0] == CURL_SOCKET_BAD)
  86. goto error;
  87. if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
  88. goto error;
  89. socks[1] = accept(listener, NULL, NULL);
  90. if(socks[1] == CURL_SOCKET_BAD)
  91. goto error;
  92. /* verify that nothing else connected */
  93. msnprintf(data[0], sizeof(data[0]), "%p", socks);
  94. dlen = strlen(data[0]);
  95. if(swrite(socks[0], data[0], dlen) != dlen)
  96. goto error;
  97. if(sread(socks[1], data[1], sizeof(data[1])) != dlen)
  98. goto error;
  99. if(memcmp(data[0], data[1], dlen))
  100. goto error;
  101. sclose(listener);
  102. return 0;
  103. error:
  104. sclose(listener);
  105. sclose(socks[0]);
  106. sclose(socks[1]);
  107. return -1;
  108. }
  109. #endif /* ! HAVE_SOCKETPAIR */