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