[pulseaudio-discuss] [PATCH v7 19/33] raop: fix sequence number overflow

Hajime Fujita crisp.fujita at gmail.com
Sun Nov 6 18:54:13 UTC 2016


From: Colin Leroy <colin at colino.net>

Wrap sequence number when we reach uint16_t's max 0xFFFF.
---
 src/modules/raop/raop-client.c        | 14 ++++++++++++--
 src/modules/raop/raop-packet-buffer.c | 17 ++++++++++++++++-
 src/modules/raop/raop-packet-buffer.h |  1 +
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/modules/raop/raop-client.c b/src/modules/raop/raop-client.c
index 81a68a0..8dc05a4 100644
--- a/src/modules/raop/raop-client.c
+++ b/src/modules/raop/raop-client.c
@@ -316,7 +316,12 @@ static size_t build_tcp_audio_packet(pa_raop_client *c, pa_memchunk *block, pa_m
     buffer += packet->index / sizeof(uint32_t);
     raw += block->index;
 
-    c->seq++;
+    if (c->seq == 0xFFFF) {
+        pa_log_debug("wrapping sequence number");
+        c->seq = pa_raop_packet_buffer_wrap_seq(c->pbuf, c->seq);
+    } else
+        c->seq++;
+
     memcpy(buffer, tcp_audio_header, sizeof(tcp_audio_header));
     buffer[1] |= htonl((uint32_t) c->seq);
     buffer[2] = htonl(c->rtptime);
@@ -421,7 +426,12 @@ static size_t build_udp_audio_packet(pa_raop_client *c, pa_memchunk *block, pa_m
     }
 
     c->rtptime += length / 4;
-    c->seq++;
+
+    if (c->seq == 0xFFFF) {
+        pa_log_debug("wrapping sequence number");
+        c->seq = pa_raop_packet_buffer_wrap_seq(c->pbuf, c->seq);
+    } else
+        c->seq++;
 
     pa_memblock_release(block->memblock);
 
diff --git a/src/modules/raop/raop-packet-buffer.c b/src/modules/raop/raop-packet-buffer.c
index 06e3125..4c45d18 100644
--- a/src/modules/raop/raop-packet-buffer.c
+++ b/src/modules/raop/raop-packet-buffer.c
@@ -88,13 +88,28 @@ void pa_raop_packet_buffer_reset(pa_raop_packet_buffer *pb, uint16_t seq) {
     }
 }
 
+uint16_t pa_raop_packet_buffer_wrap_seq(pa_raop_packet_buffer *pb, uint16_t seq) {
+    int seq_shift;
+
+    pa_assert(pb);
+
+    if (seq > pb->seq)
+        seq_shift = pb->seq - 1;
+    else
+        seq_shift = seq;
+
+    pb->seq -= seq_shift;
+
+    return seq - seq_shift;
+
+}
+
 pa_memchunk *pa_raop_packet_buffer_get(pa_raop_packet_buffer *pb, uint16_t seq, const size_t size) {
     pa_memchunk *packet = NULL;
     size_t delta, i;
 
     pa_assert(pb);
     pa_assert(pb->packets);
-    pa_assert(seq > 0);
 
     if (seq == pb->seq)
         packet = &pb->packets[pb->pos];
diff --git a/src/modules/raop/raop-packet-buffer.h b/src/modules/raop/raop-packet-buffer.h
index b8c7617..cb2bfdf 100644
--- a/src/modules/raop/raop-packet-buffer.h
+++ b/src/modules/raop/raop-packet-buffer.h
@@ -35,4 +35,5 @@ void pa_raop_packet_buffer_free(pa_raop_packet_buffer *pb);
 void pa_raop_packet_buffer_reset(pa_raop_packet_buffer *pb, uint16_t seq);
 pa_memchunk *pa_raop_packet_buffer_get(pa_raop_packet_buffer *pb, uint16_t seq, const size_t size);
 
+uint16_t pa_raop_packet_buffer_wrap_seq(pa_raop_packet_buffer *pb, uint16_t seq);
 #endif
-- 
2.9.3



More information about the pulseaudio-discuss mailing list