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 = true)
23 global $storeLocation;
25 // i have to set it to something, right?
26 $this->semKey = ftok(__FILE__, "p");
27 $this->encrypt = false;
29 $this->amserver = $am_i_a_server;
30 $this->server = $server_addr;
32 if($this->amserver) if(is_file("$storeLocation/mykey.priv")) {
34 $kh = fopen("$storeLocation/mykey.priv", "r");
35 $kdp = fread($kh, filesize("$storeLocation/mykey.priv"));
37 $key = openssl_pkey_get_private($kdp);
39 $km = openssl_pkey_export($key, $output);
40 echo "key is $output\n";
41 $this->key_priv = $output;
43 $ar_pubkey = openssl_pkey_get_details($key);
44 $this->key_pub = $ar_pubkey["key"];
48 echo "generateing key\n";
49 $key = openssl_pkey_new();
50 echo "key generated $key\n";
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;
63 $pub_f = fopen("$storeLocation/mykey.pub", "w");
64 fwrite($pub_f, $pubkey);
65 $this->key_pub = $pubkey;
72 // initiates a bind if its a server, a connect if its a client
73 function go($callback = "")
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);
82 socket_bind($this->listen_socket, $this->server, NETCOM_PORT);
83 socket_listen($this->listen_socket);
85 // here we fork our server
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);
92 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
93 $res = socket_connect($this->socket, $this->server, NETCOM_PORT);
100 function sendMessage($message_array)
102 echo "begin send message\n";
103 $datacomp = base64_encode(serialize($message_array));
104 $tosend = "PEN:$datacomp:INE";
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
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
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";
122 function receiveMessage()
124 echo "begin recieve message\n";
128 $size = socket_recv($this->socket, $recvd_a, 1024, 0);
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
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
149 $msg = "PEN:ACK:INE";
150 socket_send($this->socket, $msg, strlen($msg), 0);
152 echo "got a data packet\n";
153 $xps = explode(":", $recvd);
155 $component = unserialize(base64_decode($xps[1]));
167 private $listen_socket;
170 private $secure_socket;
175 * PEN:base64_enoded data:INE