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.55 by root, Thu Jul 17 15:44:19 2008 UTC vs.
Revision 1.63 by root, Wed Oct 1 07:40:39 2008 UTC

57 inet_aton 57 inet_aton
58 tcp_server 58 tcp_server
59 tcp_connect 59 tcp_connect
60); 60);
61 61
62our $VERSION = 4.22; 62our $VERSION = 4.3;
63 63
64=item $ipn = parse_ipv4 $dotted_quad 64=item $ipn = parse_ipv4 $dotted_quad
65 65
66Tries to parse the given dotted quad IPv4 address and return it in 66Tries to parse the given dotted quad IPv4 address and return it in
67octet form (or undef when it isn't in a parsable format). Supports all 67octet form (or undef when it isn't in a parsable format). Supports all
154 154
155If the C<$text> is C<unix/>, then this function returns a special token 155If the C<$text> is C<unix/>, then this function returns a special token
156recognised by the other functions in this module to mean "UNIX domain 156recognised by the other functions in this module to mean "UNIX domain
157socket". 157socket".
158 158
159=item $text = AnyEvent::Socket::aton $ipn
160
161Same as C<parse_address>, but not exported (think C<Socket::inet_aton> but
162I<without> name resolution).
163
159=cut 164=cut
160 165
161sub parse_address($) { 166sub parse_address($) {
162 &parse_ipv4 || &parse_ipv6 || &parse_unix 167 &parse_ipv4 || &parse_ipv6 || &parse_unix
163} 168}
164 169
165*parse_ip =\&parse_address; #d# 170*aton = \&parse_address;
166 171
167=item ($host, $service) = parse_hostport $string[, $default_service] 172=item ($host, $service) = parse_hostport $string[, $default_service]
168 173
169Splitting a string of the form C<hostname:port> is a common 174Splitting a string of the form C<hostname:port> is a common
170problem. Unfortunately, just splitting on the colon makes it hard to 175problem. Unfortunately, just splitting on the colon makes it hard to
205=cut 210=cut
206 211
207sub parse_hostport($;$) { 212sub parse_hostport($;$) {
208 my ($host, $port); 213 my ($host, $port);
209 214
210 for ("$_[0]") { # work on a copy, to reset pos 215 for ("$_[0]") { # work on a copy, just in case, and also reset pos
211 216
212 # parse host, special cases: "ipv6" or "ipv6 port" 217 # parse host, special cases: "ipv6" or "ipv6 port"
213 unless ( 218 unless (
214 ($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/xgc 219 ($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/xgc
215 and parse_ipv6 $host 220 and parse_ipv6 $host
265 270
266This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>, 271This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>,
267except it automatically detects the address type. 272except it automatically detects the address type.
268 273
269Returns C<undef> if it cannot detect the type. 274Returns C<undef> if it cannot detect the type.
275
276=item $text = AnyEvent::Socket::ntoa $ipn
277
278Same as format_address, but not exported (think C<inet_ntoa>).
270 279
271=cut 280=cut
272 281
273sub format_address; 282sub format_address;
274sub format_address($) { 283sub format_address($) {
308 } else { 317 } else {
309 return undef 318 return undef
310 } 319 }
311} 320}
312 321
313*format_ip = \&format_address; 322*ntoa = \&format_address;
314 323
315=item inet_aton $name_or_address, $cb->(@addresses) 324=item inet_aton $name_or_address, $cb->(@addresses)
316 325
317Works similarly to its Socket counterpart, except that it uses a 326Works similarly to its Socket counterpart, except that it uses a
318callback. Also, if a host has only an IPv6 address, this might be passed 327callback. Also, if a host has only an IPv6 address, this might be passed
452 461
453 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... }; 462 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... };
454 463
455=cut 464=cut
456 465
466# microsoft can't even get getprotobyname working (the etc/protocols file
467# gets lost fairly often on windows), so we have to hardcode some common
468# protocol numbers ourselves.
469our %PROTO_BYNAME;
470
471$PROTO_BYNAME{tcp} = &Socket::IPPROTO_TCP if defined &Socket::IPPROTO_TCP;
472$PROTO_BYNAME{udp} = &Socket::IPPROTO_UDP if defined &Socket::IPPROTO_UDP;
473$PROTO_BYNAME{icmp} = &Socket::IPPROTO_ICMP if defined &Socket::IPPROTO_ICMP;
474
457sub resolve_sockaddr($$$$$$) { 475sub resolve_sockaddr($$$$$$) {
458 my ($node, $service, $proto, $family, $type, $cb) = @_; 476 my ($node, $service, $proto, $family, $type, $cb) = @_;
459 477
460 if ($node eq "unix/") { 478 if ($node eq "unix/") {
461 return $cb->() if $family || !/^\//; # no can do 479 return $cb->() if $family || !/^\//; # no can do
477 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4}; 495 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4};
478 496
479 $proto ||= "tcp"; 497 $proto ||= "tcp";
480 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM; 498 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM;
481 499
482 my $proton = (getprotobyname $proto)[2] 500 my $proton = $PROTO_BYNAME{lc $proto} || (getprotobyname $proto)[2]
483 or Carp::croak "$proto: protocol unknown"; 501 or Carp::croak "$proto: protocol unknown";
484 502
485 my $port; 503 my $port;
486 504
487 if ($service =~ /^(\S+)=(\d+)$/) { 505 if ($service =~ /^(\S+)=(\d+)$/) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines