1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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 <arpa/inet.h>        /*  inet (3) funtions         */
40#include <unistd.h>           /*  misc. UNIX functions      */
41#endif
42
43#include <errno.h>
44
45/* The IP address and port number to connect to */
46#define IPADDR "127.0.0.1"
47#define PORTNUM 80
48
49static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
50{
51  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
52  return written;
53}
54
55static curl_socket_t opensocket(void *clientp,
56                                curlsocktype purpose,
57                                struct curl_sockaddr *address)
58{
59  curl_socket_t sockfd = *(curl_socket_t *)clientp;
60  /* the actual externally set socket is passed in via the OPENSOCKETDATA
61     option */
62  return sockfd;
63}
64
65static int sockopt_callback(void *clientp, curl_socket_t curlfd,
66                            curlsocktype purpose)
67{
68  /* This return code was added in libcurl 7.21.5 */
69  return CURL_SOCKOPT_ALREADY_CONNECTED;
70}
71
72int main(void)
73{
74  CURL *curl;
75  CURLcode res;
76  struct sockaddr_in servaddr;  /*  socket address structure  */
77  curl_socket_t sockfd;
78
79#ifdef WIN32
80  WSADATA wsaData;
81  int initwsa;
82
83  if((initwsa = WSAStartup(MAKEWORD(2,0), &wsaData)) != 0) {
84    printf("WSAStartup failed: %d\n", initwsa);
85    return 1;
86  }
87#endif
88
89  curl = curl_easy_init();
90  if(curl) {
91    /*
92     * Note that libcurl will internally think that you connect to the host
93     * and port that you specify in the URL option.
94     */
95    curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
96
97    /* Create the socket "manually" */
98    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
99      printf("Error creating listening socket.\n");
100      return 3;
101    }
102
103    memset(&servaddr, 0, sizeof(servaddr));
104    servaddr.sin_family = AF_INET;
105    servaddr.sin_port   = htons(PORTNUM);
106
107    if (INADDR_NONE == (servaddr.sin_addr.s_addr = inet_addr(IPADDR)))
108      return 2;
109
110    if(connect(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr)) ==
111       -1) {
112      close(sockfd);
113      printf("client error: connect: %s\n", strerror(errno));
114      return 1;
115    }
116
117    /* no progress meter please */
118    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
119
120    /* send all data to this function  */
121    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
122
123    /* call this function to get a socket */
124    curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
125    curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
126
127    /* call this function to set options for the socket */
128    curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
129
130    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
131
132    res = curl_easy_perform(curl);
133
134    curl_easy_cleanup(curl);
135
136    if(res) {
137      printf("libcurl error: %d\n", res);
138      return 4;
139    }
140  }
141  return 0;
142}
143