line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# client::udp Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Client::Udp; |
7
|
1
|
|
|
1
|
|
667
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::Client::Tcp); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
616
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable socket netcat) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
host => [ qw(host) ], |
20
|
|
|
|
|
|
|
port => [ qw(port) ], |
21
|
|
|
|
|
|
|
eof => [ qw(0|1) ], |
22
|
|
|
|
|
|
|
size => [ qw(size) ], |
23
|
|
|
|
|
|
|
rtimeout => [ qw(read_timeout) ], |
24
|
|
|
|
|
|
|
use_ipv6 => [ qw(0|1) ], |
25
|
|
|
|
|
|
|
use_broadcast => [ qw(0|1) ], |
26
|
|
|
|
|
|
|
}, |
27
|
|
|
|
|
|
|
attributes_default => { |
28
|
|
|
|
|
|
|
protocol => 'udp', |
29
|
|
|
|
|
|
|
use_broadcast => 0, |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
commands => { |
32
|
|
|
|
|
|
|
connect => [ qw(host|OPTIONAL port|OPTIONAL) ], |
33
|
|
|
|
|
|
|
read => [ ], |
34
|
|
|
|
|
|
|
read_size => [ qw(size) ], |
35
|
|
|
|
|
|
|
write => [ qw($data) ], |
36
|
|
|
|
|
|
|
disconnect => [ ], |
37
|
|
|
|
|
|
|
is_connected => [ ], |
38
|
|
|
|
|
|
|
chomp => [ qw($data) ], |
39
|
|
|
|
|
|
|
}, |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub connect { |
44
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
45
|
0
|
|
|
|
|
|
my ($host, $port) = @_; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
0
|
|
|
|
$host ||= $self->host; |
48
|
0
|
|
0
|
|
|
|
$port ||= $self->port; |
49
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('connect', $host) or return; |
50
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('connect', $port) or return; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
my $mod = $self->use_ipv6 ? 'IO::Socket::INET6' : 'IO::Socket::INET'; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my %args = ( |
55
|
|
|
|
|
|
|
PeerHost => $host, |
56
|
|
|
|
|
|
|
PeerPort => $port, |
57
|
|
|
|
|
|
|
Proto => $self->protocol, |
58
|
|
|
|
|
|
|
Timeout => $self->rtimeout, |
59
|
|
|
|
|
|
|
ReuseAddr => 1, |
60
|
|
|
|
|
|
|
); |
61
|
0
|
0
|
|
|
|
|
if ($self->use_broadcast) { |
62
|
0
|
|
|
|
|
|
$args{Broadcast} = 1; |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
my $socket = $mod->new(%args); |
65
|
0
|
0
|
|
|
|
|
if (! defined($socket)) { |
66
|
0
|
|
|
|
|
|
return $self->log->error("connect: failed connecting to target [$host:$port]: $!"); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$socket->blocking(0); |
70
|
0
|
|
|
|
|
|
$socket->autoflush(1); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
my $select = IO::Select->new or return $self->log->error("connect: IO::Select failed: $!"); |
73
|
0
|
|
|
|
|
|
$select->add($socket); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
$self->_socket($socket); |
76
|
0
|
|
|
|
|
|
$self->_select($select); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$self->log->verbose("connect: successfully connected to [$host:$port]"); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $conn = { |
81
|
|
|
|
|
|
|
ip => $socket->peerhost, |
82
|
|
|
|
|
|
|
port => $socket->peerport, |
83
|
|
|
|
|
|
|
my_ip => $socket->sockhost, |
84
|
|
|
|
|
|
|
my_port => $socket->sockport, |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return $conn; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub write { |
91
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
92
|
0
|
|
|
|
|
|
my ($data, $host, $port) = @_; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if (! $self->is_connected) { |
95
|
0
|
|
|
|
|
|
return $self->log->error("write: not connected"); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('write', $data) or return; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $socket = $self->_socket; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
eval { |
103
|
0
|
|
|
|
|
|
print $socket $data; |
104
|
|
|
|
|
|
|
}; |
105
|
0
|
0
|
|
|
|
|
if ($@) { |
106
|
0
|
|
|
|
|
|
chomp($@); |
107
|
0
|
|
|
|
|
|
return $self->log->error("write: syswrite failed with error [$@]"); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__END__ |