683572
From bc26d2e6b287cc6693f41e1a2d48b0dd77d2e427 Mon Sep 17 00:00:00 2001
683572
From: Tony Cook <tony@develop-help.com>
683572
Date: Tue, 18 Jun 2019 14:59:00 +1000
683572
Subject: [PATCH] (perl #133936) make send() a bit saner
683572
MIME-Version: 1.0
683572
Content-Type: text/plain; charset=UTF-8
683572
Content-Transfer-Encoding: 8bit
683572
683572
This undoes some of the effect of f1000aa2d in that TO will always
683572
be supplied to CORE::send() if it's supplied, otherwise whether
683572
TO is supplied to CORE::send() is based on whether the socket is
683572
connected.
683572
683572
On Linux you appear to be able to sendto() to a different address on
683572
a connected UDP socket, but this doesn't appear to be portable,
683572
failing on darwin, and presumably on other BSDs.
683572
683572
Signed-off-by: Petr Písař <ppisar@redhat.com>
683572
---
683572
 dist/IO/lib/IO/Socket.pm | 25 +++++++++++++++++--------
683572
 dist/IO/t/io_udp.t       | 11 ++++++++---
683572
 2 files changed, 25 insertions(+), 11 deletions(-)
683572
683572
diff --git a/dist/IO/lib/IO/Socket.pm b/dist/IO/lib/IO/Socket.pm
683572
index 345ffd475d..28fa1ec149 100644
683572
--- a/dist/IO/lib/IO/Socket.pm
683572
+++ b/dist/IO/lib/IO/Socket.pm
683572
@@ -277,13 +277,22 @@ sub send {
683572
     @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]])';
683572
     my $sock  = $_[0];
683572
     my $flags = $_[2] || 0;
683572
-    my $peer  = $_[3] || $sock->peername;
683572
+    my $peer;
683572
 
683572
-    croak 'send: Cannot determine peer address'
683572
-	 unless(defined $peer);
683572
+    if ($_[3]) {
683572
+        # the caller explicitly requested a TO, so use it
683572
+        # this is non-portable for "connected" UDP sockets
683572
+        $peer = $_[3];
683572
+    }
683572
+    elsif (!defined getpeername($sock)) {
683572
+        # we're not connected, so we require a peer from somewhere
683572
+        $peer = $sock->peername;
683572
+
683572
+	croak 'send: Cannot determine peer address'
683572
+	    unless(defined $peer);
683572
+    }
683572
 
683572
-    my $type = $sock->socktype;
683572
-    my $r = $type == SOCK_DGRAM || $type == SOCK_RAW
683572
+    my $r = $peer
683572
       ? send($sock, $_[1], $flags, $peer)
683572
       : send($sock, $_[1], $flags);
683572
 
683572
@@ -526,9 +535,9 @@ C<FLAGS> is optional and defaults to C<0>, and
683572
 
683572
 =item *
683572
 
683572
-after a successful send with C<TO>, further calls to send() without
683572
-C<TO> will send to the same address, and C<TO> will be used as the
683572
-result of peername().
683572
+after a successful send with C<TO>, further calls to send() on an
683572
+unconnected socket without C<TO> will send to the same address, and
683572
+C<TO> will be used as the result of peername().
683572
 
683572
 =back
683572
 
683572
diff --git a/dist/IO/t/io_udp.t b/dist/IO/t/io_udp.t
683572
index 571e4303bb..2adc6a4a69 100644
683572
--- a/dist/IO/t/io_udp.t
683572
+++ b/dist/IO/t/io_udp.t
683572
@@ -89,9 +89,14 @@ is($buf, 'FOObar');
683572
     ok($udpa->recv($buf = "", 8), "recv it");
683572
     is($buf, "fromctoa", "check value received");
683572
 
683572
-    ok($udpc->send("fromctob", 0, $udpb->sockname), "send to non-connected socket");
683572
-    ok($udpb->recv($buf = "", 8), "recv it");
683572
-    is($buf, "fromctob", "check value received");
683572
+  SKIP:
683572
+    {
683572
+        $^O eq "linux"
683572
+	  or skip "This is non-portable, known to 'work' on Linux", 3;
683572
+        ok($udpc->send("fromctob", 0, $udpb->sockname), "send to non-connected socket");
683572
+        ok($udpb->recv($buf = "", 8), "recv it");
683572
+        is($buf, "fromctob", "check value received");
683572
+    }
683572
 }
683572
 
683572
 exit(0);
683572
-- 
683572
2.20.1
683572