externalsocket.c 4.8 KB

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