externalsocket.c 4.8 KB

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