externalsocket.c 4.7 KB

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