KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > UsmKeyChange


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - UsmKeyChange.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo.snmp;
23
24 import org.snmp4j.agent.MOAccess;
25 import org.snmp4j.smi.OctetString;
26 import org.snmp4j.agent.request.SubRequest;
27 import org.snmp4j.agent.mo.MOTableRow;
28 import org.snmp4j.security.AuthenticationProtocol;
29 import org.snmp4j.PDU;
30 import org.snmp4j.security.PrivacyProtocol;
31
32 public class UsmKeyChange extends KeyChange {
33
34   public static final int AUTH_KEY_CHANGE = 0;
35   public static final int PRIV_KEY_CHANGE = 1;
36
37   private int type;
38
39   public UsmKeyChange(int columnID,
40                       MOAccess access, int type) {
41     super(columnID, access, new OctetString(), true);
42     this.type = type;
43   }
44
45   public void prepare(SubRequest subRequest, MOTableRow row,
46                       MOTableRow changeSet, int column) {
47     super.prepare(subRequest, row, changeSet, column);
48     if (!subRequest.hasError()) {
49       UsmMIB.UsmTableRow usmRow = (UsmMIB.UsmTableRow)row;
50       int digestLength = getDigestLength(usmRow, changeSet);
51       if (digestLength <= 0) {
52         subRequest.completed();
53         return;
54       }
55       OctetString v =
56           (OctetString) subRequest.getVariableBinding().getVariable();
57       if (v.length() != digestLength) {
58         subRequest.getStatus().setErrorStatus(PDU.wrongValue);
59       }
60     }
61   }
62
63   protected int getDigestLength(UsmMIB.UsmTableRow row, MOTableRow changeSet) {
64     switch (type) {
65       case AUTH_KEY_CHANGE: {
66         AuthenticationProtocol a = row.getAuthProtocol(changeSet);
67         if (a != null) {
68           return a.getDigestLength() * 2;
69         }
70         break;
71       }
72       case PRIV_KEY_CHANGE: {
73         PrivacyProtocol p = row.getPrivProtocol(changeSet);
74         if (p != null) {
75           return p.getMaxKeyLength() * 2;
76         }
77         break;
78       }
79     }
80     return -1;
81   }
82
83   public void commit(SubRequest subRequest, MOTableRow row,
84                      MOTableRow changeSet, int column) {
85     int digestLength = getDigestLength((UsmMIB.UsmTableRow)row, changeSet);
86     if (digestLength > 0) {
87       super.commit(subRequest, row, changeSet, column);
88     }
89     else {
90       subRequest.completed();
91     }
92   }
93
94 }
95
Popular Tags