| 1 | # Paranoid::Network::Socket -- Socket wrapper for seemless IPv6 support | ||||||||||
| 2 | # | ||||||||||
| 3 | # $Id: lib/Paranoid/Network/Socket.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ | ||||||||||
| 4 | # | ||||||||||
| 5 | # This software is free software. Similar to Perl, you can redistribute it | ||||||||||
| 6 | # and/or modify it under the terms of either: | ||||||||||
| 7 | # | ||||||||||
| 8 | # a) the GNU General Public License | ||||||||||
| 9 | # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the | ||||||||||
| 10 | # Free Software Foundation <http://www.fsf.org/>; either version 1 | ||||||||||
| 11 | # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version | ||||||||||
| 12 | # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or | ||||||||||
| 13 | # b) the Artistic License 2.0 | ||||||||||
| 14 | # <https://opensource.org/licenses/Artistic-2.0>, | ||||||||||
| 15 | # | ||||||||||
| 16 | # subject to the following additional term: No trademark rights to | ||||||||||
| 17 | # "Paranoid" have been or are conveyed under any of the above licenses. | ||||||||||
| 18 | # However, "Paranoid" may be used fairly to describe this unmodified | ||||||||||
| 19 | # software, in good faith, but not as a trademark. | ||||||||||
| 20 | # | ||||||||||
| 21 | # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) | ||||||||||
| 22 | # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com) | ||||||||||
| 23 | # | ||||||||||
| 24 | ##################################################################### | ||||||||||
| 25 | |||||||||||
| 26 | ##################################################################### | ||||||||||
| 27 | # | ||||||||||
| 28 | # Environment definitions | ||||||||||
| 29 | # | ||||||||||
| 30 | ##################################################################### | ||||||||||
| 31 | |||||||||||
| 32 | package Paranoid::Network::Socket; | ||||||||||
| 33 | |||||||||||
| 34 | 6 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 6 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 6 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 6 | ▶ | use Socket qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 6 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||
| 43 | |||||||||||
| 44 | ##################################################################### | ||||||||||
| 45 | # | ||||||||||
| 46 | # Module code follows | ||||||||||
| 47 | # | ||||||||||
| 48 | ##################################################################### | ||||||||||
| 49 | |||||||||||
| 50 | 6 | our $ipv6_enabled; | |||||||||
| 51 | 6 | our $socket6; | |||||||||
| 52 | |||||||||||
| 53 | 72 | ▶ | sub has_ipv6 { | ||||||||
|
Subroutine
has_ipv6: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 72 | return $ipv6_enabled; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 6 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=12 SCAR=41.3
| |||||||||||
| 58 | |||||||||||
| 59 | 6 | @EXPORT = ( 'has_ipv6', @Socket::EXPORT ); | |||||||||
| 60 | 6 | @EXPORT_OK = ( 'has_ipv6', @Socket::EXPORT_OK ); | |||||||||
| 61 | %EXPORT_TAGS = ( | ||||||||||
| 62 | %Socket::EXPORT_TAGS, | ||||||||||
| 63 | 6 | all => [ 'has_ipv6', @{ $Socket::EXPORT_TAGS{all} } ], | |||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | # Check to see if we've got any IPv6 functions available | ||||||||||
| 67 | 6 | $socket6 = 0; | |||||||||
| 68 | 6 | ▶ | $ipv6_enabled = ( defined *sockaddr_in6{CODE} ) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | # Set inet_pton/inet_ntop to import by default -- don't know why | ||||||||||
| 71 | # this isn't done in Socket at all... | ||||||||||
| 72 | 6 | ▶ | if ( grep { $_ eq 'inet_pton' } @EXPORT_OK ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 6 | push @EXPORT, qw(inet_pton inet_ntop); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 6 | ▶ | unless ($ipv6_enabled) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | # Socket didn't provide it, let's see if Socket6 is available | ||||||||||
| 79 | 0 | ▶ | if ( eval 'require Socket6; 1;' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | # Conditionally import Socket6 routines. This is important | ||||||||||
| 82 | # because perl 5.12 has partial IPv6 support, and 5.14 full. I | ||||||||||
| 83 | # want to avoid redefined and prototype mismatch warnings. | ||||||||||
| 84 | 0 | for my $symbol (@Socket6::EXPORT) { | |||||||||
| 85 | 0 | ▶ | unless ( grep /^$symbol$/s, @EXPORT, @EXPORT_OK ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | import Socket6 $symbol; | |||||||||
| 87 | 0 | push @EXPORT, $symbol; | |||||||||
| 88 | 0 | push @{ $EXPORT_TAGS{all} }, $symbol; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | 0 | for my $symbol (@Socket6::EXPORT_OK) { | |||||||||
| 92 | 0 | ▶ | unless ( grep /^$symbol$/s, @EXPORT, @EXPORT_OK ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | import Socket6 $symbol; | |||||||||
| 94 | 0 | push @EXPORT_OK, $symbol; | |||||||||
| 95 | 0 | push @{ $EXPORT_TAGS{all} }, $symbol; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | # Check one more time... | ||||||||||
| 101 | 0 | ▶ | $ipv6_enabled = ( defined *sockaddr_in6{CODE} ) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | ▶ | if ($ipv6_enabled) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | ▶ | $socket6 = *sockaddr_in{PACKAGE} eq 'Socket6' ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 6 | 1; | |||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||