lib677.c 3.3 KB

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