| 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 | import base |
|---|
| 23 | from umpa.utils.my_exceptions import UMPAAttributeException |
|---|
| 24 | |
|---|
| 25 | class HVersion(base.Field): |
|---|
| 26 | def fillout(self): |
|---|
| 27 | pass |
|---|
| 28 | |
|---|
| 29 | class HIHL(base.Field): |
|---|
| 30 | def fillout(self): |
|---|
| 31 | pass |
|---|
| 32 | |
|---|
| 33 | class HTypeOfService(base.Field): |
|---|
| 34 | def fillout(self): |
|---|
| 35 | pass |
|---|
| 36 | |
|---|
| 37 | class HTotalLength(base.Field): |
|---|
| 38 | def fillout(self): |
|---|
| 39 | pass |
|---|
| 40 | |
|---|
| 41 | class HIdentification(base.Field): |
|---|
| 42 | def fillout(self): |
|---|
| 43 | pass |
|---|
| 44 | |
|---|
| 45 | class HFlags(base.Flags): |
|---|
| 46 | def fillout(self): |
|---|
| 47 | pass |
|---|
| 48 | |
|---|
| 49 | class HFragmentOffset(base.Field): |
|---|
| 50 | def fillout(self): |
|---|
| 51 | pass |
|---|
| 52 | |
|---|
| 53 | class HTimeToLive(base.Field): |
|---|
| 54 | def fillout(self): |
|---|
| 55 | pass |
|---|
| 56 | |
|---|
| 57 | class HProtocol(base.Field): |
|---|
| 58 | def fillout(self): |
|---|
| 59 | pass |
|---|
| 60 | |
|---|
| 61 | class HHeaderChecksum(base.Field): |
|---|
| 62 | def fillout(self): |
|---|
| 63 | pass |
|---|
| 64 | |
|---|
| 65 | class HSourceAddress(base.Field): |
|---|
| 66 | def fillout(self): |
|---|
| 67 | pass |
|---|
| 68 | |
|---|
| 69 | class HDestinationAddress(base.Field): |
|---|
| 70 | def fillout(self): |
|---|
| 71 | pass |
|---|
| 72 | |
|---|
| 73 | class HOptions(base.Field): |
|---|
| 74 | def fillout(self): |
|---|
| 75 | pass |
|---|
| 76 | |
|---|
| 77 | class HPadding(base.Field): |
|---|
| 78 | def fillout(self): |
|---|
| 79 | pass |
|---|
| 80 | |
|---|
| 81 | # main IP class |
|---|
| 82 | |
|---|
| 83 | class IP(base.Protocol): |
|---|
| 84 | _ordered_fields = ('_version', '_ihl', 'type_of_service', '_total_length', |
|---|
| 85 | 'identification', 'flags', '_fragment_offset', |
|---|
| 86 | 'time_to_live', 'protocol', '_header_checksum', |
|---|
| 87 | 'source_address', 'destination_address', 'options', |
|---|
| 88 | '_padding',) |
|---|
| 89 | |
|---|
| 90 | def __init__(self, **kw): |
|---|
| 91 | base.Protocol.__init__(self, kw) |
|---|
| 92 | |
|---|
| 93 | fields_list = [ HVersion(4, True), HIHL(4, True), |
|---|
| 94 | HTypeOfService(8), HTotalLength(16, True), |
|---|
| 95 | HIdentification(16, True), HFlags(3, True), # add names to flags later |
|---|
| 96 | HFragmentOffset(13, True), HTimeToLive(8), |
|---|
| 97 | HProtocol(8, True), HHeaderChecksum(16, True), |
|---|
| 98 | HSourceAddress(16), HDestinationAddress(16), |
|---|
| 99 | HOptions(0), HPadding(0, True), ] |
|---|
| 100 | |
|---|
| 101 | # we pack objects of header's fields to the dict |
|---|
| 102 | self._fields = dict(zip(self._ordered_list, fields_list)) |
|---|
| 103 | |
|---|
| 104 | # setting up passed fields |
|---|
| 105 | for field in kw: |
|---|
| 106 | self.__setattr__(field, kw[field]) |
|---|
| 107 | |
|---|
| 108 | def _is_valid(self, name): |
|---|
| 109 | """Check if attribute is allowed.""" |
|---|
| 110 | return self._fields.has_key(name) |
|---|
| 111 | |
|---|
| 112 | def __setattr__(self, attr, val): |
|---|
| 113 | """Set value of the field.""" |
|---|
| 114 | |
|---|
| 115 | # we can do the same without _is_valid() with just try/except section |
|---|
| 116 | # but Francesco asked me about this method |
|---|
| 117 | if self._is_valid(attr): |
|---|
| 118 | self._fields[attr].set(val) |
|---|
| 119 | else: |
|---|
| 120 | raise UMPAAttributeException, attr + ' not allowed' |
|---|
| 121 | |
|---|
| 122 | def __getattr__(self, attr): |
|---|
| 123 | """Return value of the field.""" |
|---|
| 124 | if self._is_valid(attr): |
|---|
| 125 | return self._fields[attr].get() |
|---|
| 126 | else: |
|---|
| 127 | raise UMPAAttributeException, attr + ' not allowed' |
|---|