summaryrefslogtreecommitdiff
path: root/local/mib2c.array-user.conf
blob: 31559ac838030b2f987185c332821aa729835e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
## -*- c -*-
##
## For documentation on the code generated by this configuration file,
## see the file agent/helpers/table_array.c.
##
######################################################################
## Do the .h file
## @perleval $vars{shortname} =~ s/([A-Z])[a-z]+/$1/g@
######################################################################
@foreach $i table@
@open ${i}.h@
@eval $hack = "Id"
/*
 * Note: this file originally auto-generated by mib2c using
 *        $Id$
 *
 * $$hack:$
 *
 * Yes, there is lots of code here that you might not use. But it is much
 * easier to remove code than to add it!
 */
#ifndef $i.uc_H
#define $i.uc_H

#ifdef __cplusplus
extern "C" {
#endif

    
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/library/container.h>
#include <net-snmp/agent/table_array.h>

    @eval $ext_index = 0@
    @foreach $idx index@
        @if "$idx" ne ""@
        @eval $found = "external"@
        @foreach $c column@
            @if "$idx" eq "$c"@
               @eval $found = "internal"@
            @end@
        @end@
        /** Index $idx is $found */
        @if "$found" eq "external"@
           @eval $ext_index = 1@
        @end@
        @end@
    @end@

typedef struct ${i}_context_s {
    netsnmp_index index; /** THIS MUST BE FIRST!!! */

    /*************************************************************
     * You can store data internally in this structure.
     *
     * TODO: You will probably have to fix a few types here...
     */
    @if $ext_index != 0@
    /** TODO: add storage for external index(s)! */
    @end@
    @foreach $c column@
        /** $c.syntax = $c.type */
        @eval $have_type = 0@
        @if "$c.type" eq "ASN_OCTET_STR"@
            @eval $have_type = 1@
            @eval $o_len = "65535"@
            @if "$c.syntax" eq "DisplayString"@
                @eval $o_len = "255"@
            @end@
            @if "$c.syntax" eq "SnmpAdminString"@
                @eval $o_len = "255"@
            @end@
            unsigned char $c[$o_len];
            long ${c}_len;
        @end@
        @if "$c.type" eq "ASN_OBJECT_ID"@
            @eval $have_type = 1@
            oid $c[MAX_OID_LEN];
            long ${c}_len;
        @end@
        @if "$c.type" eq "ASN_UNSIGNED"@
            @eval $have_type = 1@
            unsigned long $c;
        @end@
        @if "$c.type" eq "ASN_TIMETICKS"@
            @eval $have_type = 1@
            unsigned long $c;
        @end@
        @if "$c.type" eq "ASN_IPADDRESS"@
            @eval $have_type = 1@
            unsigned long $c;
        @end@
        @if "$c.type" eq "ASN_UINTEGER"@
            @eval $have_type = 1@
            unsigned long $c;
        @end@
        @if "$c.type" eq "ASN_INTEGER"@
            @eval $have_type = 1@
            long $c;
        @end@
        @if "$c.type" eq "ASN_COUNTER"@
            @eval $have_type = 1@
            unsigned long $c;
        @end@
        @if $have_type == 0@
    /** TODO: Is this type correct? */
            long $c;
        @end@

    @end@

    /*
     * OR
     *
     * Keep a pointer to your data
     */
    void * data;

    /*
     *add anything else you want here
     */

} ${i}_context;

/*************************************************************
 * function declarations
 */
void init_$i(void);
void initialize_table_$i(void);
const ${i}_context * ${i}_get_by_idx(netsnmp_index *);
const ${i}_context * ${i}_get_by_idx_rs(netsnmp_index *,
                                        int row_status);
int ${i}_get_value(netsnmp_request_info *, netsnmp_index *, netsnmp_table_request_info *);


/*************************************************************
 * oid declarations
 */
extern const oid ${i}_oid[];
extern const size_t ${i}_oid_len;

#define ${i}_TABLE_OID $i.commaoid
    
/*************************************************************
 * column number definitions for table $i
 */
@eval $minv = 0xffffffff@
@eval $maxv = 0@
@foreach $c column@
#define COLUMN_$c.uc $c.subid
@if ! $c.noaccess@
@eval $minv = min($minv, $c.subid)@
@eval $maxv = max($maxv, $c.subid)@
@end@
@if "$c.syntax" eq "RowStatus"@
   @eval $rs_name = "$c"@
@end@
@if "$c.syntax" eq "StorageType"@
   @eval $st_name = "$c"@
@end@
@end@
#define ${i}_COL_MIN $minv
#define ${i}_COL_MAX $maxv

/* comment out the following line if you don't want a custom sort  */
#define ${i}_CUSTOM_SORT

@if "$rs_name" ne ""@
/* uncommend the following line if you allow modifications to an
 * active row */
/** define ${i}_CAN_MODIFY_ACTIVE_ROW */

@end@
@if $i.settable@
int ${i}_extract_index( ${i}_context * ctx, netsnmp_index * hdr );

void ${i}_set_reserve1( netsnmp_request_group * );
void ${i}_set_reserve2( netsnmp_request_group * );
void ${i}_set_action( netsnmp_request_group * );
void ${i}_set_commit( netsnmp_request_group * );
void ${i}_set_free( netsnmp_request_group * );
void ${i}_set_undo( netsnmp_request_group * );

${i}_context * ${i}_duplicate_row( ${i}_context* );
netsnmp_index * ${i}_delete_row( ${i}_context* );

@if "$rs_name" ne ""@
int ${i}_can_activate(${i}_context *undo_ctx,
                      ${i}_context *row_ctx,
                      netsnmp_request_group * rg);
int ${i}_can_deactivate(${i}_context *undo_ctx,
                        ${i}_context *row_ctx,
                        netsnmp_request_group * rg);
@end@
int ${i}_can_delete(${i}_context *undo_ctx,
                    ${i}_context *row_ctx,
                    netsnmp_request_group * rg);
    
    
@if $i.creatable@
${i}_context * ${i}_create_row( netsnmp_index* );
@end@
@end@

#ifdef ${i}_CUSTOM_SORT
${i}_context * ${i}_get( const char *name, int len );
#endif

#ifdef __cplusplus
}
#endif

