[svn-commits] twilson: branch group/srtp r165958 - in /team/group/srtp/res: ./ res_srtp.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Dec 19 12:58:27 CST 2008


Author: twilson
Date: Fri Dec 19 12:58:26 2008
New Revision: 165958

URL: http://svn.digium.com/view/asterisk?view=rev&rev=165958
Log:
Update svn:ignore to ignore .oo files, clean up formatting, check for allocation failures, use ast_ alloc and free functions, and add the has_stream field to ast_srtp.

Modified:
    team/group/srtp/res/   (props changed)
    team/group/srtp/res/res_srtp.c

Propchange: team/group/srtp/res/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Dec 19 12:58:26 2008
@@ -1,5 +1,6 @@
 *.a
 *.d
+*.oo
 *.eo
 *.eoo
 *.i

Modified: team/group/srtp/res/res_srtp.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/res/res_srtp.c?view=diff&rev=165958&r1=165957&r2=165958
==============================================================================
--- team/group/srtp/res/res_srtp.c (original)
+++ team/group/srtp/res/res_srtp.c Fri Dec 19 12:58:26 2008
@@ -14,19 +14,17 @@
  * This program is free software, distributed under the terms of
  * the GNU General Public License Version 2. See the LICENSE file
  * at the top of the source tree.
- * 
+ *
  * Builds on libSRTP http://srtp.sourceforge.net
  */
 
-
-
-/*! \file res_srtp.c 
+/*! \file res_srtp.c
  *
  * \brief Secure RTP (SRTP)
- * 
- * Secure RTP (SRTP) 
+ *
+ * Secure RTP (SRTP)
  * Specified in RFC 3711.
- * 
+ *
  * \author Mikael Magnusson <mikma at users.sourceforge.net>
  */
 
@@ -34,8 +32,8 @@
          <depend>srtp</depend>
 ***/
 
-/* The SIP channel will automatically use sdescriptions if received in a SDP offer, 
-   and res_srtp is loaded. SRTP with sdescriptions key exchange can be activated 
+/* The SIP channel will automatically use sdescriptions if received in a SDP offer,
+   and res_srtp is loaded. SRTP with sdescriptions key exchange can be activated
   in outgoing offers by setting _SIP_SRTP_SDES=1 in extension.conf before executing Dial
 
   The dial fails if the callee doesn't support SRTP and sdescriptions.
@@ -65,6 +63,7 @@
 	const struct ast_srtp_cb *cb;
 	void *data;
 	unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
+	unsigned int has_stream:1;
 };
 
 struct ast_srtp_policy {
@@ -73,49 +72,30 @@
 
 static int g_initialized = 0;
 
-/* Exported functions */
-int usecount(void);
-
 /* SRTP functions */
-static int res_srtp_create(struct ast_srtp **srtp,
-			   struct ast_rtp *rtp,
-			   struct ast_srtp_policy *policy);
+static int res_srtp_create(struct ast_srtp **srtp, struct ast_rtp *rtp, struct ast_srtp_policy *policy);
 static void res_srtp_destroy(struct ast_srtp *srtp);
-static int res_srtp_add_stream(struct ast_srtp *srtp,
-			       struct ast_srtp_policy *policy);
+static int res_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
 
 static int res_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len);
 static int res_srtp_protect(struct ast_srtp *srtp, void **buf, int *len);
 static int res_srtp_get_random(unsigned char *key, size_t len);
-static void res_srtp_set_cb(struct ast_srtp *srtp,
-			    const struct ast_srtp_cb *cb, void *data);
+static void res_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
 
 /* Policy functions */
 static struct ast_srtp_policy *res_srtp_policy_alloc(void);
 static void res_srtp_policy_destroy(struct ast_srtp_policy *policy);
