Codebase list libnet-frame-perl / upstream/1.16
Imported Upstream version 1.16 Florian Schlichting 8 years ago
21 changed file(s) with 498 addition(s) and 143 deletion(s). Raw diff Collapse all Expand all
0 #
1 # $Id: Build.PL 365 2015-01-28 19:04:14Z gomor $
2 #
3 use strict;
4 use warnings;
5
6 use Module::Build;
7
8 my @conditions_modules = ();
9 eval {
10 require Socket;
11 Socket->import(
12 qw(AF_INET6 getaddrinfo getnameinfo inet_pton inet_ntop)
13 );
14 };
15 if ($@) {
16 @conditions_modules = ( Socket6 => 0 );
17 }
18
19 my $builder = Module::Build->new(
20 module_name => 'Net::Frame',
21 license => 'artistic',
22 dist_author => 'GomoR <gomor_at_cpan.org>',
23 dist_version_from => 'lib/Net/Frame.pm',
24 requires => {
25 'perl' => '5.6.1',
26 'Class::Gomor' => '1.00',
27 'Net::IPv6Addr' => '0',
28 'Bit::Vector' => '0',
29 'Socket' => '0',
30 @conditions_modules,
31 },
32 configure_requires => {
33 'Module::Build' => 0,
34 },
35 );
36
37 $builder->create_build_script;
00 Revision history for Perl extension Net::Frame.
1
2 1.16 Mon Nov 16 09:31:06 CET 2015
3 - bugfix: t/13-gethostsubs.t updated cause gomor.org addresses have changed
4
5 1.15 Sun Feb 15 18:09:23 CET 2015
6 - BUGFIX: IPv4 header length calculation on Mac OS X and OpenBSD
7 => Read: support for OpenBSD and Mac OS X now ok
8 - bugfix: check if inet_ntop/getaddrinfo are *really* supported by trying to use them.
9
10 1.14 Wed Jan 28 20:01:48 CET 2015
11 - BUGFIX: getHostIpv6addr: not working on FreeBSD/Socket because of nasty getaddrinfo()
12 - tests: added tests for getHost* subs and inet* subs
13
14 1.13 Wed Jan 28 07:49:11 CET 2015
15 - BUGFIX: getHostIpv6Addr: use getaddrinfo/getnameinfo from Socket or Socket6 where available
16 => Thanks to Vince
17
18 1.12 Tue Jan 20 19:33:06 CET 2015
19 - bugfix: TCP/UDP computeChecksums() so 6to4 and other encapsulations work
20 => Thanks to Vince
21 - update: copyright notice
22 - update: Kwalitee
123
224 1.11 Tue Apr 8 15:33:43 CEST 2014
325 - bugfix: https://rt.cpan.org/Public/Bug/Display.html?id=94035
0 Build.PL
01 Changes
12 examples/arp-lookup.pl
23 examples/build-tcp-syn.pl
34 examples/pack-eth-ipv4-tcp-compute.pl
45 examples/pack-eth-ipv4-tcp.pl
6 examples/resolv-ipv6.pl
57 examples/send-recv-tcp.pl
68 examples/send-recv-udp.pl
79 examples/send-tcp-with-padding.pl
3537 t/10-null.t
3638 t/11-udp.t
3739 t/12-ppp.t
38 META.yml Module YAML meta-data (added by MakeMaker)
39 META.json Module JSON meta-data (added by MakeMaker)
40 t/13-gethostsubs.t
41 META.yml
42 META.json
00 {
11 "abstract" : "the base framework for frame crafting",
22 "author" : [
3 "GomoR <gomor-cpan_at_gomor.org>"
3 "GomoR <gomor_at_cpan.org>"
44 ],
55 "dynamic_config" : 1,
6 "generated_by" : "ExtUtils::MakeMaker version 6.94, CPAN::Meta::Converter version 2.132510",
6 "generated_by" : "Module::Build version 0.421",
77 "license" : [
88 "artistic_1"
99 ],
1212 "version" : "2"
1313 },
1414 "name" : "Net-Frame",
15 "no_index" : {
16 "directory" : [
17 "t",
18 "inc"
19 ]
20 },
2115 "prereqs" : {
22 "build" : {
23 "requires" : {
24 "ExtUtils::MakeMaker" : "0"
25 }
26 },
2716 "configure" : {
2817 "requires" : {
29 "ExtUtils::MakeMaker" : "0"
18 "Module::Build" : "0"
3019 }
3120 },
3221 "runtime" : {
3322 "requires" : {
3423 "Bit::Vector" : "0",
3524 "Class::Gomor" : "1.00",
36 "Net::IPv6Addr" : "0"
25 "Net::IPv6Addr" : "0",
26 "Socket" : "0",
27 "perl" : "v5.6.1"
3728 }
3829 }
3930 },
31 "provides" : {
32 "Net::Frame" : {
33 "file" : "lib/Net/Frame.pm",
34 "version" : "1.16"
35 },
36 "Net::Frame::Layer" : {
37 "file" : "lib/Net/Frame/Layer.pm"
38 },
39 "Net::Frame::Layer::ARP" : {
40 "file" : "lib/Net/Frame/Layer/ARP.pm"
41 },
42 "Net::Frame::Layer::ETH" : {
43 "file" : "lib/Net/Frame/Layer/ETH.pm"
44 },
45 "Net::Frame::Layer::IPv4" : {
46 "file" : "lib/Net/Frame/Layer/IPv4.pm"
47 },
48 "Net::Frame::Layer::NULL" : {
49 "file" : "lib/Net/Frame/Layer/NULL.pm"
50 },
51 "Net::Frame::Layer::PPP" : {
52 "file" : "lib/Net/Frame/Layer/PPP.pm"
53 },
54 "Net::Frame::Layer::RAW" : {
55 "file" : "lib/Net/Frame/Layer/RAW.pm"
56 },
57 "Net::Frame::Layer::SLL" : {
58 "file" : "lib/Net/Frame/Layer/SLL.pm"
59 },
60 "Net::Frame::Layer::TCP" : {
61 "file" : "lib/Net/Frame/Layer/TCP.pm"
62 },
63 "Net::Frame::Layer::UDP" : {
64 "file" : "lib/Net/Frame/Layer/UDP.pm"
65 }
66 },
4067 "release_status" : "stable",
41 "version" : "1.11"
68 "resources" : {
69 "license" : [
70 "http://www.perlfoundation.org/artistic_license_1_0"
71 ]
72 },
73 "version" : "1.16"
4274 }
00 ---
11 abstract: 'the base framework for frame crafting'
22 author:
3 - 'GomoR <gomor-cpan_at_gomor.org>'
4 build_requires:
5 ExtUtils::MakeMaker: 0
3 - 'GomoR <gomor_at_cpan.org>'
4 build_requires: {}
65 configure_requires:
7 ExtUtils::MakeMaker: 0
6 Module::Build: '0'
87 dynamic_config: 1
9 generated_by: 'ExtUtils::MakeMaker version 6.94, CPAN::Meta::Converter version 2.132510'
8 generated_by: 'Module::Build version 0.421, CPAN::Meta::Converter version 2.142690'
109 license: artistic
1110 meta-spec:
1211 url: http://module-build.sourceforge.net/META-spec-v1.4.html
13 version: 1.4
12 version: '1.4'
1413 name: Net-Frame
15 no_index:
16 directory:
17 - t
18 - inc
14 provides:
15 Net::Frame:
16 file: lib/Net/Frame.pm
17 version: '1.16'
18 Net::Frame::Layer:
19 file: lib/Net/Frame/Layer.pm
20 Net::Frame::Layer::ARP:
21 file: lib/Net/Frame/Layer/ARP.pm
22 Net::Frame::Layer::ETH:
23 file: lib/Net/Frame/Layer/ETH.pm
24 Net::Frame::Layer::IPv4:
25 file: lib/Net/Frame/Layer/IPv4.pm
26 Net::Frame::Layer::NULL:
27 file: lib/Net/Frame/Layer/NULL.pm
28 Net::Frame::Layer::PPP:
29 file: lib/Net/Frame/Layer/PPP.pm
30 Net::Frame::Layer::RAW:
31 file: lib/Net/Frame/Layer/RAW.pm
32 Net::Frame::Layer::SLL:
33 file: lib/Net/Frame/Layer/SLL.pm
34 Net::Frame::Layer::TCP:
35 file: lib/Net/Frame/Layer/TCP.pm
36 Net::Frame::Layer::UDP:
37 file: lib/Net/Frame/Layer/UDP.pm
1938 requires:
20 Bit::Vector: 0
21 Class::Gomor: 1.00
22 Net::IPv6Addr: 0
23 version: 1.11
39 Bit::Vector: '0'
40 Class::Gomor: '1.00'
41 Net::IPv6Addr: '0'
42 Socket: '0'
43 perl: v5.6.1
44 resources:
45 license: http://www.perlfoundation.org/artistic_license_1_0
46 version: '1.16'
00 #
1 # $Id: Makefile.PL 353 2014-03-10 12:25:04Z gomor $
1 # $Id: Makefile.PL 365 2015-01-28 19:04:14Z gomor $
22 #
33 use ExtUtils::MakeMaker;
44
66
77 my @conditions_modules = ();
88 eval {
9 require Socket;
10 Socket->import(
11 qw(AF_INET6 NI_NUMERICHOST NI_NUMERICSERV getaddrinfo getnameinfo inet_pton inet_ntop));
9 require Socket;
10 Socket->import(
11 qw(AF_INET6 getaddrinfo getnameinfo inet_pton inet_ntop)
12 );
1213 };
1314 if ($@) {
14 @conditions_modules = ( Socket6 => 0 );
15 @conditions_modules = ( Socket6 => 0 );
1516 }
1617
1718 WriteMakefile(
18 NAME => 'Net::Frame',
19 VERSION_FROM => 'lib/Net/Frame.pm',
20 LICENSE => 'artistic',
19 NAME => 'Net::Frame',
20 VERSION_FROM => 'lib/Net/Frame.pm',
21 LICENSE => 'artistic',
2122 ABSTRACT_FROM => 'lib/Net/Frame.pm',
22 AUTHOR => 'GomoR <gomor-cpan_at_gomor.org>',
23 PREREQ_PM => {
24 Class::Gomor => '1.00',
23 AUTHOR => 'GomoR <gomor_at_cpan.org>',
24 MIN_PERL_VERSION => '5.6.1',
25 PREREQ_PM => {
26 Class::Gomor => '1.00',
2527 Net::IPv6Addr => 0,
26 Bit::Vector => 0,
28 Bit::Vector => 0,
29 Socket => 0,
2730 @conditions_modules,
2831 },
2932 );
1313
1414 This module requires these other modules and libraries:
1515
16 Perl v5.6.1
17 Bit::Vector
18 Class::Gomor
19 Net::IPv6Addr
20 Socket6
16 Perl v5.6.1
17 Bit::Vector
18 Class::Gomor
19 Net::IPv6Addr
20 Socket
21 Socket6 (optional if Socket module supports IPv6)
2122
2223 GETTING HELP
2324
3031 You may distribute this module under the terms of the Artistic license.
3132 See LICENSE.Artistic file in the source distribution archive.
3233
33 Copyright (c) 2006-2014, Patrice <GomoR> Auffret
34 Copyright (c) 2006-2015, Patrice <GomoR> Auffret
3435
0 #!/usr/bin/perl
1 use strict;
2 use warnings;
3
4 use Net::Frame::Layer qw(:subs);
5
6 print getHostIpv6Addr('www.google.com')."\n";
3333 my $oSimple = Net::Frame::Simple->new(
3434 layers => [ $ip4, $tcp ],
3535 );
36 print "raw: ".unpack('H*', $oSimple->raw)."\n";
3637 $oWrite->open;
3738 $oSimple->send($oWrite);
3839 $oWrite->close;
00 #
1 # $Id: ARP.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: ARP.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::ARP;
44 use strict;
375375
376376 =head1 COPYRIGHT AND LICENSE
377377
378 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
378 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
379379
380380 You may distribute this module under the terms of the Artistic license.
381381 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: ETH.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: ETH.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::ETH;
44 use strict;
404404
405405 =head1 COPYRIGHT AND LICENSE
406406
407 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
407 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
408408
409409 You may distribute this module under the terms of the Artistic license.
410410 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: IPv4.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: IPv4.pm 367 2015-02-15 17:11:07Z gomor $
22 #
33 package Net::Frame::Layer::IPv4;
44 use strict;
102102 my $osname = {
103103 freebsd => [ \&_fixLenBsd, ],
104104 netbsd => [ \&_fixLenBsd, ],
105 openbsd => [ \&_fixLenBsd, ],
106 darwin => [ \&_fixLenBsd, ],
105107 };
106108
107109 *_fixLen = $osname->{$^O}->[0] || \&_fixLenOther;
586588
587589 =head1 COPYRIGHT AND LICENSE
588590
589 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
591 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
590592
591593 You may distribute this module under the terms of the Artistic license.
592594 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: NULL.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: NULL.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::NULL;
44 use strict;
286286
287287 =head1 COPYRIGHT AND LICENSE
288288
289 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
289 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
290290
291291 You may distribute this module under the terms of the Artistic license.
292292 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: PPP.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: PPP.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::PPP;
44 use strict;
231231
232232 =head1 COPYRIGHT AND LICENSE
233233
234 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
234 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
235235
236236 You may distribute this module under the terms of the Artistic license.
237237 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: RAW.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: RAW.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::RAW;
44 use strict;
157157
158158 =head1 COPYRIGHT AND LICENSE
159159
160 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
160 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
161161
162162 You may distribute this module under the terms of the Artistic license.
163163 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: SLL.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: SLL.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::SLL;
44 use strict;
384384
385385 =head1 COPYRIGHT AND LICENSE
386386
387 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
387 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
388388
389389 You may distribute this module under the terms of the Artistic license.
390390 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: TCP.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: TCP.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::TCP;
44 use strict; use warnings;
180180 if ($l->layer eq 'IPv4') {
181181 $phpkt = $self->SUPER::pack('a4a4CCn',
182182 inetAton($l->src), inetAton($l->dst), 0, 6, $len);
183 last;
184183 }
185184 elsif ($l->layer eq 'IPv6') {
186185 $phpkt = $self->SUPER::pack('a*a*NnCC',
187186 inet6Aton($l->src), inet6Aton($l->dst), $len, 0, 0, 6);
188 last;
189187 }
190188 }
191189
463461
464462 =head1 COPYRIGHT AND LICENSE
465463
466 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
464 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
467465
468466 You may distribute this module under the terms of the Artistic license.
469467 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: UDP.pm 356 2014-04-08 13:15:27Z gomor $
1 # $Id: UDP.pm 360 2015-01-20 18:36:06Z gomor $
22 #
33 package Net::Frame::Layer::UDP;
44 use strict;
115115 if ($l->layer eq 'IPv4') {
116116 $phpkt = $self->SUPER::pack('a4a4CCn',
117117 inetAton($l->src), inetAton($l->dst), 0, 17, $self->[$__length]);
118 last;
119118 }
120119 elsif ($l->layer eq 'IPv6') {
121120 $phpkt = $self->SUPER::pack('a*a*NnCC',
122121 inet6Aton($l->src), inet6Aton($l->dst), $self->[$__length],
123122 0, 0, 17);
124 last;
125123 }
126124 }
127125
331329
332330 =head1 COPYRIGHT AND LICENSE
333331
334 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
332 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
335333
336334 You may distribute this module under the terms of the Artistic license.
337335 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: Layer.pm 357 2014-04-08 13:34:04Z gomor $
1 # $Id: Layer.pm 367 2015-02-15 17:11:07Z gomor $
22 #
33 package Net::Frame::Layer;
44 use strict;
9494 # Useful subroutines
9595 #
9696
97 # load AF_INET and default imports from Socket. Safe back to at least 5.8.8
97 # Load AF_INET and default imports from Socket. Safe back to at least 5.8.8.
9898 use Socket qw(:DEFAULT AF_INET);
99
100 sub _setInet6Sub {
101 no strict 'refs';
102
103 my $inetp_found = 0;
104
105 # Check Socket against some IPv6 functions and constants.
106 eval {
107 require Socket;
108 Socket->import(qw(AF_INET6 inet_pton inet_ntop));
109 };
110 if (! $@) { # Socket has support for required functions and constants.
111 *{__PACKAGE__.'::_inet_pton'} = \&Socket::inet_pton;
112 *{__PACKAGE__.'::_inet_ntop'} = \&Socket::inet_ntop;
113
114 $inetp_found = 1;
115 }
116
117 # Fallback to Socket6
118 if (! $inetp_found) {
119 eval {
120 require Socket6;
121 Socket6->import(qw(AF_INET6 inet_pton inet_ntop));
122 };
123 if (! $@) { # Socket6 has support for required functions and constants.
124 *{__PACKAGE__.'::_inet_pton'} = \&Socket6::inet_pton;
125 *{__PACKAGE__.'::_inet_ntop'} = \&Socket6::inet_ntop;
126 }
127 }
128
129 # Unfortunately, we have to test if inet_ntop()/inet_pton() works (i.e., are implemented)
130 # If no support for inet_ntop/inet_pton, we branch to fake functions.
131 if ($inetp_found) {
132 eval {
133 # inet_pton() may exist, but die with:
134 # inet_pton not implemented on this architecture
135 _inet_pton(AF_INET6(), "::1");
136 };
137 if ($@) {
138 print "[!] inet_pton support: $@\n";
139 *{__PACKAGE__.'::_inet_pton'} = sub { return 0; };
140 *{__PACKAGE__.'::_inet_ntop'} = sub { return 0; };
141 }
142 else {
143 return 1; # OK
144 }
145 }
146
147 die("[-] Net::Frame: inet_pton/inet_ntop: not supported by Socket nor Socket6: ".
148 "try upgrading your Perl version or Socket/Socket6 modules.\n");
149 }
150
151 sub _setGetAddressSub {
152 no strict 'refs';
153
154 my $getaddr_found = 0;
155
156 # Check Socket against some IPv6 functions and constants.
157 eval {
158 require Socket;
159 Socket->import(qw(getaddrinfo getnameinfo AF_INET6));
160 };
161 if (! $@) { # Socket has support for required functions and constants.
162 *{__PACKAGE__.'::_getAddress'} = sub {
163 my ($name) = @_;
164
165 #print STDERR "*** Socket supports IPv6 OK\n";
166
167 my %hints = (
168 family => Socket::AF_INET6(),
169 );
170 my ($err, @res) = Socket::getaddrinfo($name, '', \%hints);
171 if ($err) {
172 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getaddrinfo: $err\n");
173 return;
174 }
175 if (@res > 0) {
176 my $h = $res[0];
177 my ($err, $ipv6) = Socket::getnameinfo(
178 $h->{addr}, Socket::NI_NUMERICHOST() | Socket::NI_NUMERICSERV()
179 );
180 if ($err) {
181 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getnameinfo: $err\n");
182 return;
183 }
184
185 return $ipv6;
186 }
187 else {
188 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getaddrinfo: $!\n");
189 return;
190 }
191 };
192
193 $getaddr_found = 1;
194 }
195
196 # Fallback to Socket6
197 if (! $getaddr_found) {
198 eval {
199 require Socket6;
200 Socket6->import(qw(getaddrinfo getnameinfo AF_INET6));
201 };
202 if (! $@) { # Socket6 has support for required functions and constants.
203 *{__PACKAGE__.'::_getAddress'} = sub {
204 my ($name) = @_;
205
206 #print STDERR "*** Fallback to Socket6 support\n";
207
208 my @res = Socket6::getaddrinfo($name, '', Socket6::AF_INET6(), SOCK_STREAM);
209 if (@res >= 5) {
210 my ($ipv6) = Socket6::getnameinfo(
211 $res[3], Socket6::NI_NUMERICHOST() | Socket6::NI_NUMERICSERV()
212 );
213
214 return $ipv6;
215 }
216 };
217 }
218
219 $getaddr_found = 1;
220 }
221
222 # Unfortunately, we have to test if INET6 family is supported
223 # If no support, we branch to fake functions.
224 if ($getaddr_found) {
225 eval {
226 # getaddrinfo() may exist, but die with:
227 # getaddrinfo: ai_family not supported
228 _getAddress("::1");
229 };
230 if ($@) {
231 print "[!] getaddrinfo support: $@\n";
232 *{__PACKAGE__.'::_getAddress'} = sub { return 0; };
233 *{__PACKAGE__.'::_getAddress'} = sub { return 0; };
234 }
235 else {
236 return 1; # OK
237 }
238 }
239
240 die("[-] Net::Frame: getaddrinfo/getnameinfo: not supported by Socket nor Socket6: ".
241 "try upgrading your Perl version or Socket/Socket6 modules.\n");
242 }
243
99244 BEGIN {
100 # imports that may or may not be in Socket.
101 my @imports = (
102 qw(AF_INET6 NI_NUMERICHOST NI_NUMERICSERV getaddrinfo getnameinfo));
103 my @socket6_imports;
104
105 # This might be overkill, but I'm not certain that all these imports
106 # were added to Socket at the same time.
107 for my $export (@imports) {
108 eval { Socket->import($export); };
109 if ($@) {
110 push @socket6_imports, $export;
111 }
112 }
113
114 eval {
115 # Test to see if the sub works.
116 # Socket::inet_ntop() may exist, but die with:
117 # Socket::inet_ntop not implemented on this architecture
118 Socket::inet_ntop( AF_INET, "\0\0\0\0" ); # test
119 Socket->import('inet_ntop'); # import if the test doesn't die
120 };
121 if ($@) {
122 push @socket6_imports, 'inet_ntop';
123 }
124 eval {
125 # Test to see if the sub works.
126 # Socket::inet_pton() may exist, but die with:
127 # Socket::inet_pton not implemented on this architecture
128 Socket::inet_pton( AF_INET, '0.0.0.0' ); # test
129 Socket->import('inet_pton'); # import if the test doesn't die
130 };
131 if ($@) {
132 push @socket6_imports, 'inet_pton';
133 }
134
135 if (@socket6_imports) {
136
137 # something we want wasn't found in Socket time to try Socket6
138 eval { require Socket6 };
139 die $@ if $@;
140 Socket6->import(@socket6_imports);
141 }
245 _setInet6Sub();
246 _setGetAddressSub();
142247 }
143248
144249 require Net::IPv6Addr;
146251 sub getHostIpv4Addr {
147252 my ($name) = @_;
148253
149 return undef unless $name;
150 return $name if $name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
254 # No address given
255 if (! defined($name)) {
256 return;
257 }
258
259 # Already an IPv4 address
260 if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
261 return $name;
262 }
151263
152264 my @addrs = (gethostbyname($name))[4];
153265 @addrs ? return join('.', CORE::unpack('C4', $addrs[0]))
154266 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
155 return undef;
267
268 # Error
269 return;
156270 }
157271
158272 sub getHostIpv4Addrs {
159273 my ($name) = @_;
160274
161 return undef unless $name;
162 return $name if $name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
275 # No address given
276 if (! defined($name)) {
277 return;
278 }
279
280 # Already an IPv4 address
281 if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
282 return $name;
283 }
163284
164285 my @addrs = (gethostbyname($name))[4];
165286 @addrs ? return @addrs
166287 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
288
289 # Error
167290 return ();
168291 }
169292
170293 sub getHostIpv6Addr {
171294 my ($name) = @_;
172295
173 return undef unless $name;
174 return $name if Net::IPv6Addr::is_ipv6($name);
175
176 my @res = getaddrinfo($name, 'ssh', AF_INET6, SOCK_STREAM);
177 if (@res >= 5) {
178 my ($ipv6) = getnameinfo($res[3], NI_NUMERICHOST | NI_NUMERICSERV);
179 $ipv6 =~ s/%.*$//;
180 return $ipv6;
181 }
182 else {
296 # No address given
297 if (! defined($name)) {
298 return;
299 }
300
301 # Already an IPv6 address
302 if (Net::IPv6Addr::is_ipv6($name)) {
303 return $name;
304 }
305
306 my $ipv6 = _getAddress($name);
307 if (! defined($ipv6)) {
183308 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
184 }
185 undef;
186 }
187
188 sub inetAton { inet_aton(shift()) }
189 sub inetNtoa { inet_ntoa(shift()) }
190 sub inet6Aton { inet_pton(AF_INET6, shift()) }
191 sub inet6Ntoa { inet_ntop(AF_INET6, shift()) }
309 return;
310 }
311
312 $ipv6 =~ s/%.*$//;
313
314 return $ipv6;
315 }
316
317 sub inetAton { Socket::inet_aton(shift()) }
318 sub inetNtoa { Socket::inet_ntoa(shift()) }
319 sub inet6Aton { _inet_pton(AF_INET6, shift()) }
320 sub inet6Ntoa { _inet_ntop(AF_INET6, shift()) }
192321
193322 sub getRandomHighPort {
194323 my $highPort = int rand 0xffff;
374503
375504 =head1 COPYRIGHT AND LICENSE
376505
377 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
506 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
378507
379508 You may distribute this module under the terms of the Artistic license.
380509 See LICENSE.Artistic file in the source distribution archive.
00 #
1 # $Id: Frame.pm 357 2014-04-08 13:34:04Z gomor $
1 # $Id: Frame.pm 369 2015-11-16 08:40:19Z gomor $
22 #
33 package Net::Frame;
44 use strict;
55 use warnings;
66
7 our $VERSION = '1.11';
7 our $VERSION = '1.16';
88
99 1;
1010
8383
8484 =head1 COPYRIGHT AND LICENSE
8585
86 Copyright (c) 2006-2014, Patrice E<lt>GomoRE<gt> Auffret
86 Copyright (c) 2006-2015, Patrice E<lt>GomoRE<gt> Auffret
8787
8888 You may distribute this module under the terms of the Artistic license.
8989 See LICENSE.Artistic file in the source distribution archive.
0 use Test;
1 BEGIN { plan(tests => 6) }
2
3 use Net::Frame::Layer qw(:consts :subs);
4
5 my $host = 'gomor.org';
6 my $ip6 = '2001:41d0:2:1a47::2';
7 my $ip4 = '94.23.25.71';
8
9 #
10 # IPv4 functions
11 #
12 ok(
13 sub {
14 my $ip = getHostIpv4Addr($host);
15 if ($ip eq $ip4) {
16 print "[+] $ip\n";
17 return 1; # OK
18 }
19 print "[-] $ip\n";
20 return 0; # NOK
21 },
22 1,
23 $@,
24 );
25
26 ok(
27 sub {
28 my $a = inetAton("127.0.0.1");
29 if ($a && unpack('H*', $a) eq '7f000001') {
30 print "[+] ".unpack('H*', $a)."\n";
31 return 1; # OK
32 }
33 print "[-] ".unpack('H*', $a)."\n";
34 return 0; # NOK
35 },
36 1,
37 $@,
38 );
39
40 ok(
41 sub {
42 my $a = inetNtoa(pack('H*', '7f000001'));
43 if ($a && $a eq '127.0.0.1') {
44 print "[+] $a\n";
45 return 1; # OK
46 }
47 print "[-] $a\n";
48 return 0; # NOK
49 },
50 1,
51 $@,
52 );
53
54 #
55 # IPv6 functions
56 #
57 ok(
58 sub {
59 my $ip = getHostIpv6Addr($host);
60 if ($ip eq $ip6) {
61 print "[+] $ip\n";
62 return 1; # OK
63 }
64 print "[-] $ip\n";
65 return 0; # NOK
66 },
67 1,
68 $@,
69 );
70
71 ok(
72 sub {
73 my $a = inet6Aton($ip6);
74 if ($a && unpack('H*', $a) eq '200141d000021a470000000000000002') {
75 print "[+] ".unpack('H*', $a)."\n";
76 return 1; # OK
77 }
78 print "[-] ".unpack('H*', $a)."\n";
79 return 0; # NOK
80 },
81 1,
82 $@,
83 );
84
85 ok(
86 sub {
87 my $a = inet6Ntoa(pack('H*', '200141d000021a470000000000000002'));
88 if ($a && $a eq $ip6) {
89 print "[+] $a\n";
90 return 1; # OK
91 }
92 print "[-] $a\n";
93 return 0; # NOK
94 },
95 1,
96 $@,
97 );