Since you are dealing with raw packets, you have to listen to everything and filter yourself for the packets you are interested in. You will probably want to consider the packets that are received on the port you used to send packets from, and discard all others.
This code will listen to every TCP packet and show you the IP addresses and port numbers involved.
#!/usr/bin/perl
use strict;
use Socket;
socket S, PF_INET, SOCK_RAW, 6;
my $result;
while (my $sender = recv(S, $result, 2000, 0)) {
print "got a ", length($result), " byte TCP packet...\n";
if (length($result) < 20) {
print " ...which is too small to be an IP packet\n";
} else {
printf " from %d.%d.%d.%d to %d.%d.%d.%d\n",
map { unpack("C", substr($result, $_, 1)) } (12..19);
if (length($result) < 40) {
print " ...and is too small to be a TCP packet\n";
} else {
printf " from port %d to port %d\n",
map { unpack("n", substr($result, $_, 2)) } (20, 22);
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.