/source/curl-7.21.2/tests/libtest/lib571.c

https://github.com/remotesyssupport/omnibus · C · 200 lines · 152 code · 36 blank · 12 comment · 31 complexity · baaacd7bad24613013019a664d34615c MD5 · raw file

  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. */
  9. #include "test.h"
  10. #ifdef HAVE_SYS_SOCKET_H
  11. # include <sys/socket.h>
  12. #endif
  13. #ifdef HAVE_NETINET_IN_H
  14. # include <netinet/in.h>
  15. #endif
  16. #ifdef HAVE_NETDB_H
  17. # include <netdb.h>
  18. #endif
  19. #ifdef HAVE_ARPA_INET_H
  20. # include <arpa/inet.h>
  21. #endif
  22. #ifdef HAVE_SYS_STAT_H
  23. # include <sys/stat.h>
  24. #endif
  25. #ifdef HAVE_FCNTL_H
  26. # include <fcntl.h>
  27. #endif
  28. #include <curl/mprintf.h>
  29. #include "memdebug.h"
  30. #define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
  31. #define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
  32. ((int)((unsigned char)((p)[3]))))
  33. #define RTP_DATA_SIZE 12
  34. static const char *RTP_DATA = "$_1234\n\0asdf";
  35. static int rtp_packet_count = 0;
  36. static size_t rtp_write(void *ptr, size_t size, size_t nmemb, void *stream) {
  37. char *data = (char *)ptr;
  38. int channel = RTP_PKT_CHANNEL(data);
  39. int message_size = (int)(size * nmemb) - 4;
  40. int coded_size = RTP_PKT_LENGTH(data);
  41. size_t failure = (size * nmemb) ? 0 : 1;
  42. int i;
  43. (void)stream;
  44. printf("RTP: message size %d, channel %d\n", message_size, channel);
  45. if(message_size != coded_size) {
  46. printf("RTP embedded size (%d) does not match the write size (%d).\n",
  47. coded_size, message_size);
  48. return failure;
  49. }
  50. data += 4;
  51. for(i = 0; i < message_size; i+= RTP_DATA_SIZE) {
  52. if(message_size - i > RTP_DATA_SIZE) {
  53. if(memcmp(RTP_DATA, data + i, RTP_DATA_SIZE) != 0) {
  54. printf("RTP PAYLOAD CORRUPTED [%s]\n", data + i);
  55. return failure;
  56. }
  57. } else {
  58. if (memcmp(RTP_DATA, data + i, message_size - i) != 0) {
  59. printf("RTP PAYLOAD END CORRUPTED (%d), [%s]\n",
  60. message_size - i, data + i);
  61. return failure;
  62. }
  63. }
  64. }
  65. rtp_packet_count++;
  66. fprintf(stderr, "packet count is %d\n", rtp_packet_count);
  67. return size * nmemb;
  68. }
  69. /* build request url */
  70. static char *suburl(const char *base, int i)
  71. {
  72. return curl_maprintf("%s%.4d", base, i);
  73. }
  74. int test(char *URL)
  75. {
  76. int res;
  77. CURL *curl;
  78. char *stream_uri = NULL;
  79. int request=1;
  80. FILE *protofile = NULL;
  81. protofile = fopen(libtest_arg2, "wb");
  82. if(protofile == NULL) {
  83. fprintf(stderr, "Couldn't open the protocol dump file\n");
  84. return TEST_ERR_MAJOR_BAD;
  85. }
  86. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  87. fprintf(stderr, "curl_global_init() failed\n");
  88. fclose(protofile);
  89. return TEST_ERR_MAJOR_BAD;
  90. }
  91. if ((curl = curl_easy_init()) == NULL) {
  92. fprintf(stderr, "curl_easy_init() failed\n");
  93. fclose(protofile);
  94. curl_global_cleanup();
  95. return TEST_ERR_MAJOR_BAD;
  96. }
  97. test_setopt(curl, CURLOPT_URL, URL);
  98. if((stream_uri = suburl(URL, request++)) == NULL) {
  99. res = TEST_ERR_MAJOR_BAD;
  100. goto test_cleanup;
  101. }
  102. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  103. free(stream_uri);
  104. stream_uri = NULL;
  105. test_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
  106. test_setopt(curl, CURLOPT_TIMEOUT, 3);
  107. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  108. test_setopt(curl, CURLOPT_WRITEDATA, protofile);
  109. test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "RTP/AVP/TCP;interleaved=0-1");
  110. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
  111. res = curl_easy_perform(curl);
  112. if(res)
  113. goto test_cleanup;
  114. /* This PLAY starts the interleave */
  115. if((stream_uri = suburl(URL, request++)) == NULL) {
  116. res = TEST_ERR_MAJOR_BAD;
  117. goto test_cleanup;
  118. }
  119. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  120. free(stream_uri);
  121. stream_uri = NULL;
  122. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  123. res = curl_easy_perform(curl);
  124. if(res)
  125. goto test_cleanup;
  126. /* The DESCRIBE request will try to consume data after the Content */
  127. if((stream_uri = suburl(URL, request++)) == NULL) {
  128. res = TEST_ERR_MAJOR_BAD;
  129. goto test_cleanup;
  130. }
  131. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  132. free(stream_uri);
  133. stream_uri = NULL;
  134. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
  135. res = curl_easy_perform(curl);
  136. if(res)
  137. goto test_cleanup;
  138. if((stream_uri = suburl(URL, request++)) == NULL) {
  139. res = TEST_ERR_MAJOR_BAD;
  140. goto test_cleanup;
  141. }
  142. test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
  143. free(stream_uri);
  144. stream_uri = NULL;
  145. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_PLAY);
  146. res = curl_easy_perform(curl);
  147. if(res)
  148. goto test_cleanup;
  149. fprintf(stderr, "PLAY COMPLETE\n");
  150. /* Use Receive to get the rest of the data */
  151. while(!res && rtp_packet_count < 13) {
  152. fprintf(stderr, "LOOPY LOOP!\n");
  153. test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_RECEIVE);
  154. res = curl_easy_perform(curl);
  155. }
  156. test_cleanup:
  157. if(stream_uri)
  158. free(stream_uri);
  159. if(protofile)
  160. fclose(protofile);
  161. curl_easy_cleanup(curl);
  162. curl_global_cleanup();
  163. return res;
  164. }