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