#endif /** $i.uc_H */
@end@
######################################################################
## Do the .c file
######################################################################
@foreach $i table@
@open ${i}.c@
@eval $hack = "Id"@
/*
 * Note: this file originally auto-generated by mib2c using
 *       $Id$
 *
 * $$hack:$
 *
 *
 * For help understanding NET-SNMP in general, please check the 
 *     documentation and FAQ at:
 *
 *     http://www.net-snmp.org/
 *
 *
 * For help understanding this code, the agent and how it processes
 *     requests, please check the following references.
 *
 *     http://www.net-snmp.org/tutorial-5/
 *
 *
 * You can also join the #net-snmp channel on irc.freenode.net
 *     and ask for help there.
 *
 *
 * And if all else fails, send a detailed message to the developers
 *     describing the problem you are having to:
 *
 *    net-snmp-coders@lists.sourceforge.net
 *
 *
 * Yes, there is lots of code here that you might not use. But it is much
 * easier to remove code than to add it!
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include <net-snmp/library/snmp_assert.h>

#include "${i}.h"

static     netsnmp_handler_registration *my_handler = NULL;
static     netsnmp_table_array_callbacks cb;

const oid ${i}_oid[] = { ${i}_TABLE_OID };
const size_t ${i}_oid_len = OID_LENGTH(${i}_oid);


#ifdef ${i}_CUSTOM_SORT
/************************************************************
 * keep binary tree to find context by name
 */
static int ${i}_cmp( const void *lhs, const void *rhs );

/************************************************************
 * compare two context pointers here. Return -1 if lhs < rhs,
 * 0 if lhs == rhs, and 1 if lhs > rhs.
 */
static int
${i}_cmp( const void *lhs, const void *rhs )
{
    ${i}_context *context_l =
        (${i}_context *)lhs;
    ${i}_context *context_r =
        (${i}_context *)rhs;

    /*
     * check primary key, then secondary. Add your own code if
     * there are more than 2 keys
     */
    int rc;

    /*
     * TODO: implement compare. Remove this ifdef code and
     * add your own code here.
     */
#ifdef TABLE_CONTAINER_TODO
    snmp_log(LOG_ERR,
             "${i}_compare not implemented! Container order undefined\n" );
    return 0;
#endif
    
    /*
     * EXAMPLE (assuming you want to sort on a name):
     *   
     * rc = strcmp( context_l->xxName, context_r->xxName );
     *
     * if(rc)
     *   return rc;
     *
     * TODO: fix secondary keys (or delete if there are none)
     *
     * if(context_l->yy < context_r->yy) 
     *   return -1;
     *
     * return (context_l->yy == context_r->yy) ? 0 : 1;
     */
}

