forked from byronpc/nkpacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nkpacket_protocol.erl
461 lines (321 loc) · 15.5 KB
/
nkpacket_protocol.erl
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% 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.
%%
%% -------------------------------------------------------------------
%% @doc Protocol behaviour
%% This module shows the behaviour that NkPACKET protocols must follow
%% All functions are optional. The implementation in this module shows the
%% default behaviour
-module(nkpacket_protocol).
-author('Carlos Gonzalez <[email protected]>').
-export([transports/1, default_port/1, resolve_opts/0, naptr/2]).
-export([conn_init/1, conn_parse/3, conn_encode/3, conn_encode/2, conn_bridge/4,
conn_timeout/2,
conn_handle_call/4, conn_handle_cast/3, conn_handle_info/3, conn_stop/3]).
-export([listen_init/1, listen_parse/5, listen_handle_call/4,
listen_handle_cast/3, listen_handle_info/3, listen_stop/3]).
-export([http_init/4]).
-include("nkpacket.hrl").
%% ===================================================================
%% Types
%% ===================================================================
-type conn_state() :: term().
-type listen_state() :: term().
-type nkport() :: #nkport{}.
%% ===================================================================
%% Common callbacks
%% ===================================================================
%% @doc If you implement this function, it must return, for any supported scheme,
%% the list of supported transports.
%% If you supply a tuple, it means that the first element, if used, must be
%% converted to the second.
%% The first element will be used by default, and MUST NOT be a tuple
-callback transports(nklib:scheme()) ->
[nkpacket:transport() | {nkpacket:transport(), nkpacket:transport()}].
%% @doc If you implement this function, it will be used to find the default port
%% for this transport. If not implemented, port '0' will use any port for
%% listener transports, and will fail for outbound connections.
-callback default_port(nkpacket:transport()) ->
inet:port_number() | invalid.
%% @doc If you implement this function, it will be used when calling to nkpacket:resolve()
-callback resolve_opts() ->
nkpacket:resolve_opts().
%% @doc Implement this function to allow NAPTR DNS queries.
%% When a request to resolve a URL maps to a non-IP host, with undefined
%% port and transport, NkPACKET will try a NAPTR DNS query.
%% For each response, this functions is called with the "service" part
%% of the NAPTR response, and you must extract the scheme and transport.
%% This would be an example for SIP:
%% naptr(sip, "sips+d2t") -> {ok, tls};
%% naptr(sip, "sip+d2u") -> {ok, udp};
%% naptr(sip, "sip+d2t") -> {ok, tcp};
%% naptr(sip, "sip+d2s") -> {ok, sctp};
%% naptr(sip, "sips+d2w") -> {ok, wss};
%% naptr(sip, "sip+d2w") -> {ok, ws};
%% naptr(sips, "sips+d2t") -> {ok, tls};
%% naptr(sips, "sips+d2w") -> {ok, wss};
%% naptr(_, _) -> invalid.
-callback naptr(nklib:scheme(), string()) ->
{ok, nkpacket:transport()} | invalid.
%% ===================================================================
%% Connection callbacks
%% ===================================================================
%% Connection callbacks, if implemented, are called during the life cycle of a
%% connection
%% @doc Called when the connection starts
-callback conn_init(nkport()) ->
{ok, conn_state()} | {stop, term()}.
%% @doc This function is called when a new message arrives to the connection
-callback conn_parse(nkpacket:incoming(), nkport(), conn_state()) ->
{ok, conn_state()} | {reply, term(), conn_state()} |
{bridge, nkport(), conn_state()} |
{stop, Reason::term(), conn_state()}.
%% @doc This function is called when a new message must be send to the connection
-callback conn_encode(term(), nkport(), conn_state()) ->
{ok, nkpacket:outcoming(), conn_state()} | {error, Reason::term(), conn_state()}.
%% @doc Implement this function to provide a 'quick' encode function,
%% in case you don't need the connection state to perform the encode.
%% Return 'continue' to use the default version
-callback conn_encode(term(), nkport()) ->
{ok, nkpacket:outcoming()} | continue | {error, term()}.
%% @doc This function is called on incoming data for bridged connections
-callback conn_bridge(nkpacket:incoming(), up|down, nkport(), conn_state()) ->
{ok, nkpacket:incoming(), conn_state()} | {skip, conn_state()} |
{stop, term(), conn_state()}.
%% @doc This function is called when the idle_timer timeout fires
%% If not implemented, will stop the connection
%% If ok is returned, timer is restarted
-callback conn_timeout(nkport(), conn_state()) ->
{ok, conn_state()} |
{stop, term(), conn_state()}.
%% @doc Called when the connection received a gen_server:call/2,3
-callback conn_handle_call(term(), {pid(), term()}, nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
%% @doc Called when the connection received a gen_server:cast/2
-callback conn_handle_cast(term(), nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
%% @doc Called when the connection received an erlang message
-callback conn_handle_info(term(), nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
%% @doc Called when the connection stops
-callback conn_stop(Reason::term(), nkport(), conn_state()) ->
ok.
%% ===================================================================
%% Listen callbacks
%% ===================================================================
%% Listen callbacks, if implemented, are called during the life cicle of a
%% listening transport
%% @doc Called when the listening transport starts
-callback listen_init(nkport()) ->
{ok, listen_state()}.
%% @doc This function is called only for UDP transports using no_connections=>true
-callback listen_parse(inet:ip_address(), inet:port_number(), binary(), nkport(),
listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
%% @doc Called when the listening transport received a gen_server:call/2,3
-callback listen_handle_call(term(), {pid(), term()}, nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
%% @doc Called when the listening transport received a gen_server:cast/2
-callback listen_handle_cast(term(), nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
%% @doc Called when the listening transport received an erlang message
-callback listen_handle_info(term(), nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
%% @doc Called when the listening transport stops
-callback listen_stop(Reason::term(), nkport(), listen_state()) ->
ok.
%% ===================================================================
%% HTTP callbacks
%% ===================================================================
%% @doc This callback is called for the http/https "pseudo" transports.
%% It is called from nkpacket_transport_http when the Cowboy request is
%% assigned to it
-callback http_init(SubPath::[binary()], cowboy_req:req(),
cowboy_middleware:env(), nkport()) ->
{ok, cowboy_req:req(), cowboy_middleware:env()} |
{redirect, Path::binary()} |
{cowboy_static, cowboy_static:opts()} |
{cowboy_rest, Callback::module(), State::term()} |
{stop, cowboy_req:req()}.
-optional_callbacks([
transports/1, default_port/1, resolve_opts/0, naptr/2,
conn_init/1, conn_parse/3, conn_encode/3, conn_encode/2, conn_bridge/4,
conn_timeout/2, conn_handle_call/4, conn_handle_cast/3, conn_handle_info/3, conn_stop/3,
listen_init/1, listen_parse/5, listen_handle_call/4,
listen_handle_cast/3, listen_handle_info/3, listen_stop/3,
http_init/4]).
%% ===================================================================
%% Default implementation
%% ===================================================================
%% @doc If you implement this function, it must return, for any supported scheme,
%% the list of supported transports.
%% If you supply a tuple, it means that the first element, if used, must be
%% converted to the second.
%% The first element will be used by default, and MUST NOT be a tuple
-spec transports(nklib:scheme()) ->
[nkpacket:transport() | {nkpacket:transport(), nkpacket:transport()}].
transports(_) ->
[tcp, tls, udp, sctp, ws, wss, http, https].
%% @doc If you implement this function, it will be used to find the default port
%% for this transport. If not implemented, port '0' will use any port for
%% listener transports, and will fail for outbound connections.
-spec default_port(nkpacket:transport()) ->
inet:port_number() | invalid.
default_port(http) -> 80;
default_port(https) -> 443;
default_port(ws) -> 80;
default_port(wss) -> 443;
default_port(_) -> invalid.
%% @doc If you implement this function, it will be used when calling to nkpacket:resolve()
-spec resolve_opts() ->
nkpacket:resolve_opts().
resolve_opts() ->
#{}.
%% @doc Implement this function to allow NAPTR DNS queries.
%% When a request to resolve a URL maps to a non-IP host, with undefined
%% port and transport, NkPACKET will try a NAPTR DNS query.
%% For each response, this functions is called with the "service" part
%% of the NAPTR response, and you must extract the scheme and transport.
%% This would be an example for SIP:
%% naptr(sip, "sips+d2t") -> {ok, tls};
%% naptr(sip, "sip+d2u") -> {ok, udp};
%% naptr(sip, "sip+d2t") -> {ok, tcp};
%% naptr(sip, "sip+d2s") -> {ok, sctp};
%% naptr(sip, "sips+d2w") -> {ok, wss};
%% naptr(sip, "sip+d2w") -> {ok, ws};
%% naptr(sips, "sips+d2t") -> {ok, tls};
%% naptr(sips, "sips+d2w") -> {ok, wss};
%% naptr(_, _) -> invalid.
-spec naptr(nklib:scheme(), string()) ->
{ok, nkpacket:transport()} | invalid.
naptr(_, _) ->
invalid.
%% ===================================================================
%% Connection callbacks
%% ===================================================================
%% Connection callbacks, if implemented, are called during the life cicle of a
%% connection
%% @doc Called when the connection starts
-spec conn_init(nkport()) ->
{ok, conn_state()} | {stop, term()}.
conn_init(_) ->
{ok, none}.
%% @doc This function is called when a new message arrives to the connection
-spec conn_parse(nkpacket:incoming(), nkport(), conn_state()) ->
{ok, conn_state()} | {reply, term(), conn_state()} |
{bridge, nkport(), conn_state()} |
{stop, Reason::term(), conn_state()}.
conn_parse(_Msg, _NkPort, ConnState) ->
{ok, ConnState}.
%% @doc This function is called when a new message must be send to the connection
-spec conn_encode(term(), nkport(), conn_state()) ->
{ok, nkpacket:outcoming(), conn_state()} | {error, Reason::term(), conn_state()}.
conn_encode(_Term, _NkPort, ConnState) ->
{error, not_defined, ConnState}.
%% @doc Implement this function to provide a 'quick' encode function,
%% in case you don't need the connection state to perform the encode.
%% Return 'continue' to use the default version
-spec conn_encode(term(), nkport()) ->
{ok, nkpacket:outcoming()} | continue | {error, term()}.
conn_encode(_Term, _NkPort) ->
continue.
%% @doc This function is called on incoming data for bridged connections
-spec conn_bridge(nkpacket:incoming(), up|down, nkport(), conn_state()) ->
{ok, nkpacket:incoming(), conn_state()} | {skip, conn_state()} |
{stop, term(), conn_state()}.
conn_bridge(Data, _Type, _NkPort, ConnState) ->
{ok, Data, ConnState}.
%% @doc This function is called when the idle_timer timeout fires
%% If not implemented, will stop the connection
%% If ok is returned, timer is restarted
-spec conn_timeout(nkport(), conn_state()) ->
{ok, conn_state()} |
{stop, term(), conn_state()}.
conn_timeout(_NkPort, ConnState) ->
{stop, normal, ConnState}.
%% @doc Called when the connection received a gen_server:call/2,3
-spec conn_handle_call(term(), {pid(), term()}, nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
conn_handle_call(_Msg, _From, _NkPort, ConnState) ->
{stop, not_defined, ConnState}.
%% @doc Called when the connection received a gen_server:cast/2
-spec conn_handle_cast(term(), nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
conn_handle_cast(_Msg, _NkPort, ConnState) ->
{stop, not_defined, ConnState}.
%% @doc Called when the connection received an erlang message
-spec conn_handle_info(term(), nkport(), conn_state()) ->
{ok, conn_state()} | {stop, Reason::term(), conn_state()}.
conn_handle_info(_Msg, _NkPort, ConnState) ->
{stop, not_defined, ConnState}.
%% @doc Called when the connection stops
-spec conn_stop(Reason::term(), nkport(), conn_state()) ->
ok.
conn_stop(_Reason, _NkPort, _ConnState) ->
ok.
%% ===================================================================
%% Listen callbacks
%% ===================================================================
%% Listen callbacks, if implemented, are called during the life cicle of a
%% listening transport
%% @doc Called when the listening transport starts
-spec listen_init(nkport()) ->
{ok, listen_state()}.
listen_init(_) ->
{ok, none}.
%% @doc This function is called only for UDP transports using no_connections=>true
-spec listen_parse(inet:ip_address(), inet:port_number(), binary(), nkport(),
listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
listen_parse(_Ip, _Port, _Msg, _NkPort, ListenState) ->
{stop, not_defined, ListenState}.
%% @doc Called when the listening transport received a gen_server:call/2,3
-spec listen_handle_call(term(), {pid(), term()}, nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
listen_handle_call(_Msg, _From, _NkPort, ListenState) ->
{stop, not_defined, ListenState}.
%% @doc Called when the listening transport received a gen_server:cast/2
-spec listen_handle_cast(term(), nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
listen_handle_cast(_Msg, _NkPort, ListenState) ->
{stop, not_defined, ListenState}.
%% @doc Called when the listening transport received an erlang message
-spec listen_handle_info(term(), nkport(), listen_state()) ->
{ok, listen_state()} | {stop, Reason::term(), listen_state()}.
listen_handle_info(_Msg, _NkPort, ListenState) ->
{stop, not_defined, ListenState}.
%% @doc Called when the listening transport stops
-spec listen_stop(Reason::term(), nkport(), listen_state()) ->
ok.
listen_stop(_Reason, _NkPort, _State) ->
ok.
%% ===================================================================
%% HTTP callbacks
%% ===================================================================
%% @doc This callback is called for the http/https "pseudo" transports.
%% It is called from nkpacket_transport_http when the Cowboy request is
%% assigned to it
-spec http_init(SubPath::[binary()], cowboy_req:req(),
cowboy_middleware:env(), nkport()) ->
{ok, cowboy_req:req(), cowboy_middleware:env()} |
{redirect, Path::binary()} |
{cowboy_static, cowboy_static:opts()} |
{cowboy_rest, Callback::module(), State::term()} |
{stop, cowboy_req:req()}.
http_init(_SubPath, Req, _Env, _NkPort) ->
{stop, nkpacket_cowboy:reply(404, #{}, <<"Protocol not implemented">>, Req)}.