[Asterisk-code-review] chan_unistim: Fix RTP port byte order for big-endian arch (...asterisk[17])

George Joseph asteriskteam at digium.com
Wed Aug 28 06:41:26 CDT 2019


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/12749 )

Change subject: chan_unistim: Fix RTP port byte order for big-endian arch
......................................................................

chan_unistim: Fix RTP port byte order for big-endian arch

This patch fixes one-way oudio that users expirienced on
big-endian architechtires. RTP port number bytes was stored
in improper order and phone sent RTP to wrong RTP port.

Reported-by: Andrey Ionov
Change-Id: I9a9ca7f26e31a67bbbceff12923baa10dfb8a3be
---
M channels/chan_unistim.c
1 file changed, 25 insertions(+), 43 deletions(-)

Approvals:
  Sean Bright: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/channels/chan_unistim.c b/channels/chan_unistim.c
index c1328c1..c8dfaff 100644
--- a/channels/chan_unistim.c
+++ b/channels/chan_unistim.c
@@ -62,6 +62,7 @@
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/rtp_engine.h"
+#include "asterisk/unaligned.h"
 #include "asterisk/netsock2.h"
 #include "asterisk/acl.h"
 #include "asterisk/callerid.h"
@@ -575,7 +576,7 @@
 	{ 0x16, 0x06, 0x1b, 0x00, 0x00, 0x05 };
 static const unsigned char packet_send_stream_based_tone_single_freq[] =
 	{ 0x16, 0x06, 0x1d, 0x00, 0x01, 0xb8 };
-static const unsigned char packet_send_stream_based_tone_dial_freq[] =
+static const unsigned char packet_send_stream_based_tone_dual_freq[] =
 	{ 0x16, 0x08, 0x1d, 0x00, 0x01, 0xb8, 0x01, 0x5e };
 static const unsigned char packet_send_select_output[] =
 	{ 0x16, 0x06, 0x32, 0xc0, 0x01, 0x00 };
@@ -1208,19 +1209,16 @@
 	if (!tone2) {
 		memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_single_freq,
 			   sizeof(packet_send_stream_based_tone_single_freq));
-		buffsend[10] = (tone1 & 0xff00) >> 8;
-		buffsend[11] = (tone1 & 0x00ff);
+		put_unaligned_uint16(&buffsend[10], htons(tone1));
 		send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_single_freq), buffsend,
 				   pte);
 	} else {
 		tone2 *= 8;
-		memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dial_freq,
-			   sizeof(packet_send_stream_based_tone_dial_freq));
-		buffsend[10] = (tone1 & 0xff00) >> 8;
-		buffsend[11] = (tone1 & 0x00ff);
-		buffsend[12] = (tone2 & 0xff00) >> 8;
-		buffsend[13] = (tone2 & 0x00ff);
-		send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dial_freq), buffsend,
+		memcpy(buffsend + SIZE_HEADER, packet_send_stream_based_tone_dual_freq,
+			   sizeof(packet_send_stream_based_tone_dual_freq));
+		put_unaligned_uint16(&buffsend[10], htons(tone1));
+		put_unaligned_uint16(&buffsend[12], htons(tone2));
+		send_client(SIZE_HEADER + sizeof(packet_send_stream_based_tone_dual_freq), buffsend,
 				   pte);
 	}
 