-static int res_srtp_policy_set_suite(struct ast_srtp_policy *policy,
-				     enum ast_srtp_suite suite);
-static int res_srtp_policy_set_master_key(struct ast_srtp_policy *policy,
-			      const unsigned char *key, size_t key_len,
-			      const unsigned char *salt, size_t salt_len);
-static int res_srtp_policy_set_encr_alg(struct ast_srtp_policy *policy,
-					enum ast_srtp_ealg ealg);
-static int res_srtp_policy_set_auth_alg(struct ast_srtp_policy *policy,
-					enum ast_srtp_aalg aalg);
-static void res_srtp_policy_set_encr_keylen(struct ast_srtp_policy *policy,
-					    int ekeyl);
-static void res_srtp_policy_set_auth_keylen(struct ast_srtp_policy *policy,
-					    int akeyl);
-static void res_srtp_policy_set_srtp_auth_taglen(struct ast_srtp_policy *policy,
-						 int autht);
-static void res_srtp_policy_set_srtp_encr_enable(struct ast_srtp_policy *policy,
-						 int enable);
-static void res_srtp_policy_set_srtcp_encr_enable(struct ast_srtp_policy *policy,
-						  int enable);
-static void res_srtp_policy_set_srtp_auth_enable(struct ast_srtp_policy *policy,
-						 int enable);
-static void res_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
-				     unsigned long ssrc, int inbound);
+static int res_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
+static int res_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len);
+static int res_srtp_policy_set_encr_alg(struct ast_srtp_policy *policy, enum ast_srtp_ealg ealg);
+static int res_srtp_policy_set_auth_alg(struct ast_srtp_policy *policy, enum ast_srtp_aalg aalg);
+static void res_srtp_policy_set_encr_keylen(struct ast_srtp_policy *policy, int ekeyl);
+static void res_srtp_policy_set_auth_keylen(struct ast_srtp_policy *policy, int akeyl);
+static void res_srtp_policy_set_srtp_auth_taglen(struct ast_srtp_policy *policy, int autht);
+static void res_srtp_policy_set_srtp_encr_enable(struct ast_srtp_policy *policy, int enable);
+static void res_srtp_policy_set_srtcp_encr_enable(struct ast_srtp_policy *policy, int enable);
+static void res_srtp_policy_set_srtp_auth_enable(struct ast_srtp_policy *policy, int enable);
+static void res_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
 
 static struct ast_srtp_res srtp_res = {
 	.create = res_srtp_create,
@@ -185,8 +165,13 @@
 
 static struct ast_srtp *res_srtp_new(void)
 {
-	struct ast_srtp *srtp = malloc(sizeof(*srtp));
-	memset(srtp, 0, sizeof(*srtp));
+	struct ast_srtp *srtp;
+
+	if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
+		ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
+		return NULL;
+	}
+
 	return srtp;
 }
 
@@ -218,27 +203,28 @@
 		policy->sp.ssrc.type = ssrc_specific;
 		policy->sp.ssrc.value = ssrc;
 	} else {
-		policy->sp.ssrc.type =
-			inbound ? ssrc_any_inbound : ssrc_any_outbound;
+		policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
 	}
 }
 
 static struct ast_srtp_policy *res_srtp_policy_alloc()
 {
-	struct ast_srtp_policy *tmp = malloc(sizeof(*tmp));
-
-	memset(tmp, 0, sizeof(*tmp));
+	struct ast_srtp_policy *tmp;
+
+	if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
+		ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
+	}
+
 	return tmp;
 }
 
-static void
-res_srtp_policy_destroy(struct ast_srtp_policy *policy)
+static void res_srtp_policy_destroy(struct ast_srtp_policy *policy)
 {
 	if (policy->sp.key) {
-		free(policy->sp.key);
+		ast_free(policy->sp.key);
 		policy->sp.key = NULL;
 	}
-	free(policy);
+	ast_free(policy);
 }
 
 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
@@ -268,41 +254,34 @@
 	}
 }
 
-static int
-res_srtp_policy_set_suite(struct ast_srtp_policy *policy,
-			  enum ast_srtp_suite suite)
-{
-	int res = policy_set_suite(&policy->sp.rtp, suite) |
-		policy_set_suite(&policy->sp.rtcp, suite);
-
-	return res;
-}
-
-static int
-res_srtp_policy_set_master_key(struct ast_srtp_policy *policy,
-			       const unsigned char *key, size_t key_len,
-			       const unsigned char *salt, size_t salt_len)
+static int res_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
+{
+	return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
+}
+
+static int res_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
 {
 	size_t size = key_len + salt_len;
-	unsigned char *master_key = NULL;
+	unsigned char *master_key;
 
 	if (policy->sp.key) {
-		free(policy->sp.key);
+		ast_free(policy->sp.key);
 		policy->sp.key = NULL;
 	}
 
-	master_key = malloc(size);
+	if (!(master_key = ast_calloc(1, size))) {
+		return -1;
+	}
 
 	memcpy(master_key, key, key_len);
 	memcpy(master_key + key_len, salt, salt_len);
 
 	policy->sp.key = master_key;
+
 	return 0;
 }
 