/************************************************************
 * search tree
 */
/** TODO: set additional indexes as parameters */
${i}_context *
${i}_get( const char *name, int len )
{
    ${i}_context tmp;

    /** we should have a custom container */
    netsnmp_assert(cb.container->next != NULL);
    
    /*
     * TODO: implement compare. Remove this ifdef code and
     * add your own code here.
     */
#ifdef TABLE_CONTAINER_TODO
    snmp_log(LOG_ERR, "${i}_get not implemented!\n" );
    return NULL;
#endif

    /*
     * EXAMPLE:
     *
     * if(len > sizeof(tmp.xxName))
     *   return NULL;
     *
     * strncpy( tmp.xxName, name, sizeof(tmp.xxName) );
     * tmp.xxName_len = len;
     *
     * return CONTAINER_FIND(cb.container->next, &tmp);
     */
}
#endif


/************************************************************
 * Initializes the $i module
 */
void
init_$i(void)
{
    initialize_table_$i();

    /*
     * TODO: perform any startup stuff here, such as
     * populating the table with initial data.
     *
     * ${i}_context * new_row = create_row(index);
     * CONTAINER_INSERT(cb.container,new_row);
     */
}

@if $i.settable@
/************************************************************
 * the *_row_copy routine
 */
static int ${i}_row_copy(${i}_context * dst,
                         ${i}_context * src)
{
    if(!dst||!src)
        return 1;
        
    /*
     * copy index, if provided
     */
    if(dst->index.oids)
        free(dst->index.oids);
    if(snmp_clone_mem( (void*)&dst->index.oids, src->index.oids,
                           src->index.len * sizeof(oid) )) {
        dst->index.oids = NULL;
        return 1;
    }
    dst->index.len = src->index.len;
    

    /*
     * copy components into the context structure
     */
    @if $ext_index != 0@
    /** TODO: add code for external index(s)! */
    @end@
    @foreach $c column@
    @eval $have_type = 0@
    @if "$c.type" eq "ASN_OCTET_STR"@
    @eval $have_type = 1@
    memcpy( dst->$c, src->$c, src->${c}_len );
    dst->${c}_len = src->${c}_len;
    @end@
    @if "$c.type" eq "ASN_OBJECT_ID"@
    @eval $have_type = 1@
    memcpy( dst->$c, src->$c, src->${c}_len );
    dst->${c}_len = src->${c}_len;
    @end@
    @if $have_type == 0@
    dst->$c = src->$c;
    @end@

    @end@
    return 0;
}

/**
 * the *_extract_index routine
 *
 * This routine is called when a set request is received for an index
 * that was not found in the table container. Here, we parse the oid
 * in the the individual index components and copy those indexes to the
 * context. Then we make sure the indexes for the new row are valid.
 */
