lib1960.c 3.8 KB

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