-static int
-res_srtp_policy_set_encr_alg(struct ast_srtp_policy *policy,
-			     enum ast_srtp_ealg ealg)
+static int res_srtp_policy_set_encr_alg(struct ast_srtp_policy *policy, enum ast_srtp_ealg ealg)
 {
 	int type = -1;
 
@@ -319,12 +298,11 @@
 
 	policy->sp.rtp.cipher_type = type;
 	policy->sp.rtcp.cipher_type = type;
+
 	return 0;
 }
 
-static int
-res_srtp_policy_set_auth_alg(struct ast_srtp_policy *policy,
-			     enum ast_srtp_aalg aalg)
+static int res_srtp_policy_set_auth_alg(struct ast_srtp_policy *policy, enum ast_srtp_aalg aalg)
 {
 	int type = -1;
 
@@ -341,76 +319,66 @@
 
 	policy->sp.rtp.auth_type = type;
 	policy->sp.rtcp.auth_type = type;
+
 	return 0;
 }
 
-static void
-res_srtp_policy_set_encr_keylen(struct ast_srtp_policy *policy, int ekeyl)
+static void res_srtp_policy_set_encr_keylen(struct ast_srtp_policy *policy, int ekeyl)
 {
 	policy->sp.rtp.cipher_key_len = ekeyl;
 	policy->sp.rtcp.cipher_key_len = ekeyl;
 }
 
-static void
-res_srtp_policy_set_auth_keylen(struct ast_srtp_policy *policy, int akeyl)
+static void res_srtp_policy_set_auth_keylen(struct ast_srtp_policy *policy, int akeyl)
 {
 	policy->sp.rtp.auth_key_len = akeyl;
 	policy->sp.rtcp.auth_key_len = akeyl;
 }
 
-static void
-res_srtp_policy_set_srtp_auth_taglen(struct ast_srtp_policy *policy, int autht)
+static void res_srtp_policy_set_srtp_auth_taglen(struct ast_srtp_policy *policy, int autht)
 {
 	policy->sp.rtp.auth_tag_len = autht;
 	policy->sp.rtcp.auth_tag_len = autht;
-	
-}
-
-static void
-res_srtp_policy_set_srtp_encr_enable(struct ast_srtp_policy *policy, int enable)
+
+}
+
+static void res_srtp_policy_set_srtp_encr_enable(struct ast_srtp_policy *policy, int enable)
 {
 	int serv = enable ? sec_serv_conf : sec_serv_none;
-	policy->sp.rtp.sec_serv = 
-		(policy->sp.rtp.sec_serv & ~sec_serv_conf) | serv;
-}
-
-static void
-res_srtp_policy_set_srtcp_encr_enable(struct ast_srtp_policy *policy, int enable)
+
+	policy->sp.rtp.sec_serv = (policy->sp.rtp.sec_serv & ~sec_serv_conf) | serv;
+}
+
+static void res_srtp_policy_set_srtcp_encr_enable(struct ast_srtp_policy *policy, int enable)
 {
 	int serv = enable ? sec_serv_conf : sec_serv_none;
-	policy->sp.rtcp.sec_serv = 
-		(policy->sp.rtcp.sec_serv & ~sec_serv_conf) | serv;
-}
-
-static void
-res_srtp_policy_set_srtp_auth_enable(struct ast_srtp_policy *policy, int enable)
+
+	policy->sp.rtcp.sec_serv = (policy->sp.rtcp.sec_serv & ~sec_serv_conf) | serv;
+}
+
+static void res_srtp_policy_set_srtp_auth_enable(struct ast_srtp_policy *policy, int enable)
 {
 	int serv = enable ? sec_serv_auth : sec_serv_none;
-	policy->sp.rtp.sec_serv = 
-		(policy->sp.rtp.sec_serv & ~sec_serv_auth) | serv;
-}
-
+
+	policy->sp.rtp.sec_serv = (policy->sp.rtp.sec_serv & ~sec_serv_auth) | serv;
+}
 
 static int res_srtp_get_random(unsigned char *key, size_t len)
 {
-	int res = crypto_get_random(key, len);
-
-	return res != err_status_ok ? -1: 0;
-}
-
-static void res_srtp_set_cb(struct ast_srtp *srtp,
-			    const struct ast_srtp_cb *cb, void *data)
-{
-	if (!srtp)
+	return crypto_get_random(key, len) != err_status_ok ? -1: 0;
+}
+
+static void res_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
+{
+	if (!srtp) {
 		return;
-	
+	}
+
 	srtp->cb = cb;
 	srtp->data = data;
 }
 
