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