IO-Socket-Multicast-1.12/0000755000175000017500000000000011366010237014762 5ustar andrewbandrewbIO-Socket-Multicast-1.12/META.yml0000644000175000017500000000102111366010237016225 0ustar andrewbandrewb--- #YAML:1.0 name: IO-Socket-Multicast version: 1.12 abstract: Send and receive multicast messages author: [] license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: IO::Interface: 0.94 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 IO-Socket-Multicast-1.12/README0000644000175000017500000002660111330544262015650 0ustar andrewbandrewbNAME IO::Socket::Multicast - Send and receive multicast messages SYNOPSIS use IO::Socket::Multicast; # create a new UDP socket ready to read datagrams on port 1100 my $s = IO::Socket::Multicast->new(LocalPort=>1100); # Add a multicast group $s->mcast_add('225.0.1.1'); # Add a multicast group to eth0 device $s->mcast_add('225.0.0.2','eth0'); # now receive some multicast data $s->recv($data,1024); # Drop a multicast group $s->mcast_drop('225.0.0.1'); # Set outgoing interface to eth0 $s->mcast_if('eth0'); # Set time to live on outgoing multicast packets $s->mcast_ttl(10); # Turn off loopbacking $s->mcast_loopback(0); # Multicast a message to group 225.0.0.1 $s->mcast_send('hello world!','225.0.0.1:1200'); $s->mcast_set('225.0.0.2:1200'); $s->mcast_send('hello again!'); DESCRIPTION The IO::Socket::Multicast module subclasses IO::Socket::INET to enable you to manipulate multicast groups. With this module (and an operating system that supports multicasting), you will be able to receive incoming multicast transmissions and generate your own outgoing multicast packets. This module requires IO::Interface version 0.94 or higher. INTRODUCTION Multicasting is designed for streaming multimedia applications and for conferencing systems in which one transmitting machines needs to distribute data to a large number of clients. IP addresses in the range 224.0.0.0 and 239.255.255.255 are reserved for multicasting. These addresses do not correspond to individual machines, but to multicast groups. Messages sent to these addresses will be delivered to a potentially large number of machines that have registered their interest in receiving transmissions on these groups. They work like TV channels. A program tunes in to a multicast group to receive transmissions to it, and tunes out when it no longer wishes to receive the transmissions. To receive transmissions from a multicast group, you will use IO::Socket::INET->new() to create a UDP socket and bind it to a local network port. You will then subscribe one or more multicast groups using the mcast_add() method. Subsequent calls to the standard recv() method will now receive messages incoming messages transmitted to the subscribed groups using the selected port number. To send transmissions to a multicast group, you can use the standard send() method to send messages to the multicast group and port of your choice. The mcast_set() and mcast_send() methods are provided as convenience functions. Mcast_set() will set a default multicast destination for messages which you then send with mcast_send(). To set the number of hops (routers) that outgoing multicast messages will cross, call mcast_ttl(). To activate or deactivate the looping back of multicast messages (in which a copy of the transmitted messages is received by the local machine), call mcast_loopback(). CONSTRUCTORS $socket = IO::Socket::Multicast->new([LocalPort=>$port,...]) The new() method is the constructor for the IO::Socket::Multicast class. It takes the same arguments as IO::Socket::INET, except that the Proto argument, rather than defaulting to "tcp", will default to "udp", which is more appropriate for multicasting. To create a UDP socket suitable for sending outgoing multicast messages, call new() without arguments (or with "Proto=>'udp'"). To create a UDP socket that can also receive incoming multicast transmissions on a specific port, call new() with the LocalPort argument. If you plan to run the client and server on the same machine, you may wish to set the IO::Socket ReuseAddr argument to a true value. This allows multiple multicast sockets to bind to the same address. METHODS $success = $socket->mcast_add($multicast_address [,$interface]) The mcast_add() method will add the provided multicast address to the list of subscribed multicast groups. The address may be provided either as a dotted-quad decimal, or as a packed IP address (such as produced by the inet_aton() function). On success, the method will return a true value. The optional $interface argument can be used to specify on which network interface to listen for incoming multicast messages. If the IO::Interface module is installed, you may use the device name for the interface (e.g. "tu0"). Otherwise, you must use the IP address of the desired network interface. Either dotted quad form or packed IP address is acceptable. If no interface is specified, then the multicast group is joined on INADDR_ANY, meaning that multicast transmissions received on any of the host's network interfaces will be forwarded to the socket. Note that mcast_add() operates on the underlying interface(s) and not on the socket. If you have multiple sockets listening on a port, and you mcast_add() a group to one of those sockets, subsequently all the sockets will receive mcast messages on this group. To filter messages that can be received by a socket so that only those sent to a particular multicast address are received, pass the LocalAddr option to the socket at the time you create it: my $socket = IO::Socket::Multicast->new(LocalPort=>2000, LocalAddr=>226.1.1.2', ReuseAddr=>1); $socket->mcast_add('226.1.1.2'); By combining this technique with IO::Select, you can write applications that listen to multiple multicast groups and distinguish which group a message was addressed to by identifying which socket it was received on. $success = $socket->mcast_drop($multicast_address) This reverses the action of mcast_add(), removing the indicated multicast address from the list of subscribed groups. $loopback = $socket->mcast_loopback $previous = $socket->mcast_loopback($new) The mcast_loopback() method controls whether the socket will receive its own multicast transmissions (default yes). Called without arguments, the method returns the current state of the loopback flag. Called with a boolean argument, the method will set the loopback flag, and return its previous value. $ttl = $socket->mcast_ttl $previous = $socket->mcast_ttl($new) The mcast_ttl() method examines or sets the time to live (TTL) for outgoing multicast messages. The TTL controls the numbers of routers the packet can cross before being expired. The default TTL is 1, meaning that the message is confined to the local area network. Values between 0 and 255 are valid. Called without arguments, this method returns the socket's current TTL. Called with a value, this method sets the TTL and returns its previous value. $interface = $socket->mcast_if $previous = $socket->mcast_if($new) By default, the OS will pick the network interface to use for outgoing multicasts automatically. You can control this process by using the mcast_if() method to set the outgoing network interface explicitly. Called without arguments, returns the current interface. Called with the name of an interface, sets the outgoing interface and returns its previous value. You can use the device name for the interface (e.g. "tu0") if the IO::Interface module is present. Otherwise, you must use the interface's dotted IP address. NOTE: To set the interface used for incoming multicasts, use the mcast_add() method. $dest = $socket->mcast_dest $previous = $socket->mcast_dest($new) The mcast_dest() method is a convenience function that allows you to set the default destination group for outgoing multicasts. Called without arguments, returns the current destination as a packed binary sockaddr_in data structure. Called with a new destination address, the method sets the default destination and returns the previous one, if any. Destination addresses may be provided as packed sockaddr_in structures, or in the form "XX.XX.XX.XX:YY" where the first part is the IP address, and the second the port number. $bytes = $socket->mcast_send($data [,$dest]) Mcast_send() is a convenience function that simplifies the sending of multicast messages. $data is the message contents, and $dest is an optional destination group. You can use either the dotted IP form of the destination address and its port number, or a packed sockaddr_in structure. If the destination is not supplied, it will default to the most recent value set in mcast_dest() or a previous call to mcast_send(). The method returns the number of bytes successfully queued for delivery. As a side-effect, the method will call mcast_dest() to remember the destination address. Example: $socket->mcast_send('Hi there group members!','225.0.1.1:1900') || die; $socket->mcast_send("How's the weather?") || die; Note that you may still call IO::Socket::INET->new() with a PeerAddr, and IO::Socket::INET will perform a connect(), creating a default destination for calls to send(). EXAMPLE The following is an example of a multicast server. Every 10 seconds it transmits the current time and the list of logged-in users to the local network using multicast group 226.1.1.2, port 2000 (these are chosen arbitrarily). #!/usr/bin/perl # server use strict; use IO::Socket::Multicast; use constant DESTINATION => '226.1.1.2:2000'; my $sock = IO::Socket::Multicast->new(Proto=>'udp',PeerAddr=>DESTINATION); while (1) { my $message = localtime; $message .= "\n" . `who`; $sock->send($message) || die "Couldn't send: $!"; } continue { sleep 10; } This is the corresponding client. It listens for transmissions on group 226.1.1.2, port 2000, and echoes the messages to standard output. #!/usr/bin/perl # client use strict; use IO::Socket::Multicast; use constant GROUP => '226.1.1.2'; use constant PORT => '2000'; my $sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>PORT); $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n"; while (1) { my $data; next unless $sock->recv($data,1024); print $data; } EXPORT None by default. However, if you wish to call mcast_add(), mcast_drop(), mcast_if(), mcast_loopback(), mcast_ttl, mcast_dest() and mcast_send() as functions you may import them explicitly on the use line or by importing the tag ":functions". BUGS The mcast_if(), mcast_ttl() and mcast_loopback() methods will cause a crash on versions of Linux earlier than 2.2.0 because of a kernel bug in the implementation of the multicast socket options. AUTHOR Lincoln Stein, lstein@cshl.org. This module is distributed under the same terms as Perl itself. SEE ALSO perl(1), IO::Socket(3), IO::Socket::INET(3). IO-Socket-Multicast-1.12/lib/0000755000175000017500000000000011366010237015530 5ustar andrewbandrewbIO-Socket-Multicast-1.12/lib/IO/0000755000175000017500000000000011366010237016037 5ustar andrewbandrewbIO-Socket-Multicast-1.12/lib/IO/Socket/0000755000175000017500000000000011366010237017267 5ustar andrewbandrewbIO-Socket-Multicast-1.12/lib/IO/Socket/Multicast.pm0000644000175000017500000003357111366007754021615 0ustar andrewbandrewbpackage IO::Socket::Multicast; use 5.005; use strict; use Carp 'croak'; use Exporter (); use DynaLoader (); use IO::Socket; BEGIN { eval "use IO::Interface 0.94 'IFF_MULTICAST';"; } use vars qw(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION); BEGIN { my @functions = qw( mcast_add mcast_drop mcast_if mcast_loopback mcast_ttl mcast_dest mcast_send ); $VERSION = '1.12'; @ISA = qw( Exporter DynaLoader IO::Socket::INET ); @EXPORT = ( ); %EXPORT_TAGS = ( 'all' => \@functions, 'functions' => \@functions, ); @EXPORT_OK = @{ $EXPORT_TAGS{'all'} }; } my $IP = '\d+\.\d+\.\d+\.\d+'; sub import { Socket->export_to_level(1,@_); IO::Socket::Multicast->export_to_level(1,@_); } sub new { my $class = shift; unshift @_,(Proto => 'udp') unless @_; $class->SUPER::new(@_); } sub configure { my($self,$arg) = @_; $arg->{Proto} ||= 'udp'; $self->SUPER::configure($arg); } sub mcast_add { my $sock = shift; my $group = shift || croak 'usage: $sock->mcast_add($mcast_addr [,$interface])'; $group = inet_ntoa($group) unless $group =~ /^$IP$/o; my $interface = get_if_addr($sock,shift); return $sock->_mcast_add($group,$interface); } sub mcast_drop { my $sock = shift; my $group = shift || croak 'usage: $sock->mcast_add($mcast_addr [,$interface])'; $group = inet_ntoa($group) unless $group =~ /^$IP$/o; my $interface = get_if_addr($sock,shift); return $sock->_mcast_drop($group,$interface); } sub mcast_if { my $sock = shift; my $previous = $sock->_mcast_if; $previous = $sock->addr_to_interface($previous) if $sock->can('addr_to_interface'); return $previous unless @_; my $interface = get_if_addr($sock,shift); return $sock->_mcast_if($interface) ? $previous : undef; } sub get_if_addr { my $sock = shift; return '0.0.0.0' unless defined (my $interface = shift); return $interface if $interface =~ /^$IP$/; return $interface if length $interface == 16; croak "IO::Interface module not available; use IP addr for interface" unless $sock->can('if_addr'); croak "unknown or unconfigured interace $interface" unless my $addr = $sock->if_addr($interface); croak "interface is not multicast capable" unless $interface eq 'any' or ($sock->if_flags($interface) & IFF_MULTICAST()); return $addr; } sub mcast_dest { my $sock = shift; my $prev = ${*$sock}{'io_socket_mcast_dest'}; if (my $dest = shift) { $dest = sockaddr_in($2,inet_aton($1)) if $dest =~ /^($IP):(\d+)$/; croak "invalid destination address" unless length($dest) == 16; ${*$sock}{'io_socket_mcast_dest'} = $dest; } return $prev; } sub mcast_send { my $sock = shift; my $data = shift || croak 'usage: $sock->mcast_send($data [,$address])'; $sock->mcast_dest(shift) if @_; my $dest = $sock->mcast_dest || croak "no destination specified with mcast_send() or mcast_dest()"; return send($sock,$data,0,$dest); } bootstrap IO::Socket::Multicast $VERSION; 1; __END__ =pod =head1 NAME IO::Socket::Multicast - Send and receive multicast messages =head1 SYNOPSIS use IO::Socket::Multicast; # create a new UDP socket ready to read datagrams on port 1100 my $s = IO::Socket::Multicast->new(LocalPort=>1100); # Add a multicast group $s->mcast_add('225.0.1.1'); # Add a multicast group to eth0 device $s->mcast_add('225.0.0.2','eth0'); # now receive some multicast data $s->recv($data,1024); # Drop a multicast group $s->mcast_drop('225.0.0.1'); # Set outgoing interface to eth0 $s->mcast_if('eth0'); # Set time to live on outgoing multicast packets $s->mcast_ttl(10); # Turn off loopbacking $s->mcast_loopback(0); # Multicast a message to group 225.0.0.1 $s->mcast_send('hello world!','225.0.0.1:1200'); $s->mcast_set('225.0.0.2:1200'); $s->mcast_send('hello again!'); =head1 DESCRIPTION The IO::Socket::Multicast module subclasses IO::Socket::INET to enable you to manipulate multicast groups. With this module (and an operating system that supports multicasting), you will be able to receive incoming multicast transmissions and generate your own outgoing multicast packets. This module requires IO::Interface version 0.94 or higher. =head2 INTRODUCTION Multicasting is designed for streaming multimedia applications and for conferencing systems in which one transmitting machines needs to distribute data to a large number of clients. IP addresses in the range 224.0.0.0 and 239.255.255.255 are reserved for multicasting. These addresses do not correspond to individual machines, but to multicast groups. Messages sent to these addresses will be delivered to a potentially large number of machines that have registered their interest in receiving transmissions on these groups. They work like TV channels. A program tunes in to a multicast group to receive transmissions to it, and tunes out when it no longer wishes to receive the transmissions. To receive transmissions B a multicast group, you will use IO::Socket::Multicast->new() to create a UDP socket and bind it to a local network port. You will then subscribe one or more multicast groups using the mcast_add() method. Subsequent calls to the standard recv() method will now receive messages incoming messages transmitted to the subscribed groups using the selected port number. To send transmissions B a multicast group, you can use the standard send() method to send messages to the multicast group and port of your choice. The mcast_set() and mcast_send() methods are provided as convenience functions. Mcast_set() will set a default multicast destination for messages which you then send with mcast_send(). To set the number of hops (routers) that outgoing multicast messages will cross, call mcast_ttl(). To activate or deactivate the looping back of multicast messages (in which a copy of the transmitted messages is received by the local machine), call mcast_loopback(). =head2 CONSTRUCTORS =over 4 =item $socket = IO::Socket::Multicast->new([LocalPort=>$port,...]) The new() method is the constructor for the IO::Socket::Multicast class. It takes the same arguments as IO::Socket::INET, except that the B argument, rather than defaulting to "tcp", will default to "udp", which is more appropriate for multicasting. To create a UDP socket suitable for sending outgoing multicast messages, call new() without arguments (or with C'udp'>). To create a UDP socket that can also receive incoming multicast transmissions on a specific port, call new() with the B argument. If you plan to run the client and server on the same machine, you may wish to set the IO::Socket B argument to a true value. This allows multiple multicast sockets to bind to the same address. =back =head2 METHODS =over 4 =item $success = $socket->mcast_add($multicast_address [,$interface]) The mcast_add() method will add the provided multicast address to the list of subscribed multicast groups. The address may be provided either as a dotted-quad decimal, or as a packed IP address (such as produced by the inet_aton() function). On success, the method will return a true value. The optional $interface argument can be used to specify on which network interface to listen for incoming multicast messages. If the IO::Interface module is installed, you may use the device name for the interface (e.g. "tu0"). Otherwise, you must use the IP address of the desired network interface. Either dotted quad form or packed IP address is acceptable. If no interface is specified, then the multicast group is joined on INADDR_ANY, meaning that multicast transmissions received on B of the host's network interfaces will be forwarded to the socket. Note that mcast_add() operates on the underlying interface(s) and not on the socket. If you have multiple sockets listening on a port, and you mcast_add() a group to one of those sockets, subsequently B the sockets will receive mcast messages on this group. To filter messages that can be received by a socket so that only those sent to a particular multicast address are received, pass the B option to the socket at the time you create it: my $socket = IO::Socket::Multicast->new(LocalPort=>2000, LocalAddr=>226.1.1.2', ReuseAddr=>1); $socket->mcast_add('226.1.1.2'); By combining this technique with IO::Select, you can write applications that listen to multiple multicast groups and distinguish which group a message was addressed to by identifying which socket it was received on. =item $success = $socket->mcast_drop($multicast_address) This reverses the action of mcast_add(), removing the indicated multicast address from the list of subscribed groups. =item $loopback = $socket->mcast_loopback =item $previous = $socket->mcast_loopback($new) The mcast_loopback() method controls whether the socket will receive its own multicast transmissions (default yes). Called without arguments, the method returns the current state of the loopback flag. Called with a boolean argument, the method will set the loopback flag, and return its previous value. =item $ttl = $socket->mcast_ttl =item $previous = $socket->mcast_ttl($new) The mcast_ttl() method examines or sets the time to live (TTL) for outgoing multicast messages. The TTL controls the numbers of routers the packet can cross before being expired. The default TTL is 1, meaning that the message is confined to the local area network. Values between 0 and 255 are valid. Called without arguments, this method returns the socket's current TTL. Called with a value, this method sets the TTL and returns its previous value. =item $interface = $socket->mcast_if =item $previous = $socket->mcast_if($new) By default, the OS will pick the network interface to use for outgoing multicasts automatically. You can control this process by using the mcast_if() method to set the outgoing network interface explicitly. Called without arguments, returns the current interface. Called with the name of an interface, sets the outgoing interface and returns its previous value. You can use the device name for the interface (e.g. "tu0") if the IO::Interface module is present. Otherwise, you must use the interface's dotted IP address. B: To set the interface used for B multicasts, use the mcast_add() method. =item $dest = $socket->mcast_dest =item $previous = $socket->mcast_dest($new) The mcast_dest() method is a convenience function that allows you to set the default destination group for outgoing multicasts. Called without arguments, returns the current destination as a packed binary sockaddr_in data structure. Called with a new destination address, the method sets the default destination and returns the previous one, if any. Destination addresses may be provided as packed sockaddr_in structures, or in the form "XX.XX.XX.XX:YY" where the first part is the IP address, and the second the port number. =item $bytes = $socket->mcast_send($data [,$dest]) Mcast_send() is a convenience function that simplifies the sending of multicast messages. C<$data> is the message contents, and C<$dest> is an optional destination group. You can use either the dotted IP form of the destination address and its port number, or a packed sockaddr_in structure. If the destination is not supplied, it will default to the most recent value set in mcast_dest() or a previous call to mcast_send(). The method returns the number of bytes successfully queued for delivery. As a side-effect, the method will call mcast_dest() to remember the destination address. Example: $socket->mcast_send('Hi there group members!','225.0.1.1:1900') || die; $socket->mcast_send("How's the weather?") || die; Note that you may still call IO::Socket::Multicast->new() with a B, and IO::Socket::INET will perform a connect(), creating a default destination for calls to send(). =back =head1 EXAMPLE The following is an example of a multicast server. Every 10 seconds it transmits the current time and the list of logged-in users to the local network using multicast group 226.1.1.2, port 2000 (these are chosen arbitrarily). #!/usr/bin/perl # server use strict; use IO::Socket::Multicast; use constant DESTINATION => '226.1.1.2:2000'; my $sock = IO::Socket::Multicast->new(Proto=>'udp',PeerAddr=>DESTINATION); while (1) { my $message = localtime; $message .= "\n" . `who`; $sock->send($message) || die "Couldn't send: $!"; } continue { sleep 10; } This is the corresponding client. It listens for transmissions on group 226.1.1.2, port 2000, and echoes the messages to standard output. #!/usr/bin/perl # client use strict; use IO::Socket::Multicast; use constant GROUP => '226.1.1.2'; use constant PORT => '2000'; my $sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>PORT); $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n"; while (1) { my $data; next unless $sock->recv($data,1024); print $data; } =head2 EXPORT None by default. However, if you wish to call mcast_add(), mcast_drop(), mcast_if(), mcast_loopback(), mcast_ttl, mcast_dest() and mcast_send() as functions you may import them explicitly on the B line or by importing the tag ":functions". =head2 BUGS The mcast_if(), mcast_ttl() and mcast_loopback() methods will cause a crash on versions of Linux earlier than 2.2.0 because of a kernel bug in the implementation of the multicast socket options. =head1 AUTHOR Lincoln Stein, lstein@cshl.org. This module is distributed under the same terms as Perl itself. =head1 SEE ALSO perl(1), IO::Socket(3), IO::Socket::INET(3). =cut IO-Socket-Multicast-1.12/Makefile.PL0000644000175000017500000000116711330544262016742 0ustar andrewbandrewbuse strict; BEGIN { require 5.005; } use ExtUtils::MakeMaker; use Config; my @libs = (); unless ( $Config{d_inetaton} or $^O eq 'MSWin32' ) { push @libs, '-lresolv'; } WriteMakefile( NAME => 'IO::Socket::Multicast', ABSTRACT => 'Send and receive multicast messages', VERSION_FROM => 'lib/IO/Socket/Multicast.pm', LIBS => \@libs, # e.g., '-lm' DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '', # e.g., '-I/usr/include/other' PREREQ_PM => { $^O eq 'MSWin32' ? () : ( 'IO::Interface' => 0.94 ), }, ($ExtUtils::MakeMaker::VERSION ge '6.30_00' ? ( LICENSE => 'perl', ) : ()), ); IO-Socket-Multicast-1.12/Multicast.xs0000644000175000017500000001563411330544262017315 0ustar andrewbandrewb#ifdef WIN32 #include #include #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "config.h" #include #include #ifndef WIN32 #include #endif #include #ifdef PerlIO typedef PerlIO * InputStream; #else #define PERLIO_IS_STDIO 1 typedef FILE * InputStream; #define PerlIO_fileno(f) fileno(f) #endif static int not_here(char *s) { croak("%s not implemented on this architecture", s); return -1; } /* Recent versions of Win32 platforms are confused about these constants due to problems in the order of socket header file importation #ifdef WIN32 #if (PERL_REVISION >=5) && (PERL_VERSION >= 8) && (PERL_SUBVERSION >= 6) #undef IP_OPTIONS #undef IP_HDRINCL #undef IP_TOS #undef IP_TTL #undef IP_MULTICAST_IF #undef IP_MULTICAST_TTL #undef IP_MULTICAST_LOOP #undef IP_ADD_MEMBERSHIP #undef IP_DROP_MEMBERSHIP #undef IP_DONTFRAGMENT #define IP_OPTIONS 1 #define IP_HDRINCL 2 #define IP_TOS 3 #define IP_TTL 4 #define IP_MULTICAST_IF 9 #define IP_MULTICAST_TTL 10 #define IP_MULTICAST_LOOP 11 #define IP_ADD_MEMBERSHIP 12 #define IP_DROP_MEMBERSHIP 13 #define IP_DONTFRAGMENT 14 #endif #endif */ #ifndef HAS_INET_ATON static int my_inet_aton(register const char *cp, struct in_addr *addr) { dTHX; register U32 val; register int base; register char c; int nparts; const char *s; unsigned int parts[4]; register unsigned int *pp = parts; if (!cp || !*cp) return 0; for (;;) { /* * Collect number up to ``.''. * Values are specified as for C: * 0x=hex, 0=octal, other=decimal. */ val = 0; base = 10; if (*cp == '0') { if (*++cp == 'x' || *cp == 'X') base = 16, cp++; else base = 8; } while ((c = *cp) != '\0') { if (isDIGIT(c)) { val = (val * base) + (c - '0'); cp++; continue; } if (base == 16 && (s=strchr(PL_hexdigit,c))) { val = (val << 4) + ((s - PL_hexdigit) & 15); cp++; continue; } break; } if (*cp == '.') { /* * Internet format: * a.b.c.d * a.b.c (with c treated as 16-bits) * a.b (with b treated as 24 bits) */ if (pp >= parts + 3 || val > 0xff) return 0; *pp++ = val, cp++; } else break; } /* * Check for trailing characters. */ if (*cp && !isSPACE(*cp)) return 0; /* * Concoct the address according to * the number of parts specified. */ nparts = pp - parts + 1; /* force to an int for switch() */ switch (nparts) { case 1: /* a -- 32 bits */ break; case 2: /* a.b -- 8.24 bits */ if (val > 0xffffff) return 0; val |= parts[0] << 24; break; case 3: /* a.b.c -- 8.8.16 bits */ if (val > 0xffff) return 0; val |= (parts[0] << 24) | (parts[1] << 16); break; case 4: /* a.b.c.d -- 8.8.8.8 bits */ if (val > 0xff) return 0; val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); break; } addr->s_addr = htonl(val); return 1; } #undef inet_aton #define inet_aton my_inet_aton #endif MODULE = IO::Socket::Multicast PACKAGE = IO::Socket::Multicast void _mcast_add(sock,mcast_group,interface_addr="") InputStream sock char* mcast_group char* interface_addr PROTOTYPE: $$;$ PREINIT: int fd; struct ip_mreq mreq; PPCODE: { fd = PerlIO_fileno(sock); if (!inet_aton(mcast_group,&mreq.imr_multiaddr)) croak("Invalid address used for mcast group"); if ((strlen(interface_addr) > 0)) { if (!inet_aton(interface_addr,&mreq.imr_interface)) croak("Invalid address used for local interface"); } else { mreq.imr_interface.s_addr = INADDR_ANY; } if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(void*) &mreq,sizeof(mreq)) < 0) XSRETURN_EMPTY; else XSRETURN_YES; } void _mcast_drop(sock,mcast_group,interface_addr="") InputStream sock char* mcast_group char* interface_addr PROTOTYPE: $$;$ PREINIT: int fd; struct ip_mreq mreq; PPCODE: { fd = PerlIO_fileno(sock); if (!inet_aton(mcast_group,&mreq.imr_multiaddr)) croak("Invalid address used for mcast group"); if ((strlen(interface_addr) > 0)) { if (!inet_aton(interface_addr,&mreq.imr_interface)) croak("Invalid address used for local interface"); } else { mreq.imr_interface.s_addr = htonl(INADDR_ANY); } if (setsockopt(fd,IPPROTO_IP,IP_DROP_MEMBERSHIP,(void*)&mreq,sizeof(mreq)) < 0) XSRETURN_EMPTY; else XSRETURN_YES; } int mcast_loopback(sock,...) InputStream sock PROTOTYPE: $;$ PREINIT: int fd; int len; char previous,loopback; CODE: { fd = PerlIO_fileno(sock); /* get previous value of flag */ len = sizeof(previous); if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,(void*)&previous,&len) < 0) XSRETURN_UNDEF; if (items > 1) { /* set value */ loopback = SvIV(ST(1)); if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,(void*)&loopback,sizeof(loopback)) < 0) XSRETURN_UNDEF; } RETVAL = previous; } OUTPUT: RETVAL int mcast_ttl(sock,...) InputStream sock PROTOTYPE: $;$ PREINIT: int fd; int len; char previous,ttl; CODE: { fd = PerlIO_fileno(sock); /* get previous value of flag */ len = sizeof(previous); if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_TTL,(void*)&previous,&len) < 0) XSRETURN_UNDEF; if (items > 1) { /* set value */ ttl = SvIV(ST(1)); if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_TTL,(void*)&ttl,sizeof(ttl)) < 0) XSRETURN_UNDEF; } RETVAL = previous; } OUTPUT: RETVAL void _mcast_if(sock,...) InputStream sock PROTOTYPE: $;$ PREINIT: int fd,len; STRLEN slen; char* addr; struct in_addr ifaddr; struct ip_mreq mreq; PPCODE: { fd = PerlIO_fileno(sock); if (items > 1) { /* setting interface */ addr = SvPV(ST(1),slen); if (inet_aton(addr,&ifaddr) == 0 ) XSRETURN_EMPTY; if (setsockopt(fd,IPPROTO_IP,IP_MULTICAST_IF,(void*)&ifaddr,sizeof(ifaddr)) == 0) XSRETURN_YES; else XSRETURN_NO; } else { /* getting interface address */ /* freakin' bug in Linux -- IP_MULTICAST_IF returns a struct mreqn rather than an in_addr (contrary to Stevens and the setsockopt()! We work around this by looking at size of returned thing and doing a ugly cast */ len = sizeof(mreq); if (getsockopt(fd,IPPROTO_IP,IP_MULTICAST_IF,(void*) &mreq,&len) != 0) XSRETURN_EMPTY; if (len == sizeof(mreq)) { XPUSHs(sv_2mortal(newSVpv(inet_ntoa(mreq.imr_interface),0))); } else if (len == sizeof (struct in_addr)) { XPUSHs(sv_2mortal(newSVpv(inet_ntoa(*(struct in_addr*)&mreq),0))); } else { croak("getsockopt() returned a data type I don't understand"); } } } IO-Socket-Multicast-1.12/MANIFEST0000644000175000017500000000037611366010237016121 0ustar andrewbandrewbChanges examples/client.pl examples/server.pl lib/IO/Socket/Multicast.pm Makefile.PL MANIFEST This list of files Multicast.xs README t/01_use.t t/03_multicast.t t/04_send.t META.yml Module meta-data (added by MakeMaker) IO-Socket-Multicast-1.12/Changes0000644000175000017500000000340411366010112016246 0ustar andrewbandrewbRevision history for Perl extension IO-Socket-Multicast 1.12 - Wed Apr 28 20:38:53 EST 2010 Test adjustments thankyou Christian W. (BRAMBLE) Documentation updates and corrections (BRAMBLE) 1.11 - Wed Nov 4 13:48:18 EST 2009 More test adjustments /re Win32 (BRAMBLE) 1.09 Refactored tests to prevent false negatives on Win32 (ADAMK) 1.08 Mon Nov 2 11:23:33 EST 2009 Test fixes and spelling corrections (BRAMBLE) 1.07 Tue Jul 14 12:54:46 EDT 2009 Patches to run correctly on Windows platforms under Perl 5.10 courtesy Andrew Bramble Regression test fixes. 1.06 Beta test 1.05 Sat Aug 12 17:28:41 EDT 2006 Run correctly on Windows platforms under Perl 5.8.8. 1.03-1.04 Fri Dec 2 10:44:35 EST 2005 Various fixes to compile cleanly on Windows platforms. 1.02 Thu Nov 17 12:04:21 EST 2005 Skip regression test #4 (mcast_drop) because it always fails on windows platforms. This should not affect functionality, as the test asks mcast_drop() to return a false value when dropping an address twice. 1.01 July 5, 2005 All symbols now in IO::Socket::Multicast package (some were in IO::Socket in previous versions) Incorporates patches to compile cleanly on Win32 systems, contributed by Thomas Kratz. 1.00 April 9, 2005 Reconstructed module after CVS server crash. Fixed documentation errors 0.25 August 25, 2001 Document ReuseAddr 0.11 August 1, 2000 Fixed core dumps on some systems. Added warnings about broken linuxes. IO::Interface 0.94 is now a prerequisite. 0.10 July 18, 2000 Documentation bug fixes 0.03 July 18, 2000 Turned into a subclass of IO::Socket::INET 0.02 July 17, 2000 First release 0.01 Fri Mar 31 11:28:47 2000 - original version; created by h2xs 1.20 with options -n Ioctl::Atmark -cf IO-Socket-Multicast-1.12/t/0000755000175000017500000000000011366010237015225 5ustar andrewbandrewbIO-Socket-Multicast-1.12/t/04_send.t0000644000175000017500000000135711330544262016655 0ustar andrewbandrewb#!/usr/bin/perl use strict; BEGIN { $| = 1; $^W = 1; } use Test::More; use IO::Socket::Multicast; plan skip_all => 'Developer testing only' unless (defined $ENV{TEST_UNSAFE} && $ENV{TEST_UNSAFE} == 1); plan tests => 4; my $MCAST_ADDR = '225.0.0.1'; my $MCAST_PORT = 9999; my $s = IO::Socket::Multicast->new( LocalPort => $MCAST_PORT, Blocking => 0, ); $s->mcast_loopback(1); my $payload = "IO::Socket::Multicast test packet - $$ $0"; ok( $s->mcast_add( $MCAST_ADDR ) , "Join $MCAST_ADDR" ); ok( $s->mcast_send( $payload ,"$MCAST_ADDR:$MCAST_PORT") , "Send to $MCAST_ADDR:$MCAST_PORT" ); my $data; ok( $s->recv( $data, 1024 ) , 'Received test data' ); ok( $data eq $payload , 'Received data matches sent data' ); IO-Socket-Multicast-1.12/t/01_use.t0000644000175000017500000000015711330544262016512 0ustar andrewbandrewb#!/usr/bin/perl BEGIN { $| = 1; $^W = 1; } use Test::More tests => 1; use_ok( 'IO::Socket::Multicast' ); IO-Socket-Multicast-1.12/t/03_multicast.t0000644000175000017500000000443511357753113017736 0ustar andrewbandrewb#!/usr/bin/perl use strict; BEGIN { $| = 1; $^W = 1; } use Test::More tests => 14; use IO::Socket::Multicast; my $MCAST_ADDR = '225.0.0.1'; my $MCAST_PORT = 9999; # I think winsock prefers the socket # to be bound to _something_ my $s = IO::Socket::Multicast->new( LocalPort => $MCAST_PORT, ); # Platform compatibility my $WIN32 = $^O eq 'MSWin32'; my $LINUX = $WIN32 ? 0 : (`uname -sr` =~ /^Linux (\d+\.\d+)/)[0]; my $OS_OK = ( $LINUX and $LINUX >= 2.2 ); my $IO_INTERFACE = eval "use IO::Interface ':flags'; 1;"; my $INTERFACE = $IO_INTERFACE && find_a_mcast_if($s); SKIP: { # run only if there is an interface skip("There is no interface, so we can't check", 3) unless $INTERFACE; isa_ok( $s, 'IO::Socket::Multicast' ); ok($s->mcast_add($MCAST_ADDR), 'Add socket to Multicast Group' ); ok($s->mcast_drop($MCAST_ADDR),'Drop Multicast Group' ); } # Some basics SKIP: { # Windows doesn't return true for stuff skip("Doesn't work on Win32??", 1) if $WIN32; ok( ! $s->mcast_drop($MCAST_ADDR), 'Drop unsubscribed group returns false' ); } # More subtle control SKIP: { skip("Needs Linux >= 2.2", 6) unless $OS_OK; ok($s->mcast_ttl == 1, 'Get socket TTL default is one'); ok($s->mcast_ttl(10) == 1, 'Set TTL returns previous value'); ok($s->mcast_ttl == 10, 'Get TTL post-set returns correct TTL'); ok($s->mcast_loopback == 1, 'Multicast loopback defaults to true'); ok($s->mcast_loopback(0) == 1, 'Loopback set returns previous value' ); ok($s->mcast_loopback == 0, 'Loopback get' ); } SKIP: { skip('IO::Interface not available', 4) unless $IO_INTERFACE; skip('No multicast interface available', 4) unless $INTERFACE; skip('Needs Linux >= 2.2', 4) unless $OS_OK; ok ($s->mcast_if eq 'any' , 'Default interface "any"'); ok ($s->mcast_if($INTERFACE) eq 'any', 'Multicast interface set returns previous value'); ok ($s->mcast_if eq $INTERFACE , 'Multicast interface set'); ok ($s->mcast_add($MCAST_ADDR,$INTERFACE), 'Multicast add GROUP,if'); } sub find_a_mcast_if { my $s = shift; my @ifs = $s->if_list; foreach ( reverse @ifs ) { next unless $s->if_flags($_) & IFF_MULTICAST(); next unless $s->if_flags($_) & IFF_RUNNING(); next unless $s->if_addr($_); # Having an address seems important return $_; } } IO-Socket-Multicast-1.12/examples/0000755000175000017500000000000011366010237016600 5ustar andrewbandrewbIO-Socket-Multicast-1.12/examples/client.pl0000755000175000017500000000056211330544262020422 0ustar andrewbandrewb#!/usr/bin/perl use strict; use lib '../blib/lib','../blib/arch'; use IO::Socket::Multicast; use constant GROUP => '226.1.1.2'; use constant PORT => '2000'; my $sock = IO::Socket::Multicast->new(LocalPort=>PORT,ReuseAddr=>1); $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n"; while (1) { my $data; next unless $sock->recv($data,1024); print $data; } IO-Socket-Multicast-1.12/examples/server.pl0000755000175000017500000000054711330544262020455 0ustar andrewbandrewb#!/usr/bin/perl use strict; use lib '../blib/lib','../blib/arch'; use IO::Socket::Multicast; use constant DESTINATION => '226.1.1.2:2000'; my $sock = IO::Socket::Multicast->new(ReuseAddr=>1); while (1) { my $message = localtime; $message .= "\n" . `who`; $sock->mcast_send($message,DESTINATION) || die "Couldn't send: $!"; } continue { sleep 5; }