00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <net-snmp/net-snmp-config.h>
00010 #if HAVE_STDLIB_H
00011 #include <stdlib.h>
00012 #endif
00013 #if HAVE_STRING_H
00014 #include <string.h>
00015 #else
00016 #include <strings.h>
00017 #endif
00018
00019
00020
00021
00022 #if TIME_WITH_SYS_TIME
00023 # ifdef WIN32
00024 # include <sys/timeb.h>
00025 # else
00026 # include <sys/time.h>
00027 # endif
00028 # include <time.h>
00029 #else
00030 # if HAVE_SYS_TIME_H
00031 # include <sys/time.h>
00032 # else
00033 # include <time.h>
00034 # endif
00035 #endif
00036
00037 #if HAVE_WINSOCK_H
00038 #include <winsock.h>
00039 #endif
00040 #if HAVE_NETINET_IN_H
00041 #include <netinet/in.h>
00042 #endif
00043
00044 #include <net-snmp/net-snmp-includes.h>
00045 #include <net-snmp/agent/net-snmp-agent-includes.h>
00046
00047
00048
00049
00050 #include "util_funcs.h"
00051
00052
00053
00054
00055 #include "example.h"
00056
00057
00058
00059
00060
00061
00062
00063 #define EXAMPLE_STR_LEN 300
00064 #define EXAMPLE_STR_DEFAULT "life the universe and everything"
00065 int example_int = 42;
00066 char example_str[EXAMPLE_STR_LEN];
00067
00068
00069
00070
00071 void example_parse_config_exampleint(const char *token,
00072 char *cptr);
00073 void example_parse_config_examplestr(const char *token,
00074 char *cptr);
00075 void example_free_config_exampleint(void);
00076 void example_free_config_examplestr(void);
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 struct variable2 example_variables[] = {
00125 {EXAMPLESTRING, ASN_OCTET_STR, RONLY, var_example, 1, {1}},
00126 {EXAMPLEINTEGER, ASN_INTEGER, RWRITE, var_example, 2, {2, 1}},
00127 {EXAMPLEOBJECTID, ASN_OBJECT_ID, RONLY, var_example, 2, {2, 2}},
00128 {EXAMPLETIMETICKS, ASN_TIMETICKS, RONLY, var_example, 1, {3}},
00129 {EXAMPLEIPADDRESS, ASN_IPADDRESS, RONLY, var_example, 1, {4}},
00130 {EXAMPLECOUNTER, ASN_COUNTER, RONLY, var_example, 1, {5}},
00131 {EXAMPLEGAUGE, ASN_GAUGE, RONLY, var_example, 1, {6}},
00132 {EXAMPLETRIGGERTRAP, ASN_INTEGER, RWRITE, var_example, 1, {7}},
00133 {EXAMPLETRIGGERTRAP2, ASN_INTEGER, RWRITE, var_example, 1, {8}}
00134 };
00135
00136
00137
00138
00139
00140
00141
00142
00143 oid example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 2021, 254 };
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 void
00161 init_example(void)
00162 {
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 REGISTER_MIB("example", example_variables, variable2,
00173 example_variables_oid);
00174
00175
00176
00177
00178
00179
00180
00181
00182 strncpy(example_str, EXAMPLE_STR_DEFAULT, EXAMPLE_STR_LEN);
00183
00184 snmpd_register_config_handler("exampleint",
00185 example_parse_config_exampleint,
00186 example_free_config_exampleint,
00187 "exampleint value");
00188 snmpd_register_config_handler("examplestr",
00189 example_parse_config_examplestr,
00190 example_free_config_examplestr,
00191 "examplestr value");
00192 snmpd_register_config_handler("examplestring",
00193 example_parse_config_examplestr,
00194 example_free_config_examplestr,
00195 "examplestring value");
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 void
00217 example_parse_config_exampleint(const char *token, char *cptr)
00218 {
00219 example_int = atoi(cptr);
00220 }
00221
00222 void
00223 example_parse_config_examplestr(const char *token, char *cptr)
00224 {
00225
00226
00227
00228 if (strlen(cptr) < EXAMPLE_STR_LEN)
00229 strcpy(example_str, cptr);
00230 else {
00231
00232
00233
00234
00235
00236 strncpy(example_str, cptr, EXAMPLE_STR_LEN - 4);
00237 example_str[EXAMPLE_STR_LEN - 4] = 0;
00238 strcat(example_str, "...");
00239 example_str[EXAMPLE_STR_LEN - 1] = 0;
00240 }
00241 }
00242
00243
00244
00245
00246 void
00247 example_free_config_exampleint(void)
00248 {
00249 }
00250
00251 void
00252 example_free_config_examplestr(void)
00253 {
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 u_char *
00287 var_example(struct variable *vp,
00288 oid * name,
00289 size_t * length,
00290 int exact, size_t * var_len, WriteMethod ** write_method)
00291 {
00292
00293
00294
00295
00296
00297 static char string[EXAMPLE_STR_LEN];
00298 static oid oid_ret[8];
00299 static long long_ret;
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 DEBUGMSGTL(("example", "var_example entered\n"));
00320 if (header_generic(vp, name, length, exact, var_len, write_method) ==
00321 MATCH_FAILED)
00322 return NULL;
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 switch (vp->magic) {
00343 case EXAMPLESTRING:
00344 sprintf(string, example_str);
00345
00346
00347
00348
00349
00350 *var_len = strlen(string);
00351 return (u_char *) string;
00352
00353 case EXAMPLEINTEGER:
00354
00355
00356
00357
00358
00359 long_ret = example_int;
00360 *write_method = write_exampleint;
00361 return (u_char *) & long_ret;
00362
00363 case EXAMPLEOBJECTID:
00364 oid_ret[0] = 1;
00365 oid_ret[1] = 3;
00366 oid_ret[2] = 6;
00367 oid_ret[3] = 1;
00368 oid_ret[4] = 4;
00369 oid_ret[5] = oid_ret[6] = oid_ret[7] = 42;
00370
00371
00372
00373 *var_len = 8 * sizeof(oid);
00374 return (u_char *) oid_ret;
00375
00376 case EXAMPLETIMETICKS:
00377
00378
00379
00380
00381 long_ret = 363136200;
00382 return (u_char *) & long_ret;
00383
00384 case EXAMPLEIPADDRESS:
00385
00386
00387
00388
00389
00390
00391 long_ret = ntohl(INADDR_LOOPBACK);
00392 return (u_char *) & long_ret;
00393
00394 case EXAMPLECOUNTER:
00395 long_ret = 42;
00396 return (u_char *) & long_ret;
00397
00398 case EXAMPLEGAUGE:
00399 long_ret = 42;
00400 return (u_char *) & long_ret;
00401
00402 case EXAMPLETRIGGERTRAP:
00403
00404
00405
00406
00407
00408 long_ret = 0;
00409 *write_method = write_exampletrap;
00410 return (u_char *) & long_ret;
00411
00412 case EXAMPLETRIGGERTRAP2:
00413
00414
00415
00416
00417
00418 long_ret = 0;
00419 *write_method = write_exampletrap2;
00420 return (u_char *) & long_ret;
00421
00422 default:
00423
00424
00425
00426
00427
00428
00429
00430 DEBUGMSGTL(("snmpd", "unknown sub-id %d in examples/var_example\n",
00431 vp->magic));
00432 }
00433
00434
00435
00436
00437 return NULL;
00438 }
00439
00440
00441
00442
00443
00444
00445 int
00446 write_exampleint(int action,
00447 u_char * var_val,
00448 u_char var_val_type,
00449 size_t var_val_len,
00450 u_char * statP, oid * name, size_t name_len)
00451 {
00452
00453
00454
00455 #define MAX_EXAMPLE_INT 100
00456 static long intval;
00457 static long old_intval;
00458
00459 switch (action) {
00460 case RESERVE1:
00461
00462
00463
00464 if (var_val_type != ASN_INTEGER) {
00465 DEBUGMSGTL(("example", "%x not integer type", var_val_type));
00466 return SNMP_ERR_WRONGTYPE;
00467 }
00468 if (var_val_len > sizeof(long)) {
00469 DEBUGMSGTL(("example", "wrong length %x", var_val_len));
00470 return SNMP_ERR_WRONGLENGTH;
00471 }
00472
00473 intval = *((long *) var_val);
00474 if (intval > MAX_EXAMPLE_INT) {
00475 DEBUGMSGTL(("example", "wrong value %x", intval));
00476 return SNMP_ERR_WRONGVALUE;
00477 }
00478 break;
00479
00480 case RESERVE2:
00481
00482
00483
00484
00485
00486
00487 break;
00488
00489 case FREE:
00490
00491
00492
00493
00494
00495
00496
00497 break;
00498
00499 case ACTION:
00500
00501
00502
00503
00504
00505 old_intval = example_int;
00506 example_int = intval;
00507 break;
00508
00509 case UNDO:
00510
00511
00512
00513
00514
00515 example_int = old_intval;
00516 break;
00517
00518 case COMMIT:
00519
00520
00521
00522
00523
00524
00525
00526
00527 break;
00528
00529 }
00530 return SNMP_ERR_NOERROR;
00531 }
00532
00533 int
00534 write_exampletrap(int action,
00535 u_char * var_val,
00536 u_char var_val_type,
00537 size_t var_val_len,
00538 u_char * statP, oid * name, size_t name_len)
00539 {
00540 long intval;
00541
00542 DEBUGMSGTL(("example", "write_exampletrap entered: action=%d\n",
00543 action));
00544 switch (action) {
00545 case RESERVE1:
00546
00547
00548
00549 if (var_val_type != ASN_INTEGER) {
00550 DEBUGMSGTL(("example", "%x not integer type", var_val_type));
00551 return SNMP_ERR_WRONGTYPE;
00552 }
00553 if (var_val_len > sizeof(long)) {
00554 DEBUGMSGTL(("example", "wrong length %x", var_val_len));
00555 return SNMP_ERR_WRONGLENGTH;
00556 }
00557
00558 intval = *((long *) var_val);
00559 if (intval != 1) {
00560 DEBUGMSGTL(("example", "wrong value %x", intval));
00561 return SNMP_ERR_WRONGVALUE;
00562 }
00563 break;
00564
00565 case RESERVE2:
00566
00567
00568
00569 break;
00570
00571 case FREE:
00572
00573
00574
00575 break;
00576
00577 case ACTION:
00578
00579
00580
00581
00582
00583 break;
00584
00585 case UNDO:
00586
00587
00588
00589
00590 break;
00591
00592 case COMMIT:
00593
00594
00595
00596
00597
00598
00599
00600
00601 DEBUGMSGTL(("example", "write_exampletrap sending the trap\n"));
00602 send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 99);
00603 DEBUGMSGTL(("example", "write_exampletrap trap sent\n"));
00604 break;
00605
00606 }
00607 return SNMP_ERR_NOERROR;
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633 int
00634 write_exampletrap2(int action,
00635 u_char * var_val,
00636 u_char var_val_type,
00637 size_t var_val_len,
00638 u_char * statP, oid * name, size_t name_len)
00639 {
00640 long intval;
00641
00642
00643
00644
00645 oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
00646 oid demo_trap[] = { 1, 3, 6, 1, 4, 1, 2021, 13, 990 };
00647 oid example_string_oid[] =
00648 { 1, 3, 6, 1, 4, 1, 2021, 254, 1, 0 };
00649 static netsnmp_variable_list var_trap;
00650 static netsnmp_variable_list var_obj;
00651
00652 DEBUGMSGTL(("example", "write_exampletrap2 entered: action=%d\n",
00653 action));
00654 switch (action) {
00655 case RESERVE1:
00656
00657
00658
00659 if (var_val_type != ASN_INTEGER) {
00660 DEBUGMSGTL(("example", "%x not integer type", var_val_type));
00661 return SNMP_ERR_WRONGTYPE;
00662 }
00663 if (var_val_len > sizeof(long)) {
00664 DEBUGMSGTL(("example", "wrong length %x", var_val_len));
00665 return SNMP_ERR_WRONGLENGTH;
00666 }
00667
00668 intval = *((long *) var_val);
00669 if (intval != 1) {
00670 DEBUGMSGTL(("example", "wrong value %x", intval));
00671 return SNMP_ERR_WRONGVALUE;
00672 }
00673 break;
00674
00675 case RESERVE2:
00676
00677
00678
00679 break;
00680
00681 case FREE:
00682
00683
00684
00685 break;
00686
00687 case ACTION:
00688
00689
00690
00691
00692
00693 break;
00694
00695 case UNDO:
00696
00697
00698
00699
00700 break;
00701
00702 case COMMIT:
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716 var_trap.next_variable = &var_obj;
00717 var_trap.name = objid_snmptrap;
00718 var_trap.name_length = sizeof(objid_snmptrap) / sizeof(oid);
00719 var_trap.type = ASN_OBJECT_ID;
00720 var_trap.val.objid = demo_trap;
00721 var_trap.val_len = sizeof(demo_trap);
00722
00723
00724
00725
00726
00727
00728
00729 var_obj.next_variable = NULL;
00730 var_obj.name = example_string_oid;
00731 var_obj.name_length = sizeof(example_string_oid) / sizeof(oid);
00732 var_obj.type = ASN_OCTET_STR;
00733 var_obj.val.string = example_str;
00734 var_obj.val_len = strlen(example_str);
00735 DEBUGMSGTL(("example", "write_exampletrap2 sending the v2 trap\n"));
00736 send_v2trap(&var_trap);
00737 DEBUGMSGTL(("example", "write_exampletrap2 v2 trap sent\n"));
00738
00739 break;
00740
00741 }
00742 return SNMP_ERR_NOERROR;
00743 }