X-Git-Url: http://git.pjr.cc/?p=pengine.git;a=blobdiff_plain;f=lib%2FinterComms.php;h=980a5573788be883b43b3da2549aad1996f4daa0;hp=1e7507ff0a770de5b74d5b44f0a20a519d3f0697;hb=HEAD;hpb=1450d5020fd9be9193327ac035605d8aaef29714 diff --git a/lib/interComms.php b/lib/interComms.php index 1e7507f..980a557 100644 --- a/lib/interComms.php +++ b/lib/interComms.php @@ -18,18 +18,21 @@ define("NETCOM_PORT_SSL", 14002); class netCom { - function __construct($am_i_a_server = false, $server_addr = "127.0.0.1") + 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")); @@ -70,96 +73,171 @@ class netCom { } // initiates a bind if its a server, a connect if its a client - function go() + function go($callback = "") { if($this->amserver) { echo "i am a server, bind!\n"; - $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - $this->listen_socket_ssl = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - socket_bind($this->listen_socket, "127.0.0.1", NETCOM_PORT); - socket_listen($this->listen_socket); - socket_bind($this->listen_socket_ssl, "127.0.0.1", NETCOM_PORT_SSL); - socket_listen($this->listen_socket_ssl); - echo "bound\n"; - } else { - echo "I am a client, connect!\n"; - $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - $res = socket_connect($this->socket, "127.0.0.1", NETCOM_PORT_SSL); - $this->secure_socket = true; - if(!$res) { - echo "fail on connect\n"; - socket_close($this->socket); + // here we fork our server + $pid = pcntl_fork(); + if($pid == -1) { return false; + // fork failure + } else if ($pid) { + return true; + // parent + } else { + // child + if($this->secure_server) { + $this->startSecureServerListener($callback); + } else { + $this->startServerListener($callback); + } } - echo "connected\n"; - - // now handle secure handshake; - if($this->secure_socket) { - echo "begin handshake\n"; - $msg = $this->receiveMessage(); - echo "got msg:\n"; - print_r($msg); - echo "\n"; + } else { + echo "I am a client, connect!\n"; + 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 { + $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $res = socket_connect($this->socket, $this->server, NETCOM_PORT); } } } - function waitForConnection() + function startSecureServerListener($callback) { - echo "in wait for connection\n"; + $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); - $this->socket = socket_accept($this->listen_socket); - $this->secure_socket = false; - echo "exit wait for connection\n"; + + 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 waitForSecureConnection() + + 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) { - echo "in wait for secure connection\n"; - socket_listen($this->listen_socket_ssl); - $this->socket = socket_accept($this->listen_socket_ssl); - $this->secure_socket = true; - - // now do negotiate - if($this->secure_socket) { - $arg[0] = $this->key_pub; - $this->sendMessage($arg); + $continue = true; + while($continue) { + // wait for a message + $msg_s = $this->receiveMessageInternal($socket); + if(!$msg_s) { + echo "Connection handler dieing 1\n"; + return false; + } + $msg = unserialize($msg_s); + $retmsg = $callback($msg); + $retval = $this->sendMessageInternal(serialize($retmsg), $socket); + if(!$retval) { + echo "Connection handler dieing 2\n"; + return false; + } + } + } + + function sendMessage($data) + { + $continue = true; + + $returned = false; + if($this->secure_server) { + $returned = $this->sendMessageSecure($data); + } else { + $returned = $this->sendMessageInsecure($data); } - echo "exit wait for connection\n"; + return $returned; + } + + function sendMessageInsecure($data) + { + $this->sendMessageInternal(serialize($data), $this->socket); + $returned = $this->receiveMessageInternal($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) { + echo "in smi, retval false\n"; + 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 { - echo "got ack\n"; + echo "smi got ack\n"; } + + return true; } - 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); + if($size === false) { + echo "recevie in rmi was false\n"; + return false; + } $recvd .= $recvd_a; echo "got $recvd_a so far for $size\n"; if($size == 0) return false; @@ -174,18 +252,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; } @@ -196,13 +274,12 @@ class netCom { private $encrypt; private $semKey; private $amserver; + private $secure_server; private $socket; - private $socket_ssl; private $listen_socket; - private $listen_socket_ssl; private $key_priv; private $key_pub; - private $secure_socket; + private $session_key; }