int
${i}_extract_index( ${i}_context * ctx, netsnmp_index * hdr )
{
    /*
     * temporary local storage for extracting oid index
     *
     * extract index uses varbinds (netsnmp_variable_list) to parse
     * the index OID into the individual components for each index part.
     */
    @if $ext_index != 0@
    /** TODO: add storage for external index(s)! */
    @end@
    @eval $first_idx = ""@
    @foreach $idx index@
        @if "$first_idx" eq ""@
        @eval $first_idx = $idx@
        @end@
    netsnmp_variable_list var_$idx;
    @end@
    int err;

    /*
     * copy index, if provided
     */
    if(hdr) {
        netsnmp_assert(ctx->index.oids == NULL);
        if((hdr->len > MAX_OID_LEN) ||
           snmp_clone_mem( (void*)&ctx->index.oids, hdr->oids,
                           hdr->len * sizeof(oid) )) {
            return -1;
        }
        ctx->index.len = hdr->len;
    }

    /*
     * initialize variable that will hold each component of the index.
     * If there are multiple indexes for the table, the variable_lists
     * need to be linked together, in order.
     */
    @if $ext_index != 0@
       /** TODO: add code for external index(s)! */
    @end@
    @foreach $idx index@
       memset( &var_$idx, 0x00, sizeof(var_$idx) );
       var_${idx}.type = $idx.type; /* type hint for parse_oid_indexes */
       /** TODO: link this index to the next, or NULL for the last one */
#ifdef TABLE_CONTAINER_TODO
    snmp_log(LOG_ERR, "${i}_extract_index index list not implemented!\n" );
    return 0;
#else
       var_${idx}.next_variable = &var_XX;
#endif

    @end@

    /*
     * parse the oid into the individual index components
     */
    err = parse_oid_indexes( hdr->oids, hdr->len, &var_$first_idx );
    if (err == SNMP_ERR_NOERROR) {
       /*
        * copy index components into the context structure
        */
       @foreach $idx index@
          @eval $found = "external"@
          @foreach $c column@
              @if "$idx" eq "$c"@
                 @eval $found = "internal"@
              @end@
          @end@
          @if "$found" eq "external"@
              /** skipping external index $idx */
          @end@
          @if "$found" eq "internal"@
             @eval $have_type = 0@
             @if "$idx.type" eq "ASN_OCTET_STR"@
             @eval $have_type = 1@
                if(var_${idx}.val_len > sizeof(ctx->$idx))
                   err = -1;
                else
                    memcpy( ctx->$idx, var_${idx}.val.string, var_${idx}.val_len );
                ctx->${idx}_len = var_${idx}.val_len;
             @end@
             @if "$idx.type" eq "ASN_OBJECT_ID"@
             @eval $have_type = 1@
                memcpy( ctx->$idx, var_${idx}.val.string, var_${idx}.val_len );
                ctx->${idx}_len = var_${idx}.val_len;
             @end@
             @if $have_type == 0@
                ctx->$idx = *var_${idx}.val.integer;
             @end@
          @end@
   
       @end@
   
       @foreach $c index@
           /*
            * TODO: check index for valid values. For EXAMPLE:
            *
          @eval $have_check = 0@
          @if "$c.type" eq "ASN_IPADDRESS"@
              @eval $have_check = 1@
              * if ( XXX_check_ip( *var_${c}.val.integer ) ) {
          @end@
          @if "$c.type" eq "ASN_OBJECT_ID"@
              @eval $have_check = 1@
              * if ( XXX_check_oid( var_${c}.val.objid, var_${c}.val_len /
                                    sizeof(oid) ) ) {
          @end@
          @if "$c.type" eq "ASN_OCTET_STR"@
              @eval $have_check = 1@
              * if ( XXX_check_value( var_${c}.val.string, XXX ) ) {
          @end@
          @if $have_check != 1@
              * if ( *var_${c}.val.integer != XXX ) {
          @end@
          *    err = -1;
          * }
          */
       @end@
    }

    /*
     * parsing may have allocated memory. free it.
     */
    snmp_reset_var_buffers( &var_$first_idx );

    return err;
}

@if "$rs_name" ne ""@
/************************************************************
 * the *_can_activate routine is called
 * when a row is changed to determine if all the values
 * set are consistent with the row's rules for a row status
 * of ACTIVE.
 *
 * return 1 if the row could be ACTIVE
 * return 0 if the row is not ready for the ACTIVE state
 */
int ${i}_can_activate(${i}_context *undo_ctx,
                      ${i}_context *row_ctx,
                      netsnmp_request_group * rg)
{
    /*
     * TODO: check for activation requirements here
     */


    /*
     * be optimistic.
     */
    return 1;
}

/************************************************************
 * the *_can_deactivate routine is called when a row that is
 * currently ACTIVE is set to a state other than ACTIVE. If
 * there are conditions in which a row should not be allowed
 * to transition out of the ACTIVE state (such as the row being
 * referred to by another row or table), check for them here.
 *
 * return 1 if the row can be set to a non-ACTIVE state
 * return 0 if the row must remain in the ACTIVE state
 */
int ${i}_can_deactivate(${i}_context *undo_ctx,
                        ${i}_context *row_ctx,
                        netsnmp_request_group * rg)
{
    /*
     * TODO: check for deactivation requirements here
     */
    return 1;
}

@end@
/************************************************************
 * the *_can_delete routine is called to determine if a row
 * can be deleted.
 *
 * return 1 if the row can be deleted
 * return 0 if the row cannot be deleted
 */
int ${i}_can_delete(${i}_context *undo_ctx,
                    ${i}_context *row_ctx,
                    netsnmp_request_group * rg)
{
@if "$rs_name" ne ""@
    /*
     * probably shouldn't delete a row that we can't
     * deactivate.
     */
    if(${i}_can_deactivate(undo_ctx,row_ctx,rg) != 1)
        return 0;
@end@
    
    /*
     * TODO: check for other deletion requirements here
     */
    return 1;
}

