2
0

rtp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "rtp_priv.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #ifdef HAVE_SYS_SOCKET_H
  14. # include <sys/socket.h>
  15. #endif
  16. #define PRINT_DEBUG 0 /* set to 1 to print out debugging data */
  17. #define VERBOSE_DEBUG 0 /* set to 1 to print out more data */
  18. int
  19. rtp_sendto(rtp_sender_t sender, const void* msg, int len) {
  20. int octets_sent;
  21. err_status_t stat;
  22. int pkt_len = len + RTP_HEADER_LEN;
  23. /* marshal data */
  24. strncpy(sender->message.body, msg, len);
  25. /* update header */
  26. sender->message.header.seq = ntohs(sender->message.header.seq) + 1;
  27. sender->message.header.seq = htons(sender->message.header.seq);
  28. sender->message.header.ts = ntohl(sender->message.header.ts) + 1;
  29. sender->message.header.ts = htonl(sender->message.header.ts);
  30. /* apply srtp */
  31. stat = srtp_protect(sender->srtp_ctx, &sender->message.header, &pkt_len);
  32. if (stat) {
  33. #if PRINT_DEBUG
  34. fprintf(stderr, "error: srtp protection failed with code %d\n", stat);
  35. #endif
  36. return -1;
  37. }
  38. #if VERBOSE_DEBUG
  39. srtp_print_packet(&sender->message.header, pkt_len);
  40. #endif
  41. octets_sent = sendto(sender->socket, (void*)&sender->message,
  42. pkt_len, 0, (struct sockaddr *)&sender->addr,
  43. sizeof (struct sockaddr_in));
  44. if (octets_sent != pkt_len) {
  45. #if PRINT_DEBUG
  46. fprintf(stderr, "error: couldn't send message %s", (char *)msg);
  47. perror("");
  48. #endif
  49. }
  50. return octets_sent;
  51. }
  52. int
  53. rtp_recvfrom(rtp_receiver_t receiver, void *msg, int *len) {
  54. int octets_recvd;
  55. err_status_t stat;
  56. octets_recvd = recvfrom(receiver->socket, (void *)&receiver->message,
  57. *len, 0, (struct sockaddr *) NULL, 0);
  58. if (octets_recvd == -1) {
  59. *len = 0;
  60. return -1;
  61. }
  62. /* verify rtp header */
  63. if (receiver->message.header.version != 2) {
  64. *len = 0;
  65. return -1;
  66. }
  67. #if PRINT_DEBUG
  68. fprintf(stderr, "%d octets received from SSRC %u\n",
  69. octets_recvd, receiver->message.header.ssrc);
  70. #endif
  71. #if VERBOSE_DEBUG
  72. srtp_print_packet(&receiver->message.header, octets_recvd);
  73. #endif
  74. /* apply srtp */
  75. stat = srtp_unprotect(receiver->srtp_ctx,
  76. &receiver->message.header, &octets_recvd);
  77. if (stat) {
  78. fprintf(stderr,
  79. "error: srtp unprotection failed with code %d%s\n", stat,
  80. stat == err_status_replay_fail ? " (replay check failed)" :
  81. stat == err_status_auth_fail ? " (auth check failed)" : "");
  82. return -1;
  83. }
  84. strncpy(msg, receiver->message.body, octets_recvd);
  85. return octets_recvd;
  86. }
  87. int
  88. rtp_sender_init(rtp_sender_t sender,
  89. int sock,
  90. struct sockaddr_in addr,
  91. unsigned int ssrc) {
  92. /* set header values */
  93. sender->message.header.ssrc = htonl(ssrc);
  94. sender->message.header.ts = 0;
  95. sender->message.header.seq = (uint16_t) rand();
  96. sender->message.header.m = 0;
  97. sender->message.header.pt = 0x1;
  98. sender->message.header.version = 2;
  99. sender->message.header.p = 0;
  100. sender->message.header.x = 0;
  101. sender->message.header.cc = 0;
  102. /* set other stuff */
  103. sender->socket = sock;
  104. sender->addr = addr;
  105. return 0;
  106. }
  107. int
  108. rtp_receiver_init(rtp_receiver_t rcvr,
  109. int sock,
  110. struct sockaddr_in addr,
  111. unsigned int ssrc) {
  112. /* set header values */
  113. rcvr->message.header.ssrc = htonl(ssrc);
  114. rcvr->message.header.ts = 0;
  115. rcvr->message.header.seq = 0;
  116. rcvr->message.header.m = 0;
  117. rcvr->message.header.pt = 0x1;
  118. rcvr->message.header.version = 2;
  119. rcvr->message.header.p = 0;
  120. rcvr->message.header.x = 0;
  121. rcvr->message.header.cc = 0;
  122. /* set other stuff */
  123. rcvr->socket = sock;
  124. rcvr->addr = addr;
  125. return 0;
  126. }
  127. int
  128. rtp_sender_init_srtp(rtp_sender_t sender, const srtp_policy_t *policy) {
  129. return srtp_create(&sender->srtp_ctx, policy);
  130. }
  131. int
  132. rtp_sender_deinit_srtp(rtp_sender_t sender) {
  133. return srtp_dealloc(sender->srtp_ctx);
  134. }
  135. int
  136. rtp_receiver_init_srtp(rtp_receiver_t sender, const srtp_policy_t *policy) {
  137. return srtp_create(&sender->srtp_ctx, policy);
  138. }
  139. int
  140. rtp_receiver_deinit_srtp(rtp_receiver_t sender) {
  141. return srtp_dealloc(sender->srtp_ctx);
  142. }
  143. rtp_sender_t
  144. rtp_sender_alloc(void) {
  145. return (rtp_sender_t)malloc(sizeof(rtp_sender_ctx_t));
  146. }
  147. void
  148. rtp_sender_dealloc(rtp_sender_t rtp_ctx) {
  149. free(rtp_ctx);
  150. }
  151. rtp_receiver_t
  152. rtp_receiver_alloc(void) {
  153. return (rtp_receiver_t)malloc(sizeof(rtp_receiver_ctx_t));
  154. }
  155. void
  156. rtp_receiver_dealloc(rtp_receiver_t rtp_ctx) {
  157. return free(rtp_ctx);
  158. }