[Nym3-commit] r31 - in trunk: . crypto

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Tue, 27 Jul 2004 11:55:50 +0200


Author: jr
Date: 2004-07-27 11:55:49 +0200 (Tue, 27 Jul 2004)
New Revision: 31

Added:
   trunk/message.py
Modified:
   trunk/crypto/crypto.ml
   trunk/message.ml
Log:
Starting the parser in python (message.py)


Modified: trunk/crypto/crypto.ml
===================================================================
--- trunk/crypto/crypto.ml	2004-07-10 15:15:01 UTC (rev 30)
+++ trunk/crypto/crypto.ml	2004-07-27 09:55:49 UTC (rev 31)
@@ -57,7 +57,29 @@
   in
     aux 0 (pred xlen)
 
+(* big endian counterpart of the last to function *)
 
+let i2osp_be num len =
+  let x = String.make len '\000' in
+  let rec aux idx n =
+    if idx >= 0 && n > 0 then (
+      x.[idx] <- Char.chr (n land 255);
+      aux (pred idx) (n lsr 8)
+    ) else
+      x
+  in
+    aux (pred len) num 
+
+let os2ip_be x =
+  let xlen = String.length x in
+  let rec aux accu idx =
+    if idx < xlen then
+      aux ((int_of_char x.[idx]) + (accu lsl 8)) (succ idx)
+    else
+      accu
+  in
+    aux 0 0
+
 (**
  * XOR two strings
  *

Modified: trunk/message.ml
===================================================================
--- trunk/message.ml	2004-07-10 15:15:01 UTC (rev 30)
+++ trunk/message.ml	2004-07-27 09:55:49 UTC (rev 31)
@@ -1,5 +1,7 @@
 (* $Id$ *)
 
+exception Bad_message;;
+
 let sig_length = 256;;
 
 type header = { sign : string; nl : int; nym : string; seqno : string };;
@@ -22,6 +24,7 @@
   String.blit seq_no 0 s (sig_length + 1 + nl) 20;
   s;;
 
+(*TODO change this when we add the surb properly*)
 type surb;;
 type config_option;;
 
@@ -55,8 +58,56 @@
   0,[];;
 
 let read_command2server buffer start = 
-  0,[];;
-
+  try
+    let cs = 0 in
+    let n = cs + 4 in
+    let m =
+      (* read a list of msg id, used for Get and delete *)
+      let rec aux_msgid_list string idx stop accu =
+	if idx = stop then
+	  accu
+	else
+	  aux_get string (idx + 20) stop (String.sub string idx 20)::accu
+      in
+      match Chr.code buffer.[start] with
+	0 -> (* TODO add the proof of work *) 
+	  let nnym = Chr.code buffer.[start+4] in
+	  let rec aux_create buffer idx stop accu =
+	    if idx = stop then
+	      accu
+	    else
+	      let nl = Chr.code buffer.[idx] in
+	      aux_create buffer (idx + nl + 1) stop (String.sub buffer (idx+1) nl)::accu
+	  in
+	  Create(aux_create buffer (start+5) (start+4+cs) [])
+      | 1 -> Create2(String.sub buffer (start+4) cs)
+      | 2 -> Surb([]) (*TODO change this when we add the surb properly*)
+      | 3 -> 
+	  let id_l = 0 in
+	  Newpk( String.sub buffer (start + 4) id_l , String.sub buffer (start + 4 + id_l) (cs - id_l))
+      | 4 ->
+      | 5 ->
+	  if cs mod 20 != 0 then Get([]) (* if you cannot form a Get request correctly it is ignored *)
+	  else
+	    Get(aux_msgid_list buffer (start + 4) (start + 4 + cs) [])
+      | 6 ->
+	  if cs != 22 then
+	    Summarize(0,"")
+	  else
+	    let num = in
+	    let after = String.sub buffer (start + 6) 20 in
+	    Summarize(num,after)
+      | 7 ->
+	  if cs mod 20 != 0 then Delete([]) (* if you cannot form a Delete request correctly it is ignored *)
+	  else
+	    Delete(aux_msgid_list buffer (start + 4) (start + 4 + cs) [])
+      | 8 ->
+	  0,[]
+    in
+    n,m
+  with
+    _ -> raise Bad_argument;;
+  
 let read_command_list f s start =
   let rec aux buffer start accu =
     if start > (String.length buffer) then

Added: trunk/message.py
===================================================================
--- trunk/message.py	2004-07-10 15:15:01 UTC (rev 30)
+++ trunk/message.py	2004-07-27 09:55:49 UTC (rev 31)
@@ -0,0 +1,73 @@
+class BadMessage(Exception):
+        def __init__(self,value):
+                self.value=value
+        def __str__(self):
+                return repr(self.value)
+
+class BadArgument(Exception):
+        def __init__(self,value):
+                self.value=value
+        def __str__(self):
+                return repr(self.value)
+
+        
+sigLength = 256
+seqNoLength = 20
+
+class StrReader:
+        def __init__(self,s):
+            self.s = s
+            self.a = 0
+            self.b = 0
+        def sub(self):
+            return self.s[self.a:self.b]
+        def next(self,n):
+                self.a = self.b
+                self.b = self.b + n
+
+
+class Header:
+    def __str__(self):
+        return self.sig + chr(self.nl) + self.nym + self.seqNo
+
+class ReadHeader(Header):
+    def __init__(self,header):
+        """builds a header from the beginning of a string 
+        sig : signature
+        nl : nym length
+        nym : nym
+        seqNo : sequence number which identifies the message
+        length : header length"""
+        try:
+            sr = StrReader(header)
+            sr.next(sigLength)
+            self.sig = sr.sub()
+            sr.next(1)
+            self.nl = ord(sr.sub())
+            sr.next(nl)
+            self.nym = sr.sub()
+            sr.next(seqNoLength)
+            self.seqNo = sr.sub()
+            self.length = sr.b
+        except IndexError:
+            raise BadMessage("Bad formed message : header too short")
+        
+class BuiltHeader(Header):
+    def __init__(self,nym,seqNo):
+        """Builds a header from a nym and and sequence number
+        sig : signature
+        nl : nym length
+        nym : nym
+        seqNo : sequence number which identifies the message
+        length : header length"""
+        if(len(seqNo) != seqNoLength):
+            raise BadArgument("Buildheader.__init__ : seqNo length doesn't match seqNoLength")  
+        self.sig = chr(0) * sigLength
+        self.nl = len(nym)
+        self.nym = nym
+        self.seqNo = seqNo
+        self.length = sigLength + 1 + self.nl + seqNoLength
+        
+        
+H= BuiltHeader("JR","01234567890123456789")
+print H