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 #define PRINT_DEBUG 0   /* set to 1 to print out debugging data */
57 #define VERBOSE_DEBUG 0 /* set to 1 to print out more data      */
58 
rtp_sendto(rtp_sender_t sender,const void * msg,int len)59 int rtp_sendto(rtp_sender_t sender, const void *msg, int len)
60 {
61     int octets_sent;
62     srtp_err_status_t stat;
63     int pkt_len = len + RTP_HEADER_LEN;
64 
65     /* marshal data */
66     strncpy(sender->message.body, msg, len);
67 
68     /* update header */
69     sender->message.header.seq = ntohs(sender->message.header.seq) + 1;
70     sender->message.header.seq = htons(sender->message.header.seq);
71     sender->message.header.ts = ntohl(sender->message.header.ts) + 1;
72     sender->message.header.ts = htonl(sender->message.header.ts);
73 
74     /* apply srtp */
75     stat = srtp_protect(sender->srtp_ctx, &sender->message.header, &pkt_len);
76     if (stat) {
77 #if PRINT_DEBUG
78         fprintf(stderr, "error: srtp protection failed with code %d\n", stat);
79 #endif
80         return -1;
81     }
82 #if VERBOSE_DEBUG
83     srtp_print_packet(&sender->message.header, pkt_len);
84 #endif
85     octets_sent =
86         sendto(sender->socket, (void *)&sender->message, pkt_len, 0,
87                (struct sockaddr *)&sender->addr, sizeof(struct sockaddr_in));
88 
89     if (octets_sent != pkt_len) {
90 #if PRINT_DEBUG
91         fprintf(stderr, "error: couldn't send message %s", (char *)msg);
92         perror("");
93 #endif
94     }
95 
96     return octets_sent;
97 }
98 
rtp_recvfrom(rtp_receiver_t receiver,void * msg,int * len)99 int rtp_recvfrom(rtp_receiver_t receiver, void *msg, int *len)
100 {
101     int octets_recvd;
102     srtp_err_status_t stat;
103 
104     octets_recvd = recvfrom(receiver->socket, (void *)&receiver->message, *len,
105                             0, (struct sockaddr *)NULL, 0);
106 
107     if (octets_recvd == -1) {
108         *len = 0;
109         return -1;
110     }
111 
112     /* verify rtp header */
113     if (receiver->message.header.version != 2) {
114         *len = 0;
115         return -1;
116     }
117 
118 #if PRINT_DEBUG
119     fprintf(stderr, "%d octets received from SSRC %u\n", octets_recvd,
120             receiver->message.header.ssrc);
121 #endif
122 #if VERBOSE_DEBUG
123     srtp_print_packet(&receiver->message.header, octets_recvd);
124 #endif
125 
126     /* apply srtp */
127     stat = srtp_unprotect(receiver->srtp_ctx, &receiver->message.header,
128                           &octets_recvd);
129     if (stat) {
130         fprintf(stderr, "error: srtp unprotection failed with code %d%s\n",
131                 stat,
132                 stat == srtp_err_status_replay_fail
133                     ? " (replay check failed)"
134                     : stat == srtp_err_status_auth_fail ? " (auth check failed)"
135                                                         : "");
136         return -1;
137     }
138     strncpy(msg, receiver->message.body, octets_recvd);
139 
140     return octets_recvd;
141 }
142 
rtp_sender_init(rtp_sender_t sender,int sock,struct sockaddr_in addr,unsigned int ssrc)143 int rtp_sender_init(rtp_sender_t sender,
144                     int sock,
145                     struct sockaddr_in addr,
146                     unsigned int ssrc)
147 {
148     /* set header values */
149     sender->message.header.ssrc = htonl(ssrc);
150     sender->message.header.ts = 0;
151     sender->message.header.seq = (uint16_t)rand();
152     sender->message.header.m = 0;
153     sender->message.header.pt = 0x1;
154     sender->message.header.version = 2;
155     sender->message.header.p = 0;
156     sender->message.header.x = 0;
157     sender->message.header.cc = 0;
158 
159     /* set other stuff */
160     sender->socket = sock;
161     sender->addr = addr;
162 
163     return 0;
164 }
165 
rtp_receiver_init(rtp_receiver_t rcvr,int sock,struct sockaddr_in addr,unsigned int ssrc)166 int rtp_receiver_init(rtp_receiver_t rcvr,
167                       int sock,
168                       struct sockaddr_in addr,
169                       unsigned int ssrc)
170 {
171     /* set header values */
172     rcvr->message.header.ssrc = htonl(ssrc);
173     rcvr->message.header.ts = 0;
174     rcvr->message.header.seq = 0;
175     rcvr->message.header.m = 0;
176     rcvr->message.header.pt = 0x1;
177     rcvr->message.header.version = 2;
178     rcvr->message.header.p = 0;
179     rcvr->message.header.x = 0;
180     rcvr->message.header.cc = 0;
181 
182     /* set other stuff */
183     rcvr->socket = sock;
184     rcvr->addr = addr;
185 
186     return 0;
187 }
188 
rtp_sender_init_srtp(rtp_sender_t sender,const srtp_policy_t * policy)189 int rtp_sender_init_srtp(rtp_sender_t sender, const srtp_policy_t *policy)
190 {
191     return srtp_create(&sender->srtp_ctx, policy);
192 }
193 
rtp_sender_deinit_srtp(rtp_sender_t sender)194 int rtp_sender_deinit_srtp(rtp_sender_t sender)
195 {
196     return srtp_dealloc(sender->srtp_ctx);
197 }
198 
rtp_receiver_init_srtp(rtp_receiver_t sender,const srtp_policy_t * policy)199 int rtp_receiver_init_srtp(rtp_receiver_t sender, const srtp_policy_t *policy)
200 {
201     return srtp_create(&sender->srtp_ctx, policy);
202 }
203 
rtp_receiver_deinit_srtp(rtp_receiver_t sender)204 int rtp_receiver_deinit_srtp(rtp_receiver_t sender)
205 {
206     return srtp_dealloc(sender->srtp_ctx);
207 }
208 
rtp_sender_alloc(void)209 rtp_sender_t rtp_sender_alloc(void)
210 {
211     return (rtp_sender_t)malloc(sizeof(rtp_sender_ctx_t));
212 }
213 
rtp_sender_dealloc(rtp_sender_t rtp_ctx)214 void rtp_sender_dealloc(rtp_sender_t rtp_ctx)
215 {
216     free(rtp_ctx);
217 }
218 
rtp_receiver_alloc(void)219 rtp_receiver_t rtp_receiver_alloc(void)
220 {
221     return (rtp_receiver_t)malloc(sizeof(rtp_receiver_ctx_t));
222 }
223 
rtp_receiver_dealloc(rtp_receiver_t rtp_ctx)224 void rtp_receiver_dealloc(rtp_receiver_t rtp_ctx)
225 {
226     free(rtp_ctx);
227 }
228