externalsocket.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * An example demonstrating how an application can pass in a custom
00024  * socket to libcurl to use. This example also handles the connect itself.
00025  * </DESC>
00026  */
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <curl/curl.h>
00031 
00032 #ifdef WIN32
00033 #include <windows.h>
00034 #include <winsock2.h>
00035 #include <ws2tcpip.h>
00036 #define close closesocket
00037 #else
00038 #include <sys/types.h>        /*  socket types              */
00039 #include <sys/socket.h>       /*  socket definitions        */
00040 #include <netinet/in.h>
00041 #include <arpa/inet.h>        /*  inet (3) funtions         */
00042 #include <unistd.h>           /*  misc. Unix functions      */
00043 #endif
00044 
00045 #include <errno.h>
00046 
00047 /* The IP address and port number to connect to */
00048 #define IPADDR "127.0.0.1"
00049 #define PORTNUM 80
00050 
00051 #ifndef INADDR_NONE
00052 #define INADDR_NONE 0xffffffff
00053 #endif
00054 
00055 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
00056 {
00057   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
00058   return written;
00059 }
00060 
00061 static curl_socket_t opensocket(void *clientp,
00062                                 curlsocktype purpose,
00063                                 struct curl_sockaddr *address)
00064 {
00065   curl_socket_t sockfd;
00066   (void)purpose;
00067   (void)address;
00068   sockfd = *(curl_socket_t *)clientp;
00069   /* the actual externally set socket is passed in via the OPENSOCKETDATA
00070      option */
00071   return sockfd;
00072 }
00073 
00074 static int sockopt_callback(void *clientp, curl_socket_t curlfd,
00075                             curlsocktype purpose)
00076 {
00077   (void)clientp;
00078   (void)curlfd;
00079   (void)purpose;
00080   /* This return code was added in libcurl 7.21.5 */
00081   return CURL_SOCKOPT_ALREADY_CONNECTED;
00082 }
00083 
00084 int main(void)
00085 {
00086   CURL *curl;
00087   CURLcode res;
00088   struct sockaddr_in servaddr;  /*  socket address structure  */
00089   curl_socket_t sockfd;
00090 
00091 #ifdef WIN32
00092   WSADATA wsaData;
00093   int initwsa = WSAStartup(MAKEWORD(2, 0), &wsaData);
00094   if(initwsa != 0) {
00095     printf("WSAStartup failed: %d\n", initwsa);
00096     return 1;
00097   }
00098 #endif
00099 
00100   curl = curl_easy_init();
00101   if(curl) {
00102     /*
00103      * Note that libcurl will internally think that you connect to the host
00104      * and port that you specify in the URL option.
00105      */
00106     curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
00107 
00108     /* Create the socket "manually" */
00109     sockfd = socket(AF_INET, SOCK_STREAM, 0);
00110     if(sockfd == CURL_SOCKET_BAD) {
00111       printf("Error creating listening socket.\n");
00112       return 3;
00113     }
00114 
00115     memset(&servaddr, 0, sizeof(servaddr));
00116     servaddr.sin_family = AF_INET;
00117     servaddr.sin_port   = htons(PORTNUM);
00118 
00119     servaddr.sin_addr.s_addr = inet_addr(IPADDR);
00120     if(INADDR_NONE == servaddr.sin_addr.s_addr)
00121       return 2;
00122 
00123     if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) ==
00124        -1) {
00125       close(sockfd);
00126       printf("client error: connect: %s\n", strerror(errno));
00127       return 1;
00128     }
00129 
00130     /* no progress meter please */
00131     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
00132 
00133     /* send all data to this function  */
00134     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
00135 
00136     /* call this function to get a socket */
00137     curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
00138     curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
00139 
00140     /* call this function to set options for the socket */
00141     curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
00142 
00143     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
00144 
00145     res = curl_easy_perform(curl);
00146 
00147     curl_easy_cleanup(curl);
00148 
00149     if(res) {
00150       printf("libcurl error: %d\n", res);
00151       return 4;
00152     }
00153   }
00154   return 0;
00155 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02