Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 1 | ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 2 | # |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 3 | # Copyright (c) 2011-2013, Regents of the University of California |
| 4 | # Alexander Afanasyev |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 5 | # |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 6 | # GNU 3.0 license, See the LICENSE file for more information |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 7 | # |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 8 | # Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 9 | # |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 10 | |
| 11 | # |
| 12 | # Based on PyCCN code, copyrighted and licensed as follows |
| 13 | # |
| 14 | # Copyright (c) 2011-2013, Regents of the University of California |
| 15 | # BSD license, See the COPYING file for more information |
| 16 | # Written by: Derek Kulinski <takeda@takeda.tk> |
| 17 | # Jeff Burke <jburke@ucla.edu> |
| 18 | # |
| 19 | |
| 20 | import ns.ndnSIM |
| 21 | from Name import Name |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 22 | from SignedInfo import SignedInfo |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 24 | class DataError (Exception): |
| 25 | pass |
| 26 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 27 | class Data (object): |
| 28 | _data = None |
| 29 | |
| 30 | def __init__(self, |
| 31 | name = None, content = None, signed_info = None, |
| 32 | data = None): |
| 33 | if data: |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 34 | if isinstance (data, Data): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 35 | self._data = data._data |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 36 | elif isinstance (data, ns.ndnSIM.ndn.Data): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 37 | self._data = data |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 38 | self.signedInfo = SignedInfo () |
| 39 | # timestamp |
| 40 | self.signedInfo.freshnessSeconds = self._data.GetFreshness ().ToDouble (ns.core.Time.S) |
| 41 | if self._data.GetKeyLocator (): |
| 42 | self.signedInfo.keyLocator = Name (name = self._data.GetKeyLocator ()) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 43 | else: |
| 44 | raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data)) |
| 45 | else: |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 46 | self._data = ns.ndnSIM.ndn.Data () |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 47 | |
| 48 | self.name = name |
| 49 | self.content = content |
| 50 | self.signedInfo = signed_info or SignedInfo () |
| 51 | |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 52 | @staticmethod |
| 53 | def fromWire (wire): |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 54 | return Data (data = ns.ndnSIM.ndn.Wire.ToDataStr (bytes (wire))) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 56 | def sign (self, key): |
| 57 | """There is no actual signing in ndnSIM for now, but we will assign signature bits based on key""" |
| 58 | self._data.SetSignature (key.fakeKey) |
| 59 | |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 60 | def toWire (self): |
| 61 | if self._data.GetSignature () == 0: |
| 62 | raise DataError ("Data packet has not been signed, cannot create wire format") |
| 63 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 64 | return ns.ndnSIM.ndn.Wire.FromDataStr (self._data) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 66 | def verify_signature (self, key): |
| 67 | """There is no actual signing in ndnSIM for now, but we will check if signature matches the key""" |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 68 | return True |
| 69 | # return self._data.GetSignature () == key.fakeKey |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 70 | |
| 71 | def __getattr__ (self, name): |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 72 | if name == "_data": |
| 73 | return object.__getattr__ (self, name) |
| 74 | |
| 75 | elif name == "signedInfo": |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 76 | return object.__getattr__ (self, name) |
| 77 | elif name == "name": |
| 78 | return Name (self._data.GetName ()) |
| 79 | elif name == "scope": |
| 80 | return self._data.GetScope () |
| 81 | elif name == "interestLifetime": |
| 82 | return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 83 | elif name == "content": |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 84 | pkt = self._data.GetPayload () |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 85 | return ns.ndnSIM.ndn.PacketToBuffer (pkt) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 86 | else: |
| 87 | return self._data.__getattribute__ (name) |
| 88 | |
| 89 | def __setattr__(self, name, value): |
| 90 | if name == "_data": |
| 91 | return object.__setattr__ (self, name, value) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 93 | elif name == 'signedInfo': |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 94 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 95 | return |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 96 | if isinstance (value, SignedInfo): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 97 | object.__setattr__ (self, name, value) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 99 | if value.timestamp: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 100 | # ? |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 101 | pass |
| 102 | if value.freshnessSeconds: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 103 | self._data.SetFreshness (ns.core.Seconds (value)) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 104 | if value.keyLocator: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 105 | self._data.SetKeyLocator (value._name) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 106 | else: |
| 107 | raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value)) |
| 108 | elif name == "name": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 109 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 110 | return self._data.SetName (ns.ndnSIM.ndn.Name ()) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 111 | elif isinstance (value, Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 112 | return self._data.SetName (value._name) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 113 | elif isinstance (value, ns.ndnSIM.ndn.Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 114 | return self._data.SetName (value) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 115 | elif isinstance (value, str): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 116 | return self._data.SetName (ns.ndnSIM.ndn.Name (value)) |
| 117 | else: |
| 118 | raise ValueError ("Invalid name parameter") |
| 119 | elif name == "content": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 120 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 121 | pkt = ns.network.Packet () |
| 122 | self._data.SetPayload (pkt) |
| 123 | else: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 124 | pkt = ns.ndnSIM.ndn.BufferToPacket (bytes (value)) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 125 | self._data.SetPayload (pkt) |
| 126 | else: |
| 127 | raise NameError ("Unknown attribute [%s]" % name) |
| 128 | |
| 129 | def __repr__(self): |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 130 | return "ndn.Data(%s; %s)" % str (self._data, self.signedInfo) |