intercomm's work - or not work as the case may be
authorpaulr <me@pjr.cc>
Tue, 22 Mar 2011 16:20:15 +0000 (03:20 +1100)
committerpaulr <me@pjr.cc>
Tue, 22 Mar 2011 16:20:15 +0000 (03:20 +1100)
lib/interComms.php
unittests/commrecv.php
unittests/commsend.php

index 5f77261..69680ad 100644 (file)
@@ -18,18 +18,21 @@ define("NETCOM_PORT_SSL", 14002);
 
 class netCom {
        
-       function __construct($am_i_a_server = false, $server_addr = "127.0.0.1", $secure = true)
+       function __construct($am_i_a_server = false, $server_addr = "127.0.0.1", $secure = false)
        {
                global $storeLocation;
                
                // i have to set it to something, right?
                $this->semKey = ftok(__FILE__, "p");
-               $this->encrypt = false;
+               $this->encrypt = $secure;
                
                $this->amserver = $am_i_a_server;
                $this->server = $server_addr;
                
-               if($this->amserver) if(is_file("$storeLocation/mykey.priv")) {
+               $this->secure_server = $secure;
+               
+               // this bit needs to be a bit more modular
+               if($this->amserver && $this->secure_server) if(is_file("$storeLocation/mykey.priv")) {
                        echo "loading key\n";
                        $kh = fopen("$storeLocation/mykey.priv", "r");
                        $kdp = fread($kh, filesize("$storeLocation/mykey.priv"));
@@ -74,18 +77,25 @@ class netCom {
        {
                if($this->amserver) {
                        echo "i am a server, bind!\n";
-                       $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
-                       if($this->secure_socket) {
-                               socket_bind($this->listen_socket, $this->server, NETCOM_PORT_SSL);
-                               socket_listen($this->listen_socket);
+                       // here we fork our server
+                       $pid = pcntl_fork();
+                       if($pid == -1) {
+                               return false;
+                               // fork failure
+                       } else if ($pid) {
+                               return true;
+                               // parent
                        } else {
-                               socket_bind($this->listen_socket, $this->server, NETCOM_PORT);
-                               socket_listen($this->listen_socket);
+                               // child
+                               if($this->secure_server) {
+                                       $this->startSecureServerListener($callback);
+                               } else {
+                                       $this->startServerListener($callback);
+                               }
                        }
-                       // here we fork our server
                } else {
                        echo "I am a client, connect!\n";
-                       if($this->secure_socket) {
+                       if($this->secure_server) {
                                $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                                $res = socket_connect($this->socket, $this->server, NETCOM_PORT_SSL);
                        } else {
@@ -96,22 +106,108 @@ class netCom {
                }
        }
        
+       function startSecureServerListener($callback)
+       {
+               $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+               socket_bind($this->listen_socket, $this->server, NETCOM_PORT_SSL);
+               socket_listen($this->listen_socket);
+               
+               while(true) {
+                       $newsock = socket_accept($this->listen_socket);
+                       echo "Have a new conneciton\n";
+                       // i now have a connection
+                       $pid = pcntl_fork();
+                       if($pid == -1) return false;
+                       else if (!$pid) {
+                               secureConnection($callback, $newsock);  
+                       }
+               }               
+       }
+       
+       function secureConnection($callback, $socket)
+       {
+               // when the secure conneciton starts, we send out pub key
+               sendMessageInternal($this->key_pub, $socket);
+               
+               // then we get a session key
+               $sesskey_enc = receiveMessageInternal($socket);
+               
+               // which we decrypt using our priv key and store.
+               $this->session_key = openssl_private_decypt();
+               
+               // now we go into a message loop.
+       }
+       
+       function startServerListener($callback)
+       {
+               $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+               socket_bind($this->listen_socket, $this->server, NETCOM_PORT);
+               socket_listen($this->listen_socket);
+               
+               while(true) {
+                       $newsock = socket_accept($this->listen_socket);
+                       echo "Have a new conneciton\n";
+                       // i now have a connection
+                       $pid = pcntl_fork();
+                       if($pid == -1) return false;
+                       else if (!$pid) {
+                               $this->insecureConnection($callback, $newsock); 
+                       }
+               }               
+       }
+       
+       function insecureConnection($callback, $socket)
+       {
+               $continue = true;
+               while($continue) {
+                       // wait for a message
+                       $msg = unserialize($this->receiveMessageInternal($socket));
+                       $retmsg = $callback($msg);
+                       $retval = $this->sendMessageInternal(serialize($retmsg), $socket);
+                       if(!$retval) {
+                               echo "Connection handler dieing\n";
+                               $continue = false;
+                       }
+               }
+       }
+       
+       function sendMessage($data)
+       {
+               $continue = true;
+
+               $returned = false;
+               if($this->secure_server) {
+                       $returned = $this->sendMessageSecure($data);
+               } else {
+                       $returned = $this->sendMessageInsecure($data);
+               }
+               
+               return $returned;
+       }
+       
+       function sendMessageInsecure($data)
+       {
+               $returned = $this->sendMessageInternal(serialize($data), $this->socket);
+               
+               return unserialize($returned);
+       }
        
-       function sendMessage($message_array)
+       function sendMessageInternal($message_data, $comsock)
        {               
                echo "begin send message\n";
-               $datacomp = base64_encode(serialize($message_array));
+               $datacomp = base64_encode($message_data);
                $tosend = "PEN:$datacomp:INE";
                
-               socket_send($this->socket, $tosend, strlen($tosend), 0);
-               echo "end send message\n";
+               $retval = socket_send($comsock, $tosend, strlen($tosend), 0);
+               if(!$retval) return false;
+               echo "end send message $retval\n";
                // get up to one meg of data - this is bad... i can feel this function
                // hurting alot
                // TODO FIX THIS - its garbage code... im not really sure how to handle this really
                // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
                // timeouts now.
                // we wait for an ack
-               $size = socket_recv($this->socket, $recv, 1024, 0);
+               $size = socket_recv($comsock, $recv, 1024, 0);
                if($recv != "PEN:ACK:INE") {
                        echo "invalid response?\n$recv\n";
                } else {
@@ -119,13 +215,13 @@ class netCom {
                }
        }
        
-       function receiveMessage()
+       function receiveMessageInternal($comsock)
        {
                echo "begin recieve message\n";
                $recvd = "";
                $continue = true;
                while($continue) {
-                       $size = socket_recv($this->socket, $recvd_a, 1024, 0);
+                       $size = socket_recv($comsock, $recvd_a, 1024, 0);
                        
                        $recvd .= $recvd_a;
                        echo "got $recvd_a so far for $size\n";
@@ -141,18 +237,18 @@ class netCom {
                echo "rec msg next\n";
                // first check we got something that makes sense
                if(preg_match("/^PEN:.*:INE$/", $recvd) < 1) {
-                       socket_close($this->socket);
+                       socket_close($comsock);
                        echo "Returned data is not in right format\n";
                        // we have a problem jim
                        return false;
                }
                $msg = "PEN:ACK:INE";
-               socket_send($this->socket, $msg, strlen($msg), 0);
+               socket_send($comsock, $msg, strlen($msg), 0);
                
                echo "got a data packet\n";
                $xps = explode(":", $recvd);
                
-               $component =  unserialize(base64_decode($xps[1]));
+               $component =  base64_decode($xps[1]);
 
                return $component;
        }
@@ -163,11 +259,12 @@ class netCom {
        private $encrypt;
        private $semKey;
        private $amserver;
+       private $secure_server;
        private $socket;
        private $listen_socket;
        private $key_priv;
        private $key_pub;
-       private $secure_socket;
+       private $session_key;
        
 }
 
index 2061063..000ecef 100644 (file)
@@ -1,13 +1,16 @@
 <?php 
 require_once("../lib/lib.php");
 
-$mn = new netCom(true);
-
-$mn->go();
-while(true) {
-       $mn->waitForSecureConnection();
-       while(($msg = $mn->receiveMessage()) !== false) {
-               print_r($msg);
-       }
+function myfunction($msg)
+{
+       echo "got a msg\n";
+       print_r($msg);
+       echo "end message\n";
+       $returnmsg["0"] = "asdf";
+       
+       return $returnmsg; 
 }
+$mn = new netCom(true, "127.0.0.1", false);
+
+$pid = $mn->go("myfunction");
 ?>
\ No newline at end of file
index d1eb4d3..c37256b 100644 (file)
@@ -6,8 +6,9 @@ $mn->go();
 
 $array["asdf"] = "message";
 $array["other"] = "moogie";
-for($i = 0; $i < 1; $i++) {
+for($i = 0; $i < 10; $i++) {
        $array["bl"] = $i;
        $mn->sendMessage($array);
+       sleep(1);
 }
 ?>
\ No newline at end of file