summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-08-27 11:37:31 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-08-28 05:58:25 +0900
commit4d00bc802543dc0585af01ef88d0b33bb3f6f6aa (patch)
tree7707b57adc3198a2c34fc89c4ed8ec31a1e2f76d
parent1241dafde4e64644e6d8a38cbec97d84d3789c3b (diff)
packet lib: add tcp support
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/packet/ipv4.py2
-rw-r--r--ryu/lib/packet/tcp.py65
2 files changed, 67 insertions, 0 deletions
diff --git a/ryu/lib/packet/ipv4.py b/ryu/lib/packet/ipv4.py
index cb5bc4e4..bd7b6330 100644
--- a/ryu/lib/packet/ipv4.py
+++ b/ryu/lib/packet/ipv4.py
@@ -17,6 +17,7 @@ import struct
from . import packet_base
from . import packet_utils
from . import udp
+from . import tcp
from ryu.ofproto import inet
@@ -70,4 +71,5 @@ class ipv4(packet_base.PacketBase):
struct.pack_into('H', hdr, 10, self.csum)
return hdr
+ipv4.register_packet_type(tcp.tcp, inet.IPPROTO_TCP)
ipv4.register_packet_type(udp.udp, inet.IPPROTO_UDP)
diff --git a/ryu/lib/packet/tcp.py b/ryu/lib/packet/tcp.py
new file mode 100644
index 00000000..975bfcca
--- /dev/null
+++ b/ryu/lib/packet/tcp.py
@@ -0,0 +1,65 @@
+# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import struct
+import socket
+from . import packet_base
+from . import packet_utils
+import ipv4
+
+
+class tcp(packet_base.PacketBase):
+ _PACK_STR = '!HHIIBBHHH'
+
+ def __init__(self, src_port, dst_port, seq, ack, offset,
+ bits, window_size, csum, urgent):
+ super(tcp, self).__init__()
+ self.src_port = src_port
+ self.dst_port = dst_port
+ self.seq = seq
+ self.ack = ack
+ self.offset = offset
+ self.bits = bits
+ self.window_size = window_size
+ self.csum = csum
+ self.urgent = urgent
+ self.length = self.offset * 4
+
+ @classmethod
+ def parser(cls, buf):
+ (src_port, dst_port, seq, ack, offset, bits, window_size,
+ csum, urgent) = struct.unpack_from(cls._PACK_STR, buf)
+ offset = offset >> 4
+ bits = bits & 0x3f
+ msg = cls(src_port, dst_port, seq, ack, offset, bits,
+ window_size, csum, urgent)
+ return msg, None
+
+ def serialize(self, payload, prev):
+ offset = self.offset << 4
+ h = struct.pack(tcp._PACK_STR, self.src_port, self.dst_port,
+ self.seq, self.ack, offset, self.bits,
+ self.window_size, self.csum, self.urgent)
+ if self.csum == 0:
+ length = self.length + len(payload)
+ ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 6, length)
+ f = ph + h + payload
+ if len(f) % 2:
+ f += '\0'
+ self.csum = socket.htons(packet_utils.checksum(f))
+ h = struct.pack(tcp._PACK_STR, self.src_port, self.dst_port,
+ self.seq, self.ack, offset, self.bits,
+ self.window_size, self.csum, self.urgent)
+ return h