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