[Freeswitch-svn] [commit] r11496 - freeswitch/trunk/src/mod/event_handlers/mod_erlang_event

FreeSWITCH SVN andrew at freeswitch.org
Mon Jan 26 16:25:02 PST 2009


Author: andrew
Date: Mon Jan 26 18:25:01 2009
New Revision: 11496

Log:
Allow certain tuple elements to be binaries or strings, to reduce conversion requirements on the erlang side


Modified:
   freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/ei_helpers.c
   freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/handle_msg.c
   freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c
   freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.h

Modified: freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/ei_helpers.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/ei_helpers.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/ei_helpers.c	Mon Jan 26 18:25:01 2009
@@ -272,6 +272,28 @@
 }
 
 
+int ei_decode_string_or_binary(char *buf, int *index, int maxlen, char *dst) {
+	int type, size, res;
+	long len;
+
+	ei_get_type(buf, index, &type, &size);
+
+	if (type != ERL_STRING_EXT && type != ERL_BINARY_EXT) {
+		return -1; 
+	} else if (size > maxlen) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Requested decoding of %s with size %d into a buffer of size %d\n", type == ERL_BINARY_EXT ? "binary" : "string", size, maxlen);
+		return -1;
+	} else if (type == ERL_BINARY_EXT) {
+		res = ei_decode_binary(buf, index, dst, &len);
+		dst[len] = '\0'; /* binaries aren't null terminated */
+	} else {
+		res = ei_decode_string(buf, index, dst);
+	}
+
+	return res;
+}
+
+
 switch_status_t initialise_ei(struct ei_cnode_s *ec)
 {
 	switch_status_t rv;

Modified: freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/handle_msg.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/handle_msg.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/handle_msg.c	Mon Jan 26 18:25:01 2009
@@ -164,12 +164,11 @@
 {
 	char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
 
-	if (ei_decode_string(buf->buff, &buf->index, uuid_str)) {
+	if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str)) {
 		ei_x_encode_tuple_header(rbuf, 2);
 		ei_x_encode_atom(rbuf, "error");
 		ei_x_encode_atom(rbuf, "badarg");
-	}
-	else {
+	} else {
 		ei_x_buff *nbuf = switch_core_alloc(listener->pool, sizeof(nbuf));
 		nbuf->buff = switch_core_alloc(listener->pool, buf->buffsz);
 		memcpy(nbuf->buff, buf->buff, buf->buffsz);
@@ -414,10 +413,10 @@
 
 static switch_status_t handle_msg_sendmsg(listener_t *listener, int arity, ei_x_buff *buf, ei_x_buff *rbuf)
 {
-	char uuid[37];
+	char uuid[SWITCH_UUID_FORMATTED_LENGTH + 1];
 	int headerlength;
 			
-	if (ei_decode_string(buf->buff, &buf->index, uuid) ||
+	if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid) ||
 		ei_decode_list_header(buf->buff, &buf->index, &headerlength)) {
 		ei_x_encode_tuple_header(rbuf, 2);
 		ei_x_encode_atom(rbuf, "error");
@@ -527,7 +526,7 @@
 	char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
 
 	if (arity != 3 ||
-		ei_decode_string(buf->buff, &buf->index, uuid_str) ||
+		ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str) ||
 		ei_decode_atom(buf->buff, &buf->index, reg_name)) {
 		ei_x_encode_tuple_header(rbuf, 2);
 		ei_x_encode_atom(rbuf, "error");

Modified: freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c	Mon Jan 26 18:25:01 2009
@@ -408,12 +408,12 @@
 
 	ei_get_type(rep->buff, &rep->index, &type, &size);
 
-	if (type != ERL_STRING_EXT) /* XXX no unicode or character codes > 255 */
+	if (type != ERL_STRING_EXT && type != ERL_BINARY_EXT) /* XXX no unicode or character codes > 255 */
 		return NULL;
 
 	char *xmlstr = switch_core_alloc(ptr->listener->pool, size + 1);
 
-	ei_decode_string(rep->buff, &rep->index, xmlstr);
+	ei_decode_string_or_binary(rep->buff, &rep->index, size, xmlstr);
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got data %s after %d milliseconds!\n", xmlstr, i*10);
 

Modified: freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.h
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.h	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.h	Mon Jan 26 18:25:01 2009
@@ -200,6 +200,7 @@
 int ei_sendto(ei_cnode *ec, int fd, struct erlang_process *process, ei_x_buff *buf);
 void ei_hash_ref(erlang_ref *ref, char *output);
 int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2);
+int ei_decode_string_or_binary(char *buf, int *index, int maxlen, char *dst);
 switch_status_t initialise_ei(struct ei_cnode_s *ec);
 #define ei_encode_switch_event(_b, _e) ei_encode_switch_event_tag(_b, _e, "event")
 



More information about the Freeswitch-svn mailing list