xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 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 | #include "test.h" |
| 25 | |
| 26 | #include "testutil.h" |
| 27 | #include "warnless.h" |
| 28 | #include "memdebug.h" |
| 29 | |
| 30 | static const char cmd[] = "A1 IDLE\r\n"; |
| 31 | static char buf[1024]; |
| 32 | |
| 33 | int test(char *URL) |
| 34 | { |
| 35 | CURLM *mcurl; |
| 36 | CURL *curl = NULL; |
| 37 | int mrun; |
| 38 | curl_socket_t sock = CURL_SOCKET_BAD; |
| 39 | time_t start = time(NULL); |
| 40 | int state = 0; |
| 41 | ssize_t pos = 0; |
| 42 | |
| 43 | curl_global_init(CURL_GLOBAL_DEFAULT); |
| 44 | mcurl = curl_multi_init(); |
| 45 | if(!mcurl) |
| 46 | goto fail; |
| 47 | curl = curl_easy_init(); |
| 48 | if(!curl) |
| 49 | goto fail; |
| 50 | |
| 51 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| 52 | if(curl_easy_setopt(curl, CURLOPT_URL, URL)) |
| 53 | goto fail; |
| 54 | curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); |
| 55 | if(curl_multi_add_handle(mcurl, curl)) |
| 56 | goto fail; |
| 57 | |
| 58 | while(time(NULL) - start < 5) { |
| 59 | struct curl_waitfd waitfd; |
| 60 | |
| 61 | if(curl_multi_perform(mcurl, &mrun)) |
| 62 | goto fail; |
| 63 | for(;;) { |
| 64 | int i; |
| 65 | struct CURLMsg *m = curl_multi_info_read(mcurl, &i); |
| 66 | |
| 67 | if(!m) |
| 68 | break; |
| 69 | if(m->msg == CURLMSG_DONE && m->easy_handle == curl) { |
| 70 | curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock); |
| 71 | if(sock == CURL_SOCKET_BAD) |
| 72 | goto fail; |
| 73 | printf("Connected fine, extracted socket. Moving on\n"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if(sock != CURL_SOCKET_BAD) { |
| 78 | waitfd.events = state ? CURL_WAIT_POLLIN : CURL_WAIT_POLLOUT; |
| 79 | waitfd.revents = 0; |
| 80 | curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock); |
| 81 | waitfd.fd = sock; |
| 82 | } |
| 83 | curl_multi_wait(mcurl, &waitfd, sock == CURL_SOCKET_BAD ? 0 : 1, 500, |
| 84 | &mrun); |
| 85 | if((sock != CURL_SOCKET_BAD) && (waitfd.revents & waitfd.events)) { |
| 86 | size_t len = 0; |
| 87 | |
| 88 | if(!state) { |
| 89 | curl_easy_send(curl, cmd + pos, sizeof(cmd) - 1 - pos, &len); |
| 90 | if(len > 0) |
| 91 | pos += len; |
| 92 | else |
| 93 | pos = 0; |
| 94 | if(pos == sizeof(cmd) - 1) { |
| 95 | state++; |
| 96 | pos = 0; |
| 97 | } |
| 98 | } |
| 99 | else if(pos < (ssize_t)sizeof(buf)) { |
| 100 | curl_easy_recv(curl, buf + pos, sizeof(buf) - pos, &len); |
| 101 | if(len > 0) |
| 102 | pos += len; |
| 103 | } |
| 104 | if(len <= 0) |
| 105 | sock = CURL_SOCKET_BAD; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if(state) { |
| 110 | fwrite(buf, pos, 1, stdout); |
| 111 | putchar('\n'); |
| 112 | } |
| 113 | |
| 114 | curl_multi_remove_handle(mcurl, curl); |
| 115 | fail: |
| 116 | curl_easy_cleanup(curl); |
| 117 | curl_multi_cleanup(mcurl); |
| 118 | |
| 119 | curl_global_cleanup(); |
| 120 | return 0; |
| 121 | } |
| 122 | |