@if $i.creatable@
/************************************************************
 * the *_create_row routine is called by the table handler
 * to create a new row for a given index. If you need more
 * information (such as column values) to make a decision
 * on creating rows, you must create an initial row here
 * (to hold the column values), and you can examine the
 * situation in more detail in the *_set_reserve1 or later
 * states of set processing. Simple check for a NULL undo_ctx
 * in those states and do detailed creation checking there.
 *
 * returns a newly allocated ${i}_context
 *   structure if the specified indexes are not illegal
 * returns NULL for errors or illegal index values.
 */
${i}_context *
${i}_create_row( netsnmp_index* hdr)
{
    ${i}_context * ctx =
        SNMP_MALLOC_TYPEDEF(${i}_context);
    if(!ctx)
        return NULL;
        
    /*
     * TODO: check indexes, if necessary.
     */
    if(${i}_extract_index( ctx, hdr )) {
        if (NULL != ctx->index.oids)
            free(ctx->index.oids);
        free(ctx);
        return NULL;
    }

    /* netsnmp_mutex_init(ctx->lock);
       netsnmp_mutex_lock(ctx->lock); */

    /*
     * TODO: initialize any default values here. This is also
     * first place you really should allocate any memory for
     * yourself to use.  If you allocated memory earlier,
     * make sure you free it for earlier error cases!
     */
    /**
     @foreach $c column@
     @if $c.settable@
     ctx->$c = 0;
     @end@
     @end@
    */

    return ctx;
}
@end@

/************************************************************
 * the *_duplicate row routine
 */
${i}_context *
${i}_duplicate_row( ${i}_context * row_ctx)
{
    ${i}_context * dup;

    if(!row_ctx)
        return NULL;

    dup = SNMP_MALLOC_TYPEDEF(${i}_context);
    if(!dup)
        return NULL;
        
    if(${i}_row_copy(dup,row_ctx)) {
        free(dup);
        dup = NULL;
    }

    return dup;
}

/************************************************************
 * the *_delete_row method is called to delete a row.
 */
netsnmp_index * ${i}_delete_row( ${i}_context * ctx )
{
  /* netsnmp_mutex_destroy(ctx->lock); */

    if(ctx->index.oids)
        free(ctx->index.oids);

    /*
     * TODO: release any memory you allocated here...
     */

    /*
     * release header
     */
    free( ctx );

    return NULL;
}


/************************************************************
 * RESERVE is used to check the syntax of all the variables
 * provided, that the values being set are sensible and consistent,
 * and to allocate any resources required for performing the SET.
 * After this stage, the expectation is that the set ought to
 * succeed, though this is not guaranteed. (In fact, with the UCD
 * agent, this is done in two passes - RESERVE1, and
 * RESERVE2, to allow for dependancies between variables).
 *
 * BEFORE calling this routine, the agent will call duplicate_row
 * to create a copy of the row (unless this is a new row; i.e.
 * row_created == 1).
 *
 * next state -> SET_RESERVE2 || SET_FREE
 */
void ${i}_set_reserve1( netsnmp_request_group *rg )
{
    ${i}_context *row_ctx =
            (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx =
            (${i}_context *)rg->undo_info;
    netsnmp_variable_list *var;
    netsnmp_request_group_item *current;
    int rc;

    @if "$st_name" ne ""@
        /*
         * Block all attempts to modify a readOnly row
         */
    if( row_ctx && (row_ctx->$st_name == SNMP_STORAGE_READONLY) ) {
        netsnmp_set_mode_request_error(MODE_SET_BEGIN, rg->list->ri,
                                       SNMP_ERR_NOTWRITABLE);
        return;
    }
    @end@

    /*
     * TODO: loop through columns, check syntax and lengths. For
     * columns which have no dependencies, you could also move
     * the value/range checking here to attempt to catch error
     * cases as early as possible.
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;
        rc = SNMP_ERR_NOERROR;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
            @if $c.needlength@
            /* or possibly 'netsnmp_check_vb_type_and_size' */
            rc = netsnmp_check_vb_type_and_max_size(var, $c.type,
                                                    sizeof(row_ctx->$c));
            @else@
            /* or possibly 'netsnmp_check_vb_int_range' */
            rc = netsnmp_check_vb_int( var );
            @end@
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            rc = SNMP_ERR_GENERR;
            snmp_log(LOG_ERR, "unknown column in "
                     "${i}_set_reserve1\n");
        }

        if (rc)
           netsnmp_set_mode_request_error(MODE_SET_BEGIN, current->ri, rc );
        rg->status = SNMP_MAX( rg->status, current->ri->status );
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
}

void ${i}_set_reserve2( netsnmp_request_group *rg )
{
    ${i}_context *row_ctx = (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx = (${i}_context *)rg->undo_info;
    netsnmp_request_group_item *current;
    netsnmp_variable_list *var;
    int rc;

    rg->rg_void = rg->list->ri;

    /*
     * TODO: loop through columns, check for valid
     * values and any range constraints.
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;
        rc = SNMP_ERR_NOERROR;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
            @eval $have_check = 0@
            @if "$c" eq "$st_name"@
                @eval $have_check = 1@
                rc = netsnmp_check_vb_storagetype(current->ri->requestvb,
                                                  undo_ctx ?
                                                  undo_ctx->$c:0);
            @end@
            @if "$c" eq "$rs_name"@
                @eval $have_check = 1@
                rc = netsnmp_check_vb_rowstatus(current->ri->requestvb,
                                                undo_ctx ?
                                                undo_ctx->$c:0);
                rg->rg_void = current->ri;
            @end@
            @if "$c.syntax" eq "TruthValue"@
                @eval $have_check = 1@
                rc = netsnmp_check_vb_truthvalue(current->ri->requestvb);
            @end@
            @if $have_check == 0@
                    /*
                     * TODO: routine to check valid values
                     *
                     * EXAMPLE:
                     *
                @if "$c.type" eq "ASN_IPADDRESS"@
                    @eval $have_check = 1@
                    * if ( XXX_check_ip( *var->val.integer ) ) {
                @end@
                @if "$c.type" eq "ASN_OBJECT_ID"@
                    @eval $have_check = 1@
                    * if ( XXX_check_oid( var ) ) {
                @end@
                @if "$c.type" eq "ASN_OCTET_STR"@
                    @eval $have_check = 1@
                    * if ( XXX_check_value( var->val.string, XXX ) ) {
                @end@
                @if $have_check != 1@
                    * if ( *var->val.integer != XXX ) {
                @end@
                *    rc = SNMP_ERR_INCONSISTENTVALUE;
                *    rc = SNMP_ERR_BADVALUE;
                * }
                */
            @end@
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            netsnmp_assert(0); /** why wasn't this caught in reserve1? */
        }

        if (rc)
           netsnmp_set_mode_request_error(MODE_SET_BEGIN, current->ri, rc);
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
}

/************************************************************
 * Assuming that the RESERVE phases were successful, the next
 * stage is indicated by the action value ACTION. This is used
 * to actually implement the set operation. However, this must
 * either be done into temporary (persistent) storage, or the
 * previous value stored similarly, in case any of the subsequent
 * ACTION calls fail.
 *
 * In your case, changes should be made to row_ctx. A copy of
 * the original row is in undo_ctx.
 */
void ${i}_set_action( netsnmp_request_group *rg )
{
    netsnmp_variable_list *var;
    ${i}_context *row_ctx = (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx = (${i}_context *)rg->undo_info;
    netsnmp_request_group_item *current;

    @if "$rs_name" ne ""@
    int            row_err = 0;
    @end@

    /*
     * TODO: loop through columns, copy varbind values
     * to context structure for the row.
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
            @eval $have_type = 0@
            @if "$c.type" eq "ASN_OCTET_STR"@
            @eval $have_type = 1@
            memcpy(row_ctx->$c,var->val.string,var->val_len);
            row_ctx->${c}_len = var->val_len;
            @end@
            @if "$c.type" eq "ASN_OBJECT_ID"@
            @eval $have_type = 1@
            memcpy(row_ctx->$c,var->val.objid,var->val_len);
            row_ctx->${c}_len = var->val_len;
            @end@
            @if $have_type == 0@
            row_ctx->$c = *var->val.integer;
            @end@
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            netsnmp_assert(0); /** why wasn't this caught in reserve1? */
        }
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
    @if "$rs_name" ne ""@
#ifndef ${i}_CAN_MODIFY_ACTIVE_ROW
    if( undo_ctx && RS_IS_ACTIVE(undo_ctx->$rs_name) &&
        row_ctx && RS_IS_ACTIVE(row_ctx->$rs_name) ) {
            row_err = 1;
    }
#endif

    /*
     * check activation/deactivation
     */
    row_err = netsnmp_table_array_check_row_status(&cb, rg,
                                  row_ctx ? &row_ctx->$rs_name : NULL,
                                  undo_ctx ? &undo_ctx->$rs_name : NULL);
    if(row_err) {
        netsnmp_set_mode_request_error(MODE_SET_BEGIN,
                                       (netsnmp_request_info*)rg->rg_void,
                                       row_err);
        return;
    }

    @end@
    /*
     * TODO: if you have dependencies on other tables, this would be
     * a good place to check those, too.
     */
}

