I have a program (too complex to present here) that uses an UDP socket created by IO::Socket::INET->new()
with both, Local...
and Peer...
address and port. So expectation is that a plain $sock->send($data, $flags)
will send $data
to the peer address specified when creating the socket. That seems to work.
However when I try to send an individual packet to a different destination by using $sock->send($data, $flags, $dest)
, $dest
seems to be ignored (and the packet is being sdent to the socket's peer address). I added lots of debug messages, and the parameter $dest
is passed correctly to send
, but strace
shows that sendto()
is called with NULL
for sockaddr
and 0
for socklen
.
perldoc -f send
doesn't help me. So why is the destination address ignored?
As requested, here's the (somewhat extra verbose) send_packet code:
$RE_saddr = qr/^(.+):(\d+)$/;
sub send_packet($$;$)
{
my ($sock, $packet, $dest) = @_;
my @params = ($packet, 0);
if (defined($dest)) {
if (my ($addr, $port) = $dest =~ $RE_saddr) {
if (my $addr_bin = inet_aton($addr)) {
if (defined($dest = sockaddr_in($port, $addr_bin))) {
push(@params, $dest);
} else {
warn "bad address or socket for $addr:$port";
}
} else {
warn "bad address $addr";
}
} else {
warn "bad destination address $dest";
}
}
return 1
if ($sock->send(@params));
return undef;
}
So obviously the destination is passed as "host : port" (one string without blanks (blanks are due to markup deficits here)).