PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/build/lib.linux-x86_64-2.7/pyroute2/protocols/__init__.py

https://bitbucket.org/mtajiki/pyroute2
Python | 245 lines | 218 code | 10 blank | 17 comment | 4 complexity | b6cf534e5ff3be3817a68a8fa81eabf1 MD5 | raw file
  1. import struct
  2. from socket import inet_ntop
  3. from socket import inet_pton
  4. from socket import AF_INET
  5. from pyroute2.common import basestring
  6. #
  7. # IEEE = 802.3 Ethernet magic constants. The frame sizes omit
  8. # the preamble and FCS/CRC (frame check sequence).
  9. #
  10. ETH_ALEN = 6 # Octets in one ethernet addr
  11. ETH_HLEN = 14 # Total octets in header.
  12. ETH_ZLEN = 60 # Min. octets in frame sans FCS
  13. ETH_DATA_LEN = 1500 # Max. octets in payload
  14. ETH_FRAME_LEN = 1514 # Max. octets in frame sans FCS
  15. ETH_FCS_LEN = 4 # Octets in the FCS
  16. #
  17. # These are the defined Ethernet Protocol ID's.
  18. #
  19. ETH_P_LOOP = 0x0060 # Ethernet Loopback packet
  20. ETH_P_PUP = 0x0200 # Xerox PUP packet
  21. ETH_P_PUPAT = 0x0201 # Xerox PUP Addr Trans packet
  22. ETH_P_IP = 0x0800 # Internet Protocol packet
  23. ETH_P_X25 = 0x0805 # CCITT X.25
  24. ETH_P_ARP = 0x0806 # Address Resolution packet
  25. ETH_P_BPQ = 0x08FF # G8BPQ AX.25 Ethernet Packet
  26. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  27. ETH_P_IEEEPUP = 0x0a00 # Xerox IEEE802.3 PUP packet
  28. ETH_P_IEEEPUPAT = 0x0a01 # Xerox IEEE802.3 PUP Addr Trans packet
  29. ETH_P_DEC = 0x6000 # DEC Assigned proto
  30. ETH_P_DNA_DL = 0x6001 # DEC DNA Dump/Load
  31. ETH_P_DNA_RC = 0x6002 # DEC DNA Remote Console
  32. ETH_P_DNA_RT = 0x6003 # DEC DNA Routing
  33. ETH_P_LAT = 0x6004 # DEC LAT
  34. ETH_P_DIAG = 0x6005 # DEC Diagnostics
  35. ETH_P_CUST = 0x6006 # DEC Customer use
  36. ETH_P_SCA = 0x6007 # DEC Systems Comms Arch
  37. ETH_P_TEB = 0x6558 # Trans Ether Bridging
  38. ETH_P_RARP = 0x8035 # Reverse Addr Res packet
  39. ETH_P_ATALK = 0x809B # Appletalk DDP
  40. ETH_P_AARP = 0x80F3 # Appletalk AARP
  41. ETH_P_8021Q = 0x8100 # = 802.1Q VLAN Extended Header
  42. ETH_P_IPX = 0x8137 # IPX over DIX
  43. ETH_P_IPV6 = 0x86DD # IPv6 over bluebook
  44. ETH_P_PAUSE = 0x8808 # IEEE Pause frames. See = 802.3 = 31B
  45. ETH_P_SLOW = 0x8809 # Slow Protocol. See = 802.3ad = 43B
  46. ETH_P_WCCP = 0x883E # Web-cache coordination protocol
  47. # defined in draft-wilson-wrec-wccp-v2-00.txt
  48. ETH_P_PPP_DISC = 0x8863 # PPPoE discovery messages
  49. ETH_P_PPP_SES = 0x8864 # PPPoE session messages
  50. ETH_P_MPLS_UC = 0x8847 # MPLS Unicast traffic
  51. ETH_P_MPLS_MC = 0x8848 # MPLS Multicast traffic
  52. ETH_P_ATMMPOA = 0x884c # MultiProtocol Over ATM
  53. ETH_P_LINK_CTL = 0x886c # HPNA, wlan link local tunnel
  54. ETH_P_ATMFATE = 0x8884 # Frame-based ATM Transport over Ethernet
  55. ETH_P_PAE = 0x888E # Port Access Entity (IEEE = 802.1X)
  56. ETH_P_AOE = 0x88A2 # ATA over Ethernet
  57. ETH_P_8021AD = 0x88A8 # = 802.1ad Service VLAN
  58. ETH_P_802_EX1 = 0x88B5 # = 802.1 Local Experimental = 1.
  59. ETH_P_TIPC = 0x88CA # TIPC
  60. ETH_P_8021AH = 0x88E7 # = 802.1ah Backbone Service Tag
  61. ETH_P_1588 = 0x88F7 # IEEE = 1588 Timesync
  62. ETH_P_FCOE = 0x8906 # Fibre Channel over Ethernet
  63. ETH_P_TDLS = 0x890D # TDLS
  64. ETH_P_FIP = 0x8914 # FCoE Initialization Protocol
  65. ETH_P_QINQ1 = 0x9100 # deprecated QinQ VLAN
  66. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  67. ETH_P_QINQ2 = 0x9200 # deprecated QinQ VLAN
  68. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  69. ETH_P_QINQ3 = 0x9300 # deprecated QinQ VLAN
  70. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  71. ETH_P_EDSA = 0xDADA # Ethertype DSA
  72. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  73. ETH_P_AF_IUCV = 0xFBFB # IBM af_iucv
  74. # ^^^ [ NOT AN OFFICIALLY REGISTERED ID ]
  75. #
  76. # Non DIX types. Won't clash for = 1500 types.
  77. #
  78. ETH_P_802_3 = 0x0001 # Dummy type for = 802.3 frames
  79. ETH_P_AX25 = 0x0002 # Dummy protocol id for AX.25
  80. ETH_P_ALL = 0x0003 # Every packet (be careful!!!)
  81. ETH_P_802_2 = 0x0004 # = 802.2 frames
  82. ETH_P_SNAP = 0x0005 # Internal only
  83. ETH_P_DDCMP = 0x0006 # DEC DDCMP: Internal only
  84. ETH_P_WAN_PPP = 0x0007 # Dummy type for WAN PPP frames*/
  85. ETH_P_PPP_MP = 0x0008 # Dummy type for PPP MP frames
  86. ETH_P_LOCALTALK = 0x0009 # Localtalk pseudo type
  87. ETH_P_CAN = 0x000C # Controller Area Network
  88. ETH_P_PPPTALK = 0x0010 # Dummy type for Atalk over PPP*/
  89. ETH_P_TR_802_2 = 0x0011 # = 802.2 frames
  90. ETH_P_MOBITEX = 0x0015 # Mobitex (kaz@cafe.net)
  91. ETH_P_CONTROL = 0x0016 # Card specific control frames
  92. ETH_P_IRDA = 0x0017 # Linux-IrDA
  93. ETH_P_ECONET = 0x0018 # Acorn Econet
  94. ETH_P_HDLC = 0x0019 # HDLC frames
  95. ETH_P_ARCNET = 0x001A # = 1A for ArcNet :-)
  96. ETH_P_DSA = 0x001B # Distributed Switch Arch.
  97. ETH_P_TRAILER = 0x001C # Trailer switch tagging
  98. ETH_P_PHONET = 0x00F5 # Nokia Phonet frames
  99. ETH_P_IEEE802154 = 0x00F6 # IEEE802.15.4 frame
  100. ETH_P_CAIF = 0x00F7 # ST-Ericsson CAIF protocol
  101. class msg(dict):
  102. buf = None
  103. data_len = None
  104. fields = ()
  105. _fields_names = ()
  106. types = {'uint8': 'B',
  107. 'uint16': 'H',
  108. 'uint32': 'I',
  109. 'be16': '>H',
  110. 'ip4addr': {'format': '4s',
  111. 'decode': lambda x: inet_ntop(AF_INET, x),
  112. 'encode': lambda x: [inet_pton(AF_INET, x)]},
  113. 'l2addr': {'format': '6B',
  114. 'decode': lambda x: ':'.join(['%x' % i for i in x]),
  115. 'encode': lambda x: [int(i, 16) for i in
  116. x.split(':')]},
  117. 'l2paddr': {'format': '6B10s',
  118. 'decode': lambda x: ':'.join(['%x' % i for i in
  119. x[:6]]),
  120. 'encode': lambda x: [int(i, 16) for i in
  121. x.split(':')] + [10 * b'\x00']}}
  122. def __init__(self, content=None, buf=b'', offset=0, value=None):
  123. content = content or {}
  124. dict.__init__(self, content)
  125. self.buf = buf
  126. self.offset = offset
  127. self.value = value
  128. self._register_fields()
  129. def _register_fields(self):
  130. self._fields_names = tuple([x[0] for x in self.fields])
  131. def _get_routine(self, mode, fmt):
  132. fmt = self.types.get(fmt, fmt)
  133. if isinstance(fmt, dict):
  134. return (fmt['format'],
  135. fmt.get(mode, lambda x: x))
  136. else:
  137. return (fmt, lambda x: x)
  138. def reset(self):
  139. self.buf = b''
  140. def decode(self):
  141. self._register_fields()
  142. for field in self.fields:
  143. name, sfmt = field[:2]
  144. fmt, routine = self._get_routine('decode', sfmt)
  145. size = struct.calcsize(fmt)
  146. value = struct.unpack(fmt, self.buf[self.offset:
  147. self.offset + size])
  148. if len(value) == 1:
  149. value = value[0]
  150. if isinstance(value, basestring) and sfmt[-1] == 's':
  151. value = value[:value.find(b'\x00')]
  152. self[name] = routine(value)
  153. self.offset += size
  154. return self
  155. def encode(self):
  156. self._register_fields()
  157. for field in self.fields:
  158. name, fmt = field[:2]
  159. default = b'\x00' if len(field) <= 2 else field[2]
  160. fmt, routine = self._get_routine('encode', fmt)
  161. # special case: string
  162. if fmt == 'string':
  163. self.buf += routine(self[name])[0]
  164. else:
  165. size = struct.calcsize(fmt)
  166. if self[name] is None:
  167. if not isinstance(default, basestring):
  168. self.buf += struct.pack(fmt, default)
  169. else:
  170. self.buf += default * (size // len(default))
  171. else:
  172. value = routine(self[name])
  173. if not isinstance(value, (set, tuple, list)):
  174. value = [value]
  175. self.buf += struct.pack(fmt, *value)
  176. return self
  177. def __getitem__(self, key):
  178. try:
  179. return dict.__getitem__(self, key)
  180. except KeyError:
  181. if key in self._fields_names:
  182. return None
  183. raise
  184. class ethmsg(msg):
  185. fields = (('dst', 'l2addr'),
  186. ('src', 'l2addr'),
  187. ('type', 'be16'))
  188. class ip6msg(msg):
  189. fields = (('version', 'uint8', 6 << 4),
  190. ('_flow0', 'uint8'),
  191. ('_flow1', 'uint8'),
  192. ('_flow2', 'uint8'),
  193. ('plen', 'uin16'),
  194. ('next_header', 'uint8'),
  195. ('hop_limit', 'uint8'),
  196. ('src', 'ip6addr'),
  197. ('dst', 'ip6addr'))
  198. class ip4msg(msg):
  199. fields = (('verlen', 'uint8', 0x45),
  200. ('dsf', 'uint8'),
  201. ('len', 'be16'),
  202. ('id', 'be16'),
  203. ('flags', 'uint16'),
  204. ('ttl', 'uint8', 128),
  205. ('proto', 'uint8'),
  206. ('csum', 'be16'),
  207. ('src', 'ip4addr'),
  208. ('dst', 'ip4addr'))
  209. class udp4_pseudo_header(msg):
  210. fields = (('src', 'ip4addr'),
  211. ('dst', 'ip4addr'),
  212. ('pad', 'uint8'),
  213. ('proto', 'uint8', 17),
  214. ('len', 'be16'))
  215. class udpmsg(msg):
  216. fields = (('sport', 'be16'),
  217. ('dport', 'be16'),
  218. ('len', 'be16'),
  219. ('csum', 'be16'))