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