Please help in snmp project
the problem in this project is that it is not giving mib information of another computer, though it is retreiving info of local computer on which it is running. following is the sourcecode: main starts from snmpclint class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import javax.swing.border.*;
import java.lang.*;
import java.net.*;
import java.io.*;
public class Snmpclint extends JFrame implements Runnable, ActionListener
{
private JTextField status,value,server,community;
private JLabel name;
int id;
byte type;
private List mib;
private List table;
java.util.Vector index;
Thread thread = null;
JSnmp snmp = null;
int op;
Mib root;
Mib crt;
int pos;
static int READ = 0;
static int WRITE = 1;
public Snmpclint()
{
super ("Network Traffic Controller");
super.setBounds(400,160,600,600);
setSize(600,500);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 100;
gbc.weighty = 0;
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 3));
JButton read = new JButton("Read");
read.addActionListener(this);
p2.add(read);
JButton write = new JButton("Write");
write.addActionListener(this);
p2.add(write);
JButton stop = new JButton("Stop");
stop.addActionListener(this);
p2.add(stop);
add(this, p2, gbl, gbc, 0, 0, 6, 1);
JPanel p1 = new JPanel();
p1.setBorder(new EmptyBorder(10, 10, 10, 10));
p1.setLayout(new GridLayout(3, 2));
p1.add(new JLabel("Server"));
String host = "";
try{
host = InetAddress.getLocalHost().getHostName();
} catch(UnknownHostException e){};
p1.add(server = new JTextField(host));
p1.add(new JLabel("Community"));
p1.add(community = new JTextField("public"));
name = new JLabel("");
p1.add(name);
p1.add(value = new JTextField(""));
add(this, p1, gbl, gbc, 0, 1, 6, 3);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
JPanel pmib = new JPanel();
pmib.setLayout(new GridLayout(1, 1));
mib = new List();
setMib();
pmib.add(mib);
JPanel ptable = new JPanel();
ptable.setLayout(new GridLayout(1, 1));
table = new List();
ptable.add(table);
index = new java.util.Vector();
p3.add(pmib);
p3.add(ptable);
gbc.weighty = 100;
gbc.fill = GridBagConstraints.BOTH;
add(this, p3, gbl, gbc, 0, 4, 6, 8);
status = new JTextField("Initialized");
status.setEditable(false);
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(this, status, gbl, gbc, 0, 12, 6, 1);
validate();
setVisible(true);
WindowListener wndclose = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndclose);
}
void setMib(){
root = new Mib("mib-2", null, 8);
Mib system = new Mib("system", root, 7);
system.children[0] = new Mib("sysDescr", "1.3.6.1.2.1.1.1", system);
system.children[1] = new Mib("sysObjectId", "1.3.6.1.2.1.1.2", system);
system.children[2] = new Mib("sysUpTime", "1.3.6.1.2.1.1.3", system);
system.children[3] = new Mib("sysContact", "1.3.6.1.2.1.1.4", system);
system.children[4] = new Mib("sysName", "1.3.6.1.2.1.1.5", system);
system.children[5] = new Mib("sysLocation", "1.3.6.1.2.1.1.6", system);
system.children[6] = new Mib("sysServices", "1.3.6.1.2.1.1.7", system);
root.children[0] = system;
Mib interfaces = new Mib("interfaces", root, 2);
interfaces.children[0] = new Mib("ifNumber", "1.3.6.1.2.1.2.1", interfaces);
interfaces.children[1] = new Mib("ifTable", interfaces, 1);
Mib ife = new Mib("ifEntry", interfaces, 22);
ife.children[0] = new Mib("ifIndex", "1.3.6.1.2.1.2.2.1.1", ife);
ife.children[1] = new Mib("ifDescre", "1.3.6.1.2.1.2.2.1.2", ife);
ife.children[2] = new Mib("ifType", "1.3.6.1.2.1.2.2.1.3", ife);
ife.children[3] = new Mib("ifMtu", "1.3.6.1.2.1.2.2.1.4", ife);
ife.children[4] = new Mib("ifSpeed", "1.3.6.1.2.1.2.2.1.5", ife);
ife.children[5] = new Mib("ifPhysAddress", "1.3.6.1.2.1.2.2.1.6", ife);
ife.children[6] = new Mib("ifAdminStatus", "1.3.6.1.2.1.2.2.1.7", ife);
ife.children[7] = new Mib("ifOperStatus", "1.3.6.1.2.1.2.2.1.8", ife);
ife.children[8] = new Mib("ifLastChange", "1.3.6.1.2.1.2.2.1.9", ife);
ife.children[9] = new Mib("ifInOctets", "1.3.6.1.2.1.2.2.1.10", ife);
ife.children[10] = new Mib("ifInUcastPkts", "1.3.6.1.2.1.2.2.1.11", ife);
ife.children[11] = new Mib("ifInNUcastPkts", "1.3.6.1.2.1.2.2.1.12", ife);
ife.children[12] = new Mib("ifInDiscards", "1.3.6.1.2.1.2.2.1.13", ife);
ife.children[13] = new Mib("ifInErrors", "1.3.6.1.2.1.2.2.1.14", ife);
ife.children[14] = new Mib("ifInUnknownProtos", "1.3.6.1.2.1.2.2.1.15", ife);
ife.children[15] = new Mib("ifOutOctets", "1.3.6.1.2.1.2.2.1.16", ife);
ife.children[16] = new Mib("ifOutUcastPkts", "1.3.6.1.2.1.2.2.1.17", ife);
ife.children[17] = new Mib("ifOutNUcastPkts", "1.3.6.1.2.1.2.2.1.18", ife);
ife.children[18] = new Mib("ifOutDiscards", "1.3.6.1.2.1.2.2.1.19", ife);
ife.children[19] = new Mib("ifOutErrors", "1.3.6.1.2.1.2.2.1.20", ife);
ife.children[20] = new Mib("ifOutQLen", "1.3.6.1.2.1.2.2.1.21", ife);
ife.children[21] = new Mib("ifSpecific", "1.3.6.1.2.1.2.2.1.22", ife);
interfaces.children[1].children[0] = ife;
root.children[1] = interfaces;
Mib at = new Mib("at", root, 1);
at.children[0] = new Mib("atTable", at, 1);
Mib ate = new Mib("atEntry", at.children[0], 3);
ate.children[0] = new Mib("atIfIndex", "1.3.6.1.2.1.3.1.1.1", ate);
ate.children[1] = new Mib("atPhysAddress", "1.3.6.1.2.1.3.1.1.2", ate);
ate.children[2] = new Mib("atNetAddress", "1.3.6.1.2.1.3.1.1.3", ate);
at.children[0].children[0] = ate;
root.children[2] = at;
Mib ip = new Mib("ip", root, 23);
ip.children[0] = new Mib("ipForwarding", "1.3.6.1.2.1.4.1", ip);
ip.children[1] = new Mib("ipDefaultTTL", "1.3.6.1.2.1.4.2", ip);
ip.children[2] = new Mib("ipInReceives", "1.3.6.1.2.1.4.3", ip);
ip.children[3] = new Mib("ipInHdrErrors", "1.3.6.1.2.1.4.4", ip);
ip.children[4] = new Mib("ipInAddrErrors", "1.3.6.1.2.1.4.5", ip);
ip.children[5] = new Mib("ipForwDatagrams", "1.3.6.1.2.1.4.6", ip);
ip.children[6] = new Mib("ipUnknownProtos", "1.3.6.1.2.1.4.7", ip);
ip.children[7] = new Mib("ipInDiscards", "1.3.6.1.2.1.4.8", ip);
ip.children[8] = new Mib("ipInDelivers", "1.3.6.1.2.1.4.9", ip);
ip.children[9] = new Mib("ipOutRequests", "1.3.6.1.2.1.4.10", ip);
ip.children[10] = new Mib("ipOutDiscards", "1.3.6.1.2.1.4.11", ip);
ip.children[11] = new Mib("ipOutNoRoutes", "1.3.6.1.2.1.4.12", ip);
ip.children[12] = new Mib("ipReasmTimeOut", "1.3.6.1.2.1.4.13", ip);
ip.children[13] = new Mib("ipReasmReqds", "1.3.6.1.2.1.4.14", ip);
ip.children[14] = new Mib("ipReasmOKs", "1.3.6.1.2.1.4.15", ip);
ip.children[15] = new Mib("ipReasmFails", "1.3.6.1.2.1.4.16", ip);
ip.children[16] = new Mib("ipFragOKs", "1.3.6.1.2.1.4.17", ip);
ip.children[17] = new Mib("ipFragFails", "1.3.6.1.2.1.4.18", ip);
ip.children[18] = new Mib("ipFragCreates", "1.3.6.1.2.1.4.19", ip);
ip.children[19] = new Mib("ipAddrTable", ip, 1);
Mib ipaddr = new Mib("ipAddrEntry", ip.children[19], 5);
ipaddr.children[0] = new Mib("ipAdEntAddr", "1.3.6.1.2.1.4.20.1.1", ipaddr);
ipaddr.children[1] = new Mib("ipAdEntIfIndex", "1.3.6.1.2.1.4.20.1.2", ipaddr);
ipaddr.children[2] = new Mib("ipAdEntNetMask", "1.3.6.1.2.1.4.20.1.3", ipaddr);
ipaddr.children[3] = new Mib("ipAdEntBcastAddr", "1.3.6.1.2.1.4.20.1.4", ipaddr);
ipaddr.children[4] = new Mib("ipAdEntReasmMaxSize", "1.3.6.1.2.1.4.20.1.5", ipaddr);
ip.children[19].children[0] = ipaddr;
ip.children[20] = new Mib("ipRouteTable", ip, 1);
Mib ipr = new Mib("ipRouteEntry", ip.children[20], 13);
ipr.children[0] = new Mib("ipRouteDest", "1.3.6.1.2.1.4.21.1.1", ipr);
ipr.children[1] = new Mib("ipRouteIfIndex", "1.3.6.1.2.1.4.21.1.2", ipr);
ipr.children[2] = new Mib("ipRouteMetric1", "1.3.6.1.2.1.4.21.1.3", ipr);
ipr.children[3] = new Mib("ipRouteMetric2", "1.3.6.1.2.1.4.21.1.4", ipr);
ipr.children[4] = new Mib("ipRouteMetric3", "1.3.6.1.2.1.4.21.1.5", ipr);
ipr.children[5] = new Mib("ipRouteMetric4", "1.3.6.1.2.1.4.21.1.6", ipr);
ipr.children[6] = new Mib("ipRouteNextHop", "1.3.6.1.2.1.4.21.1.7", ipr);
ipr.children[7] = new Mib("ipRouteType", "1.3.6.1.2.1.4.21.1.8", ipr);
ipr.children[8] = new Mib("ipRouteProto", "1.3.6.1.2.1.4.21.1.9", ipr);
ipr.children[9] = new Mib("ipRouteAge", "1.3.6.1.2.1.4.21.1.10", ipr);
ipr.children[10] = new Mib("ipRouteMask", "1.3.6.1.2.1.4.21.1.11", ipr);
ipr.children[11] = new Mib("ipRouteMetric5", "1.3.6.1.2.1.4.21.1.12", ipr);
ipr.children[12] = new Mib("ipRouteInfo", "1.3.6.1.2.1.4.21.1.13", ipr);
ip.children[20].children[0] = ipr;
ip.children[21] = new Mib("ipNetToMediaTable", ip, 1);
Mib ipn = new Mib("ipNetToMediaEntry", ip.children[21], 4);
ipn.children[0] = new Mib("ipNetToMediaIfIndex", "1.3.6.1.2.1.4.22.1.1", ipn);
ipn.children[1] = new Mib("ipNetToMediaPhysAddress", "1.3.6.1.2.1.4.22.1.2", ipn);
ipn.children[2] = new Mib("ipNetToMediaNetAddress", "1.3.6.1.2.1.4.22.1.3", ipn);
ipn.children[3] = new Mib("ipNetToMediaType", "1.3.6.1.2.1.4.22.1.4", ipn);
ip.children[21].children[0] = ipn;
ip.children[22] = new Mib("ipRoutingDiscards", "1.3.6.1.2.1.4.23", ip);
root.children[3] = ip;
Mib icmp = new Mib("icmp", root, 26);
icmp.children[0] = new Mib("icmpInMsgs", "1.3.6.1.2.1.5.1", icmp);
icmp.children[1] = new Mib("icmpInErrors", "1.3.6.1.2.1.5.2", icmp);
icmp.children[2] = new Mib("icmpInDestUnreachs", "1.3.6.1.2.1.5.3", icmp);
icmp.children[3] = new Mib("icmpInTimeExcds", "1.3.6.1.2.1.5.4", icmp);
icmp.children[4] = new Mib("icmpParmProbs", "1.3.6.1.2.1.5.5", icmp);
icmp.children[5] = new Mib("icmpInSrcQuenchs", "1.3.6.1.2.1.5.6", icmp);
icmp.children[6] = new Mib("icmpInRedirects", "1.3.6.1.2.1.5.7", icmp);
icmp.children[7] = new Mib("icmpInEchos", "1.3.6.1.2.1.5.8", icmp);
icmp.children[8] = new Mib("icmpInEchoReps", "1.3.6.1.2.1.5.9", icmp);
icmp.children[9] = new Mib("icmpInTimestamps", "1.3.6.1.2.1.5.10", icmp);
icmp.children[10] = new Mib("icmpInTimestampReps", "1.3.6.1.2.1.5.11", icmp);
icmp.children[11] = new Mib("icmpInAddrMasks", "1.3.6.1.2.1.5.12", icmp);
icmp.children[12] = new Mib("icmpInAddrMaskReps", "1.3.6.1.2.1.5.13", icmp);
icmp.children[13] = new Mib("icmpOutMsgs", "1.3.6.1.2.1.5.14", icmp);
icmp.children[14] = new Mib("icmpOutErrors", "1.3.6.1.2.1.5.15", icmp);
icmp.children[15] = new Mib("icmpOutDestUnreachs", "1.3.6.1.2.1.5.16", icmp);
icmp.children[16] = new Mib("icmpOutTimeExcds", "1.3.6.1.2.1.5.17", icmp);
icmp.children[17] = new Mib("icmpOutParmProbs", "1.3.6.1.2.1.5.18", icmp);
icmp.children[18] = new Mib("icmpOutSrcQuenchs", "1.3.6.1.2.1.5.19", icmp);
icmp.children[19] = new Mib("icmpOutRedirects", "1.3.6.1.2.1.5.20", icmp);
icmp.children[20] = new Mib("icmpOutEchos", "1.3.6.1.2.1.5.21", icmp);
icmp.children[21] = new Mib("icmpOutEchoReps", "1.3.6.1.2.1.5.22", icmp);
icmp.children[22] = new Mib("icmpOutTimestamps", "1.3.6.1.2.1.5.23", icmp);
icmp.children[23] = new Mib("icmpOutTimestampReps", "1.3.6.1.2.1.5.24", icmp);
icmp.children[24] = new Mib("icmpOutAddrMasks", "1.3.6.1.2.1.5.25", icmp);
icmp.children[25] = new Mib("icmpOutAddrMaskReps", "1.3.6.1.2.1.5.26", icmp);
root.children[4] = icmp;
Mib tcp = new Mib("tcp", root, 15);
tcp.children[0] = new Mib("tcpRtoAlgorithm", "1.3.6.1.2.1.6.1", tcp);
tcp.children[1] = new Mib("tcpRtoMin", "1.3.6.1.2.1.6.2", tcp);
tcp.children[2] = new Mib("tcpRtoMax", "1.3.6.1.2.1.6.3", tcp);
tcp.children[3] = new Mib("tcpMaxConn", "1.3.6.1.2.1.6.4", tcp);
tcp.children[4] = new Mib("tcpActiveOpens", "1.3.6.1.2.1.6.5", tcp);
tcp.children[5] = new Mib("tcpPassiveOpens", "1.3.6.1.2.1.6.6", tcp);
tcp.children[6] = new Mib("tcpAttemptFails", "1.3.6.1.2.1.6.7", tcp);
tcp.children[7] = new Mib("tcpEstabResets", "1.3.6.1.2.1.6.8", tcp);
tcp.children[8] = new Mib("tcpCurrEstab", "1.3.6.1.2.1.6.9", tcp);
tcp.children[9] = new Mib("tcpInSegs", "1.3.6.1.2.1.6.10", tcp);
tcp.children[10] = new Mib("tcpOutSegs", "1.3.6.1.2.1.6.11", tcp);
tcp.children[11] = new Mib("tcpRetransSegs", "1.3.6.1.2.1.6.12", tcp);
tcp.children[12] = new Mib("tcpConnTable", tcp, 1);
Mib tce = new Mib("tcpConnEntry", tcp.children[12], 5);
tce.children[0] = new Mib("tcpConnState", "1.3.6.1.2.1.6.13.1.1", tce);
tce.children[1] = new Mib("tcpConnLocalAddress", "1.3.6.1.2.1.6.13.1.2", tce);
tce.children[2] = new Mib("tcpConnLocalPort", "1.3.6.1.2.1.6.13.1.3", tce);
tce.children[3] = new Mib("tcpConnRemAddress", "1.3.6.1.2.1.6.13.1.4", tce);
tce.children[4] = new Mib("tcpConnRemPort", "1.3.6.1.2.1.6.13.1.5", tce);
tcp.children[12].children[0] = tce;
tcp.children[13] = new Mib("tcpInErrs", "1.3.6.1.2.1.6.14", tcp);
tcp.children[14] = new Mib("tcpOutRsts", "1.3.6.1.2.1.6.15", tcp);
root.children[5] = tcp;
Mib udp = new Mib("udp", root, 5);
udp.children[0] = new Mib("udpInDatagrams", "1.3.6.1.2.1.7.1", udp);
udp.children[1] = new Mib("udpNoPorts", "1.3.6.1.2.1.7.2", udp);
udp.children[2] = new Mib("udpInErrors", "1.3.6.1.2.1.7.3", udp);
udp.children[3] = new Mib("udpOutDatagrams", "1.3.6.1.2.1.7.4", udp);
udp.children[4] = new Mib("udpTable", udp, 1);
Mib ude = new Mib("udpEntry", udp.children[4], 2);
ude.children[0] = new Mib("udpLocalAddress", "1.3.6.1.2.1.7.5.1", ude);
ude.children[1] = new Mib("udpLocalPort", "1.3.6.1.2.1.7.5.2", ude);
udp.children[4].children[0] = ude;
root.children[6] = udp;
Mib sn = new Mib("snmp", root, 28);
sn.children[0] = new Mib("snmpInPkts", "1.3.6.1.2.1.11.1", sn);
sn.children[1] = new Mib("snmpOutPkts", "1.3.6.1.2.1.11.2", sn);
sn.children[2] = new Mib("snmpInBadVersion", "1.3.6.1.2.1.11.3", sn);
sn.children[3] = new Mib("snmpInBadCommunityNames", "1.3.6.1.2.1.11.4", sn);
sn.children[4] = new Mib("snmpInBadCommunityUses", "1.3.6.1.2.1.11.5", sn);
sn.children[5] = new Mib("snmpInASNParseErrs", "1.3.6.1.2.1.11.6", sn);
sn.children[6] = new Mib("snmpInTooBigs", "1.3.6.1.2.1.11.8", sn);
sn.children[7] = new Mib("snmpInNoSuchNames", "1.3.6.1.2.1.11.9", sn);
sn.children[8] = new Mib("snmpInBadValues", "1.3.6.1.2.1.11.10", sn);
sn.children[9] = new Mib("snmpInReadOnlys", "1.3.6.1.2.1.11.11", sn);
sn.children[10] = new Mib("snmpInGenErrs", "1.3.6.1.2.1.11.12", sn);
sn.children[11] = new Mib("snmpInTotalReqVars", "1.3.6.1.2.1.11.13", sn);
sn.children[12] = new Mib("snmpInTotalSetVars", "1.3.6.1.2.1.11.14", sn);
sn.children[13] = new Mib("snmpInGetRequests", "1.3.6.1.2.1.11.15", sn);
sn.children[14] = new Mib("snmpInGetNexts", "1.3.6.1.2.1.11.16", sn);
sn.children[15] = new Mib("snmpInSetRequests", "1.3.6.1.2.1.11.17", sn);
sn.children[16] = new Mib("snmpInGetResponses", "1.3.6.1.2.1.11.18", sn);
sn.children[17] = new Mib("snmpInTraps", "1.3.6.1.2.1.11.19", sn);
sn.children[18] = new Mib("snmpOutTooBigs", "1.3.6.1.2.1.11.20", sn);
sn.children[19] = new Mib("snmpOutNoSuchNames", "1.3.6.1.2.1.11.21", sn);
sn.children[20] = new Mib("snmpOutBadValues", "1.3.6.1.2.1.11.22", sn);
sn.children[21] = new Mib("snmpOutGenErrs", "1.3.6.1.2.1.11.24", sn);
sn.children[22] = new Mib("snmpOutGetRequests", "1.3.6.1.2.1.11.25", sn);
sn.children[23] = new Mib("snmpOutGetNexts", "1.3.6.1.2.1.11.26", sn);
sn.children[24] = new Mib("snmpOutSetRequests", "1.3.6.1.2.1.11.27", sn);
sn.children[25] = new Mib("snmpOutGetResponses", "1.3.6.1.2.1.11.28", sn);
sn.children[26] = new Mib("snmpOutTraps", "1.3.6.1.2.1.11.29", sn);
sn.children[27] = new Mib("snmpEnableAuthenTraps", "1.3.6.1.2.1.11.30", sn);
root.children[7] = sn;
crt = root;
pos = 0;
showMib();
}
void showMib(){
mib.removeAll();
mib.add("[..]");
for(int i = 0; i < crt.children.length; i ++)
mib.add(crt.children.name);
clearObjectFields();
validate();
}
boolean browseMib(){
if(pos == 0){
if(crt == root) status.setText("Can't go up. Root reached");
else{
crt = crt.parent;
pos = 0;
showMib();
}
return true;
}
else if(crt.children[pos - 1].children != null){
crt = crt.children[pos - 1];
pos = 0;
showMib();
return true;
}
return false;
}
void clearObjectFields(){
if(value != null) value.setText("");
if(name != null) name.setText("");
id = -1;
if(index != null && index.size() != 0) index.removeAllElements();
if(table != null && table.getItemCount() != 0) table.removeAll();
if(status != null) status.setText("Idle");
}
private void add(Container p, Component c, GridBagLayout gbl,
GridBagConstraints gbc, int x, int y, int w, int h){
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbl.setConstraints(c, gbc);
add(c);
}
public static void main(String args[])
{
Snmpclint app=new Snmpclint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Read")
{
stopReq();
op = READ;
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
else if(e.getActionCommand()=="Write")
{
stopReq();
op = WRITE;
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
else if(e.getActionCommand()=="Stop")
{
stopReq();
}
}
public boolean handleEvent(Event evt){
if(evt.target == mib && evt.id == Event.LIST_SELECT){
pos = mib.getSelectedIndex();
stopReq();
op = READ;
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
else if(evt.target == table && evt.id == Event.LIST_SELECT){
id = table.getSelectedIndex();
setValue(table.getSelectedIndex());
}
return super.handleEvent(evt);
}
void processStatus(int stat){
status.setText("Authentication error");
}
void processReq(Req req){
if(req.status != 0) processStatus(req.status);
else{
type = req.vb[0].value.type;
table.add(req.vb[0].value.toString());
index.addElement(req.vb[0].id.toString());
}
}
void stopReq(){
if(thread != null) thread.stop();
thread = null;
snmp = null;
status.setText("Idle");
}
boolean subset(String s1, String s2){
return s1.regionMatches(0, s2, 0, s2.length());
}
void readReq(){
if(browseMib()) return;
clearObjectFields();
status.setText("Request sent. Waiting...");
boolean success = true;
String crtId = crt.children[pos - 1].id;
while(subset(crtId, crt.children[pos - 1].id)){
try{
snmp = new JSnmp(server.getText(), community.getText(), SnmpCfg.SLEEP);
Req req = new Req(SnmpCfg.GETNEXT, 1);
req.vb[0] = new Vb(crtId);
// problem starts from next step when retreiving info from other computer
snmp.request(req);
crtId = req.vb[0].id.toString();
if(subset(crtId, crt.children[pos - 1].id)) processReq(req);
snmp = null;
}
catch(IOException e){
success = false;
break;
}
}
if(success){
setValue(0);
id = 0;
validate();
status.setText("Request processed");
}
else status.setText("IOException");
}
void setValue(int i){
String n = mib.getSelectedItem() + "[" + i + "]";
name.setText(n);
value.setText(table.getItem(i));
}
void writeReq(){
if(id == -1){
status.setText("Select an item before any write op");
return;
}
try{
snmp = new JSnmp(server.getText(), community.getText(), SnmpCfg.SLEEP);
Req req = new Req(SnmpCfg.SET, 1);
req.vb[0] = new Vb(((String) index.elementAt(id)), value.getText(), type);
status.setText("Request sent. Waiting...");
// similar problem here too
snmp.request(req);
snmp = null;
status.setText("Request processed");
if(req.status != 0) processStatus(req.status);
else readReq();
}
catch(IOException e){
status.setText("IOException");
}
}
public void stop1(){
stopReq();
}
public void run(){
status.setText("thread running");
if(op == READ) readReq();
else if(op == WRITE) writeReq();
}
}
import java.io.*;
import java.util.*;
import java.net.*;
public class Snmp{
int to;
Value comm;
public Snmp(String c, int sleep){
to = sleep;
comm = new Value(SnmpCfg.OCTETSTRING, c);
}
protected void finalize() throws Throwable{
tlClose();
}
public boolean tlOpen() throws IOException{
return false;
}
public boolean tlClose() throws IOException{
return false;
}
public boolean tlSend(byte[] buff) throws IOException{
return false;
}
public byte[] tlRecv() throws IOException{
return null;
}
public boolean tlReady() throws IOException{
return false;
}
public boolean tlWait(int tout) throws IOException{
return false;
}
public int sendReq(Req req, ByteArrayOutputStream baos) throws IOException{
int i, pdu_l, msg_l, vb_l, rez;
Value v = new Value();
vb_l = 0;
for(i = 0; i < req.vb.length; i ++)
vb_l += req.vb.tlvSize();
if(vb_l < 128)
pdu_l = vb_l + 11;
else
pdu_l = vb_l + SnmpNumber.lengthInt(vb_l) + 11;
if(pdu_l < 128)
msg_l = pdu_l +comm.tlvSize() + 5;
else
msg_l = pdu_l + SnmpNumber.lengthInt(pdu_l) + comm.tlvSize() + 5;
v.type = SnmpCfg.SEQUENCE;
v.length = msg_l;
rez = v.tlvSize();
if(! v.out(baos)) return 0;
v = null;
v = new Value(SnmpCfg.SNMP_VER);
if(! v.out(baos)) return 0;
if(! comm.out(baos)) return 0;
v.type = req.type;
v.length = pdu_l;
if(! v.out(baos)) return 0;
v = new Value(0);
if(! v.out(baos)) return 0;
if(! v.out(baos)) return 0;
if(! v.out(baos)) return 0;
v.type = SnmpCfg.SEQUENCE;
v.length = vb_l;
if(! v.out(baos)) return 0;
for(i = 0; i < req.vb.length; i ++)
req.vb.out(baos);
return rez;
}
public int recvReq(Req req, ByteArrayInputStream bais) throws IOException{
Value v = new Value();
int rez, dim;
if((dim = v.in(bais)) == 0) return 0;
if(v.type != SnmpCfg.SEQUENCE) return 0;
rez = dim + v.vSize();
if((dim = v.in(bais)) == 0) return 0;
if(v.type != SnmpCfg.INTEGER) return 0;
if(v.getInt() != SnmpCfg.SNMP_VER) return 0;
if((dim = v.in(bais)) == 0) return 0;
//A secure client should exit here if v != comm
if(v.in(bais) == 0) return 0;
//if(v.type != SnmpCfg.GETRESPONSE) return 0;
req.type = v.type;
if(v.in(bais) == 0) return 0;
if(v.type != SnmpCfg.INTEGER) return 0;
if(v.in(bais) == 0) return 0;
if(v.type != SnmpCfg.INTEGER) return 0;
req.status = v.getInt();
if(v.in(bais) == 0) return 0;
if(v.type != SnmpCfg.INTEGER) return 0;
req.index = v.getInt();
if(req.status != 0) return 0;
if(v.in(bais) == 0) return 0;
if(v.type != SnmpCfg.SEQUENCE) return 0;
int l = v.length;
Vector vec = new Vector();
while(l > 0){
Vb vb = new Vb();
if((dim = vb.in(bais)) == 0) return 0;
vec.addElement(vb);
l -= dim;
}
req.vb = new Vb[vec.size()];
for(int i = 0; i < req.vb.length; i ++)
req.vb = (Vb) vec.elementAt(i);
return rez;
}
public boolean request(Req req) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(sendReq(req, baos) == 0) return false;
byte[] ba = baos.toByteArray();
if(tlOpen() == false) return false;
if(tlSend(ba) == false) return false;
ba = null;
if((ba = tlRecv()) == null) return false;
tlClose();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
if(recvReq(req, bais) == 0) return false;
return true;
}
}
import java.io.*;
import java.util.*;
import java.net.*;
public class JSnmp extends Snmp{
static int AGENT_PORT = 161;
DatagramSocket socket = null;
String server = null;
JSnmp(String ser, String c, int sleep){
super(c, sleep);
server = ser;
}
public boolean tlOpen() throws IOException{
if((socket = new DatagramSocket()) == null) return false;
//Works only in 1.1.*
// socket.setSoTimeout(to);
return true;
}
public boolean tlClose(){
if(socket != null) socket.close();
socket = null;
return true;
}
public boolean tlSend(byte[] buff) throws IOException{
int port;
InetAddress address;
DatagramPacket packet;
port = AGENT_PORT;
address = InetAddress.getByName(server);
packet = new DatagramPacket(buff, buff.length, address, port);
socket.send(packet);
return true;
}
public byte[] tlRecv() throws IOException{
byte[] buff = new byte[SnmpCfg.MAX_SNMP];
DatagramPacket packet = new DatagramPacket(buff, SnmpCfg.MAX_SNMP);
socket.receive(packet);
return packet.getData();
}
}
public class Req{
public byte type;
public int status, index;
public Vb[] vb;
public Req(byte t, int n){
type = t;
vb = new Vb[n];
status = 0;
index = 0;
}
public Req(){
vb = null;
}
}
public class SnmpCfg{
public static byte INTEGER = (byte) 0x02;
public static byte OCTETSTRING = (byte) 0x04;
public static byte NULL = (byte) 0x05;
public static byte OBJECTIDENTIFIER = (byte) 0x06;
public static byte SEQUENCE = (byte) 0x30;
public static byte SEQUENCEOF = (byte) 0x30;
public static byte IPADDRESS = (byte) 0x40;
public static byte COUNTER = (byte) 0x41;
public static byte TIMETICKS = (byte) 0x43;
public static byte GAUGE = (byte) 0x42;
public static byte GET = (byte) 0xa0;
public static byte GETNEXT = (byte) 0xa1;
public static byte GETRESPONSE = (byte) 0xa2;
public static byte SET = (byte) 0xa3;
public static byte TRAP = (byte) 0xa4;
//SNMP version
public static int SNMP_VER = (byte) 0;
//Maximum length of a SNMP message
public static int MAX_SNMP = 1024;
//Maximum length of the Value part of the TLV structure
public static int MAX_UNIT = 128;
//Maximum number of variable bindings that can be retrieved in one request
public static int MAX_VB = 4;
//Sleeping period (ms) if data not available
public static int SLEEP = 5000;
public static boolean isInt(byte type){
if(type == INTEGER) return true;
return false;
}
public static boolean isLong(byte type){
if(type == COUNTER || type == GAUGE || type == TIMETICKS) return true;
return false;
}
public static boolean isStr(byte type){
if(type == OCTETSTRING || type == OBJECTIDENTIFIER || type == IPADDRESS)
return true;
return false;
}
public static boolean isScalar(byte type){
if(isInt(type) || isStr(type) || isLong(type) || type == NULL) return true;
return false;
}
}

