summaryrefslogtreecommitdiff
path: root/doc/classes/DTLSServer.xml
blob: 5d8a2bc16d75468de3ed3402298d1f036f80bcff (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?xml version="1.0" encoding="UTF-8" ?>
<class name="DTLSServer" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
	<brief_description>
		Helper class to implement a DTLS server.
	</brief_description>
	<description>
		This class is used to store the state of a DTLS server. Upon [method setup] it converts connected [PacketPeerUDP] to [PacketPeerDTLS] accepting them via [method take_connection] as DTLS clients. Under the hood, this class is used to store the DTLS state and cookies of the server. The reason of why the state and cookies are needed is outside of the scope of this documentation.
		Below a small example of how to use it:
		[codeblocks]
		[gdscript]
		# ServerNode.gd
		extends Node

		var dtls := DTLSServer.new()
		var server := UDPServer.new()
		var peers = []

		func _ready():
		    server.listen(4242)
		    var key = load("key.key") # Your private key.
		    var cert = load("cert.crt") # Your X509 certificate.
		    dtls.setup(key, cert)

		func _process(delta):
		    while server.is_connection_available():
		        var peer : PacketPeerUDP = server.take_connection()
		        var dtls_peer : PacketPeerDTLS = dtls.take_connection(peer)
		        if dtls_peer.get_status() != PacketPeerDTLS.STATUS_HANDSHAKING:
		            continue # It is normal that 50% of the connections fails due to cookie exchange.
		        print("Peer connected!")
		        peers.append(dtls_peer)

		    for p in peers:
		        p.poll() # Must poll to update the state.
		        if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
		            while p.get_available_packet_count() &gt; 0:
		                print("Received message from client: %s" % p.get_packet().get_string_from_utf8())
		                p.put_packet("Hello DTLS client".to_utf8())
		[/gdscript]
		[csharp]
		using Godot;
		using System;
		// ServerNode.cs
		public class ServerNode : Node
		{
		    public DTLSServer Dtls = new DTLSServer();
		    public UDPServer Server = new UDPServer();
		    public Godot.Collections.Array&lt;PacketPeerDTLS&gt; Peers = new Godot.Collections.Array&lt;PacketPeerDTLS&gt;();
		    public override void _Ready()
		    {
		        Server.Listen(4242);
		        var key = GD.Load&lt;CryptoKey&gt;("key.key"); // Your private key.
		        var cert = GD.Load&lt;X509Certificate&gt;("cert.crt"); // Your X509 certificate.
		        Dtls.Setup(key, cert);
		    }

		    public override void _Process(float delta)
		    {
		        while (Server.IsConnectionAvailable())
		        {
		            PacketPeerUDP peer = Server.TakeConnection();
		            PacketPeerDTLS dtlsPeer = Dtls.TakeConnection(peer);
		            if (dtlsPeer.GetStatus() != PacketPeerDTLS.Status.Handshaking)
		            {
		                continue; // It is normal that 50% of the connections fails due to cookie exchange.
		            }
		            GD.Print("Peer connected!");
		            Peers.Add(dtlsPeer);
		        }

		        foreach (var p in Peers)
		        {
		            p.Poll(); // Must poll to update the state.
		            if (p.GetStatus() == PacketPeerDTLS.Status.Connected)
		            {
		                while (p.GetAvailablePacketCount() &gt; 0)
		                {
		                    GD.Print("Received Message From Client: " + p.GetPacket().GetStringFromUTF8());
		                    p.PutPacket("Hello Dtls Client".ToUTF8());
		                }
		            }
		        }
		    }
		}
		[/csharp]
		[/codeblocks]
		[codeblocks]
		[gdscript]
		# ClientNode.gd
		extends Node

		var dtls := PacketPeerDTLS.new()
		var udp := PacketPeerUDP.new()
		var connected = false

		func _ready():
		    udp.connect_to_host("127.0.0.1", 4242)
		    dtls.connect_to_peer(udp, false) # Use true in production for certificate validation!

		func _process(delta):
		    dtls.poll()
		    if dtls.get_status() == PacketPeerDTLS.STATUS_CONNECTED:
		        if !connected:
		            # Try to contact server
		            dtls.put_packet("The answer is... 42!".to_utf8())
		        while dtls.get_available_packet_count() &gt; 0:
		            print("Connected: %s" % dtls.get_packet().get_string_from_utf8())
		            connected = true
		[/gdscript]
		[csharp]
		using Godot;
		using System.Text;
		// ClientNode.cs
		public class ClientNode : Node
		{
		    public PacketPeerDTLS Dtls = new PacketPeerDTLS();
		    public PacketPeerUDP Udp = new PacketPeerUDP();
		    public bool Connected = false;
		    public override void _Ready()
		    {
		        Udp.ConnectToHost("127.0.0.1", 4242);
		        Dtls.ConnectToPeer(Udp, false); // Use true in production for certificate validation!
		    }

		    public override void _Process(float delta)
		    {
		        Dtls.Poll();
		        if (Dtls.GetStatus() == PacketPeerDTLS.Status.Connected)
		        {
		            if (!Connected)
		            {
		                // Try to contact server
		                Dtls.PutPacket("The Answer Is..42!".ToUTF8());
		            }
		            while (Dtls.GetAvailablePacketCount() &gt; 0)
		            {
		                GD.Print("Connected: " + Dtls.GetPacket().GetStringFromUTF8());
		                Connected = true;
		            }
		        }
		    }
		}
		[/csharp]
		[/codeblocks]
	</description>
	<tutorials>
	</tutorials>
	<methods>
		<method name="setup">
			<return type="int" enum="Error" />
			<argument index="0" name="key" type="CryptoKey" />
			<argument index="1" name="certificate" type="X509Certificate" />
			<argument index="2" name="chain" type="X509Certificate" default="null" />
			<description>
				Setup the DTLS server to use the given [code]private_key[/code] and provide the given [code]certificate[/code] to clients. You can pass the optional [code]chain[/code] parameter to provide additional CA chain information along with the certificate.
			</description>
		</method>
		<method name="take_connection">
			<return type="PacketPeerDTLS" />
			<argument index="0" name="udp_peer" type="PacketPeerUDP" />
			<description>
				Try to initiate the DTLS handshake with the given [code]udp_peer[/code] which must be already connected (see [method PacketPeerUDP.connect_to_host]).
				[b]Note:[/b] You must check that the state of the return PacketPeerUDP is [constant PacketPeerDTLS.STATUS_HANDSHAKING], as it is normal that 50% of the new connections will be invalid due to cookie exchange.
			</description>
		</method>
	</methods>
</class>