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