From 5fb59d2ebd18907c537e5811b935f7d87582c15c Mon Sep 17 00:00:00 2001
From: Todd Freed <todfreed@amazon.com>
Date: Tue, 31 Jan 2017 17:36:34 -0800
Subject: [PATCH] coap: dont reorder options having the same option number

---
 scapy/contrib/coap.py  | 2 +-
 scapy/contrib/coap.uts | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/scapy/contrib/coap.py b/scapy/contrib/coap.py
index 5d03f885..fcadb6c9 100644
--- a/scapy/contrib/coap.py
+++ b/scapy/contrib/coap.py
@@ -178,7 +178,7 @@ class _CoAPOptsField(StrField):
                 opt_lst.append((coap_options[1][o[0]], o[1]))
             else:
                 opt_lst.append(o)
-        opt_lst.sort()
+        opt_lst.sort(key=lambda o:o[0])
 
         opts = _CoAPOpt(delta=opt_lst[0][0], opt_val=opt_lst[0][1])
         high_opt = opt_lst[0][0]
diff --git a/scapy/contrib/coap.uts b/scapy/contrib/coap.uts
index 915e5df5..aa83c339 100644
--- a/scapy/contrib/coap.uts
+++ b/scapy/contrib/coap.uts
@@ -29,6 +29,12 @@ str(CoAP(options=[("Uri-Query", "query")])) == '\x40\x00\x00\x00\xd5\x02\x71\x75
 = Extended option length
 str(CoAP(options=[("Location-Path", 'x' * 280)])) == '\x40\x00\x00\x00\x8e\x0b\x00' + '\x78' * 280
 
+= Options should be ordered by option number
+str(CoAP(options=[("Uri-Query", "b"),("Uri-Path","a")])) == '\x40\x00\x00\x00\xb1\x61\x41\x62'
+
+= Options of the same type should not be reordered
+str(CoAP(options=[("Uri-Path", "b"),("Uri-Path","a")])) == '\x40\x00\x00\x00\xb1\x62\x01\x61'
+
 + Test layer binding
 = Destination port
 p = UDP()/CoAP()
-- 
GitLab