lib1960.c (4272B)
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 #include "first.h" 25 26 #ifdef HAVE_INET_PTON 27 28 #ifdef HAVE_NETINET_IN_H 29 #include <netinet/in.h> 30 #endif 31 #ifdef HAVE_ARPA_INET_H 32 #include <arpa/inet.h> 33 #endif 34 35 #include "memdebug.h" 36 37 /* to prevent libcurl from closing our socket */ 38 static int closesocket_cb(void *clientp, curl_socket_t item) 39 { 40 (void)clientp; 41 (void)item; 42 return 0; 43 } 44 45 /* provide our own socket */ 46 static curl_socket_t socket_cb(void *clientp, 47 curlsocktype purpose, 48 struct curl_sockaddr *address) 49 { 50 int s = *(int *)clientp; 51 (void)purpose; 52 (void)address; 53 return (curl_socket_t)s; 54 } 55 56 /* tell libcurl the socket is connected */ 57 static int sockopt_cb(void *clientp, 58 curl_socket_t curlfd, 59 curlsocktype purpose) 60 { 61 (void)clientp; 62 (void)curlfd; 63 (void)purpose; 64 return CURL_SOCKOPT_ALREADY_CONNECTED; 65 } 66 67 #if defined(__AMIGA__) 68 #define my_inet_pton(x,y,z) inet_pton(x,(unsigned char *)y,z) 69 #else 70 #define my_inet_pton(x,y,z) inet_pton(x,y,z) 71 #endif 72 73 74 /* Expected args: URL IP PORT */ 75 static CURLcode test_lib1960(char *URL) 76 { 77 CURL *curl = NULL; 78 CURLcode res = TEST_ERR_MAJOR_BAD; 79 int status; 80 curl_socket_t client_fd = CURL_SOCKET_BAD; 81 struct sockaddr_in serv_addr; 82 unsigned short port; 83 84 if(!strcmp("check", URL)) 85 return CURLE_OK; /* no output makes it not skipped */ 86 87 port = (unsigned short)atoi(libtest_arg3); 88 89 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 90 curl_mfprintf(stderr, "curl_global_init() failed\n"); 91 return TEST_ERR_MAJOR_BAD; 92 } 93 94 /* 95 * This code connects to the TCP port "manually" so that we then can hand 96 * over this socket as "already connected" to libcurl and make sure that 97 * this works. 98 */ 99 client_fd = socket(AF_INET, SOCK_STREAM, 0); 100 if(client_fd == CURL_SOCKET_BAD) { 101 curl_mfprintf(stderr, "socket creation error\n"); 102 goto test_cleanup; 103 } 104 105 serv_addr.sin_family = AF_INET; 106 serv_addr.sin_port = htons(port); 107 108 if(my_inet_pton(AF_INET, libtest_arg2, &serv_addr.sin_addr) <= 0) { 109 curl_mfprintf(stderr, "inet_pton failed\n"); 110 goto test_cleanup; 111 } 112 113 status = connect(client_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 114 if(status < 0) { 115 curl_mfprintf(stderr, "connection failed\n"); 116 goto test_cleanup; 117 } 118 119 curl = curl_easy_init(); 120 if(!curl) { 121 curl_mfprintf(stderr, "curl_easy_init() failed\n"); 122 goto test_cleanup; 123 } 124 125 test_setopt(curl, CURLOPT_VERBOSE, 1L); 126 test_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, socket_cb); 127 test_setopt(curl, CURLOPT_OPENSOCKETDATA, &client_fd); 128 test_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_cb); 129 test_setopt(curl, CURLOPT_SOCKOPTDATA, NULL); 130 test_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket_cb); 131 test_setopt(curl, CURLOPT_CLOSESOCKETDATA, NULL); 132 test_setopt(curl, CURLOPT_VERBOSE, 1L); 133 test_setopt(curl, CURLOPT_HEADER, 1L); 134 test_setopt(curl, CURLOPT_URL, URL); 135 136 res = curl_easy_perform(curl); 137 138 test_cleanup: 139 curl_easy_cleanup(curl); 140 if(client_fd != CURL_SOCKET_BAD) 141 sclose(client_fd); 142 curl_global_cleanup(); 143 144 return res; 145 } 146 #else 147 static CURLcode test_lib1960(char *URL) 148 { 149 (void)URL; 150 curl_mprintf("lacks inet_pton\n"); 151 return CURLE_OK; 152 } 153 #endif