phemal has asked for the wisdom of the Perl Monks concerning the following question:

sub sendraw { my ($pstr)=@_; $PROTO=getprotobyname('tcp')||0; @DXX=(); $pstr=~s/\n/\r\n/g; $pstr=~s/\r\r/\r/g; debugprint("Sending raw: $pstr"); if($D{'XXinet_aton'} eq ''){ if(!($D{'XXinet_aton'}=inet_pton(FAMILY,$D{'XXTarget'}))){ wprint("! DNS lookup failure."); $D{'XXDead'}=1; return;}} if($^O=~/win/i){ sendraw_win($pstr);} else { sendraw_unix($pstr);} } sub sendraw_unix { my ($pstr)=@_; $D{'XXHaveHeaders'}=0; eval { alarm($D{'XXTimeoutVal'}); $D{'XXDead'}=1; if(!(socket(S,PF_INET,SOCK_STREAM,$PROTO))){ wprint("= Socket problems"); return;} if($D{'XXSessSplice'}>0){ # session splicing setsockopt(S,SOL_SOCKET,SO_SNDLOWAT,1); @chars=split(//,$pstr);} if(connect(S,sockaddr_in($D{'XXPort'},$D{'XXinet_aton'}))){ select(S); $|=1; if($D{'XXSessSplice'}>0){foreach $char (@chars){print $char; select(undef,undef,undef,$D{'XXIDSMode9Wait'});} }else{print $pstr;} while(<S>){ $line=$_; push(@DXX,$line); if ($line=~/^[\r\n]+$/){ # we can continue $D{'XXHaveHeaders'}=1; $D{'XXDead'}=0; +} last if ($line=~/^[\r\n]+$/ && $D{'XXNoContent +'} > 0);} select(STDOUT); close(S); alarm(0); $D{'XXDead'}=0; return; } else { wprint("= Not responding...");} alarm(0);}; if ($@) { if ($@ =~ /timeout/){wprint("= Timed out\\"); $D{'XXDead'}==1?wprint(". Aborting."):wprint(", but continuing +.");}}}
Particulrly could you pealse tell me the corresponding functions for "inet_aton", how to open a raw socket in v6,send raw data and in which format we have to provide IPv6 format (2222:10::3). I went through http://search.cpan.org/~umemoto/Socket6-0.19/Socket6.pm README, but it's not giving clear information. Aprrecitae your help!!
  • Comment on please provide solution to convert the follwoing IPv4 Socket functions to Ipv6 using Socket6
  • Download Code

Replies are listed 'Best First'.
Re: please provide solution to convert the follwoing IPv4 Socket functions to Ipv6 using Socket6
by ikegami (Patriarch) on Jan 15, 2007 at 05:38 UTC

    Particulrly could you pealse tell me the corresponding functions for "inet_aton",

    Pack an IP address Resolve a name
    IPv4 (method 1) inet_aton(...) gethostbyname(...)
    IPv4 (method 2) inet_pton(AF_INET, ...) gethostbyname2(..., AF_INET)
    IPv6 inet_pton(AF_INET6, ...) gethostbyname2(..., AF_INET6)

    The return value of all of these (in scalar context) is a packed IP address.

    Update: Reformatted as a table.

Re: please provide solution to convert the follwoing IPv4 Socket functions to Ipv6 using Socket6
by ikegami (Patriarch) on Jan 15, 2007 at 06:11 UTC

    If you want to support both IPv4 and IPv6, you'd use the code in Socket6's Synopsis. (Adjusted for your needs below.)

    use strict; use warnings; use Socket; use Socket6; my @res = getaddrinfo($D{'XXTarget'}, $D{'XXPort'}, AF_UNSPEC, SOCK_ST +REAM); my $sock; my $host; my $port; while (@res >= 5) { my ($family, $socktype, $proto, $saddr, $canonname) = splice(@res, +0, 5); ($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSER +V); # log("Trying to connect to $host port port $port...\n"); socket($sock, $family, $socktype, $proto) or next; connect($sock, $saddr) and last; undef $sock; } if (not defined $sock) { ... Handle error ... } ... $sock is connected. $host and $post are available ...
Re: please provide solution to convert the follwoing IPv4 Socket functions to Ipv6 using Socket6
by kyle (Abbot) on Jan 15, 2007 at 04:30 UTC

    Please use <code> tags and other useful suggestions from the Writeup Formatting Tips.

    For example:

    <code> sub sendraw { my ($pstr)=@_; $PROTO=getprotobyname('tcp')||0; @DXX=(); $pstr=~s/\n/\r\n/g; $pstr=~s/\r\r/\r/g; # ... etc. ... </code>

    Otherwise, consider posting to Obfuscated code.

    UPDATE: That's better, thanks.