blob: 5369380e10649d8018503da34000cc59a6562c24 [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
38 else:
39 raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data))
Alexander Afanasyev7ff81b72013-08-08 21:43:16 -070040
41 self.signedInfo = SignedInfo ()
42 # timestamp
43 self.signedInfo.freshnessSeconds = self._data.GetFreshness ().ToDouble (ns.core.Time.S)
44 if self._data.GetKeyLocator ():
45 self.signedInfo.keyLocator = Name (name = self._data.GetKeyLocator ())
Alexander Afanasyev39f53162013-07-18 16:24:31 -070046 else:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070047 self._data = ns.ndnSIM.ndn.Data ()
Alexander Afanasyev39f53162013-07-18 16:24:31 -070048
49 self.name = name
50 self.content = content
51 self.signedInfo = signed_info or SignedInfo ()
52
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070053 @staticmethod
54 def fromWire (wire):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070055 return Data (data = ns.ndnSIM.ndn.Wire.ToDataStr (bytes (wire)))
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070056
Alexander Afanasyev39f53162013-07-18 16:24:31 -070057 def sign (self, key):
58 """There is no actual signing in ndnSIM for now, but we will assign signature bits based on key"""
59 self._data.SetSignature (key.fakeKey)
60
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070061 def toWire (self):
62 if self._data.GetSignature () == 0:
63 raise DataError ("Data packet has not been signed, cannot create wire format")
64
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070065 return ns.ndnSIM.ndn.Wire.FromDataStr (self._data)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070066
Alexander Afanasyev39f53162013-07-18 16:24:31 -070067 def verify_signature (self, key):
68 """There is no actual signing in ndnSIM for now, but we will check if signature matches the key"""
Alexander Afanasyev36f10532013-08-08 11:25:14 -070069 return True
70 # return self._data.GetSignature () == key.fakeKey
Alexander Afanasyev39f53162013-07-18 16:24:31 -070071
72 def __getattr__ (self, name):
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070073 if name == "_data":
74 return object.__getattr__ (self, name)
75
76 elif name == "signedInfo":
Alexander Afanasyev39f53162013-07-18 16:24:31 -070077 return object.__getattr__ (self, name)
78 elif name == "name":
79 return Name (self._data.GetName ())
80 elif name == "scope":
81 return self._data.GetScope ()
82 elif name == "interestLifetime":
83 return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070084 elif name == "content":
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070085 pkt = self._data.GetPayload ()
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070086 return ns.ndnSIM.ndn.PacketToBuffer (pkt)
Alexander Afanasyev39f53162013-07-18 16:24:31 -070087 else:
88 return self._data.__getattribute__ (name)
89
90 def __setattr__(self, name, value):
91 if name == "_data":
92 return object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070093
Alexander Afanasyev39f53162013-07-18 16:24:31 -070094 elif name == 'signedInfo':
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070095 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070096 return
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070097 if isinstance (value, SignedInfo):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070098 object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070099
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700100 if value.timestamp:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700101 # ?
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700102 pass
103 if value.freshnessSeconds:
Alexander Afanasyev05d40942013-08-09 12:43:26 -0700104 self._data.SetFreshness (ns.core.Seconds (value.freshnessSeconds))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700105 if value.keyLocator:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700106 self._data.SetKeyLocator (value._name)
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700107 else:
108 raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value))
109 elif name == "name":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700110 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700111 return self._data.SetName (ns.ndnSIM.ndn.Name ())
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700112 elif isinstance (value, Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700113 return self._data.SetName (value._name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700114 elif isinstance (value, ns.ndnSIM.ndn.Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700115 return self._data.SetName (value)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700116 elif isinstance (value, str):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700117 return self._data.SetName (ns.ndnSIM.ndn.Name (value))
118 else:
119 raise ValueError ("Invalid name parameter")
120 elif name == "content":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700121 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700122 pkt = ns.network.Packet ()
123 self._data.SetPayload (pkt)
124 else:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700125 pkt = ns.ndnSIM.ndn.BufferToPacket (bytes (value))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700126 self._data.SetPayload (pkt)
127 else:
128 raise NameError ("Unknown attribute [%s]" % name)
129
130 def __repr__(self):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700131 return "ndn.Data(%s; %s)" % str (self._data, self.signedInfo)