[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[minion-cvs] Many documentation/code cleanups, as suggested by Roger.



Update of /home/minion/cvsroot/src/minion/src
In directory moria.seul.org:/tmp/cvs-serv25507/src

Modified Files:
	_minionlib.h crypt.c main.c tls.c 
Log Message:
Many documentation/code cleanups, as suggested by Roger.

Also...

Common.py:
	- Recover from missing /usr/bin/shred.
	- Call waitpid properly

MMTPServer.py:
	- Handle interrupted select.
	- Call setsockopt correctly. (socket.SOL_SOCKET != 0, no matter what
          the example code I was reading might have said.)
	- Simplify maxBytes argument out of expectRead method.

Packet.py:
	- Be a little stricter about reply block length.

PacketHandler.py:
	- A list of private keys requires a list of hash logs.

Queue.py:
	- Avoid having multiple instances of shred running at once; they
	  seem to step on one another's toes.
	- Add more bits to a handle.

__init__.py:
	- Make __init__.py act like a regular __init__ file.


test.py:
	- Be a bit more careful about shredding files and closing sockets.

_minionlib.h:
	- Refactor individual METHOD macros into a common declaration.

crypt.c:
	- Replace an impossible error with an assert
	
main.c:
	- More comments

tls.c:
	- Better description of SSL_ERROR_SYSCALL




Index: _minionlib.h
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/_minionlib.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- _minionlib.h	24 Jun 2002 20:28:19 -0000	1.4
+++ _minionlib.h	1 Jul 2002 18:03:05 -0000	1.5
@@ -11,9 +11,14 @@
 #error "Mixminion requires OpenSSL 0.9.7 (which might not have been released yet, but you can get snapshots from openssl.org)."
 #endif
 
+/* We provide our own implementation of counter mode; see aes_ctr.c
+ */
 void mm_aes_counter128(const char *in, char *out, unsigned int len, 
 		       AES_KEY *key, unsigned long count);
 
+/* Propagate an error from OpenSSL.  If 'crypto', it's a cryptography
+ * error.  Else, it's a TLS error.
+ */
 void mm_SSL_ERR(int crypto);
 
 extern PyTypeObject mm_RSA_Type;
@@ -26,9 +31,20 @@
 extern PyTypeObject mm_TLSContext_Type;
 extern PyTypeObject mm_TLSSock_Type;
 
+/**
+ * Macros to declare function prototypes with the proper signatures for Python.
+ **/
 #define FUNC(fn) PyObject* fn(PyObject *self, PyObject *args, PyObject *kwdict)
 #define DOC(fn) extern const char fn##__doc__[]
 #define FUNC_DOC(fn) FUNC(fn); DOC(fn)
+
+
+/* Macro to declare entries for a method table.
+ */
+#define METHOD(obj, name) { #name, (PyCFunction)obj##_##name, \
+                        METH_VARARGS|METH_KEYWORDS,       \
+                        (char*)obj##_##name##__doc__ }
+
 
 /* Functions from crypt.c */
 FUNC_DOC(mm_sha1);

Index: crypt.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/crypt.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- crypt.c	24 Jun 2002 20:28:19 -0000	1.5
+++ crypt.c	1 Jul 2002 18:03:05 -0000	1.6
@@ -213,7 +213,6 @@
 		TYPE_ERR("Mismatch between argument lengths");
 		return NULL;
 	}
-
 	
 	if (!(output = PyString_FromStringAndSize(NULL,s1len))) { 
 		PyErr_NoMemory(); 
@@ -245,7 +244,6 @@
 					 kwlist,
 					 &seed, &seedlen))
 		return NULL;
-
 	
 	RAND_seed(seed, seedlen);
 	Py_INCREF(Py_None);
@@ -289,10 +287,8 @@
 	int keylen, i;
 	char *out;
 	PyObject *output;
-	if (!mm_RSA_Check(self)) {
-		TYPE_ERR("Called RSA method with non-RSA object.");
-		return NULL;
-	}
+	assert(mm_RSA_Check(self));
+
 	if (!PyArg_ParseTupleAndKeywords(args, kwdict, 
 					 "s#ii:crypt", kwlist,
 					 &string, &stringlen, &pub, &encrypt))
