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