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 |
| 38 | else: |
| 39 | raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data)) |
| 40 | else: |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 41 | self._data = ns.ndnSIM.ndn.Data () |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 42 | |
| 43 | self.name = name |
| 44 | self.content = content |
| 45 | self.signedInfo = signed_info or SignedInfo () |
| 46 | |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 47 | @staticmethod |
| 48 | def fromWire (wire): |
| 49 | data = Data (data = ns.ndnSIM.ndn.Wire.ToData (wire)) |
| 50 | # timestamp |
| 51 | data.signedInfo.freshnessSeconds = data._data.GetFreshness () |
| 52 | if data._data.GetKeyLocator (): |
| 53 | data.keyLocator = Name (_name = data._data.GetKeyLocator ()) |
| 54 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 55 | def sign (self, key): |
| 56 | """There is no actual signing in ndnSIM for now, but we will assign signature bits based on key""" |
| 57 | self._data.SetSignature (key.fakeKey) |
| 58 | |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 59 | def toWire (self): |
| 60 | if self._data.GetSignature () == 0: |
| 61 | raise DataError ("Data packet has not been signed, cannot create wire format") |
| 62 | |
| 63 | return ns.ndnSIM.ndn.Wire.FromData (self._data) |
| 64 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 65 | def verify_signature (self, key): |
| 66 | """There is no actual signing in ndnSIM for now, but we will check if signature matches the key""" |
| 67 | return self._data.GetSignature () == key.fakeKey |
| 68 | |
| 69 | def __getattr__ (self, name): |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 70 | if name == "_data": |
| 71 | return object.__getattr__ (self, name) |
| 72 | |
| 73 | elif name == "signedInfo": |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 74 | return object.__getattr__ (self, name) |
| 75 | elif name == "name": |
| 76 | return Name (self._data.GetName ()) |
| 77 | elif name == "scope": |
| 78 | return self._data.GetScope () |
| 79 | elif name == "interestLifetime": |
| 80 | return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 81 | elif name == "content": |
| 82 | pkt = self._data.GetContent () |
| 83 | return ns.ndnSIM.ndn.PacketToBuffer (pkt) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 84 | else: |
| 85 | return self._data.__getattribute__ (name) |
| 86 | |
| 87 | def __setattr__(self, name, value): |
| 88 | if name == "_data": |
| 89 | return object.__setattr__ (self, name, value) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 91 | elif name == 'signedInfo': |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 92 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 93 | return |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 94 | if isinstance (value, SignedInfo): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 95 | object.__setattr__ (self, name, value) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 97 | if value.timestamp: |
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 | pass |
| 100 | if value.freshnessSeconds: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 101 | self._data.SetFreshness (ns.core.Seconds (value)) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 102 | if value.keyLocator: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 103 | self._data.SetKeyLocator (value._name) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 104 | else: |
| 105 | raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value)) |
| 106 | elif name == "name": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 107 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 108 | return self._data.SetName (ns.ndnSIM.ndn.Name ()) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 109 | elif isinstance (value, Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 110 | return self._data.SetName (value._name) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 111 | elif isinstance (value, ns.ndnSIM.ndn.Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 112 | return self._data.SetName (value) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 113 | elif isinstance (value, str): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 114 | return self._data.SetName (ns.ndnSIM.ndn.Name (value)) |
| 115 | else: |
| 116 | raise ValueError ("Invalid name parameter") |
| 117 | elif name == "content": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame^] | 118 | if value is None: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 119 | pkt = ns.network.Packet () |
| 120 | self._data.SetPayload (pkt) |
| 121 | else: |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 122 | pkt = ns.ndnSIM.ndn.BufferToPacket (bytes (value)) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 123 | self._data.SetPayload (pkt) |
| 124 | else: |
| 125 | raise NameError ("Unknown attribute [%s]" % name) |
| 126 | |
| 127 | def __repr__(self): |
| 128 | return "ndn.Data(%s)" % str (self._data) |