1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(msmp_handshake). |
17 |
|
|
18 |
|
|
19 |
|
-export([decode/0]). |
20 |
|
|
21 |
|
|
22 |
|
-spec decode() -> scran:parser(binary(), msmp:handshake()). |
23 |
|
|
24 |
|
decode() -> |
25 |
2 |
fun |
26 |
|
(Input) -> |
27 |
6 |
(scran_sequence:combined_with( |
28 |
|
scran_result:into_map( |
29 |
|
scran_sequence:sequence( |
30 |
|
[scran_result:kv( |
31 |
|
action, |
32 |
|
scran_combinator:value( |
33 |
|
handshake, |
34 |
|
scran_bytes:tag(<<10:8>>))), |
35 |
|
|
36 |
|
scran_result:kv( |
37 |
|
server_version, |
38 |
|
scran_bytes:null_terminated()), |
39 |
|
|
40 |
|
scran_result:kv( |
41 |
|
thread_id, |
42 |
|
msmp_integer_fixed:decode(4)), |
43 |
|
|
44 |
|
scran_result:kv( |
45 |
|
auth_plugin_data_part_1, |
46 |
|
scran_bytes:take(8)), |
47 |
|
|
48 |
|
scran_result:kv( |
49 |
|
filler, |
50 |
|
msmp_integer_fixed:decode(1)), |
51 |
|
|
52 |
|
scran_result:kv( |
53 |
|
capability_flags_1, |
54 |
|
msmp_capabilities:decode(lower)), |
55 |
|
|
56 |
|
scran_result:kv( |
57 |
|
character_set, |
58 |
|
msmp_integer_fixed:decode(1)), |
59 |
|
|
60 |
|
scran_result:kv( |
61 |
|
status_flags, |
62 |
|
msmp_integer_fixed:decode(2)), |
63 |
|
|
64 |
|
scran_result:kv( |
65 |
|
capability_flags_2, |
66 |
|
msmp_capabilities:decode(upper)), |
67 |
|
|
68 |
|
scran_result:kv( |
69 |
|
auth_plugin_data_len, |
70 |
|
msmp_integer_fixed:decode(1)), |
71 |
|
|
72 |
|
%% Determine whether we are connected to MySQL or |
73 |
|
%% MariaDB |
74 |
|
%% |
75 |
|
scran_branch:alt( |
76 |
|
[scran_sequence:sequence( |
77 |
|
[scran_result:kv( |
78 |
|
reserved, |
79 |
|
scran_bytes:tag(<<0:10/unit:8>>)), |
80 |
|
|
81 |
|
scran_result:kv( |
82 |
|
operator, |
83 |
|
scran_combinator:success(mysql))]), |
84 |
|
|
85 |
|
scran_sequence:sequence( |
86 |
|
[scran_result:kv( |
87 |
|
reserved, |
88 |
|
scran_bytes:tag(<<0:6/unit:8>>)), |
89 |
|
|
90 |
|
scran_result:kv( |
91 |
|
extended_capabilities, |
92 |
|
msmp_integer_fixed:decode(4)), |
93 |
|
|
94 |
|
scran_result:kv( |
95 |
|
operator, |
96 |
|
scran_combinator:success(mariadb))])])])), |
97 |
|
fun |
98 |
|
(#{capability_flags_2 := #{plugin_auth := true}, |
99 |
|
auth_plugin_data_len := AuthPluginDataLen}) -> |
100 |
6 |
scran_result:into_map( |
101 |
|
scran_sequence:sequence( |
102 |
|
[scran_result:kv( |
103 |
|
auth_plugin_data_part_2, |
104 |
|
scran_bytes:take(max(13, AuthPluginDataLen - 8))), |
105 |
|
|
106 |
|
scran_result:kv( |
107 |
|
auth_plugin_name, |
108 |
|
scran_result:into_atom( |
109 |
|
scran_bytes:null_terminated()))])) |
110 |
|
end))(Input) |
111 |
|
end. |