-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
52 lines (39 loc) · 1.32 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/* gm(): function for generating messages
it appends to the start of the string a byte value of the length of the string
i.e. if we want to write "abc", so string length of 3
it will return the string "\0x03" . "abc"
this function is valid up to length of 254, above is more complex
*/
function gm($str) {
$len = strlen($str);
return chr($len) . $str;
}
/* rm(): function for reading messages
it reads first the first byte, which is the length of the message
then it reads the stream for the received amount of bytes and returns it
this function is valid up to length of 254, above is more complex
*/
function rm() {
global $fp;
$len = ord(fgetc($fp));
$str = fread($fp, $len);
return $str;
}
//we open a localhost connection to IPFS, started with --disable-transport-encryption
//this is to disable encryption to make things simpler
$fp = fsockopen('localhost', 4001);
//read intro which should be /multistream/1.0.0\n
echo "Rx: " . rm();
//we send back the same thing
$tx = "/multistream/1.0.0\n";
echo "Tx: " . $tx;
fwrite($fp, gm($tx));
//we send that we want to initiate a plaintext session
$tx = "/plaintext/1.0.0\n";
echo "Tx: " . $tx;
fwrite($fp, gm($tx));
//we receive back the same thing, or na\n
//if we haven't started IPFS with ipfs --disable-transport-encryption
echo "Rx: " . rm();
?>