Author: dbkr
Date: 2006-07-23 14:16:56 +0000 (Sun, 23 Jul 2006)
New Revision: 9731

Modified:
   trunk/apps/Freemail/src/freemail/AckProcrastinator.java
   trunk/apps/Freemail/src/freemail/MessageSender.java
   trunk/apps/Freemail/src/freemail/OutboundContact.java
   trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
Log:
Send and poll for CTS messages, along with RTS retransmission


Modified: trunk/apps/Freemail/src/freemail/AckProcrastinator.java
===================================================================
--- trunk/apps/Freemail/src/freemail/AckProcrastinator.java     2006-07-23 
04:17:59 UTC (rev 9730)
+++ trunk/apps/Freemail/src/freemail/AckProcrastinator.java     2006-07-23 
14:16:56 UTC (rev 9731)
@@ -9,6 +9,7 @@
 import freemail.utils.PropsFile;
 import freemail.fcp.HighLevelFCPClient;
 import freemail.fcp.FCPBadFileException;
+import freemail.fcp.FCPInsertErrorMessage;

 /** Takes simple pieces of data to insert to keys and inserts them at some 
point
  * randomly within a given time frame in order to disguise the time at which 
messages
@@ -62,9 +63,14 @@

                                        ByteArrayInputStream bis = new 
ByteArrayInputStream(data.getBytes());

+                                       System.out.println("Inserting ack to 
"+key);
                                        try {
-                                               if (fcpcli.put(bis, key) != 
null)
+                                               FCPInsertErrorMessage err = 
fcpcli.put(bis, key);
+                                               if (err == null) {
                                                        acks[i].delete();
+                                               } else if (err.errorcode == 
FCPInsertErrorMessage.COLLISION) {
+                                                       acks[i].delete();
+                                               }
                                        } catch (FCPBadFileException bfe) {
                                                // won't occur
                                        }

Modified: trunk/apps/Freemail/src/freemail/MessageSender.java
===================================================================
--- trunk/apps/Freemail/src/freemail/MessageSender.java 2006-07-23 04:17:59 UTC 
(rev 9730)
+++ trunk/apps/Freemail/src/freemail/MessageSender.java 2006-07-23 14:16:56 UTC 
(rev 9731)
@@ -75,6 +75,7 @@
                                        outbox.mkdir();

                                this.sendDir(files[i], outbox);
+                               this.checkCTSs(files[i]);
                        }
                        // don't spin around the loop if nothing's
                        // going on
@@ -89,6 +90,21 @@
                }
        }

+       private void checkCTSs(File accdir) {
+               File contactsdir = new File(accdir, 
SingleAccountWatcher.CONTACTS_DIR);
+               
+               File outbounddir = new File(contactsdir, 
OutboundContact.OUTBOUND_DIR);
+               
+               File[] contacts = outbounddir.listFiles();
+               
+               int i;
+               for (i = 0; i < contacts.length; i++) {
+                       OutboundContact outboundcontact = new 
OutboundContact(accdir, contacts[i]);
+                       
+                       outboundcontact.checkCTS();
+               }
+       }
+       
        private void sendDir(File accdir, File dir) {
                File[] files = dir.listFiles();
                for (int i = 0; i < files.length; i++) {

Modified: trunk/apps/Freemail/src/freemail/OutboundContact.java
===================================================================
--- trunk/apps/Freemail/src/freemail/OutboundContact.java       2006-07-23 
04:17:59 UTC (rev 9730)
+++ trunk/apps/Freemail/src/freemail/OutboundContact.java       2006-07-23 
14:16:56 UTC (rev 9731)
@@ -13,6 +13,8 @@
 import freemail.fcp.HighLevelFCPClient;
 import freemail.fcp.SSKKeyPair;

+import org.archive.util.Base32;
+
 import org.bouncycastle.crypto.digests.SHA256Digest;
 import org.bouncycastle.crypto.params.RSAKeyParameters;
 import org.bouncycastle.crypto.AsymmetricBlockCipher;
@@ -20,11 +22,15 @@
 import org.bouncycastle.crypto.InvalidCipherTextException;

 public class OutboundContact {
+       public static final String OUTBOUND_DIR = "outbound";
        private final PropsFile contactfile;
        private final File accdir;
        private final EmailAddress address;
-       private static final String OUTBOUND_DIR = "outbound";
        private static final int CTS_KSK_LENGTH = 32;
+       // how long to wait for a CTS before sending the message again
+       // slightly over 24 hours since some people are likley to fire Freemail
+       // up and roughly the same time every day
+       private static final long CTS_WAIT_TIME = 26 * 60 * 60 * 1000;

        public OutboundContact(File accdir, EmailAddress a) throws 
BadFreemailAddressException {
                this.address = a;
@@ -47,6 +53,69 @@
                }
        }

+       public OutboundContact(File accdir, File ctfile) {
+               this.accdir = accdir;
+               this.address = new EmailAddress();
+               this.address.domain = 
Base32.encode(ctfile.getName().getBytes())+".freemail";
+               
+               this.contactfile = new PropsFile(ctfile);
+       }
+       
+       public void checkCTS() {
+               String status = this.contactfile.get("status");
+               if (status == null) {
+                       try {
+                               this.init();
+                       } catch (OutboundContactFatalException obctfe) {
+                               // impossible
+                       }
+               }
+               
+               if (status.equals("cts-received")) {
+                       return;
+               } else if (status.equals("rts-sent")) {
+                       // poll for the CTS message
+                       
+                       String ctsksk = this.contactfile.get("ctsksk");
+                       if (ctsksk == null) {
+                               try {
+                                       this.init();
+                               } catch (OutboundContactFatalException obctfe) {
+                                       // impossible
+                               }
+                       }
+                       
+                       HighLevelFCPClient fcpcli = new HighLevelFCPClient();
+                       
+                       File cts = fcpcli.fetch(ctsksk);
+                       
+                       if (cts == null) {
+                               // haven't got the CTS message. should we give 
up yet?
+                               String senttime = 
this.contactfile.get("rts-sent-at");
+                               
+                               if (senttime == null || 
Long.parseLong(senttime) > System.currentTimeMillis() + CTS_WAIT_TIME) {
+                                       // yes, send another RTS
+                                       try {
+                                               this.init();
+                                       } catch (OutboundContactFatalException 
obctfe) {
+                                               // impossible
+                                       }
+                               }
+                               
+                       } else {
+                               System.out.println("Sucessfully received CTS 
for "+this.address.getMailsiteKey());
+                               cts.delete();
+                               this.contactfile.put("status", "cts-received");
+                       }
+               } else {
+                       try {
+                               this.init();
+                       } catch (OutboundContactFatalException obctfe) {
+                               // impossible
+                       }
+               }
+       }
+       
        /*
         * Whether or not we're ready to communicate with the other party
         */
