/
Probe.java
184 lines (162 loc) · 6.29 KB
/
Probe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.jgroups.tests;
import org.jgroups.util.Util;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.*;
/**
* Discovers all UDP-based members running on a certain mcast address
* @author Bela Ban
* @version $Revision: 1.20 $
* Date: Jun 2, 2003
* Time: 4:35:29 PM
*/
public class Probe {
MulticastSocket mcast_sock;
volatile boolean running=true;
final Set<String> senders=new HashSet<String>();
public Probe() {
}
public void start(InetAddress addr, InetAddress bind_addr, int port, int ttl,
final long timeout, List query, String match, boolean weed_out_duplicates) throws Exception {
mcast_sock=new MulticastSocket();
mcast_sock.setTimeToLive(ttl);
if(bind_addr != null)
mcast_sock.setInterface(bind_addr);
StringBuilder request=new StringBuilder();
for(int i=0; i < query.size(); i++) {
request.append(query.get(i)).append(" ");
}
byte[] probe_buf=request.toString().getBytes();
DatagramPacket probe=new DatagramPacket(probe_buf, 0, probe_buf.length, addr, port);
mcast_sock.send(probe);
System.out.println("\n-- send probe on " + addr + ':' + port + '\n');
new Thread() {
public void run() {
Util.sleep(timeout);
mcast_sock.close();
running=false;
}
}.start();
int matched=0, not_matched=0, count=0;
String response;
while(running) {
byte[] buf=new byte[65000];
DatagramPacket rsp=new DatagramPacket(buf, 0, buf.length);
try {
mcast_sock.receive(rsp);
}
catch(Throwable t) {
System.out.println("\n");
break;
}
byte[] data=rsp.getData();
response=new String(data, 0, rsp.getLength());
if(weed_out_duplicates && checkDuplicateResponse(response)) {
continue;
}
count++;
if(matches(response, match)) {
matched++;
System.out.println("\n#" + count + " (" + rsp.getLength() + " bytes):\n" + response);
}
else
not_matched++;
}
System.out.println("\n" + count + " responses (" + matched + " matches, " + not_matched + " non matches)");
}
private boolean checkDuplicateResponse(String response) {
int index=response.indexOf("local_addr");
if(index != -1) {
String addr=parseAddress(response.substring(index+1 + "local_addr".length()));
return senders.add(addr) == false;
}
return false;
}
private static String parseAddress(String response) {
StringTokenizer st=new StringTokenizer(response);
return st.nextToken();
}
private static boolean matches(String response, String match) {
if(response == null)
return false;
if(match == null)
return true;
int index=response.indexOf(match);
return index > -1;
}
public static void main(String[] args) {
InetAddress addr=null, bind_addr=null;
int port=0;
int ttl=32;
long timeout=500;
final String DEFAULT_DIAG_ADDR="224.0.75.75";
final String DEFAULT_DIAG_ADDR_IPv6="ff0e::0:75:75";
final int DEFAULT_DIAG_PORT=7500;
List<String> query=new ArrayList<String>();
String match=null;
boolean weed_out_duplicates=false;
boolean ipv6=false;
try {
for(int i=0; i < args.length; i++) {
if("-addr".equals(args[i])) {
addr=InetAddress.getByName(args[++i]);
continue;
}
if("-bind_addr".equals(args[i])) {
bind_addr=InetAddress.getByName(args[++i]);
continue;
}
if("-port".equals(args[i])) {
port=Integer.parseInt(args[++i]);
continue;
}
if("-ttl".equals(args[i])) {
ttl=Integer.parseInt(args[++i]);
continue;
}
if("-timeout".equals(args[i])) {
timeout=Long.parseLong(args[++i]);
continue;
}
if("-match".equals(args[i])) {
match=args[++i];
continue;
}
if("-weed_out_duplicates".equals(args[i])) {
weed_out_duplicates=true;
continue;
}
if("-ipv6".equals(args[i])) {
ipv6=true;
continue;
}
if("-help".equals(args[i]) || "-h".equals(args[i]) || "--help".equals(args[i])) {
help();
return;
}
query.add(args[i]);
}
Probe p=new Probe();
if(addr == null)
addr=ipv6? InetAddress.getByName(DEFAULT_DIAG_ADDR_IPv6) : InetAddress.getByName(DEFAULT_DIAG_ADDR);
if(port == 0)
port=DEFAULT_DIAG_PORT;
p.start(addr, bind_addr, port, ttl, timeout, query, match, weed_out_duplicates);
}
catch(Throwable t) {
t.printStackTrace();
}
}
static void help() {
System.out.println("Probe [-help] [-addr <addr>] [-ipv6] [-bind_addr <addr>] " +
"[-port <port>] [-ttl <ttl>] [-timeout <timeout>] [-weed_out_duplicates] [-match pattern]" +
"[key[=value]]*\n\n" +
"Examples:\n" +
"probe.sh keys // dumps all valid commands\n" +
"probe.sh jmx=NAKACK // dumps JMX info about all NAKACK protocols\n" +
"probe.sh op=STABLE.runMessageGarbageCollection // invokes the method in all STABLE protocols\n" +
"probe.sh jmx=UDP.oob,thread_pool // dumps all attrs of UDP starting with oob* or thread_pool*\n" +
"probe.sh jmx=FLUSH.bypass=true\n");
}
}