externalsocket.c 4.8 KB

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