-
 /* Vtable functions */
-
 static int res_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len)
 {
 	int res = 0;
@@ -418,8 +386,9 @@
 
 	for (i = 0; i < 2; i++) {
 		res = srtp_unprotect(srtp->session, buf, len);
-		if (res != err_status_no_ctx)
+		if (res != err_status_no_ctx) {
 			break;
+		}
 
 		if (srtp->cb && srtp->cb->no_ctx) {
 			if (srtp->cb->no_ctx(srtp->rtp, ast_rtp_get_themssrc(srtp->rtp), srtp->data) < 0) {
@@ -440,16 +409,15 @@
 
 static int res_srtp_protect(struct ast_srtp *srtp, void **buf, int *len)
 {
-	int res = 0;
-
-	if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf))
-		return -1;
+	int res;
+
+	if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
+		return -1;
+	}
 
 	memcpy(srtp->buf, *buf, *len);
 
-	res = srtp_protect(srtp->session, srtp->buf, len);
-
-	if (res != err_status_ok) {
+	if ((res = srtp_protect(srtp->session, srtp->buf, len)) != err_status_ok) {
 		ast_debug(1, "SRTP protect: %s\n", srtp_errstr(res));
 		return -1;
 	}
@@ -460,14 +428,16 @@
 
 static int res_srtp_create(struct ast_srtp **srtp, struct ast_rtp *rtp, struct ast_srtp_policy *policy)
 {
-	int res;
-	struct ast_srtp *temp = res_srtp_new();
-
-	res = srtp_create(&temp->session, &policy->sp);
-	if (res != err_status_ok) {
-		return -1;
-	}
-	
+	struct ast_srtp *temp;
+
+	if (!(temp = res_srtp_new())) {
+		return -1;
+	}
+
+	if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
+		return -1;
+	}
+
 	temp->rtp = rtp;
 	*srtp = temp;
 
@@ -480,39 +450,34 @@
 		srtp_dealloc(srtp->session);
 	}
 
-	free(srtp);
+	ast_free(srtp);
 }
 
 static int res_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
 {
-	int res;
-	
-	res = srtp_add_stream(srtp->session, &policy->sp);
-	/* If a stream has already been set up, libsrtp will return a bad_param error
-	 * and then we'll flash a warning on CLI. Might be better to add a flag to the
-	 * ast_srtp struct.*/
-	if (!(res == err_status_ok || res == err_status_bad_param))
-		return -1;
+	if (!srtp->has_stream && srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
+		return -1;
+	}
+
+	srtp->has_stream = 1;
 
 	return 0;
 }
 
 static int res_srtp_init(void)
 {
-	int res;
-
-	if (g_initialized)
+	if (g_initialized) {
 		return 0;
-
-	res = srtp_init();
-	if (res != err_status_ok)
-		return -1;
-	
+	}
+
+	if (srtp_init() != err_status_ok) {
+		return -1;
+	}
+
 	srtp_install_event_handler(srtp_event_cb);
 
 	return ast_rtp_register_srtp(&srtp_res, &policy_res);
 }
-
 
 /*
  * Exported functions
@@ -528,13 +493,7 @@
 	return ast_rtp_unregister_srtp(&srtp_res, &policy_res);
 }
 
-int usecount(void)
-{
-	return 1;
-}
-
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Secure RTP (SRTP)",
-		.load = load_module,
-		.unload = unload_module,
-/*		.reload = reload, */
-		);
+	.load = load_module,
+	.unload = unload_module,
+);




More information about the svn-commits mailing list