@@ -2747,7 +2745,7 @@
 		   sizeof(packet_send_jitter_buffer_conf));
 	send_client(SIZE_HEADER + sizeof(packet_send_jitter_buffer_conf), buffsend, pte);
 	if (pte->device->rtp_method != 0) {
-		uint16_t rtcpsin_port = htons(us.sin_port) + 1; /* RTCP port is RTP + 1 */
+		uint16_t rtcpsin_port = ntohs(us.sin_port) + 1; /* RTCP port is RTP + 1 */
 
 		if (unistimdebug) {
 			ast_verb(0, "Sending OpenAudioStreamTX using method #%d\n", pte->device->rtp_method);
@@ -2761,20 +2759,14 @@
 		}
 		if (pte->device->rtp_method != 2) {
 			memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
-			buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
-			buffsend[21] = (htons(sin.sin_port) & 0x00ff);
-			buffsend[23] = (rtcpsin_port & 0x00ff);
-			buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
-			buffsend[25] = (us.sin_port & 0xff00) >> 8;
-			buffsend[24] = (us.sin_port & 0x00ff);
-			buffsend[27] = (rtcpsin_port & 0x00ff);
-			buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
+			put_unaligned_uint16(&buffsend[20], sin.sin_port);
+			put_unaligned_uint16(&buffsend[22], htons(rtcpsin_port));
+			put_unaligned_uint16(&buffsend[24], us.sin_port);
+			put_unaligned_uint16(&buffsend[26], htons(rtcpsin_port));
 		} else {
 			memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
-			buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
-			buffsend[16] = (htons(sin.sin_port) & 0x00ff);
-			buffsend[20] = (us.sin_port & 0xff00) >> 8;
-			buffsend[19] = (us.sin_port & 0x00ff);
+			put_unaligned_uint16(&buffsend[15], sin.sin_port);
+			put_unaligned_uint16(&buffsend[19], us.sin_port);
 		}
 		buffsend[11] = codec; /* rx */
 		buffsend[12] = codec; /* tx */
@@ -2792,20 +2784,14 @@
 		}
 		if (pte->device->rtp_method != 2) {
 			memcpy(buffsend + 28, &public.sin_addr, sizeof(public.sin_addr));
-			buffsend[20] = (htons(sin.sin_port) & 0xff00) >> 8;
-			buffsend[21] = (htons(sin.sin_port) & 0x00ff);
-			buffsend[23] = (rtcpsin_port & 0x00ff);
-			buffsend[22] = (rtcpsin_port & 0xff00) >> 8;
-			buffsend[25] = (us.sin_port & 0xff00) >> 8;
-			buffsend[24] = (us.sin_port & 0x00ff);
-			buffsend[27] = (rtcpsin_port & 0x00ff);
-			buffsend[26] = (rtcpsin_port & 0xff00) >> 8;
+			put_unaligned_uint16(&buffsend[20], sin.sin_port);
+			put_unaligned_uint16(&buffsend[22], htons(rtcpsin_port));
+			put_unaligned_uint16(&buffsend[24], us.sin_port);
+			put_unaligned_uint16(&buffsend[26], htons(rtcpsin_port));
 		} else {
 			memcpy(buffsend + 23, &public.sin_addr, sizeof(public.sin_addr));
-			buffsend[15] = (htons(sin.sin_port) & 0xff00) >> 8;
-			buffsend[16] = (htons(sin.sin_port) & 0x00ff);
-			buffsend[20] = (us.sin_port & 0xff00) >> 8;
-			buffsend[19] = (us.sin_port & 0x00ff);
+			put_unaligned_uint16(&buffsend[15], sin.sin_port);
+			put_unaligned_uint16(&buffsend[19], us.sin_port);
 		}
 		buffsend[11] = codec; /* rx */
 		buffsend[12] = codec; /* tx */
@@ -2820,11 +2806,9 @@
 		memcpy(buffsend + SIZE_HEADER, packet_send_call, sizeof(packet_send_call));
 		memcpy(buffsend + 53, &public.sin_addr, sizeof(public.sin_addr));
 		/* Destination port when sending RTP */
-		buffsend[49] = (us.sin_port & 0x00ff);
-		buffsend[50] = (us.sin_port & 0xff00) >> 8;
+		put_unaligned_uint16(&buffsend[49], us.sin_port);
 		/* Destination port when sending RTCP */
-		buffsend[52] = (rtcpsin_port & 0x00ff);
-		buffsend[51] = (rtcpsin_port & 0xff00) >> 8;
+		put_unaligned_uint16(&buffsend[51], htons(rtcpsin_port));
 		/* Codec */
 		buffsend[40] = codec;
 		buffsend[41] = codec;
@@ -2841,10 +2825,8 @@
 				ast_format_get_name(ast_channel_readformat(sub->owner)));
 		}
 		/* Source port for transmit RTP and Destination port for receiving RTP */
-		buffsend[45] = (htons(sin.sin_port) & 0xff00) >> 8;
-		buffsend[46] = (htons(sin.sin_port) & 0x00ff);
-		buffsend[47] = (rtcpsin_port & 0xff00) >> 8;
-		buffsend[48] = (rtcpsin_port & 0x00ff);
+		put_unaligned_uint16(&buffsend[45], sin.sin_port);
+		put_unaligned_uint16(&buffsend[47], htons(rtcpsin_port));
 		send_client(SIZE_HEADER + sizeof(packet_send_call), buffsend, pte);
 	}
 }

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/12749
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 17
Gerrit-Change-Id: I9a9ca7f26e31a67bbbceff12923baa10dfb8a3be
Gerrit-Change-Number: 12749
Gerrit-PatchSet: 2
Gerrit-Owner: Igor Goncharovsky <igor.goncharovsky at gmail.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190828/7956356d/attachment-0001.html>


More information about the asterisk-code-review mailing list