1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
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.
30 #include <curl/curl.h>
36 #define close closesocket
38 #include <sys/types.h> /* socket types */
39 #include <sys/socket.h> /* socket definitions */
40 #include <netinet/in.h>
41 #include <arpa/inet.h> /* inet (3) funtions */
42 #include <unistd.h> /* misc. Unix functions */
47 /* The IP address and port number to connect to */
48 #define IPADDR "127.0.0.1"
52 #define INADDR_NONE 0xffffffff
55 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
57 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
61 static curl_socket_t opensocket(void *clientp,
63 struct curl_sockaddr *address)
68 sockfd = *(curl_socket_t *)clientp;
69 /* the actual externally set socket is passed in via the OPENSOCKETDATA
74 static int sockopt_callback(void *clientp, curl_socket_t curlfd,
80 /* This return code was added in libcurl 7.21.5 */
81 return CURL_SOCKOPT_ALREADY_CONNECTED;
88 struct sockaddr_in servaddr; /* socket address structure */
95 if((initwsa = WSAStartup(MAKEWORD(2, 0), &wsaData)) != 0) {
96 printf("WSAStartup failed: %d\n", initwsa);
101 curl = curl_easy_init();
104 * Note that libcurl will internally think that you connect to the host
105 * and port that you specify in the URL option.
107 curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
109 /* Create the socket "manually" */
110 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == CURL_SOCKET_BAD) {
111 printf("Error creating listening socket.\n");
115 memset(&servaddr, 0, sizeof(servaddr));
116 servaddr.sin_family = AF_INET;
117 servaddr.sin_port = htons(PORTNUM);
119 if(INADDR_NONE == (servaddr.sin_addr.s_addr = inet_addr(IPADDR)))
122 if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) ==
125 printf("client error: connect: %s\n", strerror(errno));
129 /* no progress meter please */
130 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
132 /* send all data to this function */
133 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
135 /* call this function to get a socket */
136 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
137 curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
139 /* call this function to set options for the socket */
140 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
142 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
144 res = curl_easy_perform(curl);
146 curl_easy_cleanup(curl);
149 printf("libcurl error: %d\n", res);