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"));
{
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 {
}
}
+ 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 {
}
}
- 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";
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;
}
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;
}