Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function export($addr, $port, $str) {
- // All queries must begin with a question mark (ie "?players")
- if($str{0} != '?') $str = ('?' . $str);
- /* --- Prepare a packet to send to the server (based on a reverse-engineered packet structure) --- */
- $querysize = pack('n', strlen($str) + 6);
- $query = "\x00\x83" . pack('n', strlen($str) + 6) . "\x00\x00\x00\x00\x00" . $str . "\x00";
- /* --- Create a socket and connect it to the server --- */
- $server = socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or die('Unable to create export socket.');
- if(!socket_connect($server,$addr,$port)) {
- return "ERROR";
- }
- /* --- Send bytes to the server. Loop until all bytes have been sent --- */
- $bytestosend = strlen($query);
- $bytessent = 0;
- while ($bytessent < $bytestosend) {
- //echo $bytessent.'<br>';
- $result = socket_write($server,substr($query,$bytessent),$bytestosend-$bytessent);
- //echo 'Sent '.$result.' bytes<br>';
- if ($result===FALSE) die(socket_strerror(socket_last_error()));
- $bytessent += $result;
- }
- /* --- Idle for a while until recieved bytes from game server --- */
- $result = socket_read($server, 10000, PHP_BINARY_READ);
- socket_close($server); // we don't need this anymore
- if($result != "") {
- if($result{0} == "\x00" || $result{1} == "\x83") { // make sure it's the right packet format
- // Actually begin reading the output:
- $sizebytes = unpack('n', $result{2} . $result{3}); // array size of the type identifier and content
- $size = $sizebytes[1] - 1; // size of the string/floating-point (minus the size of the identifier byte)
- if($result{4} == "\x2a") { // 4-byte big-endian floating-point
- $unpackint = unpack('f', $result{5} . $result{6} . $result{7} . $result{8}); // 4 possible bytes: add them up together, unpack them as a floating-point
- return $unpackint[1];
- }
- else if($result{4} == "\x06") { // ASCII string
- $unpackstr = ""; // result string
- $index = 5; // string index
- while($size > 0) { // loop through the entire ASCII string
- $size--;
- $unpackstr = $unpackstr . $result{$index}; // add the string position to return string
- $index++;
- }
- return $unpackstr;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement