Debian Codegraph @

src/cowboy_quicer.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 %% QUIC transport using the emqx/quicer NIF.
16
17 -module(cowboy_quicer).
18
19 -ifdef(COWBOY_QUICER).
20
21 %% Connection.
22 -export([peername/1]).
23 -export([sockname/1]).
24 -export([peercert/1]).
25 -export([shutdown/2]).
26
27 %% Streams.
28 -export([start_bidi_stream/2]).
29 -export([start_unidi_stream/2]).
30 -export([setopt/4]).
31 -export([send/3]).
32 -export([send/4]).
33 -export([send_datagram/2]).
34 -export([shutdown_stream/2]).
35 -export([shutdown_stream/4]).
36
37 %% Messages.
38 -export([handle/1]).
39
40 %% @todo Make quicer export these types.
41 -type quicer_connection_handle() :: reference().
42 -export_type([quicer_connection_handle/0]).
43
44 -type quicer_app_errno() :: non_neg_integer().
45
46 -include_lib("quicer/include/quicer.hrl").
47
48 %% Connection.
49
50 -spec peername(quicer_connection_handle())
51 -> {ok, {inet:ip_address(), inet:port_number()}}
52 | {error, any()}.
53
54 peername(Conn) ->
55 quicer:peername(Conn).
56
57 -spec sockname(quicer_connection_handle())
58 -> {ok, {inet:ip_address(), inet:port_number()}}
59 | {error, any()}.
60
61 sockname(Conn) ->
62 quicer:sockname(Conn).
63
64 -spec peercert(quicer_connection_handle())
65 -> {ok, public_key:der_encoded()}
66 | {error, any()}.
67
68 peercert(Conn) ->
69 quicer_nif:peercert(Conn).
70
71 -spec shutdown(quicer_connection_handle(), quicer_app_errno())
72 -> ok | {error, any()}.
73
74 shutdown(Conn, ErrorCode) ->
75 quicer:shutdown_connection(Conn,
76 ?QUIC_CONNECTION_SHUTDOWN_FLAG_NONE,
77 ErrorCode).
78
79 %% Streams.
80
81 -spec start_bidi_stream(quicer_connection_handle(), iodata())
82 -> {ok, cow_http3:stream_id()}
83 | {error, any()}.
84
85 start_bidi_stream(Conn, InitialData) ->
86 start_stream(Conn, InitialData, ?QUIC_STREAM_OPEN_FLAG_NONE).
87
88 -spec start_unidi_stream(quicer_connection_handle(), iodata())
89 -> {ok, cow_http3:stream_id()}
90 | {error, any()}.
91
92 start_unidi_stream(Conn, InitialData) ->
93 start_stream(Conn, InitialData, ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL).
94
95 start_stream(Conn, InitialData, OpenFlag) ->
96 case quicer:start_stream(Conn, #{
97 active => true,
98 open_flag => OpenFlag}) of
99 {ok, StreamRef} ->
100 case quicer:send(StreamRef, InitialData) of
101 {ok, _} ->
102 {ok, StreamID} = quicer:get_stream_id(StreamRef),
103 put({quicer_stream, StreamID}, StreamRef),
104 {ok, StreamID};
105 Error ->
106 Error
107 end;
108 {error, Reason1, Reason2} ->
109 {error, {Reason1, Reason2}};
110 Error ->
111 Error
112 end.
113
114 -spec setopt(quicer_connection_handle(), cow_http3:stream_id(), active, boolean())
115 -> ok | {error, any()}.
116
117 setopt(_Conn, StreamID, active, Value) ->
118 StreamRef = get({quicer_stream, StreamID}),
119 quicer:setopt(StreamRef, active, Value).
120
121 -spec send(quicer_connection_handle(), cow_http3:stream_id(), iodata())
122 -> ok | {error, any()}.
123
124 send(Conn, StreamID, Data) ->
125 send(Conn, StreamID, Data, nofin).
126
127 -spec send(quicer_connection_handle(), cow_http3:stream_id(), iodata(), cow_http:fin())
128 -> ok | {error, any()}.
129
130 send(_Conn, StreamID, Data, IsFin) ->
131 StreamRef = get({quicer_stream, StreamID}),
132 Size = iolist_size(Data),
133 case quicer:send(StreamRef, Data, send_flag(IsFin)) of
134 {ok, Size} ->
135 ok;
136 {error, Reason1, Reason2} ->
137 {error, {Reason1, Reason2}};
138 Error ->
139 Error
140 end.
141
142 send_flag(nofin) -> ?QUIC_SEND_FLAG_NONE;
143 send_flag(fin) -> ?QUIC_SEND_FLAG_FIN.
144
145 -spec send_datagram(quicer_connection_handle(), iodata())
146 -> ok | {error, any()}.
147
148 send_datagram(Conn, Data) ->
149 %% @todo Fix/ignore the Dialyzer error instead of doing this.
150 DataBin = iolist_to_binary(Data),
151 Size = byte_size(DataBin),
152 case quicer:send_dgram(Conn, DataBin) of
153 {ok, Size} ->
154 ok;
155 %% @todo Handle error cases.
156 Error ->
157 Error
158 end.
159
160 -spec shutdown_stream(quicer_connection_handle(), cow_http3:stream_id())
161 -> ok.
162
163 shutdown_stream(_Conn, StreamID) ->
164 StreamRef = get({quicer_stream, StreamID}),
165 _ = quicer:shutdown_stream(StreamRef),
166 ok.
167
168 -spec shutdown_stream(quicer_connection_handle(),
169 cow_http3:stream_id(), both | receiving, quicer_app_errno())
170 -> ok.
171
172 shutdown_stream(_Conn, StreamID, Dir, ErrorCode) ->
173 StreamRef = get({quicer_stream, StreamID}),
174 _ = quicer:shutdown_stream(StreamRef, shutdown_flag(Dir), ErrorCode, infinity),
175 ok.
176
177 %% @todo Are these flags correct for what we want?
178 shutdown_flag(both) -> ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT;
179 shutdown_flag(receiving) -> ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT_RECEIVE.
180
181 %% Messages.
182
183 %% @todo Probably should have the Conn given as argument too?
184 -spec handle({quic, _, _, _})
185 -> {data, cow_http3:stream_id(), cow_http:fin(), binary()}
186 | {datagram, binary()}
187 | {stream_started, cow_http3:stream_id(), unidi | bidi}
188 | {stream_closed, cow_http3:stream_id(), quicer_app_errno()}
189 | closed
190 | {peer_send_shutdown, cow_http3:stream_id()}
191 | ok
192 | unknown
193 | {socket_error, any()}.
194
195 handle({quic, Data, StreamRef, #{flags := Flags}}) when is_binary(Data) ->
196 {ok, StreamID} = quicer:get_stream_id(StreamRef),
197 IsFin = case Flags band ?QUIC_RECEIVE_FLAG_FIN of
198 ?QUIC_RECEIVE_FLAG_FIN -> fin;
199 _ -> nofin
200 end,
201 {data, StreamID, IsFin, Data};
202 %% @todo Match on Conn.
203 handle({quic, Data, _Conn, Flags}) when is_binary(Data), is_integer(Flags) ->
204 {datagram, Data};
205 %% QUIC_CONNECTION_EVENT_PEER_STREAM_STARTED.
206 handle({quic, new_stream, StreamRef, #{flags := Flags}}) ->
207 case quicer:setopt(StreamRef, active, true) of
208 ok ->
209 {ok, StreamID} = quicer:get_stream_id(StreamRef),
210 put({quicer_stream, StreamID}, StreamRef),
211 StreamType = case quicer:is_unidirectional(Flags) of
212 true -> unidi;
213 false -> bidi
214 end,
215 {stream_started, StreamID, StreamType};
216 {error, Reason} ->
217 {socket_error, Reason}
218 end;
219 %% QUIC_STREAM_EVENT_SHUTDOWN_COMPLETE.
220 handle({quic, stream_closed, StreamRef, #{error := ErrorCode}}) ->
221 {ok, StreamID} = quicer:get_stream_id(StreamRef),
222 {stream_closed, StreamID, ErrorCode};
223 %% QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE.
224 handle({quic, closed, Conn, _Flags}) ->
225 _ = quicer:close_connection(Conn),
226 closed;
227 %% The following events are currently ignored either because
228 %% I do not know what they do or because we do not need to
229 %% take action.
230 handle({quic, streams_available, _Conn, _Props}) ->
231 ok;
232 handle({quic, dgram_state_changed, _Conn, _Props}) ->
233 ok;
234 %% QUIC_CONNECTION_EVENT_SHUTDOWN_INITIATED_BY_TRANSPORT
235 handle({quic, transport_shutdown, _Conn, _Flags}) ->
236 ok;
237 handle({quic, peer_send_shutdown, StreamRef, undefined}) ->
238 {ok, StreamID} = quicer:get_stream_id(StreamRef),
239 {peer_send_shutdown, StreamID};
240 handle({quic, send_shutdown_complete, _StreamRef, _IsGraceful}) ->
241 ok;
242 handle({quic, shutdown, _Conn, success}) ->
243 ok;
244 handle(_Msg) ->
245 unknown.
246
247 -endif.