@@ -131,6 +200,21 @@
                return rtsksk;
        }

+       private String getCTSKSK() {
+               String retval = this.contactfile.get("ctsksk");
+               
+               if (retval != null) return retval;
+               
+               Random rnd = new Random();
+               retval = new String("KSK@");
+                       
+               int i;
+               for (i = 0; i < CTS_KSK_LENGTH; i++) {
+                       retval += (char)(rnd.nextInt(25) + (int)'a');
+               }
+               return retval;
+       }
+       
        /**
         * Set up an outbound contact. Fetch the mailsite, generate a new SSK 
keypair and post an RTS message to the appropriate KSK.
         * Will block for mailsite retrieval and RTS insertion
@@ -155,14 +239,9 @@

                rtsmessage.append("ackssk="+ackssk.privkey+"\r\n");

-               Random rnd = new Random();
-               String ctsksk = new String("KSK@");
-                       
-               int i;
-               for (i = 0; i < CTS_KSK_LENGTH; i++) {
-                       ctsksk += (char)(rnd.nextInt(25) + (int)'a');
-               }
+               String ctsksk = this.getCTSKSK();

+               this.contactfile.put("ctsksk", ctsksk);
                rtsmessage.append("ctsksk="+ctsksk+"\r\n");

                rtsmessage.append("messagetype=rts\r\n");
@@ -225,6 +304,8 @@

                // remember the fact that we have successfully inserted the rts
                this.contactfile.put("status", "rts-sent");
+               // and remember when we sent it!
+               this.contactfile.put("rts-sent-at", 
Long.toString(System.currentTimeMillis()));

                return true;
        }

Modified: trunk/apps/Freemail/src/freemail/utils/EmailAddress.java
===================================================================
--- trunk/apps/Freemail/src/freemail/utils/EmailAddress.java    2006-07-23 
04:17:59 UTC (rev 9730)
+++ trunk/apps/Freemail/src/freemail/utils/EmailAddress.java    2006-07-23 
14:16:56 UTC (rev 9731)
@@ -47,6 +47,9 @@
                }
        }

+       public EmailAddress() {
+       }
+       
        public boolean is_freemail_address() {
                if (this.domain == null) return false;
                if (!this.domain.endsWith(".freemail")) return false;


Reply via email to