| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2008 Adriano Monteiro Marques. |
|---|
| 5 | # |
|---|
| 6 | # Author: Bartosz SKOWRON <getxsick at gmail dot com> |
|---|
| 7 | # |
|---|
| 8 | # This library is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU Lesser General Public License as published |
|---|
| 10 | # by the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This library is distributed in the hope that it will be useful, but |
|---|
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 15 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
|---|
| 16 | # License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU Lesser General Public License |
|---|
| 19 | # along with this library; if not, write to the Free Software Foundation, |
|---|
| 20 | # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | from umpa.protocols._protocols import Protocol |
|---|
| 23 | from umpa.protocols.IP import IP |
|---|
| 24 | from umpa.protocols._fields import * |
|---|
| 25 | |
|---|
| 26 | class Layer4ChecksumField(IntField): |
|---|
| 27 | bits = 16 |
|---|
| 28 | auto = True |
|---|
| 29 | def _generate_value(self): |
|---|
| 30 | return 0 |
|---|
| 31 | |
|---|
| 32 | 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. |
|---|
| 35 | It's prefixed to the protocol header before calculating. |
|---|
| 36 | """ |
|---|
| 37 | _ordered_fields = ('source_address', 'destination_address', 'reserved', |
|---|
| 38 | 'protocol_id', 'total_length') |
|---|
| 39 | |
|---|
| 40 | def __init__(self, protocol_id, total_length): |
|---|
| 41 | |
|---|
| 42 | fields_list = [ IPv4AddrField("Source Address"), |
|---|
| 43 | IPv4AddrField("Destination Address"), |
|---|
| 44 | IntField("Reserved", 0, bits=8), |
|---|
| 45 | IntField("Protocol", protocol_id, bits=8), |
|---|
| 46 | IntField("Total Length", total_length, bits=16) ] |
|---|
| 47 | super(PseudoHeader, self).__init__(fields_list) |
|---|
| 48 | |
|---|
| 49 | def _pre_raw(self, raw_value, bit, protocol_container, protocol_bits): |
|---|
| 50 | # we assign first localhost becuase if there is not IP instance |
|---|
| 51 | # than better 0 than nothing (for nonstrict users) |
|---|
| 52 | self.source_address = "127.0.0.1" |
|---|
| 53 | self.destination_address = "127.0.0.1" |
|---|
| 54 | # grabbing informations from IP's header |
|---|
| 55 | it = iter(protocol_container) |
|---|
| 56 | for proto in it: |
|---|
| 57 | if isinstance(proto,IP): |
|---|
| 58 | self.source_address = proto.source_address |
|---|
| 59 | self.destination_address = proto.destination_address |
|---|
| 60 | break |
|---|
| 61 | |
|---|
| 62 | return raw_value, bit |
|---|
| 63 | |
|---|
| 64 | def _post_raw(self, raw_value, bit, protocol_container, protocol_bits): |
|---|
| 65 | return raw_value, bit |
|---|