You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IO::Socket::send ignores peer on connected connection-less socket #16891
I stumbled over the IO::Socket::send() implementation while investigating
SO question #55161627 (https://stackoverflow.com/questions/55161627/ "Why
does sendto() via Perl Socket->send() ignore the peer address?"):
sub peername {
@_ == 1 or croak 'usage: $sock->peername()';
my($sock) = @_;
${*$sock}{'io_socket_peername'} ||= getpeername($sock);
}
...
sub send {
...
my $peer = $_[3] || $sock->peername;
# remember who we send to, if it was successful
${*$sock}{'io_socket_peername'} = $peer
if(@_ == 4 && defined $r);
Originally getpeername() wasn't called at all, i.e. the peer address passed
in by the caller was honoured. IO 1.12 ( Dual-Life/IO@9ab5475)
changed this, but only explains the What and not the Why. I have been
unable to find a better version control history entry for this change, i.e.
it is either lost or didn't exist at all.
Modified IO::Socket::send so not to pass 4 arguments to send if the
socket is connected
It could be that this paragraph in the Linux send(2) man page is the reason
behind the change:
If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
socket, the arguments dest_addr and addrlen are ignored **(and the
error
EISCONN may be returned when they are not NULL and 0)**, and the error
ENOTCONN is returned when the socket was not actually connected.
i.e. it would prevent a failure when $socket->send($data, $flags, $peer) is
used on a connected connection-mode socket. But this breaks the behaviour
for connection-less sockets, because $peer gets ignored:
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket::INET;
use Socket qw(pack_sockaddr_in unpack_sockaddr_in);
my $client = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 2222,
Proto => 'udp',
) or die "client socket: $!\n";
my $addr = pack_sockaddr_in(3333, inet_aton('127.0.0.1'));
$client->send('ABCD', 0)
or die "IO::Socket::send() no addr: $!\n";
print "default peer port after IO::Socket::send() no addr: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
$client->send('ABCD', 0, $addr)
or die "IO::Socket::send() with addr: $!\n";
print "default peer port after IO::Socket::send() with addr: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
$client->send('ABCD', 0)
or die "IO::Socket::send() no addr/2: $!\n";
print "default peer port after IO::Socket::send() no addr/2: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
send($client, 'ABCD', 0, $addr)
or die "send() with addr: $!\n";
exit 0;
Test run wrapped in strace:
$ strace -e send,sendto,connect,socket perl dummy.pl
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() no addr: 2222
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() with addr: 3333
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() no addr/2: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
Furthermore: although $peer is ignored, a successful $socket->send($data,
$flags, $peer) call still caches $peer as new default peername for the
socket!
Wouldn't it make sense to change the implementation to
sub send {
@_ >= 2 && @_ <= 4 or croak('usage: $sock->send(BUF, [FLAGS, [TO]])');
my $sock = $_[0];
my $flags = $_[2] || 0;
my $peer = $_[3] || $sock->peername;
# remember who we send to, if it was successful
${*$sock}{'io_socket_peername'} = $peer
if(@_ == 4 && defined $r);
$r;
}
which would also take care of the performance issue of calling
getpeername() on every invocation. I'm not sure if the code would need to
check for other socket types besides SOCK_DGRAM.
With that implementation I get for my test code:
$ strace -e send,sendto,connect,socket perl dummy.pl
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() no addr: 2222
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() with addr: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() no addr/2: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
Also the IO::Socket documentation should be updated to mention that a
successful $socket->send($data, $flags, $peer) caches $peer as peername,
i.e. as default peer for send() without a peer parameter.
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
I stumbled over the IO::Socket::send() implementation while
investigating
SO question #55161627 (https://stackoverflow.com/questions/55161627/
"Why
does sendto() via Perl Socket->send() ignore the peer address?"):
On Sun, 17 Mar 2019 22:06:04 -0700, tonyc wrote:
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
I stumbled over the IO::Socket::send() implementation while
investigating
SO question #55161627 (https://stackoverflow.com/questions/55161627/
"Why
does sendto() via Perl Socket->send() ignore the peer address?"):
Does the attached help for you?
Stefan responded by private email due to the email <-> RT issues.
On Tue, 04 Jun 2019 23:09:06 -0700, tonyc wrote:
On Sun, 17 Mar 2019 22:06:04 -0700, tonyc wrote:
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
I stumbled over the IO::Socket::send() implementation while
investigating
SO question #55161627 (https://stackoverflow.com/questions/55161627/
"Why
does sendto() via Perl Socket->send() ignore the peer address?"):
Does the attached help for you?
Stefan responded by private email due to the email <-> RT issues.
On Sun, 16 Jun 2019 17:20:18 -0700, tonyc wrote:
On Tue, 04 Jun 2019 23:09:06 -0700, tonyc wrote:
On Sun, 17 Mar 2019 22:06:04 -0700, tonyc wrote:
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
I stumbled over the IO::Socket::send() implementation while
investigating
SO question #55161627
(https://stackoverflow.com/questions/55161627/
"Why
does sendto() via Perl Socket->send() ignore the peer address?"):
Does the attached help for you?
Stefan responded by private email due to the email <-> RT issues.
On Mon, 17 Jun 2019 18:33:41 -0700, tonyc wrote:
On Sun, 16 Jun 2019 17:20:18 -0700, tonyc wrote:
On Tue, 04 Jun 2019 23:09:06 -0700, tonyc wrote:
On Sun, 17 Mar 2019 22:06:04 -0700, tonyc wrote:
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
I stumbled over the IO::Socket::send() implementation while
investigating
SO question #55161627
(https://stackoverflow.com/questions/55161627/
"Why
does sendto() via Perl Socket->send() ignore the peer
address?"):
Does the attached help for you?
Stefan responded by private email due to the email <-> RT issues.
Activity
p5pRT commentedon Mar 15, 2019
From chemobejk@gmail.com
This is a bug report for perl from chemobejk@gmail.com,
generated with the help of perlbug 1.41 running under perl 5.28.1.
Copied from https://github.com/Dual-Life/IO/issues/17#issuecomment-473189242
I stumbled over the IO::Socket::send() implementation while investigating
SO question #55161627 (https://stackoverflow.com/questions/55161627/ "Why
does sendto() via Perl Socket->send() ignore the peer address?"):
sub peername {
@_ == 1 or croak 'usage: $sock->peername()';
my($sock) = @_;
${*$sock}{'io_socket_peername'} ||= getpeername($sock);
}
...
sub send {
...
my $peer = $_[3] || $sock->peername;
croak 'send: Cannot determine peer address'
unless(defined $peer);
my $r = defined(getpeername($sock))
? send($sock, $_[1], $flags)
: send($sock, $_[1], $flags, $peer);
# remember who we send to, if it was successful
${*$sock}{'io_socket_peername'} = $peer
if(@_ == 4 && defined $r);
Originally getpeername() wasn't called at all, i.e. the peer address passed
in by the caller was honoured. IO 1.12 (
Dual-Life/IO@9ab5475)
changed this, but only explains the What and not the Why. I have been
unable to find a better version control history entry for this change, i.e.
it is either lost or didn't exist at all.
It could be that this paragraph in the Linux send(2) man page is the reason
behind the change:
i.e. it would prevent a failure when $socket->send($data, $flags, $peer) is
used on a connected connection-mode socket. But this breaks the behaviour
for connection-less sockets, because $peer gets ignored:
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket::INET;
use Socket qw(pack_sockaddr_in unpack_sockaddr_in);
my $client = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 2222,
Proto => 'udp',
) or die "client socket: $!\n";
my $addr = pack_sockaddr_in(3333, inet_aton('127.0.0.1'));
$client->send('ABCD', 0)
or die "IO::Socket::send() no addr: $!\n";
print "default peer port after IO::Socket::send() no addr: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
$client->send('ABCD', 0, $addr)
or die "IO::Socket::send() with addr: $!\n";
print "default peer port after IO::Socket::send() with addr: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
$client->send('ABCD', 0)
or die "IO::Socket::send() no addr/2: $!\n";
print "default peer port after IO::Socket::send() no addr/2: ",
(unpack_sockaddr_in($client->peername))[0], "\n";
send($client, 'ABCD', 0, $addr)
or die "send() with addr: $!\n";
exit 0;
Test run wrapped in strace:
$ strace -e send,sendto,connect,socket perl dummy.pl
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() no addr: 2222
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() with addr: 3333
sendto(4, "ABCD", 4, 0, NULL, 0) = 4
default peer port after IO::Socket::send() no addr/2: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
Furthermore: although $peer is ignored, a successful $socket->send($data,
$flags, $peer) call still caches $peer as new default peername for the
socket!
Wouldn't it make sense to change the implementation to
sub send {
@_ >= 2 && @_ <= 4 or croak('usage: $sock->send(BUF, [FLAGS, [TO]])');
my $sock = $_[0];
my $flags = $_[2] || 0;
my $peer = $_[3] || $sock->peername;
croak('send: Cannot determine peer address')
unless(defined $peer);
my $r = ($sock->socktype == SOCK_DGRAM)
? send($sock, $_[1], $flags, $peer)
: send($sock, $_[1], $flags);
# remember who we send to, if it was successful
${*$sock}{'io_socket_peername'} = $peer
if(@_ == 4 && defined $r);
$r;
}
which would also take care of the performance issue of calling
getpeername() on every invocation. I'm not sure if the code would need to
check for other socket types besides SOCK_DGRAM.
With that implementation I get for my test code:
$ strace -e send,sendto,connect,socket perl dummy.pl
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(2222),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() no addr: 2222
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() with addr: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
default peer port after IO::Socket::send() no addr/2: 3333
sendto(4, "ABCD", 4, 0, {sa_family=AF_INET, sin_port=htons(3333),
sin_addr=inet_addr("127.0.0.1")}, 16) = 4
Also the IO::Socket documentation should be updated to mention that a
successful $socket->send($data, $flags, $peer) caches $peer as peername,
i.e. as default peer for send() without a peer parameter.
Flags:
category=library
severity=low
module=IO
p5pRT commentedon Mar 18, 2019
From @tonycoz
On Fri, 15 Mar 2019 11:52:31 -0700, chemobejk@gmail.com wrote:
Does the attached help for you?
Tony
p5pRT commentedon Mar 18, 2019
From @tonycoz
133936-socket-send.patch
p5pRT commentedon Mar 18, 2019
The RT System itself - Status changed from 'new' to 'open'
p5pRT commentedon Jun 5, 2019
From @tonycoz
On Sun, 17 Mar 2019 22:06:04 -0700, tonyc wrote:
Stefan responded by private email due to the email <-> RT issues.
Updated patch attached.
Tony
p5pRT commentedon Jun 5, 2019
From @tonycoz
133936-socket-send2.patch
p5pRT commentedon Jun 17, 2019
From @tonycoz
On Tue, 04 Jun 2019 23:09:06 -0700, tonyc wrote:
Applied as f1000aa, 229dff1 and
1d9630e.
Tony
p5pRT commentedon Jun 17, 2019
@tonycoz - Status changed from 'open' to 'pending release'
p5pRT commentedon Jun 18, 2019
@tonycoz - Status changed from 'pending release' to 'open'
p5pRT commentedon Jun 18, 2019
From @tonycoz
On Sun, 16 Jun 2019 17:20:18 -0700, tonyc wrote:
Due to #134201 I've re-opened this, my patch was wrong.
UDP sockets can be connected, and on some systems, calling send() with a TO address will fail.
UDP sockets can even be connected to a new address while connected or disconnected by trying to connect to a AF_UNSPEC address.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
I'm going to re-work this.
Tony
p5pRT commentedon Jun 19, 2019
From @tonycoz
On Mon, 17 Jun 2019 18:33:41 -0700, tonyc wrote:
Done in bc26d2e.
Tony
p5pRT commentedon Jun 19, 2019
@tonycoz - Status changed from 'open' to 'pending release'