/
pgsql_sock.erl
395 lines (324 loc) · 11.6 KB
/
pgsql_sock.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
%%% Copyright (C) 2009 - Will Glozer. All rights reserved.
-module(pgsql_sock).
-behavior(gen_server).
-export([start_link/0,
connect/5,
close/1,
get_parameter/2,
squery/2,
equery/3,
parse/4,
bind/4,
execute/4,
describe/3,
close/3,
sync/1,
cancel/1]).
-export([handle_call/3, handle_cast/2, handle_info/2]).
-export([init/1, code_change/3, terminate/2]).
%% state callbacks
-export([auth/2, initializing/2, on_message/2]).
-include("pgsql.hrl").
-include("pgsql_binary.hrl").
-record(state, {mod,
sock,
data = <<>>,
backend,
handler,
queue = queue:new(),
async,
parameters = [],
txstatus}).
%% -- client interface --
start_link() ->
gen_server:start_link(?MODULE, [], []).
connect(C, Host, Username, Password, Opts) ->
cast(C, {connect, Host, Username, Password, Opts}).
close(C) when is_pid(C) ->
catch gen_server:cast(C, stop),
ok.
get_parameter(C, Name) ->
gen_server:call(C, {get_parameter, to_binary(Name)}, infinity).
squery(C, Sql) ->
cast(C, {squery, Sql}).
equery(C, Statement, Parameters) ->
cast(C, {equery, Statement, Parameters}).
parse(C, Name, Sql, Types) ->
cast(C, {parse, Name, Sql, Types}).
bind(C, Statement, PortalName, Parameters) ->
cast(C, {bind, Statement, PortalName, Parameters}).
execute(C, Statement, PortalName, MaxRows) ->
cast(C, {execute, Statement, PortalName, MaxRows}).
describe(C, Type, Name) ->
cast(C, {describe, Type, Name}).
close(C, Type, Name) ->
cast(C, {close, Type, Name}).
sync(C) ->
cast(C, sync).
cancel(S) ->
gen_server:cast(S, cancel).
%% -- gen_server implementation --
init([]) ->
{ok, #state{}}.
handle_call({get_parameter, Name}, _From, State) ->
case lists:keysearch(Name, 1, State#state.parameters) of
{value, {Name, Value}} -> Value;
false -> Value = undefined
end,
{reply, {ok, Value}, State}.
handle_cast(Req = {{_, _}, {connect, Host, Username, Password, Opts}},
#state{queue = Q} = State) ->
Timeout = proplists:get_value(timeout, Opts, 5000),
Port = proplists:get_value(port, Opts, 5432),
SockOpts = [{active, false}, {packet, raw}, binary, {nodelay, true}],
{ok, Sock} = gen_tcp:connect(Host, Port, SockOpts, Timeout),
State2 = case proplists:get_value(ssl, Opts) of
T when T == true; T == required ->
start_ssl(Sock, T, Opts, State);
_ ->
State#state{mod = gen_tcp, sock = Sock}
end,
Opts2 = ["user", 0, Username, 0],
case proplists:get_value(database, Opts, undefined) of
undefined -> Opts3 = Opts2;
Database -> Opts3 = [Opts2 | ["database", 0, Database, 0]]
end,
send(State2, [<<196608:?int32>>, Opts3, 0]),
Async = proplists:get_value(async, Opts, undefined),
setopts(State2, [{active, true}]),
put(username, Username),
put(password, Password),
{noreply,
State2#state{handler = auth,
queue = queue:in(Req, Q),
async = Async}};
handle_cast(stop, #state{queue = Q} = State) ->
%% TODO flush queue
{stop, normal, State};
handle_cast(Req = {{_, _}, {parse, Name, Sql, Types}}, State) ->
#state{queue = Q} = State,
Bin = pgsql_wire:encode_types(Types),
send(State, $P, [Name, 0, Sql, 0, Bin]),
send(State, $D, [$S, Name, 0]), % TODO remove it
send(State, $H, []),
{noreply, State#state{queue = queue:in(Req, Q)}};
handle_cast(Req = {{_,_}, {equery, Statement, Parameters}}, State) ->
#state{queue = Q} = State,
#statement{name = StatementName, columns = Columns} = Statement,
Bin1 = pgsql_wire:encode_parameters(Parameters),
%% TODO do we really need Columns here?
Bin2 = pgsql_wire:encode_formats(Columns),
send(State, $B, ["", 0, StatementName, 0, Bin1, Bin2]),
%% TODO send Describe there
send(State, $E, ["", 0, <<0:?int32>>]),
send(State, $C, [$S, "", 0]),
send(State, $S, []),
{noreply, State#state{queue = queue:in(Req, Q)}};
handle_cast(cancel, State = #state{backend = {Pid, Key}}) ->
{ok, {Addr, Port}} = inet:peername(State#state.sock),
SockOpts = [{active, false}, {packet, raw}, binary],
{ok, Sock} = gen_tcp:connect(Addr, Port, SockOpts),
Msg = <<16:?int32, 80877102:?int32, Pid:?int32, Key:?int32>>,
ok = gen_tcp:send(Sock, Msg),
gen_tcp:close(Sock),
{noreply, State}.
handle_info({Closed, Sock}, #state{sock = Sock} = State)
when Closed == tcp_closed; Closed == ssl_closed ->
%% TODO flush queue
{stop, sock_closed, State};
handle_info({Error, Sock, Reason}, #state{sock = Sock} = State)
when Error == tcp_error; Error == ssl_error ->
%% TODO flush queue
{stop, {sock_error, Reason}, State};
handle_info({_, Sock, Data2}, #state{data = Data, sock = Sock} = State) ->
loop(State#state{data = <<Data/binary, Data2/binary>>}).
loop(#state{data = Data, handler = Handler} = State) ->
case pgsql_wire:decode_message(Data) of
{Message, Tail} ->
case ?MODULE:Handler(Message, State#state{data = Tail}) of
{noreply, State2} ->
loop(State2);
R = {stop, _Reason2, _State2} ->
R
end;
_ ->
{noreply, State}
end.
terminate(_Reason, _State) ->
%% TODO send termination msg, close socket ??
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% -- internal functions --
cast(C, Command) ->
Ref = make_ref(),
gen_server:cast(C, {{self(), Ref}, Command}),
Ref.
start_ssl(S, Flag, Opts, State) ->
ok = gen_tcp:send(S, <<8:?int32, 80877103:?int32>>),
Timeout = proplists:get_value(timeout, Opts, 5000),
{ok, <<Code>>} = gen_tcp:recv(S, 1, Timeout),
case Code of
$S ->
case ssl:connect(S, Opts, Timeout) of
{ok, S2} -> State#state{mod = ssl, sock = S2};
{error, Reason} -> exit({ssl_negotiation_failed, Reason})
end;
$N ->
case Flag of
true -> State;
required -> exit(ssl_not_available)
end
end.
setopts(#state{mod = Mod, sock = Sock}, Opts) ->
case Mod of
gen_tcp -> inet:setopts(Sock, Opts);
ssl -> ssl:setopts(Sock, Opts)
end.
send(#state{mod = Mod, sock = Sock}, Data) ->
Mod:send(Sock, pgsql_wire:encode(Data)).
send(#state{mod = Mod, sock = Sock}, Type, Data) ->
Mod:send(Sock, pgsql_wire:encode(Type, Data)).
notify(#state{queue = Q}, Message) ->
{{From, Ref}, _} = queue:get(Q),
From ! {Ref, Message}.
notify_async(#state{async = Pid}, Msg) ->
case is_pid(Pid) of
true -> Pid ! {pgsql, self(), Msg};
false -> false
end.
%% -- backend message handling --
%% AuthenticationOk
auth({$R, <<0:?int32>>}, State) ->
{noreply, State#state{handler = initializing}};
%% AuthenticationCleartextPassword
auth({$R, <<3:?int32>>}, State) ->
send(State, $p, [get(password), 0]),
{noreply, State};
%% AuthenticationMD5Password
auth({$R, <<5:?int32, Salt:4/binary>>}, State) ->
Digest1 = hex(erlang:md5([get(password), get(username)])),
Str = ["md5", hex(erlang:md5([Digest1, Salt])), 0],
send(State, $p, Str),
{noreply, State};
auth({$R, <<M:?int32, _/binary>>}, State) ->
case M of
2 -> Method = kerberosV5;
4 -> Method = crypt;
6 -> Method = scm;
7 -> Method = gss;
8 -> Method = sspi;
_ -> Method = unknown
end,
notify(State, {error, {unsupported_auth_method, Method}}),
{stop, normal, State};
%% ErrorResponse
auth({error, E}, State) ->
case E#error.code of
<<"28000">> -> Why = invalid_authorization_specification;
<<"28P01">> -> Why = invalid_password;
Any -> Why = Any
end,
notify(State, {error, Why}),
{stop, normal, State};
auth(Other, State) ->
on_message(Other, State).
%% BackendKeyData
initializing({$K, <<Pid:?int32, Key:?int32>>}, State) ->
State2 = State#state{backend = {Pid, Key}},
{noreply, State2};
%% ReadyForQuery
initializing({$Z, <<Status:8>>}, State) ->
#state{parameters = Parameters, queue = Q} = State,
erase(username),
erase(password),
%% TODO decode dates to now() format
case lists:keysearch(<<"integer_datetimes">>, 1, Parameters) of
{value, {_, <<"on">>}} -> put(datetime_mod, pgsql_idatetime);
{value, {_, <<"off">>}} -> put(datetime_mod, pgsql_fdatetime)
end,
notify(State, connected),
{noreply, State#state{handler = on_message,
txstatus = Status,
queue = queue:drop(Q)}};
initializing({error, _} = Error, State) ->
notify(State, Error),
{stop, normal, State};
initializing(Other, State) ->
on_message(Other, State).
%% ParseComplete
on_message({$1, <<>>}, State) ->
{noreply, State};
%% ParameterDescription
on_message({$t, <<_Count:?int16, Bin/binary>>}, State) ->
Types = [pgsql_types:oid2type(Oid) || <<Oid:?int32>> <= Bin],
notify(State, {types, Types}),
{noreply, State};
%% RowDescription
on_message({$T, <<Count:?int16, Bin/binary>>}, State) ->
#state{queue = Q} = State,
Columns = pgsql_wire:decode_columns(Count, Bin),
Columns2 = [C#column{format = pgsql_wire:format(C#column.type)} || C <- Columns],
notify(State, {columns, Columns2}),
%% TODO wrong for squery
{noreply, State#state{queue = queue:drop(Q)}};
%% NoData
on_message({$n, <<>>}, State) ->
#state{queue = Q} = State,
notify(State, no_data),
{noreply, State#state{queue = queue:drop(Q)}};
%% BindComplete
on_message({$2, <<>>}, State) ->
{noreply, State};
%% CloseComplete
on_message({$3, <<>>}, State) ->
{noreply, State};
%% DataRow
on_message({$D, <<_Count:?int16, Bin/binary>>}, State) ->
#state{queue = Q} = State,
%% TODO wrong for squery
{_, {equery, #statement{columns = Columns}, _}} = queue:get(Q),
Data = pgsql_wire:decode_data(Columns, Bin),
notify(State, {data, Data}),
{noreply, State};
%% CommandComplete
on_message({$C, Bin}, State) ->
Complete = pgsql_wire:decode_complete(Bin),
notify(State, {complete, Complete}),
{noreply, State};
%% ReadyForQuery
on_message({$Z, <<_Status:8>>}, State) ->
#state{queue = Q} = State,
notify(State, done),
{noreply, State#state{queue = queue:drop(Q)}};
on_message(Error = {error, _}, State) ->
#state{queue = Q} = State,
notify(State, Error),
%% TODO wrong for squery at least
{noreply, State#state{queue = queue:drop(Q)}};
%% NoticeResponse
on_message({$N, Data}, State) ->
notify_async(State, {notice, pgsql_wire:decode_error(Data)}),
{noreply, State};
%% ParameterStatus
on_message({$S, Data}, State) ->
[Name, Value] = pgsql_wire:decode_strings(Data),
Parameters2 = lists:keystore(Name, 1, State#state.parameters,
{Name, Value}),
{noreply, State#state{parameters = Parameters2}};
%% NotificationResponse
on_message({$A, <<Pid:?int32, Strings/binary>>}, State) ->
case pgsql_wire:decode_strings(Strings) of
[Channel, Payload] -> ok;
[Channel] -> Payload = <<>>
end,
%% TODO use it
notify_async(State, {notification, Channel, Pid, Payload}),
{noreply, State}.
to_binary(B) when is_binary(B) -> B;
to_binary(L) when is_list(L) -> list_to_binary(L).
hex(Bin) ->
HChar = fun(N) when N < 10 -> $0 + N;
(N) when N < 16 -> $W + N
end,
<<<<(HChar(H)), (HChar(L))>> || <<H:4, L:4>> <= Bin>>.