ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Socket.pm (file contents):
Revision 1.92 by root, Thu Jul 16 20:55:38 2009 UTC vs.
Revision 1.93 by root, Thu Jul 16 23:27:46 2009 UTC

47use AnyEvent::DNS (); 47use AnyEvent::DNS ();
48 48
49use base 'Exporter'; 49use base 'Exporter';
50 50
51our @EXPORT = qw( 51our @EXPORT = qw(
52 getprotobyname
52 parse_hostport 53 parse_hostport
53 parse_ipv4 parse_ipv6 54 parse_ipv4 parse_ipv6
54 parse_ip parse_address 55 parse_ip parse_address
55 format_ipv4 format_ipv6 56 format_ipv4 format_ipv6
56 format_ip format_address 57 format_ip format_address
178 } 179 }
179 } 180 }
180} 181}
181 182
182*aton = \&parse_address; 183*aton = \&parse_address;
184
185=item ($name, $aliases, $proto) = getprotobyname $name
186
187Works like the builtin function of the same name, except it tries hard to
188work even on broken platforms (well, that's windows), where getprotobyname
189is traditionally very unreliable.
190
191=cut
192
193# microsoft can't even get getprotobyname working (the etc/protocols file
194# gets lost fairly often on windows), so we have to hardcode some common
195# protocol numbers ourselves.
196our %PROTO_BYNAME;
197
198$PROTO_BYNAME{tcp} = Socket::IPPROTO_TCP () if defined &Socket::IPPROTO_TCP;
199$PROTO_BYNAME{udp} = Socket::IPPROTO_UDP () if defined &Socket::IPPROTO_UDP;
200$PROTO_BYNAME{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP;
201
202sub getprotobyname($) {
203 my $name = lc shift;
204
205 defined (my $proton = $PROTO_BYNAME{$name} || (getprotobyname $name)[2])
206 or return;
207
208 ($name, uc $name, $proton)
209}
183 210
184=item ($host, $service) = parse_hostport $string[, $default_service] 211=item ($host, $service) = parse_hostport $string[, $default_service]
185 212
186Splitting a string of the form C<hostname:port> is a common 213Splitting a string of the form C<hostname:port> is a common
187problem. Unfortunately, just splitting on the colon makes it hard to 214problem. Unfortunately, just splitting on the colon makes it hard to
499 526
500 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... }; 527 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... };
501 528
502=cut 529=cut
503 530
504# microsoft can't even get getprotobyname working (the etc/protocols file
505# gets lost fairly often on windows), so we have to hardcode some common
506# protocol numbers ourselves.
507our %PROTO_BYNAME;
508
509$PROTO_BYNAME{tcp} = &Socket::IPPROTO_TCP if defined &Socket::IPPROTO_TCP;
510$PROTO_BYNAME{udp} = &Socket::IPPROTO_UDP if defined &Socket::IPPROTO_UDP;
511$PROTO_BYNAME{icmp} = &Socket::IPPROTO_ICMP if defined &Socket::IPPROTO_ICMP;
512
513sub resolve_sockaddr($$$$$$) { 531sub resolve_sockaddr($$$$$$) {
514 my ($node, $service, $proto, $family, $type, $cb) = @_; 532 my ($node, $service, $proto, $family, $type, $cb) = @_;
515 533
516 if ($node eq "unix/") { 534 if ($node eq "unix/") {
517 return $cb->() if $family || $service !~ /^\//; # no can do 535 return $cb->() if $family || $service !~ /^\//; # no can do
533 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4}; 551 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4};
534 552
535 $proto ||= "tcp"; 553 $proto ||= "tcp";
536 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM; 554 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM;
537 555
538 my $proton = $PROTO_BYNAME{lc $proto} || (getprotobyname $proto)[2] 556 my $proton = getprotobyname $proto
539 or Carp::croak "$proto: protocol unknown"; 557 or Carp::croak "$proto: protocol unknown";
540 558
541 my $port; 559 my $port;
542 560
543 if ($service =~ /^(\S+)=(\d+)$/) { 561 if ($service =~ /^(\S+)=(\d+)$/) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines