forked from net-snmp/net-snmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_registry.c
2441 lines (2174 loc) · 77.4 KB
/
agent_registry.c
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
/*
* agent_registry.c
*/
/* Portions of this file are subject to the following copyright(s). See
* the Net-SNMP's COPYING file for more details and other copyrights
* that may apply:
*/
/*
* Portions of this file are copyrighted by:
* Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*
* Portions of this file are copyrighted by:
* Copyright (c) 2016 VMware, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
/** @defgroup agent_registry Registry of MIB subtrees, modules, sessions, etc
* Maintain a registry of MIB subtrees, together with related information
* regarding MIB modules, sessions, etc
* @ingroup agent
*
* @{
*/
#define IN_SNMP_VARS_C
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <signal.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/snmp_assert.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/agent_callbacks.h>
#include "snmpd.h"
#include "agent_global_vars.h"
#include "mibgroup/struct.h"
#include <net-snmp/agent/old_api.h>
#include <net-snmp/agent/null.h>
#include <net-snmp/agent/table.h>
#include <net-snmp/agent/table_iterator.h>
#include <net-snmp/agent/agent_index.h>
#include <net-snmp/agent/agent_registry.h>
#ifdef USING_AGENTX_SUBAGENT_MODULE
#include "agentx/subagent.h"
#include "agentx/client.h"
#endif
netsnmp_feature_child_of(agent_registry_all, libnetsnmpagent);
netsnmp_feature_child_of(unregister_mib_table_row, agent_registry_all);
/** @defgroup agent_lookup_cache Lookup cache, storing the registered OIDs.
* Maintain the cache used for locating sub-trees and OIDs.
* @ingroup agent_registry
*
* @{
*/
/** Lookup cache - default size.*/
#define SUBTREE_DEFAULT_CACHE_SIZE 8
/** Lookup cache - max acceptable size.*/
#define SUBTREE_MAX_CACHE_SIZE 32
int lookup_cache_size = 0; /*enabled later after registrations are loaded */
typedef struct lookup_cache_s {
netsnmp_subtree *next;
netsnmp_subtree *previous;
} lookup_cache;
typedef struct lookup_cache_context_s {
char *context;
struct lookup_cache_context_s *next;
int thecachecount;
int currentpos;
lookup_cache cache[SUBTREE_MAX_CACHE_SIZE];
} lookup_cache_context;
static lookup_cache_context *thecontextcache = NULL;
/** Set the lookup cache size for optimized agent registration performance.
* Note that it is only used by master agent - sub-agent doesn't need the cache.
* The rough guide is that the cache size should be equal to the maximum
* number of simultaneous managers you expect to talk to the agent (M) times 80%
* (or so, he says randomly) the average number (N) of varbinds you
* expect to receive in a given request for a manager. ie, M times N.
* Bigger does NOT necessarily mean better. Certainly 16 should be an
* upper limit. 32 is the hard coded limit.
*
* @param newsize set to the maximum size of a cache for a given
* context. Set to 0 to completely disable caching, or to -1 to set
* to the default cache size (8), or to a number of your chosing. The
*/
void
netsnmp_set_lookup_cache_size(int newsize) {
if (newsize < 0)
lookup_cache_size = SUBTREE_DEFAULT_CACHE_SIZE;
else if (newsize < SUBTREE_MAX_CACHE_SIZE)
lookup_cache_size = newsize;
else
lookup_cache_size = SUBTREE_MAX_CACHE_SIZE;
}
/** Retrieves the current value of the lookup cache size
* Should be called from master agent only - sub-agent doesn't need the cache.
*
* @return the current lookup cache size
*/
int
netsnmp_get_lookup_cache_size(void) {
return lookup_cache_size;
}
/** Returns lookup cache entry for the context of given name.
*
* @param context Name of the context. Name is case sensitive.
*
* @return the lookup cache context
*/
NETSNMP_STATIC_INLINE lookup_cache_context *
get_context_lookup_cache(const char *context) {
lookup_cache_context *ptr;
if (!context)
context = "";
for(ptr = thecontextcache; ptr; ptr = ptr->next) {
if (strcmp(ptr->context, context) == 0)
break;
}
if (!ptr) {
if (netsnmp_subtree_find_first(context)) {
ptr = SNMP_MALLOC_TYPEDEF(lookup_cache_context);
if (!ptr)
return NULL;
ptr->next = thecontextcache;
ptr->context = strdup(context);
thecontextcache = ptr;
} else {
return NULL;
}
}
return ptr;
}
/** Adds an entry to the Lookup Cache under specified context name.
*
* @param context Name of the context. Name is case sensitive.
*
* @param next Next subtree item.
*
* @param previous Previous subtree item.
*/
NETSNMP_STATIC_INLINE void
lookup_cache_add(const char *context,
netsnmp_subtree *next, netsnmp_subtree *previous) {
lookup_cache_context *cptr;
if ((cptr = get_context_lookup_cache(context)) == NULL)
return;
if (cptr->thecachecount < lookup_cache_size)
cptr->thecachecount++;
cptr->cache[cptr->currentpos].next = next;
cptr->cache[cptr->currentpos].previous = previous;
if (++cptr->currentpos >= lookup_cache_size)
cptr->currentpos = 0;
}
/** @private
* Replaces next and previous pointer in given Lookup Cache.
*
* @param ptr Lookup Cache pointer.
*
* @param next Next subtree item.
*
* @param previous Previous subtree item.
*/
NETSNMP_STATIC_INLINE void
lookup_cache_replace(lookup_cache *ptr,
netsnmp_subtree *next, netsnmp_subtree *previous) {
ptr->next = next;
ptr->previous = previous;
}
/** Finds an entry in the Lookup Cache.
*
* @param context Case sensitive name of the context.
*
* @param name The OID we're searching for.
*
* @param name_len Number of sub-ids (single integers) in the OID.
*
* @param retcmp Value set to snmp_oid_compare() call result.
* The value, if set, is always nonnegative.
*
* @return gives Lookup Cache entry, or NULL if not found.
*
* @see snmp_oid_compare()
*/
NETSNMP_STATIC_INLINE lookup_cache *
lookup_cache_find(const char *context, const oid *name, size_t name_len,
int *retcmp) {
lookup_cache_context *cptr;
lookup_cache *ret = NULL;
int cmp;
int i;
if ((cptr = get_context_lookup_cache(context)) == NULL)
return NULL;
for(i = 0; i < cptr->thecachecount && i < lookup_cache_size; i++) {
if (cptr->cache[i].previous->start_a)
cmp = snmp_oid_compare(name, name_len,
cptr->cache[i].previous->start_a,
cptr->cache[i].previous->start_len);
else
cmp = 1;
if (cmp >= 0) {
*retcmp = cmp;
ret = &(cptr->cache[i]);
}
}
return ret;
}
/** @private
* Clears cache count and position in Lookup Cache.
*/
NETSNMP_STATIC_INLINE void
invalidate_lookup_cache(const char *context) {
lookup_cache_context *cptr;
if ((cptr = get_context_lookup_cache(context)) != NULL) {
cptr->thecachecount = 0;
cptr->currentpos = 0;
}
}
void
clear_lookup_cache(void) {
lookup_cache_context *ptr = NULL, *next = NULL;
ptr = thecontextcache;
while (ptr) {
next = ptr->next;
SNMP_FREE(ptr->context);
SNMP_FREE(ptr);
ptr = next;
}
thecontextcache = NULL; /* !!! */
}
/** @} */
/* End of Lookup cache code */
/** @defgroup agent_context_cache Context cache, storing the OIDs under their contexts.
* Maintain the cache used for locating sub-trees registered under different contexts.
* @ingroup agent_registry
*
* @{
*/
subtree_context_cache *context_subtrees = NULL;
/** Returns the top element of context subtrees cache.
* Use it if you wish to sweep through the cache elements.
* Note that the return may be NULL (cache may be empty).
*
* @return pointer to topmost context subtree cache element.
*/
subtree_context_cache *
get_top_context_cache(void)
{
return context_subtrees;
}
/** Finds the first subtree registered under given context.
*
* @param context_name Text name of the context we're searching for.
*
* @return pointer to the first subtree element, or NULL if not found.
*/
netsnmp_subtree *
netsnmp_subtree_find_first(const char *context_name)
{
subtree_context_cache *ptr;
if (!context_name) {
context_name = "";
}
DEBUGMSGTL(("subtree", "looking for subtree for context: \"%s\"\n",
context_name));
for (ptr = context_subtrees; ptr != NULL; ptr = ptr->next) {
if (ptr->context_name != NULL &&
strcmp(ptr->context_name, context_name) == 0) {
DEBUGMSGTL(("subtree", "found one for: \"%s\"\n", context_name));
return ptr->first_subtree;
}
}
DEBUGMSGTL(("subtree", "didn't find a subtree for context: \"%s\"\n",
context_name));
return NULL;
}
/** Adds the subtree to Context Cache under given context name.
*
* @param context_name Text name of the context we're adding.
*
* @param new_tree The subtree to be added.
*
* @return copy of the new_tree pointer, or NULL if cannot add.
*/
netsnmp_subtree *
add_subtree(netsnmp_subtree *new_tree, const char *context_name)
{
subtree_context_cache *ptr = SNMP_MALLOC_TYPEDEF(subtree_context_cache);
if (!context_name) {
context_name = "";
}
if (!ptr) {
return NULL;
}
DEBUGMSGTL(("subtree", "adding subtree for context: \"%s\"\n",
context_name));
ptr->next = context_subtrees;
ptr->first_subtree = new_tree;
ptr->context_name = strdup(context_name);
context_subtrees = ptr;
return ptr->first_subtree;
}
void
netsnmp_remove_subtree(netsnmp_subtree *tree)
{
subtree_context_cache *ptr;
if (!tree->prev) {
for (ptr = context_subtrees; ptr; ptr = ptr->next)
if (ptr->first_subtree == tree)
break;
netsnmp_assert(ptr);
if (ptr)
ptr->first_subtree = tree->next;
} else
tree->prev->next = tree->next;
if (tree->next)
tree->next->prev = tree->prev;
}
/** Replaces first subtree registered under given context name.
* Overwrites a subtree pointer in Context Cache for the context name.
* The previous subtree pointer is lost. If there's no subtree
* under the supplied name, then a new cache item is created.
*
* @param new_tree The new subtree to be set.
*
* @param context_name Text name of the context we're replacing.
* It is case sensitive.
*
* @return copy of the new_tree pointer, or NULL on error.
*/
netsnmp_subtree *
netsnmp_subtree_replace_first(netsnmp_subtree *new_tree,
const char *context_name)
{
subtree_context_cache *ptr;
if (!context_name) {
context_name = "";
}
for (ptr = context_subtrees; ptr != NULL; ptr = ptr->next) {
if (ptr->context_name != NULL &&
strcmp(ptr->context_name, context_name) == 0) {
ptr->first_subtree = new_tree;
return ptr->first_subtree;
}
}
return add_subtree(new_tree, context_name);
}
void clear_subtree (netsnmp_subtree *sub);
/** Completely clears both the Context cache and the Lookup cache.
*/
void
clear_context(void) {
subtree_context_cache *ptr = NULL, *next = NULL;
netsnmp_subtree *t, *u;
DEBUGMSGTL(("agent_registry", "clear context\n"));
ptr = get_top_context_cache();
while (ptr) {
next = ptr->next;
for (t = ptr->first_subtree; t; t = u) {
u = t->next;
clear_subtree(t);
}
free(NETSNMP_REMOVE_CONST(char*, ptr->context_name));
SNMP_FREE(ptr);
ptr = next;
}
context_subtrees = NULL; /* !!! */
clear_lookup_cache();
}
/** @} */
/* End of Context cache code */
/** @defgroup agent_mib_subtree Maintaining MIB subtrees.
* Maintaining MIB nodes and subtrees.
* @ingroup agent_registry
*
* @{
*/
static void register_mib_detach_node(netsnmp_subtree *s);
/** Frees single subtree item.
* Deallocated memory for given netsnmp_subtree item, including
* Handle Registration structure stored inside this item.
* After calling this function, the pointer is invalid
* and should be set to NULL.
*
* @param a The subtree item to dispose.
*/
void
netsnmp_subtree_free(netsnmp_subtree *a)
{
if (a != NULL) {
if (a->variables != NULL && netsnmp_oid_equals(a->name_a, a->namelen,
a->start_a, a->start_len) == 0) {
SNMP_FREE(a->variables);
}
SNMP_FREE(a->name_a);
a->namelen = 0;
SNMP_FREE(a->start_a);
a->start_len = 0;
SNMP_FREE(a->end_a);
a->end_len = 0;
SNMP_FREE(a->label_a);
netsnmp_handler_registration_free(a->reginfo);
a->reginfo = NULL;
SNMP_FREE(a);
}
}
/** Creates deep copy of a subtree item.
* Duplicates all properties stored in the structure, including
* Handle Registration structure stored inside the item.
*
* @param a The subtree item to copy.
*
* @return deep copy of the subtree item, or NULL on error.
*/
netsnmp_subtree *
netsnmp_subtree_deepcopy(netsnmp_subtree *a)
{
netsnmp_subtree *b = calloc(1, sizeof(netsnmp_subtree));
if (b != NULL) {
memcpy(b, a, sizeof(netsnmp_subtree));
b->name_a = snmp_duplicate_objid(a->name_a, a->namelen);
b->start_a = snmp_duplicate_objid(a->start_a, a->start_len);
b->end_a = snmp_duplicate_objid(a->end_a, a->end_len);
b->label_a = strdup(a->label_a);
if (b->name_a == NULL || b->start_a == NULL ||
b->end_a == NULL || b->label_a == NULL) {
netsnmp_subtree_free(b);
return NULL;
}
if (a->variables != NULL) {
b->variables = (struct variable *)malloc(a->variables_len *
a->variables_width);
if (b->variables != NULL) {
memcpy(b->variables, a->variables,a->variables_len*a->variables_width);
} else {
netsnmp_subtree_free(b);
return NULL;
}
}
if (a->reginfo != NULL) {
b->reginfo = netsnmp_handler_registration_dup(a->reginfo);
if (b->reginfo == NULL) {
netsnmp_subtree_free(b);
return NULL;
}
}
}
return b;
}
/** @private
* Replaces next subtree pointer in given subtree.
*/
NETSNMP_STATIC_INLINE void
netsnmp_subtree_change_next(netsnmp_subtree *ptr, netsnmp_subtree *thenext)
{
ptr->next = thenext;
if (thenext)
netsnmp_oid_compare_ll(ptr->start_a,
ptr->start_len,
thenext->start_a,
thenext->start_len,
&thenext->oid_off);
}
/** @private
* Replaces previous subtree pointer in given subtree.
*/
NETSNMP_STATIC_INLINE void
netsnmp_subtree_change_prev(netsnmp_subtree *ptr, netsnmp_subtree *theprev)
{
ptr->prev = theprev;
if (theprev)
netsnmp_oid_compare_ll(theprev->start_a,
theprev->start_len,
ptr->start_a,
ptr->start_len,
&ptr->oid_off);
}
netsnmp_feature_child_of(netsnmp_subtree_compare,netsnmp_unused);
#ifndef NETSNMP_FEATURE_REMOVE_NETSNMP_SUBTREE_COMPARE
/** Compares OIDs of given subtrees.
*
* @param ap,bp Pointers to the subtrees to be compared.
*
* @return OIDs lexicographical comparison result.
*
* @see snmp_oid_compare()
*/
int
netsnmp_subtree_compare(const netsnmp_subtree *ap, const netsnmp_subtree *bp)
{
return snmp_oid_compare(ap->name_a, ap->namelen, bp->name_a, bp->namelen);
}
#endif /* NETSNMP_FEATURE_REMOVE_NETSNMP_SUBTREE_COMPARE */
/** Joins the given subtree with the current tree.
* Trees are joined and the one supplied as parameter is freed.
*
* @param root The subtree to be merged with current subtree.
* Do not use the pointer after joining - it may be invalid.
*/
void
netsnmp_subtree_join(netsnmp_subtree *root)
{
netsnmp_subtree *s, *tmp, *c, *d;
while (root != NULL) {
s = root->next;
while (s != NULL && root->reginfo == s->reginfo) {
tmp = s->next;
DEBUGMSGTL(("subtree", "root start "));
DEBUGMSGOID(("subtree", root->start_a, root->start_len));
DEBUGMSG(("subtree", " (original end "));
DEBUGMSGOID(("subtree", root->end_a, root->end_len));
DEBUGMSG(("subtree", ")\n"));
DEBUGMSGTL(("subtree", " JOINING to "));
DEBUGMSGOID(("subtree", s->start_a, s->start_len));
SNMP_FREE(root->end_a);
root->end_a = s->end_a;
root->end_len = s->end_len;
s->end_a = NULL;
for (c = root; c != NULL; c = c->children) {
netsnmp_subtree_change_next(c, s->next);
}
for (c = s; c != NULL; c = c->children) {
netsnmp_subtree_change_prev(c, root);
}
DEBUGMSG(("subtree", " so new end "));
DEBUGMSGOID(("subtree", root->end_a, root->end_len));
DEBUGMSG(("subtree", "\n"));
/*
* Probably need to free children too?
*/
for (c = s->children; c != NULL; c = d) {
d = c->children;
netsnmp_subtree_free(c);
}
netsnmp_subtree_free(s);
s = tmp;
}
root = root->next;
}
}
/** Split the subtree into two at the specified point.
* Subtrees of the given OID and separated and formed into the
* returned subtree.
*
* @param current The element at which splitting is started.
*
* @param name The OID we'd like to split.
*
* @param name_len Length of the OID.
*
* @return head of the new (second) subtree.
*/
netsnmp_subtree *
netsnmp_subtree_split(netsnmp_subtree *current, oid name[], int name_len)
{
struct variable *vp = NULL;
netsnmp_subtree *new_sub, *ptr;
int i = 0, rc = 0, rc2 = 0;
size_t common_len = 0;
char *cp;
oid *tmp_a, *tmp_b;
if (snmp_oid_compare(name, name_len, current->end_a, current->end_len)>0) {
/* Split comes after the end of this subtree */
return NULL;
}
new_sub = netsnmp_subtree_deepcopy(current);
if (new_sub == NULL) {
return NULL;
}
/* Set up the point of division. */
tmp_a = snmp_duplicate_objid(name, name_len);
if (tmp_a == NULL) {
netsnmp_subtree_free(new_sub);
return NULL;
}
tmp_b = snmp_duplicate_objid(name, name_len);
if (tmp_b == NULL) {
netsnmp_subtree_free(new_sub);
SNMP_FREE(tmp_a);
return NULL;
}
SNMP_FREE(current->end_a);
current->end_a = tmp_a;
current->end_len = name_len;
if (new_sub->start_a != NULL) {
SNMP_FREE(new_sub->start_a);
}
new_sub->start_a = tmp_b;
new_sub->start_len = name_len;
/* Split the variables between the two new subtrees. */
i = current->variables_len;
current->variables_len = 0;
for (vp = current->variables; i > 0; i--) {
/* Note that the variable "name" field omits the prefix common to the
whole registration, hence the strange comparison here. */
rc = snmp_oid_compare(vp->name, vp->namelen,
name + current->namelen,
name_len - current->namelen);
if (name_len - current->namelen > vp->namelen) {
common_len = vp->namelen;
} else {
common_len = name_len - current->namelen;
}
rc2 = snmp_oid_compare(vp->name, common_len,
name + current->namelen, common_len);
if (rc >= 0) {
break; /* All following variables belong to the second subtree */
}
current->variables_len++;
if (rc2 < 0) {
new_sub->variables_len--;
cp = (char *) new_sub->variables;
new_sub->variables = (struct variable *)(cp +
new_sub->variables_width);
}
vp = (struct variable *) ((char *) vp + current->variables_width);
}
/* Delegated trees should retain their variables regardless */
if (current->variables_len > 0 &&
IS_DELEGATED((u_char) current->variables[0].type)) {
new_sub->variables_len = 1;
new_sub->variables = current->variables;
}
/* Propogate this split down through any children */
if (current->children) {
new_sub->children = netsnmp_subtree_split(current->children,
name, name_len);
}
/* Retain the correct linking of the list */
for (ptr = current; ptr != NULL; ptr = ptr->children) {
netsnmp_subtree_change_next(ptr, new_sub);
}
for (ptr = new_sub; ptr != NULL; ptr = ptr->children) {
netsnmp_subtree_change_prev(ptr, current);
}
for (ptr = new_sub->next; ptr != NULL; ptr=ptr->children) {
netsnmp_subtree_change_prev(ptr, new_sub);
}
return new_sub;
}
/** Loads the subtree under given context name.
*
* @param new_sub The subtree to be loaded into current subtree.
*
* @param context_name Text name of the context we're searching for.
*
* @return gives MIB_REGISTERED_OK on success, error code otherwise.
*/
int
netsnmp_subtree_load(netsnmp_subtree *new_sub, const char *context_name)
{
netsnmp_subtree *tree1, *tree2;
netsnmp_subtree *prev, *next;
if (new_sub == NULL) {
return MIB_REGISTERED_OK; /* Degenerate case */
}
if (!netsnmp_subtree_find_first(context_name)) {
static int inloop = 0;
if (!inloop) {
oid ccitt[1] = { 0 };
oid iso[1] = { 1 };
oid joint_ccitt_iso[1] = { 2 };
inloop = 1;
netsnmp_register_null_context(snmp_duplicate_objid(ccitt, 1), 1,
context_name);
netsnmp_register_null_context(snmp_duplicate_objid(iso, 1), 1,
context_name);
netsnmp_register_null_context(snmp_duplicate_objid(joint_ccitt_iso, 1),
1, context_name);
inloop = 0;
}
}
/* Find the subtree that contains the start of the new subtree (if
any)...*/
tree1 = netsnmp_subtree_find(new_sub->start_a, new_sub->start_len,
NULL, context_name);
/* ... and the subtree that follows the new one (NULL implies this is the
final region covered). */
if (tree1 == NULL) {
tree2 = netsnmp_subtree_find_next(new_sub->start_a, new_sub->start_len,
NULL, context_name);
} else {
tree2 = tree1->next;
}
/* Handle new subtrees that start in virgin territory. */
if (tree1 == NULL) {
/*netsnmp_subtree *new2 = NULL;*/
/* Is there any overlap with later subtrees? */
if (tree2 && snmp_oid_compare(new_sub->end_a, new_sub->end_len,
tree2->start_a, tree2->start_len) > 0) {
/*new2 =*/
netsnmp_subtree_split(new_sub, tree2->start_a, tree2->start_len);
}
/* Link the new subtree (less any overlapping region) with the list of
existing registrations. */
if (tree2) {
netsnmp_subtree_change_prev(new_sub, tree2->prev);
netsnmp_subtree_change_prev(tree2, new_sub);
} else {
netsnmp_subtree_change_prev(new_sub,
netsnmp_subtree_find_prev(new_sub->start_a,
new_sub->start_len, NULL, context_name));
if (new_sub->prev) {
netsnmp_subtree_change_next(new_sub->prev, new_sub);
} else {
netsnmp_subtree_replace_first(new_sub, context_name);
}
netsnmp_subtree_change_next(new_sub, tree2);
#if 0
/* The code below cannot be reached which is why it has been
surrounded with #if 0 / #endif. */
/* If there was any overlap, recurse to merge in the overlapping
region (including anything that may follow the overlap). */
if (new2) {
return netsnmp_subtree_load(new2, context_name);
}
#endif
}
} else {
/* If the new subtree starts *within* an existing registration
(rather than at the same point as it), then split the existing
subtree at this point. */
if (netsnmp_oid_equals(new_sub->start_a, new_sub->start_len,
tree1->start_a, tree1->start_len) != 0) {
tree1 = netsnmp_subtree_split(tree1, new_sub->start_a,
new_sub->start_len);
}
if (tree1 == NULL) {
return MIB_REGISTRATION_FAILED;
}
/* Now consider the end of this existing subtree:
If it matches the new subtree precisely,
simply merge the new one into the list of children
If it includes the whole of the new subtree,
split it at the appropriate point, and merge again
If the new subtree extends beyond this existing region,
split it, and recurse to merge the two parts. */
switch (snmp_oid_compare(new_sub->end_a, new_sub->end_len,
tree1->end_a, tree1->end_len)) {
case -1:
/* Existing subtree contains new one. */
netsnmp_subtree_split(tree1, new_sub->end_a, new_sub->end_len);
NETSNMP_FALLTHROUGH;
case 0:
/* The two trees match precisely. */
/* Note: This is the only point where the original registration
OID ("name") is used. */
prev = NULL;
next = tree1;
while (next && next->namelen > new_sub->namelen) {
prev = next;
next = next->children;
}
while (next && next->namelen == new_sub->namelen &&
next->priority < new_sub->priority ) {
prev = next;
next = next->children;
}
if (next && (next->namelen == new_sub->namelen) &&
(next->priority == new_sub->priority)) {
if (new_sub->namelen != 1) { /* ignore root OID dups */
size_t out_len = 0;
size_t buf_len = 0;
char *buf = NULL;
int buf_overflow = 0;
netsnmp_sprint_realloc_objid((u_char **) &buf, &buf_len, &out_len,
1, &buf_overflow,
new_sub->start_a,
new_sub->start_len);
snmp_log(LOG_ERR,
"duplicate registration: MIB modules %s and %s (oid %s%s).\n",
next->label_a, new_sub->label_a,
buf ? buf : "",
buf_overflow ? " [TRUNCATED]" : "");
free(buf);
}
return MIB_DUPLICATE_REGISTRATION;
}
if (prev) {
prev->children = new_sub;
new_sub->children = next;
netsnmp_subtree_change_prev(new_sub, prev->prev);
netsnmp_subtree_change_next(new_sub, prev->next);
} else {
new_sub->children = next;
netsnmp_subtree_change_prev(new_sub, next->prev);
netsnmp_subtree_change_next(new_sub, next->next);
for (next = new_sub->next; next != NULL;next = next->children){
netsnmp_subtree_change_prev(next, new_sub);
}
for (prev = new_sub->prev; prev != NULL;prev = prev->children){
netsnmp_subtree_change_next(prev, new_sub);
}
}
break;
case 1:
/* New subtree contains the existing one. */
{
netsnmp_subtree *new2 =
netsnmp_subtree_split(new_sub, tree1->end_a,tree1->end_len);
int res = netsnmp_subtree_load(new_sub, context_name);
if (res != MIB_REGISTERED_OK) {
netsnmp_remove_subtree(new2);
netsnmp_subtree_free(new2);
return res;
}
return netsnmp_subtree_load(new2, context_name);
}
}
}
return 0;
}
/** Free the given subtree and all its children.
*
* @param sub Subtree branch to be cleared and freed.
* After the call, this pointer is invalid
* and should be set to NULL.
*/
void
clear_subtree (netsnmp_subtree *sub) {
netsnmp_subtree *c;
if (sub == NULL)
return;
for(c = sub; c;) {
sub = c;
c = c->children;
netsnmp_subtree_free(sub);
}
}
netsnmp_subtree *
netsnmp_subtree_find_prev(const oid *name, size_t len, netsnmp_subtree *subtree,
const char *context_name)
{
lookup_cache *lookup_cache = NULL;
netsnmp_subtree *myptr = NULL, *previous = NULL;
int cmp = 1;
size_t ll_off = 0;
if (subtree) {
myptr = subtree;
} else {
/* look through everything */
if (lookup_cache_size) {
lookup_cache = lookup_cache_find(context_name, name, len, &cmp);
if (lookup_cache) {
myptr = lookup_cache->next;
previous = lookup_cache->previous;
}
if (!myptr)
myptr = netsnmp_subtree_find_first(context_name);
} else {
myptr = netsnmp_subtree_find_first(context_name);