1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from . import packet_base
19 from . import packet_utils
27 from . import in_proto as inet
28 from ryu.lib import addrconv
31 IPV4_ADDRESS_PACK_STR = '!I'
32 IPV4_ADDRESS_LEN = struct.calcsize(IPV4_ADDRESS_PACK_STR)
33 IPV4_PSEUDO_HEADER_PACK_STR = '!4s4s2xHH'
36 class ipv4(packet_base.PacketBase):
37 """IPv4 (RFC 791) header encoder/decoder class.
39 NOTE: When decoding, this implementation tries to decode the upper
40 layer protocol even for a fragmented datagram. It isn't likely
41 what a user would want.
43 An instance has the following attributes at least.
44 Most of them are same to the on-wire counterparts but in host byte order.
45 IPv4 addresses are represented as a string like '192.0.2.1'.
46 __init__ takes the corresponding args in this order.
48 ============== ======================================== ==================
49 Attribute Description Example
50 ============== ======================================== ==================
54 total_length Total Length
55 (0 means automatically-calculate
57 identification Identification
59 offset Fragment Offset
63 (Ignored and automatically-calculated
65 src Source Address '192.0.2.1'
66 dst Destination Address '192.0.2.2'
67 option A bytearray which contains the entire
68 Options, or None for no Options
69 ============== ======================================== ==================
72 _PACK_STR = '!BBHHHBBH4s4s'
73 _MIN_LEN = struct.calcsize(_PACK_STR)
80 def __init__(self, version=4, header_length=5, tos=0,
81 total_length=0, identification=0, flags=0,
82 offset=0, ttl=255, proto=0, csum=0,
86 super(ipv4, self).__init__()
87 self.version = version
88 self.header_length = header_length
90 self.total_length = total_length
91 self.identification = identification
102 return self.header_length * 4
105 def parser(cls, buf):
106 (version, tos, total_length, identification, flags, ttl, proto, csum,
107 src, dst) = struct.unpack_from(cls._PACK_STR, buf)
108 header_length = version & 0xf
109 version = version >> 4
110 offset = flags & ((1 << 13) - 1)
112 length = header_length * 4
113 if length > ipv4._MIN_LEN:
114 option = buf[ipv4._MIN_LEN:length]
117 msg = cls(version, header_length, tos, total_length, identification,
118 flags, offset, ttl, proto, csum,
119 addrconv.ipv4.bin_to_text(src),
120 addrconv.ipv4.bin_to_text(dst), option)
122 return msg, ipv4.get_packet_type(proto), buf[length:total_length]
124 def serialize(self, payload, prev):
126 hdr = bytearray(length)
127 version = self.version << 4 | self.header_length
128 flags = self.flags << 13 | self.offset
129 if self.total_length == 0:
130 self.total_length = self.header_length * 4 + len(payload)
131 struct.pack_into(ipv4._PACK_STR, hdr, 0, version, self.tos,
132 self.total_length, self.identification, flags,
133 self.ttl, self.proto, 0,
134 addrconv.ipv4.text_to_bin(self.src),
135 addrconv.ipv4.text_to_bin(self.dst))
138 assert (length - ipv4._MIN_LEN) >= len(self.option)
139 hdr[ipv4._MIN_LEN:ipv4._MIN_LEN + len(self.option)] = self.option
141 self.csum = packet_utils.checksum(hdr)
142 struct.pack_into('!H', hdr, 10, self.csum)
146 ipv4.register_packet_type(icmp.icmp, inet.IPPROTO_ICMP)
147 ipv4.register_packet_type(igmp.igmp, inet.IPPROTO_IGMP)
148 ipv4.register_packet_type(tcp.tcp, inet.IPPROTO_TCP)
149 ipv4.register_packet_type(udp.udp, inet.IPPROTO_UDP)
150 ipv4.register_packet_type(sctp.sctp, inet.IPPROTO_SCTP)
151 ipv4.register_packet_type(ospf.ospf, inet.IPPROTO_OSPF)
152 ipv4.register_packet_type(gre.gre, inet.IPPROTO_GRE)