Source code: com/act365/net/ping/PingSender.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.ping ;
28
29 import com.act365.net.*;
30 import com.act365.net.icmp.* ;
31 import com.act365.net.ip.* ;
32
33 import java.net.*;
34 import java.util.*;
35
36 /**
37 *
38 * <code>PingSender</code> objects transmit Ping packets.
39 */
40
41 public class PingSender extends Thread {
42
43 DatagramSocket socket ;
44
45 InetAddress hostaddr ,
46 localaddr ;
47
48 short identifier ,
49 ttl ;
50
51 Ping ping ;
52
53 int count ,
54 nbytes ;
55
56 /**
57 * Sole constructor
58 *
59 * @param socket socket to be used to transmit
60 * @param hostaddr remote address to be pinged
61 * @param localaddr local address to appear in IP header (RawICMP only)
62 * @param identifier identifier to appear in ICMP message
63 * @param ping object that created the <code>PingSender</code> object
64 * @param count number of packets to be transmitted
65 * @param nbytes size of packets to be transmitted
66 * @param ttl time-to-live of packets (RawICMP only)
67 */
68
69 public PingSender( DatagramSocket socket ,
70 InetAddress hostaddr ,
71 InetAddress localaddr ,
72 short identifier ,
73 Ping ping ,
74 int count ,
75 int nbytes ,
76 short ttl ) {
77
78 this.socket = socket ;
79 this.hostaddr = hostaddr ;
80 this.localaddr = localaddr ;
81 this.identifier = identifier ;
82 this.ping = ping ;
83 this.count = count ;
84 this.nbytes = nbytes ;
85 this.ttl = ttl ;
86 }
87
88 /**
89 * Starts the transmission process.
90 */
91
92 public void run() {
93
94 ICMPWriter writer = new ICMPWriter( identifier );
95
96 byte[] buffer ,
97 databuffer = new byte[ nbytes ];
98
99 int i = -1 ;
100
101 while( ++ i < databuffer.length ){
102 databuffer[i] = (byte) i ;
103 }
104
105 try {
106 while( count -- != 0 ){
107 if( nbytes >= 8 ){
108 SocketUtils.longToBytes( new Date().getTime() , databuffer , 0 );
109 }
110 buffer = writer.write( ICMP.ICMP_ECHO , (byte) 0 , databuffer );
111
112 if( SocketUtils.includeHeader() ){
113 buffer = IP4Writer.write( IP4.TOS_ICMP ,
114 ttl ,
115 (byte) SocketConstants.IPPROTO_ICMP ,
116 localaddr != null ? localaddr.getAddress() : new byte[4],
117 hostaddr.getAddress() ,
118 buffer );
119 }
120
121 socket.send( new DatagramPacket( buffer , buffer.length , hostaddr , 0 ) );
122
123 sleep( 1000 );
124 }
125 ping.interrupt();
126 } catch ( Exception e ) {
127 System.err.println( e.getMessage() );
128 System.exit( 1 );
129 }
130 }
131 }
132