Communicating with the server via sockets
Hello,
I am looking into the method of communicating with the server via php using sockets. I am aware that I am able achieve something similar by wgetting jsp files. However, for curiosity's sake, I wish to achieve this purely through php.
Based on results from my searches, I am led to believe that the method of communicating with the server via php is by using creating sockets to the port that each service is listening to. (29400, 29100, 29000 for GameDB, GDelivery, and Glink). After that, I must send packets to the service to retrieve information, and then receive packets back from the service and interpret them. Right now, I am confused as to what exactly I am supposed to send in the packets to each service to get the response I am looking for.
Through my searches, I only managed to find 1 example of a PhP script that use this method. (Here: http://forum.ragezone.com/f751/relea...painel-865975/) The script is used to check a character's inventory. The simplified script is as follow:
PHP Code:
function cuint($data) {
if($data < 64)
return strrev(pack("C", $data));
else if($data < 16384)
return strrev(pack("S", ($data | 0x8000)));
else if($data < 536870912)
return strrev(pack("I", ($data | 0xC0000000)));
return strrev(pack("c", -32) . pack("I", $data));
}
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$sock)
die(socket_strerror(socket_last_error()));
if(socket_connect($sock, "127.0.0.1", "29400")){
socket_set_block($sock);
$data = cuint(3053)."\x08\x80\x00\x00\x01".pack("N", $IDchar);
$sbytes = socket_send($sock, $data, 8192, 0);
$rbytes = socket_recv($sock, $buf, 8192, 0);
//Intrepret the packets received here
}
I do not fully understand the format of the $data. So far, the only thing that I can for sure conclude is that pack("N", $IDchar); is the character's id, since the function getRolePocket requires only 1 argument - the character's id.
I am not entirely sure as to the meaning of "\x08\x80\x00\x00\x01". However, changing it seemed to yield no effect, which was strange.
I believe cuint is used to convert a number to it's unsigned format, then changing its endian for the server.
However, I am unsure why the number 3053 is passed to this function. How does this number in anyway relate to the function getRolePocket? This is what I am mainly confused about. I do not understand why this data, when passed to gamedbd, tells the service to return the character's inventory. What is the purpose of getting the uint of 3053? And why 3053? How was it determined that getRolePocket is called by the uint 3053?
Any help or insight would be greatly appreciated. (Also, I apologies if this is the wrong section. Please move it if there is a better section for this)
Thanks in advance!
Re: Communicating with the server via sockets
I won't say much about this, as I hope it will remain a secret shared by me and a few other people whom I respect. I will leave it to other people to answer you in full length, i'll just give pointers.
CUINT is the packed int format introduced by microsoft in terms of MPPE/MPPC. It has nothing to do with converting signs, it is used to "pack" a literal to 1, 2, and 4 byte length depending on size. And also, this function is wrong.
As for the 3053, it is the packet opcode. The other hardcoded literals are packet length, and type/version.
As much as I hope that this remains a secret, you'll have much fun fighting with a few protocol specific "anomalies" later on :)
Re: Communicating with the server via sockets
Thanks, that actually explains quite a bit. I guess I will have to spend a while figuring out the opcode and interpreting everything. Looks like that will take some long hours.