/************************************************************
 * Only once the ACTION phase has completed successfully, can
 * the final COMMIT phase be run. This is used to complete any
 * writes that were done into temporary storage, and then release
 * any allocated resources. Note that all the code in this phase
 * should be "safe" code that cannot possibly fail (cue
 * hysterical laughter). The whole intent of the ACTION/COMMIT
 * division is that all of the fallible code should be done in
 * the ACTION phase, so that it can be backed out if necessary.
 *
 * BEFORE calling this routine, the agent will update the
 * container (inserting a row if row_created == 1, or removing
 * the row if row_deleted == 1).
 *
 * AFTER calling this routine, the agent will delete the
 * undo_info.
 */
void ${i}_set_commit( netsnmp_request_group *rg )
{
    netsnmp_variable_list *var;
    ${i}_context *row_ctx = (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx = (${i}_context *)rg->undo_info;
    netsnmp_request_group_item *current;

    /*
     * loop through columns
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            netsnmp_assert(0); /** why wasn't this caught in reserve1? */
        }
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
}

/************************************************************
 * If either of the RESERVE calls fail, the write routines
 * are called again with the FREE action, to release any resources
 * that have been allocated. The agent will then return a failure
 * response to the requesting application.
 *
 * AFTER calling this routine, the agent will delete undo_info.
 */
void ${i}_set_free( netsnmp_request_group *rg )
{
    netsnmp_variable_list *var;
    ${i}_context *row_ctx = (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx = (${i}_context *)rg->undo_info;
    netsnmp_request_group_item *current;

    /*
     * loop through columns
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            /** should have been logged in reserve1 */
        }
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
}

/************************************************************
 * If the ACTION phase does fail (for example due to an apparently
 * valid, but unacceptable value, or an unforeseen problem), then
 * the list of write routines are called again, with the UNDO
 * action. This requires the routine to reset the value that was
 * changed to its previous value (assuming it was actually changed),
 * and then to release any resources that had been allocated. As
 * with the FREE phase, the agent will then return an indication
 * of the error to the requesting application.
 *
 * BEFORE calling this routine, the agent will update the container
 * (remove any newly inserted row, re-insert any removed row).
 *
 * AFTER calling this routing, the agent will call row_copy
 * to restore the data in existing_row from the date in undo_info.
 * Then undo_info will be deleted (or existing row, if row_created
 * == 1).
 */
void ${i}_set_undo( netsnmp_request_group *rg )
{
    netsnmp_variable_list *var;
    ${i}_context *row_ctx = (${i}_context *)rg->existing_row;
    ${i}_context *undo_ctx = (${i}_context *)rg->undo_info;
    netsnmp_request_group_item *current;

    /*
     * loop through columns
     */
    for( current = rg->list; current; current = current->next ) {

        var = current->ri->requestvb;

        switch(current->tri->colnum) {

        @foreach $c column@
        @if $c.settable@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
        break;

        @end@
        @end@
        default: /** We shouldn't get here */
            netsnmp_assert(0); /** why wasn't this caught in reserve1? */
        }
    }

    /*
     * done with all the columns. Could check row related
     * requirements here.
     */
}

@end@


/************************************************************
 *
 * Initialize the $i table by defining its contents and how it's structured
 */