@@ -376,10 +372,7 @@
 	PyObject *output;
 	unsigned char *out, *outp;
 
-	if (!mm_RSA_Check(self)) {
-		TYPE_ERR("Called RSA method with non-RSA object.");
-		return NULL;
-	}
+	assert(mm_RSA_Check(self));
 	if (!PyArg_ParseTupleAndKeywords(args, kwdict, 
 					 "i:rsa_encode_key", kwlist, &public))
 		return NULL;
@@ -507,10 +500,7 @@
 	PyObject *n, *e;
 	PyObject *output;
 
-	if (!mm_RSA_Check(self)) {
-		TYPE_ERR("Called RSA method with non-RSA object.");
-		return NULL;
-	}
+	assert(mm_RSA_Check(self));
 	if (!PyArg_ParseTupleAndKeywords(args, kwdict, 
 					 ":rsa_get_public_key", kwlist))
 		return NULL;
@@ -570,10 +560,7 @@
 	static char *kwlist[] = { NULL };
 	RSA *rsa;
 
-	if (!mm_RSA_Check(self)) {
-		TYPE_ERR("Called RSA method with non-RSA object.");
-		return NULL;
-	}
+	assert(mm_RSA_Check(self));
 	rsa = ((mm_RSA*)self)->rsa;
 	if (!PyArg_ParseTupleAndKeywords(args, kwdict,
 					 ":get_modulus_bytes", kwlist))
@@ -582,16 +569,11 @@
 	return PyInt_FromLong(BN_num_bytes(rsa->n));
 }
 
- 
-#define METHOD(name) { #name, (PyCFunction)mm_RSA_##name, \
-                        METH_VARARGS|METH_KEYWORDS,       \
-                        (char*)mm_RSA_##name##__doc__ }
-
 static PyMethodDef mm_RSA_methods[] = {
-	METHOD(crypt),
-	METHOD(encode_key),
-	METHOD(get_modulus_bytes),
-	METHOD(get_public_key),
+	METHOD(mm_RSA, crypt),
+	METHOD(mm_RSA, encode_key),
+	METHOD(mm_RSA, get_modulus_bytes),
+	METHOD(mm_RSA, get_public_key),
 	{ NULL, NULL }
 };
  
@@ -694,7 +676,6 @@
 		PyErr_SetString(mm_CryptoError, "Bad padding");
 		return NULL;
 	}
-	
 	
 	if (!(output = PyString_FromStringAndSize(NULL,keylen))) { 
 		PyErr_NoMemory(); return NULL; 

Index: main.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/main.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- main.c	24 Jun 2002 20:28:19 -0000	1.3
+++ main.c	1 Jul 2002 18:03:05 -0000	1.4
@@ -1,11 +1,19 @@
 /* Copyright 2002 Nick Mathewson.  See LICENSE for licensing information */
 /* $Id$ */
+
+/*
+  If you're not familiar with writing Python extensions, you should
+  read "Extending and Embedding the Python Interpreter" at
+  "http://www.python.org/doc/current/ext/ext.html";.
+*/
+
 #include <_minionlib.h>
 
 #include <openssl/ssl.h>
 #include <openssl/err.h>
 #include <openssl/rsa.h>
 
+/* Macros to declare function tables for Python. */
 #define ENTRY_ND(fn) { #fn, (PyCFunction)mm_##fn, METH_VARARGS|METH_KEYWORDS,\
                        0}
 #define ENTRY(fn) { #fn, (PyCFunction)mm_##fn, METH_VARARGS|METH_KEYWORDS, \
@@ -27,7 +35,16 @@
 	{ NULL, NULL }
 };
 
-/* return 1 on failure. */
+/* Helper method to create an exception object and register it in a
+   module's dictionary.
+
+   module_dict: A PyDictObject* for the module's namespace.
+   exception: Set to point to a pointer to the newly allocated exception.
+   longName: The fully qualified name of this exception.
+   itemString: The name of this exception within the module.
+   doc: The doctring for this exception.
+
+   returns 1 on failure; 0 on success */
 static int
 exc(PyObject *module_dict, PyObject **exception, char *longName, 
     char *itemString, char *doc)
