| 1 | package Sidef::Types::Glob::Socket; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | our $AUTOLOAD; | ||
| 6 | |||
| 7 | use parent qw( | ||
| 8 | Sidef::Object::Object | ||
| 9 | ); | ||
| 10 | |||
| 11 | use Sidef::Types::Bool::Bool; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | bless {}, __PACKAGE__; | ||
| 15 | } | ||
| 16 | |||
| 17 | { | ||
| 18 | no strict 'refs'; | ||
| 19 | require Socket; | ||
| 20 | |||
| 21 | foreach my $name (@Socket::EXPORT, @Socket::EXPORT_OK) { | ||
| 22 | $name =~ /^[a-z]/i or next; | ||
| 23 | *{__PACKAGE__ . '::' . $name} = sub { | ||
| 24 | my ($self, @args) = @_; | ||
| 25 | |||
| 26 | my $func = \&{'Socket' . '::' . $name}; | ||
| 27 | @args = map { $_->get_value } @args; | ||
| 28 | |||
| 29 | my @results = eval { $func->(@args) }; | ||
| 30 | |||
| 31 | if ($@) { | ||
| 32 | @results = scalar $func->(@args); | ||
| 33 | } | ||
| 34 | |||
| 35 | return ( | ||
| 36 | @results > 1 | ||
| 37 | ? (map { Sidef::Types::Perl::Perl->to_sidef($_) } @results) | ||
| 38 | : Sidef::Types::Perl::Perl->to_sidef($results[0]) | ||
| 39 | ); | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | sub open { | ||
| 45 | my ($self, $domain, $type, $protocol) = @_; | ||
| 46 | CORE::socket(my $sh, $domain, $type, $protocol) || return undef; | ||
| 47 | Sidef::Types::Glob::SocketHandle->new($sh); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub socketpair { | ||
| 51 | my ($self, $socket1, $socket2, $domain, $type, $protocol) = @_; | ||
| 52 | CORE::socketpair(my $sh1, my $sh2, $domain, $type, $protocol) | ||
| 53 | || return (Sidef::Types::Bool::Bool::FALSE); | ||
| 54 | ${$socket1} = Sidef::Types::Glob::SocketHandle->new($sh1); | ||
| 55 | ${$socket2} = Sidef::Types::Glob::SocketHandle->new($sh2); | ||
| 56 | (Sidef::Types::Bool::Bool::TRUE); | ||
| 57 | } | ||
| 58 | |||
| 59 | # | ||
| 60 | ## gethost* | ||
| 61 | # | ||
| 62 | sub gethostbyname { | ||
| 63 | my ($self, $name) = @_; | ||
| 64 | Sidef::Types::String::String->new(CORE::gethostbyname("$name") // return undef); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub gethostbyaddr { | ||
| 68 | my ($self, $addr, $addrtype) = @_; | ||
| 69 | Sidef::Types::String::String->new(CORE::gethostbyaddr("$addr", "$addrtype") // return undef); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub gethostent { | ||
| 73 | my ($self) = @_; | ||
| 74 | Sidef::Types::String::String->new(CORE::gethostent() // return undef); | ||
| 75 | } | ||
| 76 | |||
| 77 | # | ||
| 78 | ## getnet* | ||
| 79 | # | ||
| 80 | sub getnetbyname { | ||
| 81 | my ($self, $name) = @_; | ||
| 82 | Sidef::Types::String::String->new(CORE::getnetbyname("$name") // return undef); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub getnetbyaddr { | ||
| 86 | my ($self, $addr, $addrtype) = @_; | ||
| 87 | Sidef::Types::String::String->new(CORE::getnetbyaddr("$addr", "$addrtype") // return undef); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub getnetent { | ||
| 91 | my ($self) = @_; | ||
| 92 | Sidef::Types::String::String->new(CORE::getnetent() // return undef); | ||
| 93 | } | ||
| 94 | |||
| 95 | # | ||
| 96 | ## getserv* | ||
| 97 | # | ||
| 98 | sub getservbyname { | ||
| 99 | my ($self, $name, $proto) = @_; | ||
| 100 | Sidef::Types::String::String->new(CORE::getservbyname("$name", $proto) // return undef); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub getservbyport { | ||
| 104 | my ($self, $port, $proto) = @_; | ||
| 105 | Sidef::Types::String::String->new(CORE::getservbyport($port, $proto) // return undef); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub getservent { | ||
| 109 | my ($self) = @_; | ||
| 110 | Sidef::Types::String::String->new(CORE::getservent() // return undef); | ||
| 111 | } | ||
| 112 | |||
| 113 | # | ||
| 114 | ## getproto* | ||
| 115 | # | ||
| 116 | sub getprotobynumber { | ||
| 117 | my ($self, $num) = @_; | ||
| 118 | Sidef::Types::String::String->new(CORE::getprotobynumber(CORE::int($num)) // return undef); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub getprotobyname { | ||
| 122 | my ($self, $name) = @_; | ||
| 123 | Sidef::Types::Number::Number->new(CORE::getprotobyname("$name") // (return undef)); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub getprotoent { | ||
| 127 | my ($self) = @_; | ||
| 128 | Sidef::Types::Number::Number->new(CORE::getprotoent() // (return undef)); | ||
| 129 | } | ||
| 130 | |||
| 131 | # | ||
| 132 | ## set* | ||
| 133 | # | ||
| 134 | sub sethostent { | ||
| 135 | my ($self, $stayopen) = @_; | ||
| 136 | (CORE::sethostent($stayopen ? 1 : 0)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub setnetent { | ||
| 140 | my ($self, $stayopen) = @_; | ||
| 141 | (CORE::setnetent($stayopen ? 1 : 0)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub setprotoent { | ||
| 145 | my ($self, $stayopen) = @_; | ||
| 146 | (CORE::setprotoent($stayopen ? 1 : 0)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub setservent { | ||
| 150 | my ($self, $stayopen) = @_; | ||
| 151 | (CORE::setservent($stayopen ? 1 : 0)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 152 | } | ||
| 153 | |||
| 154 | 1 |