package org.snmp4j.agent.mo.snmp;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Session;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.agent.NotificationOriginator;
import org.snmp4j.agent.mo.DefaultMOMutableTableModel;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.security.VACM;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TimeTicks;
import org.snmp4j.smi.VariableBinding;

/* JADX INFO: loaded from: classes.dex */
public class NotificationOriginatorImpl implements NotificationOriginator {
    private SnmpCommunityMIB communityMIB;
    private long notificationEventID;
    private transient Vector<NotificationLogListener> notificationLogListeners;
    private SnmpNotificationMIB notificationMIB;
    private Session session;
    private SysUpTime sysUpTime;
    private SnmpTargetMIB targetMIB;
    private VACM vacm;
    private static final LogAdapter logger = LogFactory.getLogger(NotificationOriginatorImpl.class);
    private static OctetString EMPTY_CONTEXT_ENGINE_ID = new OctetString();

    public NotificationOriginatorImpl(Session session, VACM vacm, SysUpTime sysUpTime, SnmpTargetMIB snmpTargetMIB, SnmpNotificationMIB snmpNotificationMIB) {
        this.notificationEventID = 0L;
        this.session = session;
        this.sysUpTime = sysUpTime;
        this.vacm = vacm;
        this.targetMIB = snmpTargetMIB;
        this.notificationMIB = snmpNotificationMIB;
    }

    private boolean isAccessGranted(MOTableRow mOTableRow, MOTableRow mOTableRow2, OctetString octetString, OID oid, VariableBinding[] variableBindingArr) {
        if (!this.notificationMIB.passesFilter(mOTableRow2.getIndex(), oid, variableBindingArr)) {
            if (logger.isInfoEnabled()) {
                logger.info("Notification " + oid + " did not pass filter " + mOTableRow2.getIndex());
            }
            return false;
        }
        OctetString octetString2 = (OctetString) mOTableRow2.getValue(2);
        Integer32 integer32 = (Integer32) mOTableRow2.getValue(3);
        Integer32 integer322 = (Integer32) mOTableRow2.getValue(1);
        int iIsAccessAllowed = this.vacm.isAccessAllowed(octetString, octetString2, integer322.getValue(), integer32.getValue(), 0, oid);
        for (int i2 = 0; iIsAccessAllowed == 0 && i2 < variableBindingArr.length; i2++) {
            iIsAccessAllowed = this.vacm.isAccessAllowed(octetString, octetString2, integer322.getValue(), integer32.getValue(), 0, variableBindingArr[i2].getOid());
        }
        return iIsAccessAllowed == 0;
    }

