externalsocket.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. /* <DESC>
  25. * An example demonstrating how an application can pass in a custom
  26. * socket to libcurl to use. This example also handles the connect itself.
  27. * </DESC>
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <curl/curl.h>
  33. #ifdef WIN32
  34. #include <windows.h>
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #define close closesocket
  38. #else
  39. #include <sys/types.h> /* socket types */
  40. #include <sys/socket.h> /* socket definitions */
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h> /* inet (3) functions */
  43. #include <unistd.h> /* misc. Unix functions */
  44. #endif
  45. #include <errno.h>
  46. /* The IP address and port number to connect to */
  47. #define IPADDR "127.0.0.1"
  48. #define PORTNUM 80
  49. #ifndef INADDR_NONE
  50. #define INADDR_NONE 0xffffffff
  51. #endif
  52. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
  53. {
  54. size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  55. return written;
  56. }
  57. static int closecb(void *clientp, curl_socket_t item)
  58. {
  59. (void)clientp;
  60. printf("libcurl wants to close %d now\n", (int)item);
  61. return 0;
  62. }
  63. static curl_socket_t opensocket(void *clientp,
  64. curlsocktype purpose,
  65. struct curl_sockaddr *address)
  66. {
  67. curl_socket_t sockfd;
  68. (void)purpose;
  69. (void)address;
  70. sockfd = *(curl_socket_t *)clientp;
  71. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  72. option */
  73. return sockfd;
  74. }
  75. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  76. curlsocktype purpose)
  77. {
  78. (void)clientp;
  79. (void)curlfd;
  80. (void)purpose;
  81. /* This return code was added in libcurl 7.21.5 */
  82. return CURL_SOCKOPT_ALREADY_CONNECTED;
  83. }
  84. int main(void)
  85. {
  86. CURL *curl;
  87. CURLcode res;
  88. struct sockaddr_in servaddr; /* socket address structure */
  89. curl_socket_t sockfd;
  90. #ifdef WIN32
  91. WSADATA wsaData;
  92. int initwsa = WSAStartup(MAKEWORD(2, 2), &wsaData);
  93. if(initwsa) {
  94. printf("WSAStartup failed: %d\n", initwsa);
  95. return 1;
  96. }
  97. #endif
  98. curl = curl_easy_init();
  99. if(curl) {
  100. /*
  101. * Note that libcurl will internally think that you connect to the host
  102. * and port that you specify in the URL option.
  103. */
  104. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  105. /* Create the socket "manually" */
  106. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  107. if(sockfd == CURL_SOCKET_BAD) {
  108. printf("Error creating listening socket.\n");
  109. return 3;
  110. }
  111. memset(&servaddr, 0, sizeof(servaddr));
  112. servaddr.sin_family = AF_INET;
  113. servaddr.sin_port = htons(PORTNUM);
  114. servaddr.sin_addr.s_addr = inet_addr(IPADDR);
  115. if(INADDR_NONE == servaddr.sin_addr.s_addr) {
  116. close(sockfd);
  117. return 2;
  118. }
  119. if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) ==
  120. -1) {
  121. close(sockfd);
  122. printf("client error: connect: %s\n", strerror(errno));
  123. return 1;
  124. }
  125. /* no progress meter please */
  126. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  127. /* send all data to this function */
  128. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  129. /* call this function to get a socket */
  130. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  131. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  132. /* call this function to close sockets */
  133. curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closecb);
  134. curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &sockfd);
  135. /* call this function to set options for the socket */
  136. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  137. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  138. res = curl_easy_perform(curl);
  139. curl_easy_cleanup(curl);
  140. close(sockfd);
  141. if(res) {
  142. printf("libcurl error: %d\n", res);
  143. return 4;
  144. }
  145. }
  146. #ifdef WIN32
  147. WSACleanup();
  148. #endif
  149. return 0;
  150. }