recoding the pengine intercomms class for more "what we want"
[pengine.git] / lib / interComms.php
1 <?php
2 define("NETCOM_PORT", 14001);
3 define("NETCOM_PORT_SSL", 14002);
4
5
6 // comms on this thing are very serial, the server are only capable of processing
7 // one thing at a time... for now
8
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
18
19 class netCom {
20         
21         function __construct($am_i_a_server = false, $server_addr = "127.0.0.1", $secure = true)
22         {
23                 global $storeLocation;
24                 
25                 // i have to set it to something, right?
26                 $this->semKey = ftok(__FILE__, "p");
27                 $this->encrypt = false;
28                 
29                 $this->amserver = $am_i_a_server;
30                 $this->server = $server_addr;
31                 
32                 if($this->amserver) if(is_file("$storeLocation/mykey.priv")) {
33                         echo "loading key\n";
34                         $kh = fopen("$storeLocation/mykey.priv", "r");
35                         $kdp = fread($kh, filesize("$storeLocation/mykey.priv"));
36                         
37                         $key = openssl_pkey_get_private($kdp);
38                         $output = "";
39                         $km = openssl_pkey_export($key, $output);
40                         echo "key is $output\n";
41                         $this->key_priv = $output;
42                         
43                         $ar_pubkey = openssl_pkey_get_details($key);
44                         $this->key_pub = $ar_pubkey["key"];
45                         
46                         
47                 } else {
48                         echo "generateing key\n";
49                         $key = openssl_pkey_new();
50                         echo "key generated $key\n";
51                         $output = "";
52                         $km = openssl_pkey_export($key, $output);
53                         echo "key is $output\n";
54                         $ar_pubkey = openssl_pkey_get_details($key);
55                         $pubkey = $ar_pubkey["key"];
56                         echo "array is $pubkey\n";
57                         // now lets write some shit
58                         $priv_f = fopen("$storeLocation/mykey.priv", "w");
59                         fwrite($priv_f, $output);
60                         $this->key_priv = $output;
61                         fclose($priv_f);
62                         
63                         $pub_f = fopen("$storeLocation/mykey.pub", "w");
64                         fwrite($pub_f, $pubkey);
65                         $this->key_pub = $pubkey;
66                         fclose($pub_f);
67                         
68                         
69                 }
70         }
71         
72         // initiates a bind if its a server, a connect if its a client
73         function go($callback = "")
74         {
75                 if($this->amserver) {
76                         echo "i am a server, bind!\n";
77                         $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
78                         if($this->secure_socket) {
79                                 socket_bind($this->listen_socket, $this->server, NETCOM_PORT_SSL);
80                                 socket_listen($this->listen_socket);
81                         } else {
82                                 socket_bind($this->listen_socket, $this->server, NETCOM_PORT);
83                                 socket_listen($this->listen_socket);
84                         }
85                         // here we fork our server
86                 } else {
87                         echo "I am a client, connect!\n";
88                         if($this->secure_socket) {
89                                 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
90                                 $res = socket_connect($this->socket, $this->server, NETCOM_PORT_SSL);
91                         } else {
92                                 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
93                                 $res = socket_connect($this->socket, $this->server, NETCOM_PORT);
94                                 
95                         }
96                 }
97         }
98         
99         
100         function sendMessage($message_array)
101         {               
102                 echo "begin send message\n";
103                 $datacomp = base64_encode(serialize($message_array));
104                 $tosend = "PEN:$datacomp:INE";
105                 
106                 socket_send($this->socket, $tosend, strlen($tosend), 0);
107                 echo "end send message\n";
108                 // get up to one meg of data - this is bad... i can feel this function
109                 // hurting alot
110                 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
111                 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
112                 // timeouts now.
113                 // we wait for an ack
114                 $size = socket_recv($this->socket, $recv, 1024, 0);
115                 if($recv != "PEN:ACK:INE") {
116                         echo "invalid response?\n$recv\n";
117                 } else {
118                         echo "got ack\n";
119                 }
120         }
121         
122         function receiveMessage()
123         {
124                 echo "begin recieve message\n";
125                 $recvd = "";
126                 $continue = true;
127                 while($continue) {
128                         $size = socket_recv($this->socket, $recvd_a, 1024, 0);
129                         
130                         $recvd .= $recvd_a;
131                         echo "got $recvd_a so far for $size\n";
132                         if($size == 0) return false;
133                         if(preg_match("/.*\:INE$/", $recvd)) {
134                                 // we have a full string... break out
135                                 $continue = false;
136                                 break;
137                         }
138                 }
139                 
140                 
141                 echo "rec msg next\n";
142                 // first check we got something that makes sense
143                 if(preg_match("/^PEN:.*:INE$/", $recvd) < 1) {
144                         socket_close($this->socket);
145                         echo "Returned data is not in right format\n";
146                         // we have a problem jim
147                         return false;
148                 }
149                 $msg = "PEN:ACK:INE";
150                 socket_send($this->socket, $msg, strlen($msg), 0);
151                 
152                 echo "got a data packet\n";
153                 $xps = explode(":", $recvd);
154                 
155                 $component =  unserialize(base64_decode($xps[1]));
156
157                 return $component;
158         }
159          
160         
161         
162         private $server;
163         private $encrypt;
164         private $semKey;
165         private $amserver;
166         private $socket;
167         private $listen_socket;
168         private $key_priv;
169         private $key_pub;
170         private $secure_socket;
171         
172 }
173
174 /* packets look like
175  * PEN:base64_enoded data:INE
176  * 
177  * 
178  */
179
180 ?>