lib1960.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #ifdef HAVE_INET_PTON
  26. #ifdef WIN32
  27. #include <winsock2.h>
  28. #include <ws2tcpip.h>
  29. #include <windows.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_SYS_SOCKET_H
  35. #include <sys/socket.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #include "memdebug.h"
  41. /* to prevent libcurl from closing our socket */
  42. static int closesocket_cb(void *clientp, curl_socket_t item)
  43. {
  44. (void)clientp;
  45. (void)item;
  46. return 0;
  47. }
  48. /* provide our own socket */
  49. static curl_socket_t socket_cb(void *clientp,
  50. curlsocktype purpose,
  51. struct curl_sockaddr *address)
  52. {
  53. int s = *(int *)clientp;
  54. (void)purpose;
  55. (void)address;
  56. return (curl_socket_t)s;
  57. }
  58. /* tell libcurl the socket is connected */
  59. static int sockopt_cb(void *clientp,
  60. curl_socket_t curlfd,
  61. curlsocktype purpose)
  62. {
  63. (void)clientp;
  64. (void)curlfd;
  65. (void)purpose;
  66. return CURL_SOCKOPT_ALREADY_CONNECTED;
  67. }
  68. /* Expected args: URL IP PORT */
  69. int test(char *URL)
  70. {
  71. CURL *curl = NULL;
  72. CURLcode res = TEST_ERR_MAJOR_BAD;
  73. int status;
  74. curl_socket_t client_fd = CURL_SOCKET_BAD;
  75. struct sockaddr_in serv_addr;
  76. unsigned short port;
  77. if(!strcmp("check", URL))
  78. return 0; /* no output makes it not skipped */
  79. port = (unsigned short)atoi(libtest_arg3);
  80. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  81. fprintf(stderr, "curl_global_init() failed\n");
  82. return TEST_ERR_MAJOR_BAD;
  83. }
  84. /*
  85. * This code connects to the TCP port "manually" so that we then can hand
  86. * over this socket as "already connected" to libcurl and make sure that
  87. * this works.
  88. */
  89. client_fd = socket(AF_INET, SOCK_STREAM, 0);
  90. if(client_fd == CURL_SOCKET_BAD) {
  91. fprintf(stderr, "socket creation error\n");
  92. goto test_cleanup;
  93. }
  94. serv_addr.sin_family = AF_INET;
  95. serv_addr.sin_port = htons(port);
  96. if(inet_pton(AF_INET, libtest_arg2, &serv_addr.sin_addr) <= 0) {
  97. fprintf(stderr, "inet_pton failed\n");
  98. goto test_cleanup;
  99. }
  100. status = connect(client_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
  101. if(status < 0) {
  102. fprintf(stderr, "connection failed\n");
  103. goto test_cleanup;
  104. }
  105. curl = curl_easy_init();
  106. if(!curl) {
  107. fprintf(stderr, "curl_easy_init() failed\n");
  108. goto test_cleanup;
  109. }
  110. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  111. test_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, socket_cb);
  112. test_setopt(curl, CURLOPT_OPENSOCKETDATA, &client_fd);
  113. test_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_cb);
  114. test_setopt(curl, CURLOPT_SOCKOPTDATA, NULL);
  115. test_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket_cb);
  116. test_setopt(curl, CURLOPT_CLOSESOCKETDATA, NULL);
  117. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  118. test_setopt(curl, CURLOPT_HEADER, 1L);
  119. test_setopt(curl, CURLOPT_URL, URL);
  120. res = curl_easy_perform(curl);
  121. test_cleanup:
  122. if(client_fd != CURL_SOCKET_BAD)
  123. sclose(client_fd);
  124. curl_easy_cleanup(curl);
  125. curl_global_cleanup();
  126. return res;
  127. }
  128. #else
  129. int test(char *URL)
  130. {
  131. (void)URL;
  132. printf("lacks inet_pton\n");
  133. return 0;
  134. }
  135. #endif