Changeset 3608
- Timestamp:
- 08/23/08 16:48:00 (5 years ago)
- Location:
- branch/UMPA/umpa/protocols
- Files:
-
- 8 modified
-
ICMP.py (modified) (5 diffs)
-
IP.py (modified) (12 diffs)
-
Payload.py (modified) (6 diffs)
-
TCP.py (modified) (10 diffs)
-
UDP.py (modified) (7 diffs)
-
_fields.py (modified) (4 diffs)
-
_layer4.py (modified) (4 diffs)
-
_protocols.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branch/UMPA/umpa/protocols/ICMP.py
r3540 r3608 19 19 # along with this library; if not, write to the Free Software Foundation, 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 """ 23 This module contains ICMP (Internet Control Message Protocol) protocol 24 implementation. 25 """ 21 26 22 27 from umpa.protocols import _consts … … 282 287 283 288 class _HChecksum(_fields.IntField): 289 """ 290 A checksum of the ICMP protocol. 291 """ 292 284 293 bits = 16 285 294 auto = True … … 291 300 292 301 class ICMP(_protocols.Protocol): 302 """ 303 Internet Control Message Protocol implementation. 304 305 It the most common protocol in the Internet on fourth layer 306 of the OSI model. 307 """ 308 293 309 layer = 4 294 310 protocol_id = _consts.PROTOCOL_ICMP … … 298 314 299 315 def __init__(self, **kw): 316 """ 317 Create a new ICMP(). 318 319 @param **kw: pass to super-constructor. 320 """ 321 300 322 raise NotImplementedError("not finished yet") 301 323 fields_list = [ _HType("Type"), … … 307 329 308 330 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 331 """ 332 Handle with fields before calling fillout() for them. 333 334 @type raw_value: C{int} 335 @param raw_value: currently raw value for the packet. 336 337 @type bit: C{int} 338 @param bit: currently length of the protocol. 339 340 @type protocol_container: C{tuple} 341 @param protocol_container: tuple of protocols included in the packet. 342 343 @type protocol_bits: C{int} 344 @param protocol_bits: currently length of the packet. 345 346 @return: C{raw_value, bit} 347 """ 348 309 349 return raw_value, bit 310 350 311 351 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 352 """ 353 Handle with fields after calling fillout() for them. 354 355 @type raw_value: C{int} 356 @param raw_value: currently raw value for the packet. 357 358 @type bit: C{int} 359 @param bit: currently length of the protocol. 360 361 @type protocol_container: C{tuple} 362 @param protocol_container: tuple of protocols included in the packet. 363 364 @type protocol_bits: C{int} 365 @param protocol_bits: currently length of the packet. 366 367 @return: C{raw_value, bit} 368 """ 369 312 370 return raw_value, bit 313 371 -
branch/UMPA/umpa/protocols/IP.py
r3479 r3608 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 """ 23 This module contains IP (Internet Protocol) protocol implementation. 24 """ 25 22 26 from umpa.protocols import _consts 23 27 from umpa.protocols import _fields … … 27 31 28 32 class _HVersion(_fields.EnumField): 29 """The Version field indicates the format of the internet header. 30 31 See RFC 791 for more. 32 """ 33 """ 34 The Version field indicates the format of the internet header. 35 36 See RFC 791 for more. 37 """ 38 33 39 bits = 4 34 40 auto = True … … 41 47 42 48 def _generate_value(self): 49 """ 50 Generate value for undefined field yet. 51 52 @return: auto-generated value of the field. 53 """ 54 43 55 return _consts.IPVERSION_4 44 56 45 57 class _HIHL(_fields.SpecialIntField): 46 """Internet Header Length is the length of the internet header in 32 bit 58 """ 59 Internet Header Length is the length of the internet header in 32 bit 47 60 words, and thus points to the beginning of the data. 48 61 49 62 See RFC 791 for more. 50 63 """ 64 51 65 bits = 4 52 66 auto = True 53 def _generate_value(self): 67 68 def _generate_value(self): 69 """ 70 Generate value for undefined field yet. 71 72 @return: auto-generated value of the field. 73 """ 74 54 75 return 5 + self._tmp_value / 32 # 5 is a minimum value (see RFC 791) 55 76 56 77 class _HTotalLength(_fields.SpecialIntField): 57 """Total Length is the length of the datagram, measured in octets, 78 """ 79 Total Length is the length of the datagram, measured in octets, 58 80 including internet header and data. 59 81 60 82 See RFC 791 for more. 61 83 """ 84 62 85 bits = 16 63 86 auto = True 64 def _generate_value(self): 87 88 def _generate_value(self): 89 """ 90 Generate value for undefined field yet. 91 92 @return: auto-generated value of the field. 93 """ 94 65 95 return self._tmp_value / _consts.BYTE 66 96 67 97 class _HIdentification(_fields.IntField): 68 """An identifying value assigned by the sender to aid in assembling the 98 """ 99 An identifying value assigned by the sender to aid in assembling the 69 100 fragments of a datagram. 70 101 71 102 See RFC 791 for more. 72 103 """ 104 73 105 bits = 16 74 106 auto = True 75 def _generate_value(self): 107 108 def _generate_value(self): 109 """ 110 Generate value for undefined field yet. 111 112 @return: auto-generated value of the field. 113 """ 114 76 115 # TODO: implementation of fragmentation 77 116 # otherwise we can simple return 0 ;-) … … 79 118 80 119 class _HFragmentOffset(_fields.IntField): 81 """This field indicates where in the datagram this fragment belongs. 82 83 See RFC 791 for more. 84 """ 120 """ 121 This field indicates where in the datagram this fragment belongs. 122 123 See RFC 791 for more. 124 """ 125 85 126 bits = 13 86 127 auto = True 87 def _generate_value(self): 128 129 def _generate_value(self): 130 """ 131 Generate value for undefined field yet. 132 133 @return: auto-generated value of the field. 134 """ 135 88 136 # TODO: implementation of fragmentation 89 137 # otherwise we can simple return 0 ;-) … … 91 139 92 140 class _HTTL(_fields.IntField): 93 """This field indicates the maximum time the datagram is allowed to 141 """ 142 This field indicates the maximum time the datagram is allowed to 94 143 remain in the internet system. 95 144 96 145 See RFC 791 for more. 97 146 """ 147 98 148 bits = 8 99 149 auto = True 100 def _generate_value(self): 150 151 def _generate_value(self): 152 """ 153 Generate value for undefined field yet. 154 155 @return: auto-generated value of the field. 156 """ 157 101 158 # TODO: checking platform to get correct value of TTL 102 159 # unfortunately, there isn't any official document which described … … 106 163 107 164 def ttl(self, name): 108 """To set correct value of TTL for following platforms: 165 """ 166 Set TTL field to default value of the passed platform. 167 168 Set correct value of TTL for the following platforms: 109 169 AIX, DEC, FREEBSD, HPUX, IRIX, LINUX, MACOS, OS2, SOLARIS, 110 170 SUNOS, ULTRIX, WINDOWS. 111 171 112 name argument can be pass as shown above or as TTL_NAME 172 @type name: C{str} 173 @param name: name of the platform (from the list above, TTL_ prefix 174 also accepted. 113 175 """ 114 176 … … 118 180 119 181 class _HProtocol(_fields.SpecialIntField, _fields.EnumField): 120 """This field indicates the next level protocol used in the data portion 182 """ 183 This field indicates the next level protocol used in the data portion 121 184 of the internet datagram. 122 185 123 186 See RFC 791 for more. 124 187 """ 188 125 189 bits = 8 126 190 auto = True … … 157 221 158 222 def _generate_value(self): 223 """ 224 Generate value for undefined field yet. 225 226 @return: auto-generated value of the field. 227 """ 228 159 229 return self._tmp_value 160 230 161 231 class _HHeaderChecksum(_fields.IntField): 162 """A checksum on the header only. 163 164 See RFC 791 for more. 165 """ 232 """ 233 A checksum of the header only. 234 235 See RFC 791 for more. 236 """ 237 166 238 bits = 16 167 239 auto = True 168 def _generate_value(self): 240 241 def _generate_value(self): 242 """ 243 Generate value for undefined field yet. 244 245 @return: auto-generated value of the field. 246 """ 247 169 248 return 0 # HeaderChecksum field should be initialized by 0 170 249 … … 172 251 173 252 class IP(_protocols.Protocol): 174 """This is Internet Protocol. 253 """ 254 Internet Protocol implementation. 255 175 256 The main protocol in third layer of OSI model. 176 257 """ 258 177 259 layer = 3 # layer of OSI 178 260 protocol_id = _consts.ETHERTYPE_IP … … 186 268 187 269 def __init__(self, **kw): 270 """ 271 Create a new IP(). 272 273 @param **kw: pass to super-constructor. 274 """ 275 188 276 tos = ('precedence0','precedence1', 'precedence2', 'delay', 189 277 'throughput', 'relibility', 'reserved0', 'reserved1') … … 237 325 238 326 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 327 """ 328 Handle with fields before calling fillout() for them. 329 330 Set Padding field, calculate header and total length, set protocol of the upper 331 layer. 332 333 @type raw_value: C{int} 334 @param raw_value: currently raw value for the packet. 335 336 @type bit: C{int} 337 @param bit: currently length of the protocol. 338 339 @type protocol_container: C{tuple} 340 @param protocol_container: tuple of protocols included in the packet. 341 342 @type protocol_bits: C{int} 343 @param protocol_bits: currently length of the packet. 344 345 @return: C{raw_value, bit} 346 """ 347 239 348 # Padding 240 349 self._get_field('_padding')._tmp_value = \ … … 265 374 266 375 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 376 """ 377 Handle with fields after calling fillout() for them. 378 379 Calculate header checksum. 380 381 @type raw_value: C{int} 382 @param raw_value: currently raw value for the packet. 383 384 @type bit: C{int} 385 @param bit: currently length of the protocol. 386 387 @type protocol_container: C{tuple} 388 @param protocol_container: tuple of protocols included in the packet. 389 390 @type protocol_bits: C{int} 391 @param protocol_bits: currently length of the packet. 392 393 @return: C{raw_value, bit} 394 """ 395 267 396 # Header Checksum 268 397 # a checksum on the header only. -
branch/UMPA/umpa/protocols/Payload.py
r3482 r3608 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 """ 23 This module contains Payload "protocol" implementation. 24 25 Payload is the abstract protocol of 5-7 layers of the OSI model. 26 """ 27 22 28 from umpa.protocols._fields import Field 23 29 from umpa.protocols._protocols import Protocol … … 25 31 26 32 class _HData(Field): # FIXME: should we move it to _fields.py as a common field? 27 """Data as a strings.28 33 """ 34 Data as a strings. 35 """ 36 29 37 bits = 0 30 38 auto = False 31 39 32 40 def _is_valid(self, val): 41 """ 42 Validate if the value is not bigger than expected. 43 44 @param val: the new value. 45 46 @rtype: C{bool} 47 @return: C{True}. 48 """ 33 49 return True # we use str() so everything is ok 34 50 35 51 def set(self, value): 52 """ 53 Set the new value of the field. 54 55 @param value: assign new value with str() casting. 56 """ 36 57 if self._is_valid(value): 37 58 self._value = str(value) # converting to str … … 43 64 44 65 def clear(self): 45 super(Field, self).lear() 66 """ 67 Clear the current value of the field. 68 """ 69 70 super(Field, self).clear() 46 71 self.bits = 0 47 72 48 73 def _raw_value(self): 74 """ 75 Convert the value to the raw mode. 76 77 Convert every character into the integer ordinal. 78 Merge the integer values. 79 Raw value's type is a number. It has to be in big-endian order. 80 The bits of the result of this method are inserted into the raw number 81 of the whole protocol. 82 83 @rtype: C{number} 84 @return: raw value of the field. 85 """ 86 49 87 raw = 0 50 88 for char in self._value: … … 56 94 57 95 class Payload(Protocol): 58 """Use this as a protocol upper 4th layer of OSI model.59 96 """ 97 Payload -- data of 5-7 layers of the OSI model. 98 """ 99 60 100 layer = 5 61 101 name = "Payload" … … 63 103 64 104 def __init__(self, **kw): 105 """ 106 Create a new Payload(). 107 108 @param **kw: pass to super-constructor. 109 """ 110 65 111 fields_list = [ _HData("Data"), ] 66 112 … … 68 114 69 115 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 116 """ 117 Handle with fields before calling fillout() for them. 118 119 Nothing to do for Payload class here. Return required vars. 120 121 @type raw_value: C{int} 122 @param raw_value: currently raw value for the packet. 123 124 @type bit: C{int} 125 @param bit: currently length of the protocol. 126 127 @type protocol_container: C{tuple} 128 @param protocol_container: tuple of protocols included in the packet. 129 130 @type protocol_bits: C{int} 131 @param protocol_bits: currently length of the packet. 132 133 @return: C{raw_value, bit} 134 """ 135 70 136 return raw_value, bit 71 137 72 138 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 139 """ 140 Handle with fields after calling fillout() for them. 141 142 Nothing to do for Payload class here. Return required vars. 143 144 @type raw_value: C{int} 145 @param raw_value: currently raw value for the packet. 146 147 @type bit: C{int} 148 @param bit: currently length of the protocol. 149 150 @type protocol_container: C{tuple} 151 @param protocol_container: tuple of protocols included in the packet. 152 153 @type protocol_bits: C{int} 154 @param protocol_bits: currently length of the packet. 155 156 @return: C{raw_value, bit} 157 """ 158 73 159 return raw_value, bit 74 160 -
branch/UMPA/umpa/protocols/TCP.py
r3479 r3608 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 """ 23 This module contains TCP (Transmission Control Protocol) protocol 24 implementation. 25 """ 26 22 27 from umpa.protocols import _consts 23 28 from umpa.protocols import _fields … … 28 33 29 34 class _HPort(_fields.EnumField): 35 """ 36 TCP uses the notion of port numbers to identify sending and receiving 37 application end-points on a host, or Internet sockets. 38 """ 39 30 40 bits = 16 31 41 auto = False … … 324 334 325 335 class _HSequenceNumber(_fields.IntField): 326 """The sequence number of the first data octet in this segment (except 336 """ 337 The sequence number of the first data octet in this segment (except 327 338 when SYN is present). 328 339 329 340 See RFC 793 for more. 330 341 """ 342 331 343 bits = 32 332 344 auto = True 345 333 346 def _generate_value(self): 347 """ 348 Generate value for undefined field yet. 349 350 @return: auto-generated value of the field. 351 """ 352 334 353 # TODO: implemention real auto-filling here ;) 335 354 # otherwise we can simple return 0 … … 337 356 338 357 class _HAcknowledgmentNumber(_fields.IntField): 339 """If the ACK control bit is set this field contains the value of the 358 """ 359 If the ACK control bit is set this field contains the value of the 340 360 next sequence number the sender of the segment is expecting to receive. 341 361 342 362 See RFC 793 for more. 343 363 """ 364 344 365 bits = 32 345 366 auto = True 367 346 368 def _generate_value(self): 369 """ 370 Generate value for undefined field yet. 371 372 @return: auto-generated value of the field. 373 """ 374 347 375 # TODO: implemention real auto-filling here ;) 348 376 # otherwise we can simple return 0 … … 350 378 351 379 class _HDataOffset(_fields.SpecialIntField): 352 """The number of 32 bit words in the TCP Header. This indicates where 380 """ 381 The number of 32 bit words in the TCP Header. This indicates where 353 382 the data begins. 354 383 355 384 See RFC 793 for more. 356 385 """ 386 357 387 bits = 4 358 388 auto = True 389 359 390 def _generate_value(self): 391 """ 392 Generate value for undefined field yet. 393 394 @return: auto-generated value of the field. 395 """ 396 360 397 # returns in 32-bits units 361 398 return 5 + self._tmp_value / 32 # 5 is a minimum value 362 399 363 400 class _HReserved(_fields.IntField): 364 """Reserved for future use. 401 """ 402 Reserved for future use. 365 403 366 404 See RFC 793 for more. 367 405 """ 406 368 407 bits = 6 369 408 auto = True 409 370 410 def _generate_value(self): 411 """ 412 Generate value for undefined field yet. 413 414 @return: auto-generated value of the field. 415 """ 416 371 417 return 0 372 418 373 419 class _HWindow(_fields.IntField): 374 """The number of data octets beginning with the one indicated in the 420 """ 421 The number of data octets beginning with the one indicated in the 375 422 acknowledgment field which the sender of this segment is willing to accept. 376 423 377 424 See RFC 793 for more. 378 425 """ 426 379 427 bits = 16 380 428 auto = True 429 381 430 def _generate_value(self): 431 """ 432 Generate value for undefined field yet. 433 434 @return: auto-generated value of the field. 435 """ 436 382 437 # TODO: implemention real auto-filling here ;) 383 438 # otherwise we can simple return 0 … … 385 440 386 441 class _HUrgentPointer(_fields.IntField): 387 """This field communicates the current value of the urgent pointer as a 442 """ 443 This field communicates the current value of the urgent pointer as a 388 444 positive offset from the sequence number in this segment. 389 445 390 446 See RFC 793 for more. 391 447 """ 448 392 449 bits = 16 393 450 auto = True 451 394 452 def _generate_value(self): 453 """ 454 Generate value for undefined field yet. 455 456 @return: auto-generated value of the field. 457 """ 458 395 459 # TODO: implemention real auto-filling here ;) 396 460 # otherwise we can simple return 0 … … 398 462 399 463 class TCP(_protocols.Protocol): 400 """This is Transmission Control Protocol. 464 """ 465 Transmission Control Protocol implementation. 466 401 467 It the most common protocol in the Internet on fourth layer 402 468 of the OSI model. 403 469 """ 470 404 471 layer = 4 # layer of the OSI 405 472 protocol_id = _consts.PROTOCOL_TCP … … 412 479 413 480 def __init__(self, **kw): 481 """ 482 Create a new TCP(). 483 484 @param **kw: pass to super-constructor. 485 """ 486 414 487 control_bits = ('urg', 'ack', 'psh', 'rst', 'syn', 'fin') 415 488 control_bits_predefined = dict.fromkeys(control_bits, 0) … … 450 523 451 524 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 525 """ 526 Handle with fields before calling fillout() for them. 527 528 Set Padding field and calculate header length. 529 530 @type raw_value: C{int} 531 @param raw_value: currently raw value for the packet. 532 533 @type bit: C{int} 534 @param bit: currently length of the protocol. 535 536 @type protocol_container: C{tuple} 537 @param protocol_container: tuple of protocols included in the packet. 538 539 @type protocol_bits: C{int} 540 @param protocol_bits: currently length of the packet. 541 542 @return: C{raw_value, bit} 543 """ 544 452 545 # Padding 453 546 self._get_field('_padding')._tmp_value = \ … … 461 554 462 555 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 556 """ 557 Handle with fields after calling fillout() for them. 558 559 Calculate header checksum with new instance of PseudoHeader object. 560 561 @type raw_value: C{int} 562 @param raw_value: currently raw value for the packet. 563 564 @type bit: C{int} 565 @param bit: currently length of the protocol. 566 567 @type protocol_container: C{tuple} 568 @param protocol_container: tuple of protocols included in the packet. 569 570 @type protocol_bits: C{int} 571 @param protocol_bits: currently length of the packet. 572 573 @return: C{raw_value, bit} 574 """ 575 463 576 # rev_offset it the offset from the right side 464 577 cksum_rev_offset = bit - self.get_offset('_checksum') - \ -
branch/UMPA/umpa/protocols/UDP.py
r3479 r3608 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 """ 23 This module contains UDP (Uset Datagram Protocol) protocol implementation. 24 """ 25 22 26 from umpa.protocols import _consts 23 27 from umpa.protocols import _fields … … 28 32 29 33 class _HPort(_fields.EnumField): 34 """ 35 UDP uses ports to allow application-to-application communication. 36 """ 37 30 38 bits = 16 31 39 auto = False … … 242 250 243 251 class _HLength(_fields.SpecialIntField): 244 """Length is the length in octets of this user datagram including this 252 """ 253 Length is the length in octets of this user datagram including this 245 254 header and the data. 246 """ 255 256 See RFC 768 for more. 257 """ 258 247 259 bits = 16 248 260 auto = True 249 261 def _generate_value(self): 262 """ 263 Generate value for undefined field yet. 264 265 @return: auto-generated value of the field. 266 """ 267 250 268 # returns in byte units 251 269 return 8 + self._tmp_value/8 # minimum is 8 252 270 253 271 class UDP(_protocols.Protocol): 254 """This is User Datagram Protocol. 272 """ 273 User Datagram Protocol implementation. 255 274 256 275 This protocol provides a procedure for application programs to send … … 259 278 are not guaranteed. 260 279 """ 280 261 281 layer = 4 262 282 protocol_id = _consts.PROTOCOL_UDP … … 267 287 268 288 def __init__(self, **kw): 289 """ 290 Create a new UDP(). 291 292 @param **kw: pass to super-constructor. 293 """ 294 269 295 fields_list = [ _HPort("Source Port", 0), 270 296 _HPort("Destination Port", 0), … … 285 311 286 312 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 313 """ 314 Handle with fields before calling fillout() for them. 315 316 Store temp value of protocols bits for checksum field. 317 318 @type raw_value: C{int} 319 @param raw_value: currently raw value for the packet. 320 321 @type bit: C{int} 322 @param bit: currently length of the protocol. 323 324 @type protocol_container: C{tuple} 325 @param protocol_container: tuple of protocols included in the packet. 326 327 @type protocol_bits: C{int} 328 @param protocol_bits: currently length of the packet. 329 330 @return: C{raw_value, bit} 331 """ 332 287 333 # Length 288 334 self._get_field('_checksum')._tmp_value = protocol_bits … … 291 337 292 338 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 339 """ 340 Handle with fields after calling fillout() for them. 341 342 Calculate header checksum with new instance of PseudoHeader object. 343 344 @type raw_value: C{int} 345 @param raw_value: currently raw value for the packet. 346 347 @type bit: C{int} 348 @param bit: currently length of the protocol. 349 350 @type protocol_container: C{tuple} 351 @param protocol_container: tuple of protocols included in the packet. 352 353 @type protocol_bits: C{int} 354 @param protocol_bits: currently length of the packet. 355 356 @return: C{raw_value, bit} 357 """ 358 293 359 cksum_rev_offset = 0 294 360 # checking if user not defined his own value of checksum -
branch/UMPA/umpa/protocols/_fields.py
r3605 r3608 151 151 Convert the value to the raw mode. 152 152 153 Raw value is a number type. It has to be in big-endian order.153 Raw value's type is a number. It has to be in big-endian order. 154 154 The bits of the result of this method are inserted into the raw number 155 155 of the whole protocol. … … 216 216 Convert the value to the raw mode. 217 217 218 Raw value is a number type. It has to be in big-endian order.218 Raw value's type is a number. It has to be in big-endian order. 219 219 The bits of the result of this method are inserted into the raw number 220 220 of the whole protocol. … … 383 383 Convert the value to the raw mode. 384 384 385 Raw value is a number type. It has to be in big-endian order.385 Raw value's type is a number. It has to be in big-endian order. 386 386 The bits of the result of this method are inserted into the raw number 387 387 of the whole protocol. … … 527 527 def _generate_value(self): 528 528 """ 529 Generate value for undefined yet field.529 Generate value for undefined field yet. 530 530 531 531 @return: auto-generated value of the field. -
branch/UMPA/umpa/protocols/_layer4.py
r3411 r3608 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 """ 23 This module contains usefull classes for 4th layer's protocols. 24 25 TCP/UDP use special pseudo header to calculate checksum. These classes 26 are provided. 27 """ 28 22 29 from umpa.protocols._protocols import Protocol 23 30 from umpa.protocols.IP import IP … … 25 32 26 33 class Layer4ChecksumField(IntField): 34 """ 35 A checksum for the common classes of 4th layer of OSI model. 36 37 Especially UDP/TCP use it. The checksum is calculated from the Pseudo 38 Header, the main header and the payload. 39 """ 40 27 41 bits = 16 28 42 auto = True 29 43 def _generate_value(self): 44 """ 45 Generate value for undefined field yet. 46 47 @return: auto-generated value of the field. 48 """ 49 30 50 return 0 31 51 32 52 class PseudoHeader(Protocol): 33 """This is class is useful for some protocols like TCP or UDP. 34 It has been used to calculate checksum of those protocols. 53 """ 54 This is Pseudo Header. 55 56 This class is useful for some protocols like TCP or UDP. 57 It's used to calculate checksum of those protocols. 35 58 It's prefixed to the protocol header before calculating. 36 59 """ 60 37 61 _ordered_fields = ('source_address', 'destination_address', 'reserved', 38 62 'protocol_id', 'total_length') 39 63 40 64 def __init__(self, protocol_id, total_length): 65 """ 66 Create a new PseudoHeader() 67 68 @type protocol_id: C{int} 69 @param protocol_id: id of the protocol which use PseudoHeader. 70 71 @type total_length: C{int} 72 @param total_length: length of the real header and payload. 73 74 """ 41 75 42 76 fields_list = [ IPv4AddrField("Source Address"), … … 48 82 49 83 def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): 84 """ 85 Handle with fields before calling fillout() for them. 86 87 Parse lower protocol (usually IP) to get source/destination address. 88 89 @type raw_value: C{int} 90 @param raw_value: currently raw value for the packet. 91 92 @type bit: C{int} 93 @param bit: currently length of the protocol. 94 95 @type protocol_container: C{tuple} 96 @param protocol_container: tuple of protocols included in the packet. 97 98 @type protocol_bits: C{int} 99 @param protocol_bits: currently length of the packet. 100 101 @return: C{raw_value, bit} 102 """ 103 50 104 # we assign first localhost becuase if there is not IP instance 51 105 # than better 0 than nothing (for nonstrict users) … … 63 117 64 118 def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): 119 """ 120 Handle with fields after calling fillout() for them. 121 122 Nothing to do for PseudoHeader class here. Return required vars. 123 124 @type raw_value: C{int} 125 @param raw_value: currently raw value for the packet. 126 127 @type bit: C{int} 128 @param bit: currently length of the protocol. 129 130 @type protocol_container: C{tuple} 131 @param protocol_container: tuple of protocols included in the packet. 132 133 @type protocol_bits: C{int} 134 @param protocol_bits: currently length of the packet. 135 136 @return: C{raw_value, bit} 137 """ 138 65 139 return raw_value, bit -
branch/UMPA/umpa/protocols/_protocols.py
r3604 r3608 36 36 37 37 class Protocol(object): 38 39 """Superclass for every protocol's implementations.38 """ 39 Superclass for every protocol's implementations. 40 40 41 41 You have to override following methods:
