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
// Copyright 2020-2022 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef DMSDK_SSLSOCKET_H
#define DMSDK_SSLSOCKET_H
#include <stdint.h>
// Cannot forward declate enums
#include <dmsdk/dlib/socket.h>
/*# SDK Secure socket API documentation
*
* Secure socket functions.
*
* @document
* @name SSLSocket
* @namespace dmSSLSocket
* @path engine/dlib/src/dmsdk/dlib/sslsocket.h
*/
namespace dmSSLSocket
{
/*# result enumeration
*
* Result enumeration.
*
* @enum
* @name dmSSLSocket::Result
* @member dmSSLSocket::RESULT_OK (0)
* @member dmSSLSocket::RESULT_SSL_INIT_FAILED (-2000)
* @member dmSSLSocket::RESULT_HANDSHAKE_FAILED (-2001)
* @member dmSSLSocket::RESULT_WOULDBLOCK (-2002)
* @member dmSSLSocket::RESULT_CONNREFUSED (-2003)
*
*/
enum Result
{
RESULT_OK = 0,
RESULT_SSL_INIT_FAILED = -2000,
RESULT_HANDSHAKE_FAILED = -2001,
RESULT_WOULDBLOCK = -2002,
RESULT_CONNREFUSED = -2003,
};
/*# Socket type definition
* @typedef
* @name Socket
*/
typedef struct SSLSocket* Socket;
/*# SSLSocket socket handle
* @variable
* @name dmSSLSocket::INVALID_SOCKET_HANDLE
*/
const Socket INVALID_SOCKET_HANDLE = 0;
/*# create a secure socket
* Create a new secure socket
* @name dmSSLSocket::New
* @param socket [type:dmSocket::Socket] The socket to wrap
* @param host [type:const char*] The name of the host (e.g. "httpbin.org")
* @param timeout [type:uint64_t] The timeout for the handshake procedure. (microseconds)
* @param sslsocket [type:dmSSLSocket::Socket*] Pointer to a secure socket
* @return RESULT_OK on succcess
* @examples
* ```cpp
* dmSSLSocket::Result result;
* dmSSLSocket::Socket sslsocket;
* result = dmSSLSocket::New(socket, "httpbin.org", 500*1000, &sslsocket);
* if (dmSSLSocket::RESULT_OK == result)
* {
* // ...
* } else {
* // ...
* }
* ```
*/
Result New(dmSocket::Socket socket, const char* host, uint64_t timeout, Socket* sslsocket);
/*# delete a secure socket
* Delete a secure socket. Does not close the underlying socket
* @name dmSSLSocket::Delete
* @param socket [type:dmSSLSocket::Socket] Secure socket to close
* @return RESULT_OK on success
* @examples
* ```cpp
* dmSSLSocket::Delete(sslsocket);
* ```
*/
Result Delete(Socket socket);
/*# send a message on a secure socket
* Send a message on a secure socket
* @name dmSSLSocket::Send
* @param socket [type:dmSSLSocket::Socket] SSL socket to send a message on
* @param buffer Buffer to send
* @param length Length of buffer to send
* @param sent_bytes Number of bytes sent (result)
* @return RESULT_OK on success
*/
dmSocket::Result Send(Socket socket, const void* buffer, int length, int* sent_bytes);
/*# receive data on a secure socket
* Receive data on a secure socket
* @name dmSSLSocket::Receive
* @param socket [type:dmSSLSocket::Socket] Socket to receive data on
* @param buffer Buffer to receive to
* @param length Receive buffer length
* @param received_bytes Number of received bytes (result)
* @return RESULT_OK on success
*/
dmSocket::Result Receive(Socket socket, void* buffer, int length, int* received_bytes);
/*#
* Set socket receive timeout
* @note Timeout resolution might be in milliseconds, e.g. windows. Use values
* larger than or equal to 1000
* @name dmSocket::SetReceiveTimeout
* @param socket [type:dmSocket::Socket] socket
* @param timeout [type:uint64_t] timeout in microseconds
* @return RESULT_OK on success
*/
dmSocket::Result SetReceiveTimeout(Socket socket, uint64_t timeout);
}
#endif // DMSDK_SSLSOCKET_H