protocol.txt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. Minetest-c55 protocol (incomplete, early draft):
  2. Updated 2011-06-18
  3. A custom protocol over UDP.
  4. Integers are big endian.
  5. Refer to connection.{h,cpp} for further reference.
  6. Initialization:
  7. - A dummy reliable packet with peer_id=PEER_ID_INEXISTENT=0 is sent to the server:
  8. - Actually this can be sent without the reliable packet header, too, i guess,
  9. but the sequence number in the header allows the sender to re-send the
  10. packet without accidentally getting a double initialization.
  11. - Packet content:
  12. # Basic header
  13. u32 protocol_id = PROTOCOL_ID = 0x4f457403
  14. u16 sender_peer_id = PEER_ID_INEXISTENT = 0
  15. u8 channel = 0
  16. # Reliable packet header
  17. u8 type = TYPE_RELIABLE = 3
  18. u16 seqnum = SEQNUM_INITIAL = 65500
  19. # Original packet header
  20. u8 type = TYPE_ORIGINAL = 1
  21. # And no actual payload.
  22. - Server responds with something like this:
  23. - Packet content:
  24. # Basic header
  25. u32 protocol_id = PROTOCOL_ID = 0x4f457403
  26. u16 sender_peer_id = PEER_ID_INEXISTENT = 0
  27. u8 channel = 0
  28. # Reliable packet header
  29. u8 type = TYPE_RELIABLE = 3
  30. u16 seqnum = SEQNUM_INITIAL = 65500
  31. # Control packet header
  32. u8 type = TYPE_CONTROL = 0
  33. u8 controltype = CONTROLTYPE_SET_PEER_ID = 1
  34. u16 peer_id_new = assigned peer id to client (other than 0 or 1)
  35. - Then the connection can be disconnected by sending:
  36. - Packet content:
  37. # Basic header
  38. u32 protocol_id = PROTOCOL_ID = 0x4f457403
  39. u16 sender_peer_id = whatever was gotten in CONTROLTYPE_SET_PEER_ID
  40. u8 channel = 0
  41. # Control packet header
  42. u8 type = TYPE_CONTROL = 0
  43. u8 controltype = CONTROLTYPE_DISCO = 3
  44. - Here's a quick untested connect-disconnect done in PHP:
  45. # host: ip of server (use gethostbyname(hostname) to get from a dns name)
  46. # port: port of server
  47. function check_if_minetestserver_up($host, $port)
  48. {
  49. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  50. $timeout = array("sec" => 1, "usec" => 0);
  51. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
  52. $buf = "\x4f\x45\x74\x03\x00\x00\x00\x03\xff\xdc\x01";
  53. socket_sendto($socket, $buf, strlen($buf), 0, $host, $port);
  54. $buf = socket_read($socket, 1000);
  55. if($buf != "")
  56. {
  57. # We got a reply! read the peer id from it.
  58. $peer_id = substr($buf, 9, 2);
  59. # Disconnect
  60. $buf = "\x4f\x45\x74\x03".$peer_id."\x00\x00\x03";
  61. socket_sendto($socket, $buf, strlen($buf), 0, $host, $port);
  62. socket_close($socket);
  63. return true;
  64. }
  65. return false;
  66. }