blob: 6d36dc91f9215cc748c1fc1229c2931bc66a793a [file] [log] [blame]
Alexander Afanasyev39f53162013-07-18 16:24:31 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07002#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07003# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07005#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07006# GNU 3.0 license, See the LICENSE file for more information
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07007#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07008# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07009#
Alexander Afanasyev39f53162013-07-18 16:24:31 -070010
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
20import ns.ndnSIM
21from Name import Name
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070022from SignedInfo import SignedInfo
Alexander Afanasyev39f53162013-07-18 16:24:31 -070023
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070024class DataError (Exception):
25 pass
26
Alexander Afanasyev39f53162013-07-18 16:24:31 -070027class 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 Afanasyevfce5bbd2013-08-07 18:50:00 -070034 if isinstance (data, Data):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070035 self._data = data._data
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070036 elif isinstance (data, ns.ndnSIM.ndn.Data):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070037 self._data = data
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070038 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 Afanasyev39f53162013-07-18 16:24:31 -070043 else:
44 raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data))
45 else:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070046 self._data = ns.ndnSIM.ndn.Data ()
Alexander Afanasyev39f53162013-07-18 16:24:31 -070047
48 self.name = name
49 self.content = content
50 self.signedInfo = signed_info or SignedInfo ()
51
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070052 @staticmethod
53 def fromWire (wire):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070054 return Data (data = ns.ndnSIM.ndn.Wire.ToDataStr (bytes (wire)))
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070055
Alexander Afanasyev39f53162013-07-18 16:24:31 -070056 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 Afanasyevf6f9ef22013-07-26 11:34:00 -070060 def toWire (self):
61 if self._data.GetSignature () == 0:
62 raise DataError ("Data packet has not been signed, cannot create wire format")
63
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070064 return ns.ndnSIM.ndn.Wire.FromDataStr (self._data)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070065
Alexander Afanasyev39f53162013-07-18 16:24:31 -070066 def verify_signature (self, key):
67 """There is no actual signing in ndnSIM for now, but we will check if signature matches the key"""
68 return self._data.GetSignature () == key.fakeKey
69
70 def __getattr__ (self, name):
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070071 if name == "_data":
72 return object.__getattr__ (self, name)
73
74 elif name == "signedInfo":
Alexander Afanasyev39f53162013-07-18 16:24:31 -070075 return object.__getattr__ (self, name)
76 elif name == "name":
77 return Name (self._data.GetName ())
78 elif name == "scope":
79 return self._data.GetScope ()
80 elif name == "interestLifetime":
81 return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070082 elif name == "content":
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070083 pkt = self._data.GetPayload ()
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070084 return ns.ndnSIM.ndn.PacketToBuffer (pkt)
Alexander Afanasyev39f53162013-07-18 16:24:31 -070085 else:
86 return self._data.__getattribute__ (name)
87
88 def __setattr__(self, name, value):
89 if name == "_data":
90 return object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070091
Alexander Afanasyev39f53162013-07-18 16:24:31 -070092 elif name == 'signedInfo':
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070093 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070094 return
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070095 if isinstance (value, SignedInfo):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070096 object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070097
Alexander Afanasyev39f53162013-07-18 16:24:31 -070098 if value.timestamp:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070099 # ?
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700100 pass
101 if value.freshnessSeconds:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700102 self._data.SetFreshness (ns.core.Seconds (value))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700103 if value.keyLocator:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700104 self._data.SetKeyLocator (value._name)
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700105 else:
106 raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value))
107 elif name == "name":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700108 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700109 return self._data.SetName (ns.ndnSIM.ndn.Name ())
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700110 elif isinstance (value, Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700111 return self._data.SetName (value._name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700112 elif isinstance (value, ns.ndnSIM.ndn.Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700113 return self._data.SetName (value)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700114 elif isinstance (value, str):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700115 return self._data.SetName (ns.ndnSIM.ndn.Name (value))
116 else:
117 raise ValueError ("Invalid name parameter")
118 elif name == "content":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700119 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700120 pkt = ns.network.Packet ()
121 self._data.SetPayload (pkt)
122 else:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700123 pkt = ns.ndnSIM.ndn.BufferToPacket (bytes (value))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700124 self._data.SetPayload (pkt)
125 else:
126 raise NameError ("Unknown attribute [%s]" % name)
127
128 def __repr__(self):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700129 return "ndn.Data(%s; %s)" % str (self._data, self.signedInfo)