ws_pingpong.c (3843B)
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 #ifndef CURL_DISABLE_WEBSOCKETS 27 28 static CURLcode ping(CURL *curl, const char *send_payload) 29 { 30 size_t sent; 31 CURLcode result = 32 curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0, 33 CURLWS_PING); 34 curl_mfprintf(stderr, "ws: curl_ws_send returned %u, sent %u\n", 35 (int)result, (int)sent); 36 37 return result; 38 } 39 40 static CURLcode recv_pong(CURL *curl, const char *expected_payload) 41 { 42 size_t rlen; 43 const struct curl_ws_frame *meta; 44 char buffer[256]; 45 CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta); 46 if(result) { 47 curl_mfprintf(stderr, "ws: curl_ws_recv returned %u, received %ld\n", 48 (int)result, (long)rlen); 49 return result; 50 } 51 52 if(!(meta->flags & CURLWS_PONG)) { 53 curl_mfprintf(stderr, "recv_pong: wrong frame, got %d bytes rflags %x\n", 54 (int)rlen, meta->flags); 55 return CURLE_RECV_ERROR; 56 } 57 58 curl_mfprintf(stderr, "ws: got PONG back\n"); 59 if(rlen == strlen(expected_payload) && 60 !memcmp(expected_payload, buffer, rlen)) { 61 curl_mfprintf(stderr, "ws: got the same payload back\n"); 62 return CURLE_OK; 63 } 64 curl_mfprintf(stderr, "ws: did NOT get the same payload back\n"); 65 return CURLE_RECV_ERROR; 66 } 67 68 static CURLcode pingpong(CURL *curl, const char *payload) 69 { 70 CURLcode res; 71 int i; 72 73 res = ping(curl, payload); 74 if(res) 75 return res; 76 for(i = 0; i < 10; ++i) { 77 curl_mfprintf(stderr, "Receive pong\n"); 78 res = recv_pong(curl, payload); 79 if(res == CURLE_AGAIN) { 80 curlx_wait_ms(100); 81 continue; 82 } 83 websocket_close(curl); 84 return res; 85 } 86 websocket_close(curl); 87 return CURLE_RECV_ERROR; 88 } 89 90 #endif 91 92 static int test_ws_pingpong(int argc, char *argv[]) 93 { 94 #ifndef CURL_DISABLE_WEBSOCKETS 95 CURL *curl; 96 CURLcode res = CURLE_OK; 97 const char *url, *payload; 98 99 if(argc != 3) { 100 curl_mfprintf(stderr, "usage: ws-pingpong url payload\n"); 101 return 2; 102 } 103 url = argv[1]; 104 payload = argv[2]; 105 106 curl_global_init(CURL_GLOBAL_ALL); 107 108 curl = curl_easy_init(); 109 if(curl) { 110 curl_easy_setopt(curl, CURLOPT_URL, url); 111 112 /* use the callback style */ 113 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong"); 114 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 115 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */ 116 res = curl_easy_perform(curl); 117 curl_mfprintf(stderr, "curl_easy_perform() returned %u\n", (int)res); 118 if(res == CURLE_OK) 119 res = pingpong(curl, payload); 120 121 /* always cleanup */ 122 curl_easy_cleanup(curl); 123 } 124 curl_global_cleanup(); 125 return (int)res; 126 127 #else /* !CURL_DISABLE_WEBSOCKETS */ 128 (void)argc; 129 (void)argv; 130 curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n"); 131 return 1; 132 #endif /* CURL_DISABLE_WEBSOCKETS */ 133 }