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