vici: Make sure to read all requested data from socket in Perl binding
authorTobias Brunner <tobias@strongswan.org>
Wed, 7 Mar 2018 09:31:11 +0000 (10:31 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 21 Mar 2018 09:10:35 +0000 (10:10 +0100)
Closes strongswan/strongswan#91.

src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Transport.pm

index 6524bf7..b0a7b62 100644 (file)
@@ -22,12 +22,27 @@ sub send {
 sub receive {
     my $self = shift;
     my $packet_header;
-    my $data;
 
-    $self->{'Socket'}->recv($packet_header, 4);
+    $packet_header = $self->_recv_all(4);
     my $packet_len = unpack('N', $packet_header);
-    $self->{'Socket'}->recv($data, $packet_len);
-       return $data;
+    return $self->_recv_all($packet_len);
+}
+
+sub _recv_all {
+    my ($self, $len) = @_;
+    my $data;
+
+    while ($len)
+    {
+        my $buf;
+        unless (defined $self->{'Socket'}->recv($buf, $len))
+        {
+            die "error reading from socket\n";
+        }
+        $len -= length($buf);
+        $data .= $buf;
+    }
+    return $data;
 }
 
 1;