79) unsigned char *
80) var_ustScalarSet(struct variable *vp,
81) oid *name,
82) size_t *length,
83) int exact,
84) size_t *var_len,
85) WriteMethod **write_method)
86) {
87) /* variables we may use later */
88) static long long_ret;

89) if (header_generic(vp,name,length,exact,var_len,write_method)
90) == MATCH_FAILED )
91) return NULL;

92) /*
93) this is where we do the value assignments for the mib results.
94) */
95) switch(vp->;magic) {
96) case USTSSSIMPLESTRING:
97) *write_method = write_ustSSSimpleString;
98) *var_len = ustSSSimpleString_len;
99) return (unsigned char *) ustSSSimpleString;

100) case USTSSSECONDSSINCECHANGED:
101) /* TimeTicks are seconds*100 */
102) long_ret = (time(NULL) - lastChanged)*100;
103) *var_len = sizeof(long_ret);
104) return (unsigned char *) &long_ret;

105) default:
106) ERROR_MSG("");
107) }
108) return NULL;
109) }

110) int
111) write_ustSSSimpleString(int action,
112) u_char *var_val,
113) u_char var_val_type,
114) size_t var_val_len,
115) u_char *statP,
116) oid *name,
117) size_t name_len)
118) {
119) static unsigned char *new_string = 0, *old_string = 0;
120) static size_t size, old_size;

121) /* this long complex series of "action"s is to preserve proper
122) transaction handling with other transactions in the same set
123) request. */

124) switch ( action ) {
125) case RESERVE1:
126) /* check to see that everything is possible */
127) if (var_val_type != ASN_OCTET_STR){
128) fprintf(stderr,
129) "write to ustSSSimpleString not ASN_OCTET_STR\n");
130) return SNMP_ERR_WRONGTYPE;
131) }
132) break;

133) case RESERVE2:
134) /* allocate memory needed here. */
135) size = var_val_len;
136) new_string = (char *) malloc(size+1);
137) if (new_string == NULL) {
138) return SNMP_ERR_GENERR; /* malloc failed! */
139) }
140) break;

141) case ACTION:
142) /* Actually make the change requested. Note that
143) anything done here must be reversable in the UNDO case */
144) if (new_string) {
145) old_string = ustSSSimpleString;
146) old_size = ustSSSimpleString_len;
147) memcpy(new_string, var_val, var_val_len);
148) new_string[var_val_len] = 0;
149) ustSSSimpleString = new_string;
150) ustSSSimpleString_len = size;
151) new_string = NULL;
152) } else {
153) /* something seriously wrong if we got here */
154) return SNMP_ERR_GENERR;
155) }
156) break;

157) case UNDO:
158) /* Back out any changes made in the ACTION case */
159) if (old_string == NULL) {
160) return SNMP_ERR_UNDOFAILED;
161) }
162) if (ustSSSimpleString)
163) free(ustSSSimpleString);
164) ustSSSimpleString = old_string;
165) ustSSSimpleString_len = old_size;
166) break;

167) case COMMIT:
168) /* Things are working well, so it's now safe to make the change
169) permanently. Make sure that anything done here can't fail! */
170) lastChanged = time(NULL);
171) break;

172) /* Treat the rest the same as FREE */
173) case FREE:
174) // break;
175) /* Release any resources that have been allocated */
176) if (new_s tring) {
177) free(new_string);
178) new_string = NULL;
179) }
180) if (old_string) {
181) free(old_string);
182) old_string = NULL;
183) }
184) break;

185) }
186) return SNMP_ERR_NOERROR;
187) }

我们定义的MIB模块是如此的简单,我们只需要在mib2c生成的代码上作黑体标识的修改即可.


相关内容