[tor-commits] [stem/master] Stub Netinfo packing function

atagar at torproject.org atagar at torproject.org
Sun Jan 21 02:04:04 UTC 2018


commit 9952f9341174c12df63cf5b34a8d7e3e1a4ae2ed
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Jan 16 10:36:38 2018 -0800

    Stub Netinfo packing function
    
    Not yet tested or even fully implemented yet since this requires the ability to
    pack an Address.
---
 stem/client/__init__.py |  8 ++++++++
 stem/client/cell.py     | 26 ++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index 271237d8..d15f0aa4 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -90,6 +90,14 @@ class Address(collections.namedtuple('Address', ['type', 'type_int', 'value', 'v
   """
 
   @staticmethod
+  def pack(addr):
+    """
+    Bytes payload for an address.
+    """
+
+    raise NotImplementedError('Not yet available')
+
+  @staticmethod
   def pop(content):
     if not content:
       raise ValueError('Payload empty where an address was expected')
diff --git a/stem/client/cell.py b/stem/client/cell.py
index 8a6480c7..bc44298f 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -45,7 +45,7 @@ import sys
 
 from stem import UNDEFINED
 from stem.client import ZERO, Address, Certificate, Size
-from stem.util import _hash_attr
+from stem.util import _hash_attr, datetime_to_unix
 
 FIXED_PAYLOAD_LEN = 509
 AUTH_CHALLENGE_SIZE = 32
@@ -352,7 +352,29 @@ class NetinfoCell(Cell):
 
   @classmethod
   def pack(cls, link_version, receiver_address, sender_addresses, timestamp = None):
-    raise NotImplementedError('Netinfo packing not yet implemented')
+    """
+    Payload about our timestamp and versions.
+
+    :param int link_version: link protocol version
+    :param stem.client.Address receiver_address: address of the receiver
+    :param list sender_addresses: our addresses
+    :param datetime timestamp: current time according to our clock
+
+    :returns: **bytes** with a payload for these versions
+    """
+
+    if timestamp is None:
+      timestamp = datetime.datetime.now()
+
+    payload = io.BytesIO()
+    payload.write(Size.LONG.pack(int(datetime_to_unix(timestamp))))
+    payload.write(Address.pack(receiver_address))
+    payload.write(Size.CHAR.pack(len(sender_addresses)))
+
+    for addr in sender_addresses:
+      payload.write(Address.pack(addr))
+
+    return cls._pack(link_version, payload.getvalue())
 
   @classmethod
   def _unpack(cls, content, circ_id, link_version):





More information about the tor-commits mailing list