001: /*_############################################################################
002: _##
003: _## SNMP4J-Agent - TDomainAddressFactoryImpl.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.agent.mo.snmp;
022:
023: import org.snmp4j.smi.OID;
024: import java.net.Inet4Address;
025: import org.snmp4j.smi.UdpAddress;
026: import java.net.Inet6Address;
027: import org.snmp4j.smi.OctetString;
028: import org.snmp4j.smi.Address;
029: import org.snmp4j.smi.TransportIpAddress;
030: import org.snmp4j.smi.TcpAddress;
031: import org.snmp4j.log.LogFactory;
032: import org.snmp4j.log.LogAdapter;
033:
034: public class TDomainAddressFactoryImpl implements TDomainAddressFactory {
035:
036: private static final LogAdapter logger = LogFactory
037: .getLogger(TDomainAddressFactoryImpl.class);
038:
039: public TDomainAddressFactoryImpl() {
040: }
041:
042: public Address createAddress(OID transportDomain,
043: OctetString address) {
044: if (TransportDomains.snmpUDPDomain.equals(transportDomain)
045: || TransportDomains.transportDomainUdpIpv4
046: .equals(transportDomain)
047: || TransportDomains.transportDomainTcpIpv4
048: .equals(transportDomain)
049: || TransportDomains.transportDomainUdpIpv6
050: .equals(transportDomain)
051: || TransportDomains.transportDomainTcpIpv6
052: .equals(transportDomain)) {
053: TransportIpAddress transportIpAddress;
054: if (TransportDomains.transportDomainTcpIpv4
055: .equals(transportDomain)
056: || TransportDomains.transportDomainTcpIpv6
057: .equals(transportDomain)) {
058: transportIpAddress = new TcpAddress();
059: } else {
060: transportIpAddress = new UdpAddress();
061: }
062: try {
063: transportIpAddress.setTransportAddress(address);
064: } catch (Exception ex) {
065: logger.debug("Invalid TransportAddress format '"
066: + address + "' for domain " + transportDomain);
067: return null;
068: }
069: return transportIpAddress;
070: }
071: return null;
072: }
073:
074: public boolean isValidAddress(OID transportDomain,
075: OctetString address) {
076: try {
077: Address addr = createAddress(transportDomain, address);
078: if (addr != null) {
079: return true;
080: }
081: } catch (Exception ex) {
082: logger.debug("Address is not valid TDomain address: "
083: + address + "; details: " + ex.getMessage());
084: }
085: return false;
086: }
087:
088: public OID getTransportDomain(Address address) {
089: if (address instanceof TransportIpAddress) {
090: TransportIpAddress tipaddr = (TransportIpAddress) address;
091: if (tipaddr.getInetAddress() instanceof Inet4Address) {
092: if (address instanceof UdpAddress) {
093: return TransportDomains.transportDomainUdpIpv4;
094: } else if (address instanceof TcpAddress) {
095: return TransportDomains.transportDomainTcpIpv4;
096: }
097: } else if (tipaddr.getInetAddress() instanceof Inet6Address) {
098: if (address instanceof UdpAddress) {
099: return TransportDomains.transportDomainUdpIpv6;
100: } else if (address instanceof TcpAddress) {
101: return TransportDomains.transportDomainTcpIpv6;
102: }
103: }
104: }
105: return null;
106: }
107:
108: public OctetString getAddress(Address address) {
109: if (address instanceof TransportIpAddress) {
110: TransportIpAddress tipaddr = (TransportIpAddress) address;
111: byte[] addrBytes = tipaddr.getInetAddress().getAddress();
112: OctetString addr = new OctetString(addrBytes);
113: addr.append((byte) (tipaddr.getPort() >> 16));
114: addr.append((byte) (tipaddr.getPort() & 0xFF));
115: return addr;
116: }
117: return null;
118: }
119:
120: }
|