1 20 21 22 23 package org.snmp4j.smi; 24 25 import java.util.StringTokenizer ; 26 import java.io.IOException ; 27 import org.snmp4j.asn1.BERInputStream; 28 import java.io.OutputStream ; 29 import java.net.InetAddress ; 30 import java.net.UnknownHostException ; 31 import org.snmp4j.log.LogFactory; 32 import org.snmp4j.log.LogAdapter; 33 34 41 public abstract class TransportIpAddress extends IpAddress { 42 43 private static final LogAdapter logger = 44 LogFactory.getLogger(TransportIpAddress.class); 45 46 static final long serialVersionUID = 695596530250216972L; 47 48 protected int port = 0; 49 50 public int getPort() { 51 return port; 52 } 53 54 public void setPort(int port) { 55 if ((port < 0) || (port > 65535)) { 56 throw new IllegalArgumentException ("Illegal port specified: " + port); 57 } 58 this.port = port; 59 } 60 61 public boolean isValid() { 62 return super.isValid() && (port >= 0) && (port <= 65535); 63 } 64 65 public int compareTo(Object o) { 66 int result = super.compareTo(o); 67 if (result == 0) { 68 return (port - ((TransportIpAddress) o).getPort()); 69 } 70 return result; 71 } 72 73 public boolean equals(Object o) { 74 if (o instanceof TransportIpAddress) { 75 return (super.equals(o) && ((TransportIpAddress)o).getPort() == port); 76 } 77 return false; 78 } 79 80 public boolean parseAddress(String address) { 81 try { 82 StringTokenizer st = new StringTokenizer (address, "/"); 83 String addr = st.nextToken(); 84 String port = st.nextToken(); 85 if (super.parseAddress(addr)) { 86 this.port = Integer.parseInt(port); 87 return true; 88 } 89 return false; 90 } 91 catch (Exception ex) { 92 return false; 93 } 94 } 95 96 public static Address parse(String address) { 97 try { 98 UdpAddress a = new UdpAddress(); 99 if (a.parseAddress(address)) { 100 return a; 101 } 102 } 103 catch (Exception ex) { 104 } 105 return null; 106 } 107 108 public String toString() { 109 return super.toString()+"/"+port; 110 } 111 112 public int hashCode() { 113 return super.hashCode()^2 + port; 114 } 115 116 125 public void setTransportAddress(OctetString transportAddress) throws 126 UnknownHostException { 127 OctetString inetAddr = 128 transportAddress.substring(0, transportAddress.length()-2); 129 setInetAddress(InetAddress.getByAddress(inetAddr.getValue())); 130 port = ((transportAddress.get(transportAddress.length()-2) & 0xFF) << 8); 131 port += (transportAddress.get(transportAddress.length()-1) & 0xFF); 132 } 133 134 141 public byte[] getValue() { 142 byte[] addr = getInetAddress().getAddress(); 143 byte[] retval = new byte[addr.length+2]; 144 System.arraycopy(addr, 0, retval, 0, addr.length); 145 retval[addr.length] = (byte)((port >> 8) & 0xFF); 146 retval[addr.length+1] = (byte)(port & 0xFF); 147 return retval; 148 } 149 150 public void decodeBER(BERInputStream inputStream) throws IOException { 151 OctetString os = new OctetString(); 152 os.decodeBER(inputStream); 153 try { 154 setTransportAddress(os); 155 } 156 catch (Exception ex) { 157 String txt = "Wrong encoding of TransportAddress"; 158 logger.error(txt); 159 throw new IOException (txt+": "+ex.getMessage()); 160 } 161 } 162 163 public void encodeBER(OutputStream outputStream) throws IOException { 164 OctetString os = new OctetString(getValue()); 165 os.encodeBER(outputStream); 166 } 167 168 public int getBERLength() { 169 return getInetAddress().getAddress().length + 2; 170 } 171 172 public int getBERPayloadLength() { 173 return getBERLength(); 174 } 175 176 public int getSyntax() { 177 return SMIConstants.SYNTAX_OCTET_STRING; 178 } 179 180 } 181 | Popular Tags |