externalsocket.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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. /*
  23. * This is 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. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <curl/curl.h>
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #include <winsock2.h>
  33. #include <ws2tcpip.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) funtions */
  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. int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  52. return written;
  53. }
  54. static curl_socket_t opensocket(void *clientp,
  55. curlsocktype purpose,
  56. struct curl_sockaddr *address)
  57. {
  58. curl_socket_t sockfd;
  59. (void)purpose;
  60. (void)address;
  61. sockfd = *(curl_socket_t *)clientp;
  62. /* the actual externally set socket is passed in via the OPENSOCKETDATA
  63. option */
  64. return sockfd;
  65. }
  66. static int sockopt_callback(void *clientp, curl_socket_t curlfd,
  67. curlsocktype purpose)
  68. {
  69. (void)clientp;
  70. (void)curlfd;
  71. (void)purpose;
  72. /* This return code was added in libcurl 7.21.5 */
  73. return CURL_SOCKOPT_ALREADY_CONNECTED;
  74. }
  75. int main(void)
  76. {
  77. CURL *curl;
  78. CURLcode res;
  79. struct sockaddr_in servaddr; /* socket address structure */
  80. curl_socket_t sockfd;
  81. #ifdef WIN32
  82. WSADATA wsaData;
  83. int initwsa;
  84. if((initwsa = WSAStartup(MAKEWORD(2,0), &wsaData)) != 0) {
  85. printf("WSAStartup failed: %d\n", initwsa);
  86. return 1;
  87. }
  88. #endif
  89. curl = curl_easy_init();
  90. if(curl) {
  91. /*
  92. * Note that libcurl will internally think that you connect to the host
  93. * and port that you specify in the URL option.
  94. */
  95. curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
  96. /* Create the socket "manually" */
  97. if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == CURL_SOCKET_BAD ) {
  98. printf("Error creating listening socket.\n");
  99. return 3;
  100. }
  101. memset(&servaddr, 0, sizeof(servaddr));
  102. servaddr.sin_family = AF_INET;
  103. servaddr.sin_port = htons(PORTNUM);
  104. if (INADDR_NONE == (servaddr.sin_addr.s_addr = inet_addr(IPADDR)))
  105. return 2;
  106. if(connect(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr)) ==
  107. -1) {
  108. close(sockfd);
  109. printf("client error: connect: %s\n", strerror(errno));
  110. return 1;
  111. }
  112. /* no progress meter please */
  113. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  114. /* send all data to this function */
  115. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  116. /* call this function to get a socket */
  117. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
  118. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
  119. /* call this function to set options for the socket */
  120. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
  121. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  122. res = curl_easy_perform(curl);
  123. curl_easy_cleanup(curl);
  124. if(res) {
  125. printf("libcurl error: %d\n", res);
  126. return 4;
  127. }
  128. }
  129. return 0;
  130. }