2 define("NETCOM_PORT", 14001);
3 define("NETCOM_PORT_SSL", 14002);
6 // comms on this thing are very serial, the server are only capable of processing
7 // one thing at a time... for now
9 // the encryption we use here yes, it will be pub/priv but not ssl per se.
10 // how encryption works:
11 // initiate connection:
12 // server -> PEN:b64enc(pubkey):INE
13 // client -> PEN:ACK:INE
14 // client -> PEN:encrypt(pubkey, session key):INE
15 // server -> PEN:ACK:INE
16 // client -> PEN:encrypt(data, session key):INE <-- data transmission starts here
17 // server -> PEN:ACK:INE
21 function __construct($am_i_a_server = false, $server_addr = "127.0.0.1", $secure = false)
23 global $storeLocation;
25 // i have to set it to something, right?
26 $this->semKey = ftok(__FILE__, "p");
27 $this->encrypt = $secure;
29 $this->amserver = $am_i_a_server;
30 $this->server = $server_addr;
32 $this->secure_server = $secure;
34 // this bit needs to be a bit more modular
35 if($this->amserver && $this->secure_server) if(is_file("$storeLocation/mykey.priv")) {
37 $kh = fopen("$storeLocation/mykey.priv", "r");
38 $kdp = fread($kh, filesize("$storeLocation/mykey.priv"));
40 $key = openssl_pkey_get_private($kdp);
42 $km = openssl_pkey_export($key, $output);
43 echo "key is $output\n";
44 $this->key_priv = $output;
46 $ar_pubkey = openssl_pkey_get_details($key);
47 $this->key_pub = $ar_pubkey["key"];
51 echo "generateing key\n";
52 $key = openssl_pkey_new();
53 echo "key generated $key\n";
55 $km = openssl_pkey_export($key, $output);
56 echo "key is $output\n";
57 $ar_pubkey = openssl_pkey_get_details($key);
58 $pubkey = $ar_pubkey["key"];
59 echo "array is $pubkey\n";
60 // now lets write some shit
61 $priv_f = fopen("$storeLocation/mykey.priv", "w");
62 fwrite($priv_f, $output);
63 $this->key_priv = $output;
66 $pub_f = fopen("$storeLocation/mykey.pub", "w");
67 fwrite($pub_f, $pubkey);
68 $this->key_pub = $pubkey;
75 // initiates a bind if its a server, a connect if its a client
76 function go($callback = "")
79 echo "i am a server, bind!\n";
80 // here we fork our server
90 if($this->secure_server) {
91 $this->startSecureServerListener($callback);
93 $this->startServerListener($callback);
97 echo "I am a client, connect!\n";
98 if($this->secure_server) {
99 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
100 $res = socket_connect($this->socket, $this->server, NETCOM_PORT_SSL);
102 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
103 $res = socket_connect($this->socket, $this->server, NETCOM_PORT);
109 function startSecureServerListener($callback)
111 $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
112 socket_bind($this->listen_socket, $this->server, NETCOM_PORT_SSL);
113 socket_listen($this->listen_socket);
116 $newsock = socket_accept($this->listen_socket);
117 echo "Have a new conneciton\n";
118 // i now have a connection
120 if($pid == -1) return false;
122 secureConnection($callback, $newsock);
127 function secureConnection($callback, $socket)
129 // when the secure conneciton starts, we send out pub key
130 sendMessageInternal($this->key_pub, $socket);
132 // then we get a session key
133 $sesskey_enc = receiveMessageInternal($socket);
135 // which we decrypt using our priv key and store.
136 $this->session_key = openssl_private_decypt();
138 // now we go into a message loop.
141 function startServerListener($callback)
143 $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
144 socket_bind($this->listen_socket, $this->server, NETCOM_PORT);
145 socket_listen($this->listen_socket);
148 $newsock = socket_accept($this->listen_socket);
149 echo "Have a new conneciton\n";
150 // i now have a connection
152 if($pid == -1) return false;
154 $this->insecureConnection($callback, $newsock);
159 function insecureConnection($callback, $socket)
163 // wait for a message
164 $msg = unserialize($this->receiveMessageInternal($socket));
165 $retmsg = $callback($msg);
166 $retval = $this->sendMessageInternal(serialize($retmsg), $socket);
168 echo "Connection handler dieing\n";
174 function sendMessage($data)
179 if($this->secure_server) {
180 $returned = $this->sendMessageSecure($data);
182 $returned = $this->sendMessageInsecure($data);
188 function sendMessageInsecure($data)
190 $returned = $this->sendMessageInternal(serialize($data), $this->socket);
192 return unserialize($returned);
195 function sendMessageInternal($message_data, $comsock)
197 echo "begin send message\n";
198 $datacomp = base64_encode($message_data);
199 $tosend = "PEN:$datacomp:INE";
201 $retval = socket_send($comsock, $tosend, strlen($tosend), 0);
202 if(!$retval) return false;
203 echo "end send message $retval\n";
204 // get up to one meg of data - this is bad... i can feel this function
206 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
207 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
209 // we wait for an ack
210 $size = socket_recv($comsock, $recv, 1024, 0);
211 if($recv != "PEN:ACK:INE") {
212 echo "invalid response?\n$recv\n";
218 function receiveMessageInternal($comsock)
220 echo "begin recieve message\n";
224 $size = socket_recv($comsock, $recvd_a, 1024, 0);
227 echo "got $recvd_a so far for $size\n";
228 if($size == 0) return false;
229 if(preg_match("/.*\:INE$/", $recvd)) {
230 // we have a full string... break out
237 echo "rec msg next\n";
238 // first check we got something that makes sense
239 if(preg_match("/^PEN:.*:INE$/", $recvd) < 1) {
240 socket_close($comsock);
241 echo "Returned data is not in right format\n";
242 // we have a problem jim
245 $msg = "PEN:ACK:INE";
246 socket_send($comsock, $msg, strlen($msg), 0);
248 echo "got a data packet\n";
249 $xps = explode(":", $recvd);
251 $component = base64_decode($xps[1]);
262 private $secure_server;
264 private $listen_socket;
267 private $session_key;
272 * PEN:base64_enoded data:INE