Solved

UDP Sockets

Posted on 1997-06-18
12
597 Views
Last Modified: 2008-03-10
Hello there,

I am trying to make a program in perl that can basically send data to an IP address. I don't need to connect, so I want to use UDP.
If you know where I can find a tutorial for this it would be very much appreciated.
Could you give me some code that would send the string $aaaa to 206.45.35.12
Thanks...
0
Comment
Question by:Sunil
  • 6
  • 4
  • 2
12 Comments
 

Author Comment

by:Sunil
Comment Utility
Adjusted points to 240
0
 
LVL 5

Expert Comment

by:icd
Comment Utility
To send a string to 206.45.35.12 you need more than just the address. When you connect to
206.45.35.12, you have to specify that you want to talk to the FTP server (for example) this is done through assigned port numbers.

You can find a very good tutorial at:-

http://spectral.mscs.mu.edu/NetworksClass/Materials/tcptut3.html

0
 
LVL 5

Expert Comment

by:icd
Comment Utility
Since you gave not reason for the rejection I have to make another stab in the dark :-)

--- cut here ---
#!/usr/bin/perl

$aaaa = "this is your string";
($them,$port) =@ARGV;
$port=2345 unless $port;
$them='localhost' unless $them;

$AF_INET=2;
$SOCK_STREAM=1;

$SIG{'INT'}= 'dokill';
sub dokill {
      kill 9,$child if $child;
}

$sockaddr ='S n a4 x8';
chop($hostname=`hostname`);
($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

$this=pack($sockaddr, $AF_INET, 0, $thisaddr);
$that=pack($sockaddr, $AF_INET, $port, $thataddr);

if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
      print "socket ok\n";
} else {
      die $!;
}

if (bind(S, $this)) {
      print "bind OK\n";
} else {
      die $!;
}

if (connect(S,$that)) {
      print "connect OK\n";
} else {
      die $!;
}

select(S); $|=1; select(STDOUT);

print S "$aaaa";
close (S);
--- end ---

0
 

Author Comment

by:Sunil
Comment Utility
Sorry to be so picky but I really need it to work.
The example you have shown me does not include the IP address that I want to send it to.

Also, I need to use UDP because I will not be able to connect to the remote machine. It just has to send automatically.
E.g. suppose my next door neighbour is on the net. This program should just bomb his or hers IP address with data. I know you can do this with UDP.

The site you told me didn't have any perl examples.

Hope you can help.
0
 
LVL 5

Expert Comment

by:icd
Comment Utility
OK. Third and last try. This will connect to port 80 (http port)
or you could try other ports (e.g. mail port 25) or ftp (port 21)

--- cut here ---
#!/usr/bin/perl

$port=80;

($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;;

$sockaddr ='S n a4 x8';
$thisaddr = pack('C4', 127,0,0,1);
$thataddr = pack('C4', 206,45,35,12);

$this=pack($sockaddr, 2, 0, $thisaddr);
$that=pack($sockaddr, 2, $port, $thataddr);

if (socket(S, 2, 1, $proto)) {
      print "socket ok\n";
} else {
      die $!;
}

if (bind(S, $this)) {
      print "bind OK\n";
} else {
      die $!;
}

if (connect(S,$that)) {
      print "connect OK\n";
} else {
      die $!;
}

select(S); $|=1; select(STDOUT);

for (1...10000) {
      print S "I am going to try and crash your server!";
}
close (S);
--- end ---
0
 

Author Comment

by:Sunil
Comment Utility
Sorry.

It didn't work.
I need to use UDP anyway. It has to send datagrams and not connect.

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Expert Comment

by:icd
Comment Utility
I just found an example UDP message passing program that will probably be closer to what you want.

Look at

http://www.perl.com/perl/all_about/ipc/udp.html


0
 

Author Comment

by:Sunil
Comment Utility
Sorry but it is no good giving me a a lump of code and not telling me how to use it. My knowledge of Perl is not extensive so i need a detailed explanation of each line.

Hope you can help me soon.
0
 

Expert Comment

by:shawni
Comment Utility

This requires a recent perl, preferably 5.004.  If you don't
have a recent perl, you might be able to get by with 5.003+
and go install the IO module from CPAN (I use ftp.cdrom.com/pub/
perl/CPAN).

Doing this with an older perl is possible, just a lot more
involved.

#!/usr/bin/perl

use IO::Socket;  # this is why it has to be recent

$aaaa = "TEST";  # data to send
$flags = 0;      # you might want MSG_OOB or such later
$udp_port = 7;   # destination UDP port (you didn't say, so
                 # we'll use the echo port

# create a UDP socket, with the endpoint set ("connected").
$socket=IO::Socket::INET->new(PeerAddr=>"206.45.35.12:$udp_port",
     Proto=>"udp");

# send one packet.
send $socket,$aaaa,$flags;

# send another.
send $socket,"Hi there!",$flags;

exit(0);

0
 

Author Comment

by:Sunil
Comment Utility
Hi.

Thank you so much for helping me out with this problem.
I was just wondering whether there is any way that I can test to see whether it works or not?

Hope to hear from you soon.

Thanks.

0
 

Accepted Solution

by:
shawni earned 240 total points
Comment Utility

I usually use tcpdump, but this program will receive a single
UDP packet so you can test the other program.  Run this in
one window, and run the sending program in another window.
This program will receive the packet and print the contents.

You'll also have to change the port number/host IP address
in the UDP packet sending program to point at your testing
host and testing port number.

For more documentation on how these functions work, use

perldoc IO::Socket

..on any machine that this program runs on.



#!/usr/local/bin/perl
use IO::Socket;

$aaaa = "";  # these have the same meaning as the other
$len = 100;  # program, $len is maximum length of packet you
             # wish to receive
$port = 10000;  # local service port.  Remember to change the
                # port number in the other program too!
$flags = 0;

$sock = IO::Socket::INET->new(LocalPort=>$port,
        Proto=>"udp");

recv $sock,$aaaa,$len,$flags;

print $aaaa,"\n";

0
 

Author Comment

by:Sunil
Comment Utility
Everything works fine.
Thank you so much for giving me the code.
I am so grateful.
If you know any good perl resource sites for beginers could you please tell me.
Thanks.



0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now