Debian Codegraph @

main/sdp_srtp.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2006 - 2007, Mikael Magnusson
5 *
6 * Mikael Magnusson <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief SRTP and SDP Security descriptions
22 *
23 * Specified in RFC 3711, 6188, 7714, and 4568
24 *
25 * \author Mikael Magnusson <[email protected]>
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
35 #include "asterisk/logger.h" /* for ast_log, LOG_ERROR, etc */
36 #include "asterisk/sdp_srtp.h" /* for ast_sdp_srtp, etc */
37
38 /*! Registered SDP crypto API */
39 static struct ast_sdp_crypto_api *sdp_crypto_api;
40
41 struct ast_sdp_srtp *ast_sdp_srtp_alloc(void)
42 {
43 if (!ast_rtp_engine_srtp_is_registered()) {
44 ast_debug(1, "No SRTP module loaded, can't setup SRTP session.\n");
45 return NULL;
46 }
47
48 return ast_calloc(1, sizeof(struct ast_sdp_srtp));
49 }
50
51 void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp)
52 {
53 struct ast_sdp_srtp *next;
54
55 for (next = AST_LIST_NEXT(srtp, sdp_srtp_list);
56 srtp;
57 srtp = next, next = srtp ? AST_LIST_NEXT(srtp, sdp_srtp_list) : NULL) {
58 ast_sdp_crypto_destroy(srtp->crypto);
59 srtp->crypto = NULL;
60 ast_free(srtp);
61 }
62 }
63
64 void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto)
65 {
66 if (sdp_crypto_api) {
67 sdp_crypto_api->dtor(crypto);
68 }
69 }
70
71 struct ast_sdp_crypto *ast_sdp_crypto_alloc(void)
72 {
73 if (!sdp_crypto_api) {
74 return NULL;
75 }
76 return sdp_crypto_api->alloc();
77 }
78
79 int ast_sdp_crypto_process(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
80 {
81 if (!sdp_crypto_api) {
82 return -1;
83 }
84 return sdp_crypto_api->parse_offer(rtp, srtp, attr);
85 }
86
87 int ast_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
88 {
89 if (!sdp_crypto_api) {
90 return -1;
91 }
92 return sdp_crypto_api->build_offer(p, taglen);
93 }
94
95 const char *ast_sdp_srtp_get_attrib(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
96 {
97 if (!sdp_crypto_api) {
98 return NULL;
99 }
100 return sdp_crypto_api->get_attr(srtp, dtls_enabled, default_taglen_32);
101 }
102
103 char *ast_sdp_get_rtp_profile(unsigned int sdes_active, struct ast_rtp_instance *instance, unsigned int using_avpf,
104 unsigned int force_avp)
105 {
106 struct ast_rtp_engine_dtls *dtls;
107
108 if ((dtls = ast_rtp_instance_get_dtls(instance)) && dtls->active(instance)) {
109 if (force_avp) {
110 return using_avpf ? "RTP/SAVPF" : "RTP/SAVP";
111 } else {
112 return using_avpf ? "UDP/TLS/RTP/SAVPF" : "UDP/TLS/RTP/SAVP";
113 }
114 } else {
115 if (using_avpf) {
116 return sdes_active ? "RTP/SAVPF" : "RTP/AVPF";
117 } else {
118 return sdes_active ? "RTP/SAVP" : "RTP/AVP";
119 }
120 }
121 }
122
123 int ast_sdp_crypto_register(struct ast_sdp_crypto_api *api)
124 {
125 if (sdp_crypto_api) {
126 return -1;
127 }
128 sdp_crypto_api = api;
129 return 0;
130 }
131
132 void ast_sdp_crypto_unregister(struct ast_sdp_crypto_api *api)
133 {
134 if (sdp_crypto_api == api) {
135 sdp_crypto_api = NULL;
136 }
137 }