void
initialize_table_$i(void)
{
    netsnmp_table_registration_info *table_info;

    if(my_handler) {
        snmp_log(LOG_ERR, "initialize_table_${i}_handler called again\n");
        return;
    }

    memset(&cb, 0x00, sizeof(cb));

    /** create the table structure itself */
    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);

    my_handler = netsnmp_create_handler_registration("$i",
                                             netsnmp_table_array_helper_handler,
                                             ${i}_oid,
                                             ${i}_oid_len,
@if $i.settable@
                                             HANDLER_CAN_RWRITE
@else@
                                             HANDLER_CAN_RONLY
@end@
                                             );
            
    if (!my_handler || !table_info) {
        snmp_log(LOG_ERR, "malloc failed in "
                 "initialize_table_${i}_handler\n");
        return; /** mallocs failed */
    }

    /***************************************************
     * Setting up the table's definition
     */
    /*
     * TODO: add any external indexes here.
     */
    @if $ext_index != 0@
        /** TODO: add code for external index(s)! */
    @end@

    /*
     * internal indexes
     */
    @foreach $idx index@
        /** index: $idx */
        netsnmp_table_helper_add_index(table_info, $idx.type);
    @end@

    table_info->min_column = ${i}_COL_MIN;
    table_info->max_column = ${i}_COL_MAX;

    /***************************************************
     * registering the table with the master agent
     */
    cb.get_value = ${i}_get_value;
    cb.container = netsnmp_container_find("${i}_primary:"
                                          "${i}:"
                                          "table_container");
#ifdef ${i}_CUSTOM_SORT
    netsnmp_container_add_index(cb.container,
                                netsnmp_container_find("${i}_custom:"
                                                       "${i}:"
                                                       "table_container"));
    cb.container->next->compare = ${i}_cmp;
#endif
@if $i.settable@
    cb.can_set = 1;
@if $i.creatable@
    cb.create_row = (UserRowMethod*)${i}_create_row;
@end@
    cb.duplicate_row = (UserRowMethod*)${i}_duplicate_row;
    cb.delete_row = (UserRowMethod*)${i}_delete_row;
    cb.row_copy = (Netsnmp_User_Row_Operation *)${i}_row_copy;

@if "$rs_name" ne ""@
    cb.can_activate = (Netsnmp_User_Row_Action *)${i}_can_activate;
    cb.can_deactivate = (Netsnmp_User_Row_Action *)${i}_can_deactivate;
@end@
    cb.can_delete = (Netsnmp_User_Row_Action *)${i}_can_delete;

    cb.set_reserve1 = ${i}_set_reserve1;
    cb.set_reserve2 = ${i}_set_reserve2;
    cb.set_action = ${i}_set_action;
    cb.set_commit = ${i}_set_commit;
    cb.set_free = ${i}_set_free;
    cb.set_undo = ${i}_set_undo;
@end@
    DEBUGMSGTL(("initialize_table_$i",
                "Registering table $i "
                "as a table array\n"));
    netsnmp_table_container_register(my_handler, table_info, &cb,
                                     cb.container, 1);
}

/************************************************************
 * ${i}_get_value
 *
 * This routine is called for get requests to copy the data
 * from the context to the varbind for the request. If the
 * context has been properly maintained, you don't need to
 * change in code in this fuction.
 */
int ${i}_get_value(
            netsnmp_request_info *request,
            netsnmp_index *item,
            netsnmp_table_request_info *table_info )
{
    netsnmp_variable_list *var = request->requestvb;
    ${i}_context *context = (${i}_context *)item;

    switch(table_info->colnum) {

        @foreach $c column@
        @if $c.readable@
            @eval $have_type = 0@
        case COLUMN_$c.uc:
            /** $c.syntax = $c.type */
            @if "$c.type" eq "ASN_OBJECT_ID"@
                @eval $have_type = 1@
            snmp_set_var_typed_value(var, $c.type,
                         (char*)&context->$c,
                         context->${c}_len );
            @end@
            @if "$c.type" eq "ASN_OCTET_STR"@
                @eval $have_type = 1@
            snmp_set_var_typed_value(var, $c.type,
                         (char*)&context->$c,
                         context->${c}_len );
            @end@
            @if $have_type == 0@
            snmp_set_var_typed_value(var, $c.type,
                         (char*)&context->$c,
                         sizeof(context->$c) );
            @end@
        break;
    
        @end@
        @end@
    default: /** We shouldn't get here */
        snmp_log(LOG_ERR, "unknown column in "
                 "${i}_get_value\n");
        return SNMP_ERR_GENERR;
    }
    return SNMP_ERR_NOERROR;
}

/************************************************************
 * ${i}_get_by_idx
 */
const ${i}_context *
${i}_get_by_idx(netsnmp_index * hdr)
{
    return (const ${i}_context *)
        CONTAINER_FIND(cb.container, hdr );
}


@end@