2
0

lib1960.c 4.0 KB

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