lib677.c 3.3 KB

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