2
0

rtp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * rtp.c
  3. *
  4. * library functions for the real-time transport protocol
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #include "rtp.h"
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <sys/types.h>
  48. #ifdef HAVE_SYS_SOCKET_H
  49. #include <sys/socket.h>
  50. #endif
  51. #include "cipher_priv.h"
  52. #define PRINT_DEBUG 0 /* set to 1 to print out debugging data */
  53. #define VERBOSE_DEBUG 0 /* set to 1 to print out more data */
  54. int rtp_sendto(rtp_sender_t sender, const void *msg, int len)
  55. {
  56. int octets_sent;
  57. srtp_err_status_t stat;
  58. int pkt_len = len + RTP_HEADER_LEN;
  59. /* marshal data */
  60. strncpy(sender->message.body, msg, len);
  61. /* update header */
  62. sender->message.header.seq = ntohs(sender->message.header.seq) + 1;
  63. sender->message.header.seq = htons(sender->message.header.seq);
  64. sender->message.header.ts = ntohl(sender->message.header.ts) + 1;
  65. sender->message.header.ts = htonl(sender->message.header.ts);
  66. /* apply srtp */
  67. stat = srtp_protect(sender->srtp_ctx, &sender->message.header, &pkt_len);
  68. if (stat) {
  69. #if PRINT_DEBUG
  70. fprintf(stderr, "error: srtp protection failed with code %d\n", stat);
  71. #endif
  72. return -1;
  73. }
  74. #if VERBOSE_DEBUG
  75. srtp_print_packet(&sender->message.header, pkt_len);
  76. #endif
  77. octets_sent =
  78. sendto(sender->socket, (void *)&sender->message, pkt_len, 0,
  79. (struct sockaddr *)&sender->addr, sizeof(struct sockaddr_in));
  80. if (octets_sent != pkt_len) {
  81. #if PRINT_DEBUG
  82. fprintf(stderr, "error: couldn't send message %s", (char *)msg);
  83. perror("");
  84. #endif
  85. }
  86. return octets_sent;
  87. }
  88. int rtp_recvfrom(rtp_receiver_t receiver, void *msg, int *len)
  89. {
  90. int octets_recvd;
  91. srtp_err_status_t stat;
  92. octets_recvd = recvfrom(receiver->socket, (void *)&receiver->message, *len,
  93. 0, (struct sockaddr *)NULL, 0);
  94. if (octets_recvd == -1) {
  95. *len = 0;
  96. return -1;
  97. }
  98. /* verify rtp header */
  99. if (receiver->message.header.version != 2) {
  100. *len = 0;
  101. return -1;
  102. }
  103. #if PRINT_DEBUG
  104. fprintf(stderr, "%d octets received from SSRC %u\n", octets_recvd,
  105. receiver->message.header.ssrc);
  106. #endif
  107. #if VERBOSE_DEBUG
  108. srtp_print_packet(&receiver->message.header, octets_recvd);
  109. #endif
  110. /* apply srtp */
  111. stat = srtp_unprotect(receiver->srtp_ctx, &receiver->message.header,
  112. &octets_recvd);
  113. if (stat) {
  114. fprintf(stderr, "error: srtp unprotection failed with code %d%s\n",
  115. stat,
  116. stat == srtp_err_status_replay_fail
  117. ? " (replay check failed)"
  118. : stat == srtp_err_status_auth_fail ? " (auth check failed)"
  119. : "");
  120. return -1;
  121. }
  122. strncpy(msg, receiver->message.body, octets_recvd);
  123. return octets_recvd;
  124. }
  125. int rtp_sender_init(rtp_sender_t sender,
  126. int sock,
  127. struct sockaddr_in addr,
  128. unsigned int ssrc)
  129. {
  130. /* set header values */
  131. sender->message.header.ssrc = htonl(ssrc);
  132. sender->message.header.ts = 0;
  133. sender->message.header.seq = (uint16_t)srtp_cipher_rand_u32_for_tests();
  134. sender->message.header.m = 0;
  135. sender->message.header.pt = 0x1;
  136. sender->message.header.version = 2;
  137. sender->message.header.p = 0;
  138. sender->message.header.x = 0;
  139. sender->message.header.cc = 0;
  140. /* set other stuff */
  141. sender->socket = sock;
  142. sender->addr = addr;
  143. return 0;
  144. }
  145. int rtp_receiver_init(rtp_receiver_t rcvr,
  146. int sock,
  147. struct sockaddr_in addr,
  148. unsigned int ssrc)
  149. {
  150. /* set header values */
  151. rcvr->message.header.ssrc = htonl(ssrc);
  152. rcvr->message.header.ts = 0;
  153. rcvr->message.header.seq = 0;
  154. rcvr->message.header.m = 0;
  155. rcvr->message.header.pt = 0x1;
  156. rcvr->message.header.version = 2;
  157. rcvr->message.header.p = 0;
  158. rcvr->message.header.x = 0;
  159. rcvr->message.header.cc = 0;
  160. /* set other stuff */
  161. rcvr->socket = sock;
  162. rcvr->addr = addr;
  163. return 0;
  164. }
  165. int rtp_sender_init_srtp(rtp_sender_t sender, const srtp_policy_t *policy)
  166. {
  167. return srtp_create(&sender->srtp_ctx, policy);
  168. }
  169. int rtp_sender_deinit_srtp(rtp_sender_t sender)
  170. {
  171. return srtp_dealloc(sender->srtp_ctx);
  172. }
  173. int rtp_receiver_init_srtp(rtp_receiver_t sender, const srtp_policy_t *policy)
  174. {
  175. return srtp_create(&sender->srtp_ctx, policy);
  176. }
  177. int rtp_receiver_deinit_srtp(rtp_receiver_t sender)
  178. {
  179. return srtp_dealloc(sender->srtp_ctx);
  180. }
  181. rtp_sender_t rtp_sender_alloc(void)
  182. {
  183. return (rtp_sender_t)malloc(sizeof(rtp_sender_ctx_t));
  184. }
  185. void rtp_sender_dealloc(rtp_sender_t rtp_ctx)
  186. {
  187. free(rtp_ctx);
  188. }
  189. rtp_receiver_t rtp_receiver_alloc(void)
  190. {
  191. return (rtp_receiver_t)malloc(sizeof(rtp_receiver_ctx_t));
  192. }
  193. void rtp_receiver_dealloc(rtp_receiver_t rtp_ctx)
  194. {
  195. free(rtp_ctx);
  196. }