[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip SNMP.java,1.5,1.6
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2005-12-09 23:30:48
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11743/core/protocolsuite/tcp_ip Modified Files: SNMP.java Log Message: Index: SNMP.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/SNMP.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SNMP.java 8 Dec 2005 16:45:10 -0000 1.5 --- SNMP.java 9 Dec 2005 23:30:33 -0000 1.6 *************** *** 47,53 **** public class SNMP extends Application{ ! private static int SNMPID = 0; ! private static Vector<SNMPInstance> SNMPgroups; public SNMP(ProtocolStack inParentStack, int listenPort, int appType, int UID) { --- 47,74 ---- public class SNMP extends Application{ ! ! public final static int SNMP_ERROR_DPI_noError = 0; ! public final static int SNMP_ERROR_DPI_notFound = 102; ! public final static int SNMP_GET = 1; ! public final static int SNMP_GETNEXT = 2; ! public final static int SNMP_SET = 3; ! public final static int SNMP_TRAP = 4; ! public final static int SNMP_RESPONSE = 5; private static int SNMPID = 0; ! private Vector<SNMPInstance> SNMPgroups; ! //following variables be actual temporality ! private int m_len=0; ! private int m_majorver=0; ! private int m_minorver=0; ! private int m_release=0; ! private int m_id=0; ! private int m_type=0; ! private int m_errorstatus=0; ! private int m_errorindex=0; ! private int m_generictrap=0; ! private int m_specifictrap=0; ! private String m_password=""; ! private Vector<String> m_vars = new Vector<String>(0); ! private Vector<String> m_values = new Vector<String>(0); public SNMP(ProtocolStack inParentStack, int listenPort, int appType, int UID) { *************** *** 67,71 **** * This method start to listen on application port * @author QweR - * @return Nothing. * @version v0.01 */ --- 88,91 ---- *************** *** 82,86 **** * This method stop listening on application port * @author QweR - * @return Nothing. * @version v0.01 */ --- 102,105 ---- *************** *** 129,133 **** */ public void RecvData(String Data) throws LowLinkException, TransportLayerException { ! } --- 148,190 ---- */ public void RecvData(String Data) throws LowLinkException, TransportLayerException { ! try { ! if(parse(Data)==0) { ! updateValues(); ! switch(m_type) { ! case SNMP_GET: { ! if(appType==0) { ! getResponse(m_vars, m_values, m_errorstatus, m_errorindex); ! } ! break; ! } ! case SNMP_GETNEXT: { ! if(appType==0) { ! // some actions ! getResponse(m_vars, m_values, m_errorstatus, m_errorindex); ! } ! break; ! } ! case SNMP_SET: { ! if(appType==0) { ! getResponse(m_vars, m_values, m_errorstatus, m_errorindex); ! } ! break; ! } ! case SNMP_TRAP: { ! ! break; ! } ! case SNMP_RESPONSE: { ! ! break; ! } ! default: break; ! } ! } ! else printInfo("invalid(corrupted?) packet!!!"); ! } ! catch (CommunicationException e) { ! //print anything...? ! } } *************** *** 135,144 **** * This method realize Get-request function of SNMPv2. * @param r vector of String. String as 'group.instance' * @author QweR * @version v0.01 */ ! public void GetRequest(Vector<String> r) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = CreateSNMPHeader(getNextID(),0); ! SendData(pack); } --- 192,208 ---- * This method realize Get-request function of SNMPv2. * @param r vector of String. String as 'group.instance' + * @return false if one or more variables not exist * @author QweR * @version v0.01 */ ! public boolean getRequest(Vector<String> r,String password) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = createSNMPHeader(getNextID(),SNMP_GET); ! pack += createSNMPPassword(password); ! String tmp = createSNMPVars(r); ! if(tmp.compareTo("")==0) { ! return false; ! } ! SendData(pack+tmp); ! return true; } *************** *** 146,155 **** * This method realize Get-next-request function of SNMPv2. * @param r vector of String. String as 'group.instance' * @author QweR * @version v0.01 */ ! public void GetNextRequest(Vector<String> r) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = CreateSNMPHeader(getNextID(),1); ! SendData(pack); } --- 210,226 ---- * This method realize Get-next-request function of SNMPv2. * @param r vector of String. String as 'group.instance' + * @return false if one or more variables not exist * @author QweR * @version v0.01 */ ! public boolean getNextRequest(Vector<String> r,String password) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = createSNMPHeader(getNextID(),SNMP_GETNEXT); ! pack += createSNMPPassword(password); ! String tmp = createSNMPVars(r); ! if(tmp.compareTo("")==0) { ! return false; ! } ! SendData(pack+tmp); ! return true; } *************** *** 158,167 **** * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' * @author QweR * @version v0.01 */ ! public void SetRequest(Vector<String> r,Vector<String> v) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = CreateSNMPHeader(getNextID(),2); ! SendData(pack); } --- 229,245 ---- * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' + * @return false if one or more variables not exist * @author QweR * @version v0.01 */ ! public boolean setRequest(Vector<String> r,Vector<String> v,String password) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = createSNMPHeader(getNextID(),SNMP_SET); ! pack += createSNMPPassword(password); ! String tmp = createSNMPVarsValues(r,v); ! if(tmp.compareTo("")==0) { ! return false; ! } ! SendData(pack+tmp); ! return true; } *************** *** 170,179 **** * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' * @author QweR * @version v0.01 */ ! public void GetResponse(Vector<String> r,Vector<String> v) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = CreateSNMPHeader(getNextID(),3); ! SendData(pack); } --- 248,264 ---- * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' + * @return false if one or more variables not exist * @author QweR * @version v0.01 */ ! public boolean getResponse(Vector<String> r,Vector<String> v,int es,int ei) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = createSNMPHeader(getNextID(),SNMP_RESPONSE); ! pack += createSNMPError(es,ei); ! String tmp = createSNMPVarsValues(r,v); ! if(tmp.compareTo("")==0) { ! return false; ! } ! SendData(pack+tmp); ! return true; } *************** *** 182,199 **** * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' * @author QweR * @version v0.01 */ ! public void Trap(Vector<String> r,Vector<String> v) throws LowLinkException, TransportLayerException, CommunicationException { } /** ! * This method create a SNMP header for all type packets. ! * @param Data to recv * @author QweR * @version v0.01 */ ! protected static String CreateSNMPHeader(int id,int type) { char[] c = new char[8]; --- 267,443 ---- * @param r vector of String. String as 'group.instance' * @param v vector of String. String as 'value' + * @return false if one or more variables not exist * @author QweR * @version v0.01 */ ! public boolean trap(Vector<String> r,Vector<String> v,int gtc,int stc) throws LowLinkException, TransportLayerException, CommunicationException { ! String pack = createSNMPHeader(getNextID(),SNMP_TRAP); ! pack += createSNMPTrap(gtc,stc); ! String tmp = createSNMPVarsValues(r,v); ! if(tmp.compareTo("")==0) { ! return false; ! } ! SendData(pack+tmp); ! return true; ! } ! ! /** ! * This method parse incoming packet. ! * @param in incoming packet ! * @return result of parsing: ! * if 0 then successful pasre ! * if 1 then error in main header ! * if 2 then error in password block ! * if 3 then error in error block ! * if 4 then error in trap block ! * if 5 then error in variables block ! * if 6 then error in values block ! * @author QweR ! * @version v0.01 ! */ ! public int parse(String in) { ! String out=""; + if(parseSNMPHeader(in,out)) { + in=out; + switch(m_type) { + case SNMP_GET: { + if(parseSNMPPassword(in, out)) { + if(parseSNMPVars(out)) { + //printInfo("recieved GET"); + } + else return 5; + } + else return 2; + break; + } + case SNMP_GETNEXT: { + if(parseSNMPPassword(in, out)) { + if(parseSNMPVars(out)) { + //printInfo("recieved GETNEXT"); + } + else return 5; + } + else return 2; + break; + } + case SNMP_SET: { + if(parseSNMPPassword(in, out)) { + if(parseSNMPVarsValues(out)) { + //printInfo("recieved SET"); + } + else return 6; + } + else return 2; + break; + } + case SNMP_TRAP: { + if(parseSNMPTrap(in, out)) { + if(parseSNMPVars(out)) { + //printInfo("recieved GET."); + } + else return 5; + } + else return 4; + break; + } + case SNMP_RESPONSE: { + if(parseSNMPError(in, out)) { + if(parseSNMPVarsValues(out)) { + //printInfo("recieved RESPONSE"); + } + else return 6; + } + else return 3; + break; + } + default: return 1; + } + } + else return 1; + return 0; } /** ! * This method update SNMP values. * @author QweR * @version v0.01 */ ! protected void updateValues() { ! ! } ! ! /** ! * This method gets values from vector of 'group.instance'. ! * @param r vector of String. String as 'group.instance' ! * @param v vector of String. String as 'value' ! * return if result equals zero then function successful complete ! * if result == 1 then invalid variable name (without dots). ! * if result == 2 then not found groupID. ! * if result == 3 then not found instenceID ! * @author QweR ! * @version v0.01 ! */ ! protected int getValues(Vector<String> r,Vector<String> v) { ! v = new Vector<String>(0); ! String[] a; ! int i,g,h; ! ! for(i=0;i<r.size();i++) { ! a = r.get(i).split("."); ! if(a.length>=2) { ! if((g=getGroupID(a[0]))!=-1) { ! if((h=SNMPgroups.get(g).getInstanceID(a[1]))!=-1) { ! v.add(SNMPgroups.get(g).value.get(h)); ! } ! else return 3; ! } ! else return 2; ! } ! else return 1; ! } ! return 0; ! } ! ! /** ! * This method sets values from vector of 'group.instance' and 'value'. ! * @param r vector of String. String as 'group.instance' ! * @param v vector of String. String as 'value' ! * return if result equals zero then function successful complete ! * if result == 1 then invalid variable name (without dots) ! * if result == 2 then not found groupID ! * if result == 3 then not found instenceID ! * @author QweR ! * @version v0.01 ! */ ! protected int setValues(Vector<String> r,Vector<String> v) { ! String[] a; ! int i,g,h; ! ! for(i=0;i<r.size();i++) { ! a = r.get(i).split("."); ! if(a.length>=2) { ! if((g=getGroupID(a[0]))!=-1) { ! if((h=SNMPgroups.get(g).getInstanceID(a[1]))!=-1) { ! SNMPgroups.get(g).value.set(h,v.get(i)); ! } ! else return 3; ! } ! else return 2; ! } ! else return 1; ! } ! return 0; ! } ! ! /** ! * This method create SNMP header for all type packets. ! * @param id identifier of message ! * @param type type of message ! * @return string SNMP header ! * @author QweR ! * @version v0.01 ! */ ! protected String createSNMPHeader(int id,int type) { char[] c = new char[8]; *************** *** 209,249 **** /** ! * This method recieves data from the other side. ! * @param Data to recv * @author QweR * @version v0.01 */ ! //protected static String CreateSNMPVars(Vector<String> r) { ! //} /** ! * This method recieves data from the other side. ! * @param Data to recv * @author QweR * @version v0.01 */ ! //protected static String CreateSNMPVarsValue(Vector<String> r,Vector<String> v) { ! //} /** ! * This method parse incomimg packet and return readable data. ! * @param Data to recv * @author QweR * @version v0.01 */ ! protected static boolean ParseSNMPMessage(int ver,String pass,int type,int id,int er,int erindex,Vector<String> r) { return true; } /** * This method gets next value of SNMPID modulo 2^16. - * @param Data to recv * @author QweR * @version v0.01 */ ! protected static int getNextID() { if(SNMPID>=65535) SNMPID=0; else SNMPID++; --- 453,714 ---- /** ! * This method create SNMP password header. ! * @param pass password ! * @return string SNMP password header * @author QweR * @version v0.01 */ ! protected String createSNMPPassword(String pass) { ! char[] c = new char[2]; ! c[0]=(char)(pass.length()/256); ! c[1]=(char)(pass.length()%256); ! return (String.copyValueOf(c)+pass); ! } /** ! * This method create SNMP array of group+instance. ! * @param r vector of variables ! * @return string SNMP variables * @author QweR * @version v0.01 */ ! protected String createSNMPVars(Vector<String> r) { ! int i,g,h; ! String[] gi; ! String out = null; ! char[] c = new char[4]; ! for(i=0;i<r.size();i++) { ! gi = r.get(i).split("."); ! ! if((g=getGroupID(gi[0]))!=-1) { ! if((h=SNMPgroups.get(g).getInstanceID(gi[1]))!=-1) { ! c[0]=(char) g; ! c[1]=0; ! c[2]=(char) h; ! c[3]=0; ! out+=String.copyValueOf(c); ! } ! else return ""; ! } ! else return ""; ! } ! return out; ! } /** ! * This method create SNMP array of group+instance+value. ! * @param r vector of variables ! * @param v vector of values conform variables ! * @return string SNMP variables with values * @author QweR * @version v0.01 */ ! protected String createSNMPVarsValues(Vector<String> r,Vector<String> v) { ! int i,g,h; ! String[] gi; ! String out = null; ! char[] c = new char[4]; ! char[] cv = null; + if(r.size()!=v.size()) return ""; + for(i=0;i<r.size();i++) { + gi = r.get(i).split("."); + + if((g=getGroupID(gi[0]))!=-1) { + if((h=SNMPgroups.get(g).getInstanceID(gi[1]))!=-1) { + c[0]=(char) g; + c[1]=0; + c[2]=(char) h; + c[3]=0; + cv = new char[3]; + cv[0]=2; + cv[1]=(char)(v.get(i).length()/256); + cv[2]=(char)(v.get(i).length()%256); + out+=String.copyValueOf(c)+String.copyValueOf(cv)+v.get(i); + } + else return ""; + } + else return ""; + } + return out; + } + + /** + * This method create SNMP error body for packet. + * @param es error status + * @param ei error index + * @return string SNMP error body + * @author QweR + * @version v0.01 + */ + protected String createSNMPError(int es,int ei) { + char[] c = new char[7]; + + c[0]=(char) es; + c[1]=(char)(ei/16777216); + c[2]=(char)((ei%16777216)/65536); + c[3]=(char)((ei%65536)/256); + c[4]=(char)(ei%256); + c[5]=0; + c[6]=0; + return (String.copyValueOf(c)); + } + + /** + * This method create SNMP trap body for packet. + * @param g generic trap code + * @param s specific trap code + * @return string SNMP trap body + * @author QweR + * @version v0.01 + */ + protected String createSNMPTrap(int g,int s) { + char[] c = new char[6]; + + c[0]=(char)(g/16777216); + c[1]=(char)((g%16777216)/65536); + c[2]=(char)((g%65536)/256); + c[3]=(char)(g%256); + c[4]=(char)(s/256); + c[5]=(char)(s%256); + return (String.copyValueOf(c)); + } + + /** + * This method parse a SNMP header to normal data. + * @param in packet + * @param last last piece of packet + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPHeader(String in,String last) { + m_len=in.charAt(0)*256+in.charAt(1); + m_majorver=in.charAt(2); + m_minorver=in.charAt(3); + m_release=in.charAt(4); + m_id=in.charAt(5)*256+in.charAt(6); + m_type=in.charAt(7); + last=in.substring(8); + return (m_len==in.length()-2); + } + + /** + * This method parse SNMP password header to Password. + * @param in packet, incipient from password + * @param last last piece of packet + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPPassword(String in,String last) { + int l; + l=in.charAt(0)*256+in.charAt(1); + if(l+2<in.length()) { + m_password=in.substring(2, l+2); + last=in.substring(l+2); + return true; + } + return false; + } + + /** + * This method parse SNMP body to array of group+instance. + * @param r packet body + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPVars(String r) { + char[] c = r.toCharArray(); + int i; + + m_vars.clear(); + for(i=0;i<c.length;i+=4) { + if(c[i+1]==0 && c[i+3]==0 && c[i]<SNMPgroups.size() && c[i+2]<SNMPgroups.get(c[i]).instance.size()) { + m_vars.add(SNMPgroups.get(c[i])+"."+SNMPgroups.get(c[i]).instance.get(c[i+2])); + } + else { + m_errorstatus=SNMP_ERROR_DPI_notFound; + m_errorindex=m_vars.size(); + return false; + } + } + return true; + } + + /** + * This method parse SNMP body to array of group+instance+value. + * @param in packet body + * @param v (output data) vector of values conform returned variables + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPVarsValues(String in) { + char[] c = in.toCharArray(); + int i,len; + + m_vars.clear(); + m_values.clear(); + for(i=0;i<c.length;i+=7) { + if(c[i+1]==0 && c[i+3]==0 && c[i]<SNMPgroups.size() && c[i+2]<SNMPgroups.get(c[i]).instance.size() && c[i+4]==2) { + len = c[i+5]*256+c[i+6]; + m_vars.add(SNMPgroups.get(c[i])+"."+SNMPgroups.get(c[i]).instance.get(c[i+2])); + m_values.add(String.copyValueOf(c, i+7, len)); + } + else { + m_errorstatus=SNMP_ERROR_DPI_notFound; + m_errorindex=m_vars.size(); + return false; + } + } return true; } /** + * This method parse SNMP error body of packet to normal data. + * @param in error header + * @param last last piece of packet + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPError(String in,String last) { + if(in.length()>7) { + m_errorstatus=in.charAt(0); + m_errorindex=((in.charAt(1)*256+in.charAt(2))*256+in.charAt(3))*256+in.charAt(4); + last = in.substring(7); + return true; + } + return false; + } + + /** + * This method parse SNMP trap body of packet to normal data. + * @param in trap header + * @param last last piece of packet + * @return result of parsing: if false then error + * @author QweR + * @version v0.01 + */ + protected boolean parseSNMPTrap(String in,String last) { + if(in.length()>6) { + m_generictrap=((in.charAt(0)*256+in.charAt(1))*256+in.charAt(2))*256+in.charAt(3); + m_specifictrap=in.charAt(4)+in.charAt(5); + last = in.substring(6); + return true; + } + return false; + } + + /** * This method gets next value of SNMPID modulo 2^16. * @author QweR * @version v0.01 */ ! protected int getNextID() { if(SNMPID>=65535) SNMPID=0; else SNMPID++; *************** *** 251,255 **** } ! public static int getGroupID(String n) { int i; --- 716,726 ---- } ! /** ! * This method return index by string name of variable. ! * @param n name of variable ! * @author QweR ! * @version v0.01 ! */ ! public int getGroupID(String n) { int i; *************** *** 261,264 **** --- 732,744 ---- } + protected void printInfo(String s) { + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("SNMP Protocol Data"); + protInfo.setLayer("Application"); + protInfo.setDescription(s); + Simulation.addLayerInfo(protInfo); + } + public class SNMPInstance { public String name; *************** *** 270,273 **** --- 750,759 ---- } + /** + * This method return index by string name of variable. + * @param n name of variable + * @author QweR + * @version v0.01 + */ public int getInstanceID(String n) { int i; *************** *** 280,283 **** --- 766,776 ---- } + /** + * This method add variable and variable value to my MIB. + * @param i instance of variables + * @param v value of varialbes + * @author QweR + * @version v0.01 + */ public void add(String i,String v) { instance.add(i); |