    private ResponseEvent sendNotification(MOTableRow mOTableRow, MOTableRow mOTableRow2, OctetString octetString, OID oid, TimeTicks timeTicks, VariableBinding[] variableBindingArr, int i2, long j2) {
        Address address;
        Target communityTarget;
        PDU pdu;
        TransportMapping transport;
        Integer32 integer32 = (Integer32) mOTableRow2.getValue(0);
        OctetString octetString2 = (OctetString) mOTableRow2.getValue(2);
        Integer32 integer322 = (Integer32) mOTableRow2.getValue(3);
        Address address2 = ((SnmpTargetMIB.SnmpTargetAddrEntryRow) mOTableRow).getAddress();
        try {
            if (octetString2 == null) {
                throw new IllegalArgumentException("Security name must not be null");
            }
            OctetString community = this.communityMIB != null ? this.communityMIB.getCommunity(octetString2, null, octetString) : octetString2;
            if (address2 == null) {
                String str = "Invalid address in row " + mOTableRow + ", notification skipped";
                logger.debug(str);
                throw new IllegalArgumentException(str);
            }
            if ((integer32.getValue() == 0 || integer32.getValue() == 1) && community == null) {
                throw new IllegalArgumentException("Security name '" + octetString2 + "' is not mapped to any community, notification not sent (RFC 3584 §5.2.3)");
            }
            int value = integer32.getValue();
            if (value == 0) {
                CommunityTarget communityTarget2 = new CommunityTarget(address2, community);
                PDUv1 pDUv1 = new PDUv1();
                if (timeTicks != null) {
                    address = address2;
                    pDUv1.setTimestamp(timeTicks.getValue());
                } else {
                    address = address2;
                    pDUv1.setTimestamp(this.sysUpTime.get().getValue());
                }
                int genericTrapID = SnmpConstants.getGenericTrapID(oid);
                if (genericTrapID < 0) {
                    pDUv1.setGenericTrap(6);
                    if (oid.size() <= 2 || oid.get(oid.size() - 2) != 0) {
                        pDUv1.setEnterprise(new OID(oid.getValue(), 0, oid.size() - 1));
                    } else {
                        pDUv1.setEnterprise(new OID(oid.getValue(), 0, oid.size() - 2));
                    }
                    pDUv1.setSpecificTrap(oid.last());
                } else {
                    pDUv1.setGenericTrap(genericTrapID);
                    pDUv1.setEnterprise(new OID(new int[]{0, 0}));
                }
                Session session = this.session;
                if ((session instanceof Snmp) && (transport = ((Snmp) session).getMessageDispatcher().getTransport(address)) != null && (transport.getListenAddress() instanceof IpAddress)) {
                    InetAddress inetAddress = ((IpAddress) transport.getListenAddress()).getInetAddress();
                    if (inetAddress == null) {
                        pDUv1.setAgentAddress(new IpAddress("0.0.0.0"));
                    } else {
                        pDUv1.setAgentAddress(new IpAddress(inetAddress));
                    }
                }
                communityTarget = communityTarget2;
                pdu = pDUv1;
            } else if (value != 1) {
                UserTarget userTarget = new UserTarget(address2, octetString2, i2 == 2 ? new byte[0] : this.targetMIB.getLocalEngineID(), integer322.getValue());
                ScopedPDU scopedPDU = new ScopedPDU();
                scopedPDU.setContextName(octetString);
                setContextEngineID(scopedPDU, octetString, oid);
                pdu = scopedPDU;
                communityTarget = userTarget;
            } else {
                communityTarget = new CommunityTarget(address2, community);
                pdu = new PDU();
            }
            communityTarget.setVersion(integer32.getValue());
            Integer32 integer323 = (Integer32) mOTableRow.getValue(2);
            Integer32 integer324 = (Integer32) mOTableRow.getValue(3);
            communityTarget.setTimeout(integer323.getValue() * 10);
            communityTarget.setRetries(integer324.getValue());
            if (integer32.getValue() != 0) {
                if (timeTicks != null) {
                    pdu.add(new VariableBinding(SnmpConstants.sysUpTime, timeTicks));
                } else {
                    pdu.add(new VariableBinding(SnmpConstants.sysUpTime, this.sysUpTime.get()));
                }
                pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
            }
            pdu.addAll(variableBindingArr);
            pdu.setType(i2 == 2 ? -90 : integer32.getValue() == 0 ? -92 : -89);
            try {
                OctetString octetString3 = new OctetString();
                OctetString octetString4 = new OctetString();
                if (pdu instanceof ScopedPDU) {
                    octetString3.setValue(this.targetMIB.getLocalEngineID());
                    octetString4 = ((ScopedPDU) pdu).getContextEngineID();
                }
                ResponseEvent responseEventSend = this.session.send(pdu, communityTarget);
                fireNotificationLogEvent(new NotificationLogEvent(this, octetString3, communityTarget, octetString4, octetString, oid, timeTicks, variableBindingArr, j2, true));
                logger.info("Sent notification with ID " + j2 + " " + pdu + " to " + communityTarget);
                return responseEventSend;
            } catch (IOException e2) {
                logger.error("Failed to send notification: " + e2.getMessage(), e2);
                return null;
            }
        } catch (IllegalArgumentException e3) {
            logger.warn("Notification not sent due to configuration error: " + e3.getMessage());
            return null;
        }
    }

    public synchronized void addNotificationLogListener(NotificationLogListener notificationLogListener) {
        if (this.notificationLogListeners == null) {
            this.notificationLogListeners = new Vector<>(2);
        }
        this.notificationLogListeners.add(notificationLogListener);
    }

    protected void fireNotificationLogEvent(NotificationLogEvent notificationLogEvent) {
        Vector<NotificationLogListener> vector = this.notificationLogListeners;
        if (vector != null) {
            int size = vector.size();
            for (int i2 = 0; i2 < size; i2++) {
                vector.get(i2).notificationLogEvent(notificationLogEvent);
            }
        }
    }

