diff options
author | Martin Willi <martin@revosec.ch> | 2014-11-19 14:20:47 +0100 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2014-12-04 10:42:22 +0100 |
commit | b164cc8e150b7acf9b823643473584c45de5e4a3 (patch) | |
tree | 0064a56a485e2fe88b882285a916857cd3a06a9f | |
parent | dcae0a3935e93bc0b0ac9804a98b79b6c71a7c4b (diff) | |
download | strongswan-b164cc8e150b7acf9b823643473584c45de5e4a3.tar.bz2 strongswan-b164cc8e150b7acf9b823643473584c45de5e4a3.tar.xz |
vici: Make sure to send/recv all requested bytes over socket
As the underlying C functions, send/recv on ruby sockets are not guaranteed
to send/recv all requested bytes. Use wrapper functions to make sure we get
all bytes needed.
-rw-r--r-- | src/libcharon/plugins/vici/ruby/lib/vici.rb | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/libcharon/plugins/vici/ruby/lib/vici.rb b/src/libcharon/plugins/vici/ruby/lib/vici.rb index e8a9ddca9..852bcb615 100644 --- a/src/libcharon/plugins/vici/ruby/lib/vici.rb +++ b/src/libcharon/plugins/vici/ruby/lib/vici.rb @@ -243,6 +243,25 @@ module Vici end ## + # Receive data from socket, until len bytes read + def recv_all(len) + encoding = "" + while encoding.length < len do + encoding << @socket.recv(len - encoding.length) + end + encoding + end + + ## + # Send data to socket, until all bytes sent + def send_all(encoding) + len = 0 + while len < encoding.length do + len += @socket.send(encoding[len..-1], 0) + end + end + + ## # Write a packet prefixed by its length over the transport socket. Type # specifies the message, the optional label and message get appended. def write(type, label, message) @@ -253,15 +272,15 @@ module Vici if message encoding << message.encoding end - @socket.send([encoding.length + 1, type].pack("Nc") + encoding, 0) + send_all([encoding.length + 1, type].pack("Nc") + encoding) end ## # Read a packet from the transport socket. Returns the packet type, and # if available in the packet a label and the contained message. def read - len = @socket.recv(4).unpack("N")[0] - encoding = @socket.recv(len) + len = recv_all(4).unpack("N")[0] + encoding = recv_all(len) type = encoding.unpack("c")[0] len = 1 case type |