Last active
October 6, 2015 07:52
-
-
Save ytakano/51c45af5a5dbbbf23639 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Socket; | |
my @sessions; | |
# connect IF by UNIX domain socket | |
my $path = "/tmp/sf-tap/tcp/http"; | |
my $s; | |
socket($s, AF_UNIX, SOCK_STREAM, 0); | |
connect($s, pack_sockaddr_un($path)) or die "cannot connect to $path"; | |
while (1) { | |
# read header | |
$header = readline $s or die "remote socket was closed"; | |
chomp $header; | |
@lst1 = split(/,/, $header); | |
my %hdic = (); | |
foreach my $elm (@lst1) { | |
@lst2 = split(/=/, $elm); | |
$hdic{@lst2[0]} = @lst2[1]; | |
} | |
# generate session ID | |
my $sid = $hdic{"ip1"} . "@" . $hdic{"port1"} . "@" . $hdic{"ip2"} . "@" . | |
$hdic{"port2"} . "@" . $hdic{"hop"}; | |
# handle events | |
if ($hdic{"event"} eq "CREATED") { | |
$sessions{$sid} = ([], []); | |
print "created: sid = $sid\n"; | |
} elsif ($hdic{"event"} eq "DATA") { | |
my $readlen = read $s, my($buf), $hdic{"len"} or die "remote socket was closed"; | |
if (not exists $sessions{$sid}) { | |
next; | |
} | |
my $from, $to; | |
if ($hdic{"from"} == 1) { | |
$from = $hdic{"ip1"} . ":" . $hdic{"port1"}; | |
$to = $hdic{"ip2"} . ":" . $hdic{"port2"}; | |
push @{$sessions{$sid}[0]}, $buf; | |
} else { | |
$from = $hdic{"ip2"} . ":" . $hdic{"port2"}; | |
$to = $hdic{"ip1"} . ":" . $hdic{"port1"}; | |
push @{$sessions{$sid}[1]}, $buf; | |
} | |
print "data: readlen = $readlen, from = $from, to = $to\n"; | |
} elsif ($hdic{"event"} eq "DESTROYED") { | |
if (exists $sessions{$sid}) { | |
delete $sessions{$sid}; | |
print "deleted sid = $sid\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment