protocol.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Luanti 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. }
  67. - Here's a Python script for checking if a Luanti server is up, confirmed working
  68. #!/usr/bin/env python3
  69. import sys, time, socket
  70. address = ""
  71. port = 30000
  72. if len(sys.argv) <= 1:
  73. print("Usage: %s <address>" % sys.argv[0])
  74. exit()
  75. if ":" in sys.argv[1]:
  76. address = sys.argv[1].split(":")[0]
  77. try:
  78. port = int(sys.argv[1].split(":")[1])
  79. except ValueError:
  80. print("Please specify a valid port")
  81. exit()
  82. else:
  83. address = sys.argv[1]
  84. try:
  85. start = time.time()
  86. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  87. sock.settimeout(2.0)
  88. buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
  89. sock.sendto(buf, (address, port))
  90. data, addr = sock.recvfrom(1000)
  91. if data:
  92. peer_id = data[12:14]
  93. buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
  94. sock.sendto(buf, (address, port))
  95. sock.close()
  96. end = time.time()
  97. print("%s is up (%0.5fms)" % (sys.argv[1], end - start))
  98. else:
  99. print("%s seems to be down " % sys.argv[1])
  100. except Exception as err:
  101. print("%s seems to be down (%s) " % (sys.argv[1], str(err)))