Skip to content

Instantly share code, notes, and snippets.

View deadc0de6's full-sized avatar

deadc0de deadc0de6

View GitHub Profile
-module(fp_mysql_greeting).
-define(TIMEOUT, 3000). % milli-seconds
-define(PORT, 3306). % mysql port
-define(MAXPKT, 1). % only greeting packet is needed
-define(DESCRIPTION, "TCP/3306: Mysql version identification").
find_null(Bin, Pos) ->
case Bin of
<<Start:Pos/binary, 0, Rest/binary>> ->
% found
{Start, Rest};
<<Bin:Pos/binary>> ->
% not found
error;
<<_:Pos/binary, _/binary>>=B ->
% go forward
-define(PROTO, 16#0a). % only interested in protocol version 10
parse_content(_Args,
<<
?PROTO:8, % protocol version
Rest/binary
>>) ->
case find_null(Rest, 1) of
error ->
{{error, up}, unexpected_data};
parse_header(Args,
<<
Pldlen:24/little, % 3 bytes payload length
_:8, % 1 byte sequence id or packet nb
Pld/binary % content
>>) ->
case byte_size(Pld) == Pldlen of
true ->
parse_content(Args, Pld);
false ->
callback_next_step(Args) when Args#args.moddata == undefined ->
{continue, Args#args.maxpkt, "", true};
callback_next_step(Args) when Args#args.packetrcv < 1 ->
{result, {{error,up}, timeout}};
callback_next_step(Args) ->
{result, parse_header(Args, Args#args.datarcv)}.