SNMP/Transport/IPv6.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 148 CC 12 SCAR 34.8
CC
12
Cov
0%
_hostname_resolve
110.0
_taddress
6.0
_taddr
6.0
CRAP 32.4
1 # -*- mode: perl -*-
2 # ============================================================================
3  
4 package Net::SNMP::Transport::IPv6;
5  
6 # $Id: IPv6.pm,v 1.1 2009/09/09 15:08:31 dtown Rel $
7  
8 # Base object for the IPv6 Transport Domains.
9  
10 # Copyright (c) 2008-2009 David M. Town <dtown@cpan.org>
11 # All rights reserved.
12  
13 # This program is free software; you may redistribute it and/or modify it
14 # under the same terms as the Perl 5 programming language system itself.
15  
16 # ============================================================================
17  
18 use strict;
19  
20 use Net::SNMP::Transport qw( DEBUG_INFO );
21  
22 use Socket6 0.23 qw(
23 PF_INET6 AF_INET6 in6addr_any in6addr_loopback getaddrinfo
24 pack_sockaddr_in6_all unpack_sockaddr_in6_all inet_pton inet_ntop
25 );
26  
27 ## Version of the Net::SNMP::Transport::IPv6 module
28  
29 our $VERSION = v1.0.0;
30  
31 # [public methods] -----------------------------------------------------------
32  
33 sub agent_addr
34 {
35 return '0.0.0.0';
36 }
37  
38 sub sock_flowinfo
39 {
40 return $_[0]->_flowinfo($_[0]->sock_name());
41 }
42  
43 sub sock_scope_id
44 {
45 return $_[0]->_scope_id($_[0]->sock_name());
46 }
47  
48 sub sock_tzone
49 {
50 goto &sock_scope_id;
51 }
52  
53 sub dest_flowinfo
54 {
55 return $_[0]->_flowinfo($_[0]->dest_name());
56 }
57  
58 sub dest_scope_id
59 {
60 return $_[0]->_scope_id($_[0]->dest_name());
61 }
62  
63 sub dest_tzone
64 {
65 goto &dest_scope_id;
66 }
67  
68 sub peer_flowinfo
69 {
70 return $_[0]->_flowinfo($_[0]->peer_name());
71 }
72  
73 sub peer_scope_id
74 {
75 return $_[0]->_scope_id($_[0]->peer_name());
76 }
77  
78 sub peer_tzone
79 {
80 goto &peer_scope_id;
81 }
82  
83 # [private methods] ----------------------------------------------------------
84  
85 sub _protocol_family
86 {
87 return PF_INET6;
88 }
89  
90 sub _addr_any
91 {
92 return in6addr_any;
93 }
94  
95 sub _addr_loopback
96 {
97 return in6addr_loopback;
98 }
99  
100 sub _hostname_resolve
101 {
102 my ($this, $host, $nh) = @_;
103  
104 $nh->{addr} = undef;
105  
106 # See if the service/port was included in the address.
107  
108 my $serv = ($host =~ s/^\[(.+)\]:([\w\(\)\/]+)$/$1/) ? $2 : undef;
109  
110 if (defined($serv) && (!defined $this->_service_resolve($serv, $nh))) {
111 return $this->_error('Failed to resolve the %s service', $this->type());
112 }
113  
114 # See if the scope zone index was included in the address.
115  
116 $nh->{scope_id} = ($host =~ s/%(\d+)$//) ? $1 : 0; # <address>%<index>
117  
118 # Resolve the address.
119  
120 my @info = getaddrinfo(($_[1] = $host), q{}, PF_INET6);
121  
122 if (@info >= 5) {
123 if ($host =~ s/(.*)%.*$/$1/) { # <address>%<ifName>
124 $_[1] = $1;
125 }
126 while (@info >= 5) {
127 if ($info[0] == PF_INET6) {
128 $nh->{flowinfo} = $this->_flowinfo($info[3]);
129 $nh->{scope_id} ||= $this->_scope_id($info[3]);
130 return $nh->{addr} = $this->_addr($info[3]);
131 }
132 DEBUG_INFO('family = %d, sin = %s', $info[0], unpack 'H*', $info[3]);
133 splice @info, 0, 5;
134 }
135 } else {
136 DEBUG_INFO('getaddrinfo(): %s', $info[0]);
137 if ((my @host = split /:/, $host) == 2) { # <hostname>:<service>
138 $_[1] = sprintf '[%s]:%s', @host;
139 return $this->_hostname_resolve($_[1], $nh);
140 }
141 }
142  
143 # Last attempt to resolve the address.
144 if (!defined $nh->{addr}) {
145 $nh->{addr} = inet_pton(AF_INET6, $host);
146 }
147  
148 if (!defined $nh->{addr}) {
149 return $this->_error(
150 q{Unable to resolve the %s address "%s"}, $this->type(), $host
151 );
152 }
153  
154 return $nh->{addr};
155 }
156  
157 sub _name_pack
158 {
159 return pack_sockaddr_in6_all(
160 $_[1]->{port}, $_[1]->{flowinfo} || 0,
161 $_[1]->{addr}, $_[1]->{scope_id} || 0
162 );
163 }
164  
165 sub _address
166 {
167 return inet_ntop(AF_INET6, $_[0]->_addr($_[1]));
168 }
169  
170 sub _addr
171 {
172 return (unpack_sockaddr_in6_all($_[1]))[2];
173 }
174  
175 sub _port
176 {
177 return (unpack_sockaddr_in6_all($_[1]))[0];
178 }
179  
180 sub _taddress
181 {
182 my $s = $_[0]->_scope_id($_[1]);
183 $s = $s ? sprintf('%%%u', $s) : q{};
184 return sprintf '[%s%s]:%u', $_[0]->_address($_[1]), $s, $_[0]->_port($_[1]);
185 }
186  
187 sub _taddr
188 {
189 my $s = $_[0]->_scope_id($_[1]);
190 $s = $s ? pack('N', $s) : q{};
191 return $_[0]->_addr($_[1]) . $s . pack 'n', $_[0]->_port($_[1]);
192 }
193  
194 sub _scope_id
195 {
196 return (unpack_sockaddr_in6_all($_[1]))[3];
197 }
198  
199 sub _flowinfo
200 {
201 return (unpack_sockaddr_in6_all($_[1]))[1];
202 }
203  
204 # ============================================================================
205 1; # [end Net::SNMP::Transport::IPv6]
206