@@ -52,6 +69,9 @@
 	return 0;
 }
 
+/* Required by Python: magic method to tell the Python runtime about our
+ * new module and its contents.  Also initializes OpenSSL as needed.
+ */
 DL_EXPORT(void)
 init_minionlib(void)
 {
@@ -59,7 +79,6 @@
 	m = Py_InitModule("_minionlib", _mixcryptlib_functions);
 	d = PyModule_GetDict(m);
 
-
 	SSL_library_init();
 	SSL_load_error_strings();
 
@@ -85,14 +104,14 @@
 		return;
 
 	Py_INCREF(&mm_TLSContext_Type);
-	if (PyDict_SetItemString(d, "TLSContext", (PyObject*)&mm_TLSContext_Type) < 0)
+	if (PyDict_SetItemString(d, "TLSContext", 
+				 (PyObject*)&mm_TLSContext_Type) < 0)
 		return;
 
 	Py_INCREF(&mm_TLSSock_Type);
-	if (PyDict_SetItemString(d, "TLSSock", (PyObject*)&mm_TLSSock_Type) < 0)
+	if (PyDict_SetItemString(d, "TLSSock", 
+				 (PyObject*)&mm_TLSSock_Type) < 0)
 		return;
-
-
 }
 
 /*

Index: tls.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/tls.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- tls.c	25 Jun 2002 11:41:08 -0000	1.2
+++ tls.c	1 Jul 2002 18:03:05 -0000	1.3
@@ -20,7 +20,12 @@
 "Exception raised when a non-blocking TLS operation would block on writing.\n";
 PyObject *mm_TLSWantWrite = NULL;
 
+/* Convenience macro to set a type error with a given string. */
 #define TYPE_ERR(s) PyErr_SetString(PyExc_TypeError, s)
+
+/* Convenience macro to set an error and quit if a 0-argument function
+   was called with arguments.  (We can't just use 'METH_NOARGS', since
+   that wasn't available in Python 2.0.) */
 #define FAIL_IF_ARGS() if (PyTuple_Size(args)) { \
                            TYPE_ERR("No arguments expected"); \
                            return NULL; \
@@ -54,6 +59,9 @@
 		  PyErr_SetNone(mm_TLSWantWrite);
 		  return ERROR;
  	  case SSL_ERROR_SYSCALL:
+		  /* ???? We may want to act differently here; this is
+		   * ???? (almost?) always an unexpected close. 
+		   */
  	  default:
 		  mm_SSL_ERR(0);
 		  return ERROR;
@@ -201,12 +209,8 @@
 	return (PyObject*)ret;
 }
 
-#define METHOD(name) { #name, (PyCFunction)mm_TLSContext_##name, \
-                        METH_VARARGS|METH_KEYWORDS,       \
-                        (char*)mm_TLSContext_##name##__doc__ }
-
 static PyMethodDef mm_TLSContext_methods[] = {
-	METHOD(sock),
+	METHOD(mm_TLSContext, sock),
 	{ NULL, NULL }
 };
  
@@ -495,20 +499,15 @@
 	return (PyObject*) result;
 }
 
-#undef METHOD
-#define METHOD(name) { #name, (PyCFunction)mm_TLSSock_##name, \
-                        METH_VARARGS|METH_KEYWORDS,       \
-                        (char*)mm_TLSSock_##name##__doc__ }
-
 static PyMethodDef mm_TLSSock_methods[] = {
-	METHOD(accept),
-	METHOD(connect),
-	METHOD(pending),
-	METHOD(read),
-	METHOD(write),
-	METHOD(shutdown),
-	METHOD(get_peer_cert_pk),
-	METHOD(fileno),
+	METHOD(mm_TLSSock, accept),
+	METHOD(mm_TLSSock, connect),
+	METHOD(mm_TLSSock, pending),
+	METHOD(mm_TLSSock, read),
+	METHOD(mm_TLSSock, write),
+	METHOD(mm_TLSSock, shutdown),
+	METHOD(mm_TLSSock, get_peer_cert_pk),
+	METHOD(mm_TLSSock, fileno),
 	{ NULL, NULL }
 };