    @Override // org.snmp4j.agent.NotificationOriginator
    public Object notify(OctetString octetString, OID oid, VariableBinding[] variableBindingArr) {
        return notify(octetString, oid, null, variableBindingArr);
    }

    public synchronized void removeNotificationLogListener(NotificationLogListener notificationLogListener) {
        if (this.notificationLogListeners != null) {
            this.notificationLogListeners.remove(notificationLogListener);
        }
    }

    protected void setContextEngineID(ScopedPDU scopedPDU, OctetString octetString, OID oid) {
        scopedPDU.setContextEngineID(new OctetString(this.targetMIB.getLocalEngineID()));
    }

    public void setSession(Session session) {
        this.session = session;
    }

    @Override // org.snmp4j.agent.NotificationOriginator
    public Object notify(OctetString octetString, OID oid, TimeTicks timeTicks, VariableBinding[] variableBindingArr) {
        OctetString octetString2 = octetString;
        if (logger.isInfoEnabled()) {
            logger.info("Notification " + oid + " reported with " + Arrays.asList(variableBindingArr) + " for context " + octetString2);
        }
        if (octetString2 == null) {
            octetString2 = new OctetString();
        }
        LinkedList linkedList = new LinkedList();
        synchronized (this) {
            this.notificationEventID++;
        }
        OctetString octetString3 = null;
        SnmpTargetMIB snmpTargetMIB = this.targetMIB;
        if (snmpTargetMIB != null && snmpTargetMIB.getLocalEngineID() != null) {
            octetString3 = new OctetString(this.targetMIB.getLocalEngineID());
        }
        fireNotificationLogEvent(new NotificationLogEvent(this, octetString3, null, EMPTY_CONTEXT_ENGINE_ID, octetString2, oid, timeTicks, variableBindingArr, this.notificationEventID, false));
        for (MOTableRow mOTableRow : (DefaultMOMutableTableModel) this.notificationMIB.getNotifyTable().getModel()) {
            OctetString octetString4 = (OctetString) mOTableRow.getValue(0);
            Integer32 integer32 = (Integer32) mOTableRow.getValue(1);
            Collection<SnmpTargetMIB.SnmpTargetAddrEntryRow> targetAddrRowsForTag = this.targetMIB.getTargetAddrRowsForTag(octetString4);
            RowStatus.ActiveRowsFilter activeRowsFilter = new RowStatus.ActiveRowsFilter(7);
            for (SnmpTargetMIB.SnmpTargetAddrEntryRow snmpTargetAddrEntryRow : targetAddrRowsForTag) {
                if (activeRowsFilter.passesFilter(snmpTargetAddrEntryRow)) {
                    OctetString octetString5 = (OctetString) snmpTargetAddrEntryRow.getValue(5);
                    MOTableRow targetParamsRow = this.targetMIB.getTargetParamsRow(octetString5);
                    if (targetParamsRow != null) {
                        if (!RowStatus.isRowActive(targetParamsRow, 5)) {
                            logger.warn("Found active target address but corresponding params  are not active");
                        } else if (isAccessGranted(snmpTargetAddrEntryRow, targetParamsRow, octetString2, oid, variableBindingArr)) {
                            linkedList.add(sendNotification(snmpTargetAddrEntryRow, targetParamsRow, octetString2, oid, timeTicks, variableBindingArr, integer32.getValue(), this.notificationEventID));
                        } else if (logger.isWarnEnabled()) {
                            logger.warn("Access denied by VACM for " + oid);
                        }
                    } else if (logger.isWarnEnabled()) {
                        logger.warn("Found active target address but not corresponding params row: '" + octetString5 + "'");
                    }
                }
            }
        }
        return linkedList.toArray(new ResponseEvent[linkedList.size()]);
    }

    public NotificationOriginatorImpl(Session session, VACM vacm, SysUpTime sysUpTime, SnmpTargetMIB snmpTargetMIB, SnmpNotificationMIB snmpNotificationMIB, SnmpCommunityMIB snmpCommunityMIB) {
        this(session, vacm, sysUpTime, snmpTargetMIB, snmpNotificationMIB);
        this.communityMIB = snmpCommunityMIB;
    }
}
