summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-08-27 11:37:29 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-08-28 05:58:25 +0900
commit2814763bb86cc136fa7882446cd14e9e6b2e5339 (patch)
tree6912076c94564becaa40f80f7434d422ee35fc41
parent0153f492ea0af63281c70d4d309b1b36f125454e (diff)
packet lib: add udp data transmit and checksum support
- UDP supports sending data. - UDP calculates the checksum if not given. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/packet/udp.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/ryu/lib/packet/udp.py b/ryu/lib/packet/udp.py
index 05973c70..6fc84fc1 100644
--- a/ryu/lib/packet/udp.py
+++ b/ryu/lib/packet/udp.py
@@ -14,7 +14,10 @@
# limitations under the License.
import struct
+import socket
from . import packet_base
+from . import packet_utils
+import ipv4
class udp(packet_base.PacketBase):
@@ -40,5 +43,14 @@ class udp(packet_base.PacketBase):
def serialize(self, payload, prev):
if self.length == 0:
self.length = struct.calcsize(udp._PACK_STR) + len(payload)
- return struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
- self.length, self.csum)
+ h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
+ self.length, self.csum)
+ if self.csum == 0:
+ ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 17, self.length)
+ f = ph + h + payload
+ if len(f) % 2:
+ f += '\0'
+ self.csum = socket.htons(packet_utils.checksum(f))
+ h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
+ self.length, self.csum)
+ return h