diff options
author | YAMASHINA Hio <[email protected]> | 2010-02-09 10:46:38 +0900 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2010-02-12 09:54:31 +0100 |
commit | f435df0e065c06e2ae11308c0ec5a19a9ce887fa (patch) | |
tree | 94d800edf244201cc462eeb309e868bbbb70f926 /lib/ssl/src | |
parent | 6edfe6c0fed6e610289166c51bb007f7ba4d0a3b (diff) | |
download | otp-f435df0e065c06e2ae11308c0ec5a19a9ce887fa.tar.gz otp-f435df0e065c06e2ae11308c0ec5a19a9ce887fa.tar.bz2 otp-f435df0e065c06e2ae11308c0ec5a19a9ce887fa.zip |
prepend packet size bytes in ssl:send() in new_ssl implementation
With the {ssl_imp,new} option enabled, {packet,PacketType} only
works when receiving. When sending, {packet,0} is always used.
Diffstat (limited to 'lib/ssl/src')
-rw-r--r-- | lib/ssl/src/ssl_connection.erl | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/lib/ssl/src/ssl_connection.erl b/lib/ssl/src/ssl_connection.erl index d9377fe3d6..0aed85a9ef 100644 --- a/lib/ssl/src/ssl_connection.erl +++ b/lib/ssl/src/ssl_connection.erl @@ -610,7 +610,7 @@ connection(hello, State = #state{host = Host, port = Port, %% gen_fsm:sync_send_event/2,3, the instance of this function with the same %% name as the current state name StateName is called to handle the event. %%-------------------------------------------------------------------- -connection({application_data, Data}, _From, +connection({application_data, Data0}, _From, State = #state{socket = Socket, negotiated_version = Version, transport_cb = Transport, @@ -618,10 +618,16 @@ connection({application_data, Data}, _From, %% We should look into having a worker process to do this to %% parallize send and receive decoding and not block the receiver %% if sending is overloading the socket. - {Msgs, ConnectionStates1} = encode_data(Data, Version, ConnectionStates0), - Result = Transport:send(Socket, Msgs), - {reply, Result, - connection, State#state{connection_states = ConnectionStates1}}. + try + Data = encode_packet(Data0, State#state.socket_options), + {Msgs, ConnectionStates1} = encode_data(Data, Version, ConnectionStates0), + Result = Transport:send(Socket, Msgs), + {reply, Result, + connection, State#state{connection_states = ConnectionStates1}} + + catch throw:Error -> + {reply, Error, connection, State} + end. %%-------------------------------------------------------------------- %% Function: @@ -1404,6 +1410,23 @@ encode_handshake(HandshakeRec, SigAlg, Version, ConnectionStates0, Hashes0) -> ssl_record:encode_handshake(Frag, Version, ConnectionStates0), {E, ConnectionStates1, Hashes1}. +encode_packet(Data, #socket_options{packet=Packet}) -> + case Packet of + 0 -> Data; + 1 -> encode_size_packet(Data, 8, (1 bsl 8) - 1); + 2 -> encode_size_packet(Data, 16, (1 bsl 16) - 1); + 4 -> encode_size_packet(Data, 32, (1 bsl 32) - 1); + _ -> + throw({error, {badarg, {eoptions, {packet, Packet}}}}) + end. + +encode_size_packet(Bin, Size, Max) -> + Len = byte_size(Bin), + case Len > Max of + true -> throw({error, {badarg, {packet_to_large, Len, Max}}}); + false -> <<Len:Size, Bin/binary>> + end. + encode_data(Data, Version, ConnectionStates) -> ssl_record:encode_data(Data, Version, ConnectionStates). |