forked from net-snmp/net-snmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp_agent.c
4273 lines (3785 loc) · 130 KB
/
snmp_agent.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
/*
* snmp_agent.c
*
* Simple Network Management Protocol (RFC 1067).
*/
/* Portions of this file are subject to the following copyrights. See
* the Net-SNMP's COPYING file for more details and other copyrights
* that may apply:
*/
/***********************************************************
Copyright 1988, 1989 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
/*
* 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 snmp_agent net-snmp agent related processing
* @ingroup agent
*
* @{
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <sys/types.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#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_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <errno.h>
#define SNMP_NEED_REQUEST_LIST
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/agent_callbacks.h>
#include <net-snmp/library/large_fd_set.h>
#include <net-snmp/library/snmp_assert.h>
#include "agent_global_vars.h"
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef NETSNMP_USE_LIBWRAP
#include <tcpd.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif
#include "snmpd.h"
#include <net-snmp/agent/mib_module_config.h>
#include <net-snmp/agent/mib_modules.h>
#ifdef USING_AGENTX_PROTOCOL_MODULE
#include "agentx/protocol.h"
#endif
#ifdef USING_AGENTX_MASTER_MODULE
#include "agentx/master.h"
#endif
#ifdef USING_SMUX_MODULE
#include "smux/smux.h"
#endif
netsnmp_feature_child_of(snmp_agent, libnetsnmpagent);
netsnmp_feature_child_of(agent_debugging_utilities, libnetsnmpagent);
netsnmp_feature_child_of(allocate_globalcacheid, snmp_agent);
netsnmp_feature_child_of(free_agent_snmp_session_by_session, snmp_agent);
netsnmp_feature_child_of(check_all_requests_error, snmp_agent);
netsnmp_feature_child_of(check_requests_error, snmp_agent);
netsnmp_feature_child_of(request_set_error_idx, snmp_agent);
netsnmp_feature_child_of(set_agent_uptime, snmp_agent);
netsnmp_feature_child_of(agent_check_and_process, snmp_agent);
netsnmp_feature_child_of(dump_sess_list, agent_debugging_utilities);
netsnmp_feature_child_of(agent_remove_list_data, netsnmp_unused);
netsnmp_feature_child_of(set_all_requests_error, netsnmp_unused);
netsnmp_feature_child_of(addrcache_age, netsnmp_unused);
netsnmp_feature_child_of(delete_subtree_cache, netsnmp_unused);
#ifndef NETSNMP_NO_PDU_STATS
static netsnmp_container *_pdu_stats = NULL;
static int _pdu_stats_max = 0;
static u_long _pdu_stats_threshold = 0;
static u_long _pdu_stats_current_lowest = 0;
netsnmp_container *
netsnmp_get_pdu_stats(void)
{
return _pdu_stats;
}
int _pdu_stats_compare(const netsnmp_pdu_stats * lhs,
const netsnmp_pdu_stats * rhs)
{
if (NULL == lhs || NULL == rhs) {
snmp_log(LOG_WARNING,
"WARNING: results undefined for compares with NULL\n");
return 0;
}
/** we want list sorted in reverse order, so invert tests */
if (lhs->processing_time > rhs->processing_time)
return -1;
else if (lhs->processing_time < rhs->processing_time)
return 1;
if (lhs->timestamp > rhs->timestamp)
return -1;
else if (lhs->timestamp < rhs->timestamp)
return 1;
return 0;
}
static void
_pdu_stats_init(void) {
static int done = 0;
if ((NULL != _pdu_stats) || (++done != 1))
return;
_pdu_stats = netsnmp_container_find("netsnmp_pdustats:binary_array");
if (NULL == _pdu_stats) {
done = 0;
return;
}
_pdu_stats->compare = (netsnmp_container_compare*)_pdu_stats_compare;
_pdu_stats->get_subset = NULL; /** subsets not supported */
_pdu_stats_max = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PDU_STATS_MAX);
_pdu_stats_threshold =
netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_PDU_STATS_THRESHOLD);
if (_pdu_stats_threshold < 100)
_pdu_stats_threshold = 100;
DEBUGMSGTL(("stats:pdu", "max: %d, threshold %ld ms\n", _pdu_stats_max,
_pdu_stats_threshold));
}
static void
_pdu_stats_shutdown(void)
{
netsnmp_pdu_stats *entry;
int x = 0;
if (NULL == _pdu_stats)
return;
for( ; x < CONTAINER_SIZE(_pdu_stats); ++x) {
CONTAINER_GET_AT(_pdu_stats, x, (void**)&entry);
if (NULL == entry)
continue;
snmp_free_pdu(entry->pdu);
free(entry);
}
CONTAINER_FREE(_pdu_stats);
_pdu_stats = NULL;
}
static void
_dump_pdu_stats(void)
{
int x = 0;
struct tm *tm;
char timestr[40];
netsnmp_pdu_stats *entry;
for( ; x < CONTAINER_SIZE(_pdu_stats); ++x) {
netsnmp_pdu *response;
netsnmp_variable_list *vars;
CONTAINER_GET_AT(_pdu_stats, x, (void**)&entry);
if (NULL == entry) {
DEBUGMSGT_NC(("9:stats:pdu", "[%d] ERROR\n", x));
continue;
}
tm = localtime(&entry->timestamp);
if (NULL == tm)
sprintf(timestr, "UNKNOWN");
else if (strftime(timestr, sizeof(timestr), "%m/%d/%Y %H:%M:%S",
tm) == 0)
sprintf(timestr, "UNKNOWN");
DEBUGMSGT_NC(("9:stats:pdu", "[%d] %ld ms, %s\n",
x, entry->processing_time, timestr));
response = entry->pdu;
if (response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars;
vars = vars->next_variable) {
DEBUGMSGT_NC(("9:stats:pdu", " vb "));
DEBUGMSGVAR(("9:stats:pdu", vars));
DEBUGMSG_NC(("9:stats:pdu", "\n"));
}
} else {
DEBUGMSGT_NC(("9:stats:pdu", "Error in packet: Reason: %s\n",
snmp_errstring(response->errstat)));
if (response->errindex != 0) {
int count;
DEBUGMSGT_NC(("9:stats:pdu", "Failed object: "));
for (count = 1, vars = response->variables;
vars && count != response->errindex;
vars = vars->next_variable, count++)
/*EMPTY*/;
if (vars) {
DEBUGMSGOID(("9:stats:pdu", vars->name,
vars->name_length));
}
DEBUGMSG_NC(("9:stats:pdu", "\n"));
}
}
}
}
#endif /* NETSNMP_NO_PDU_STATS */
NETSNMP_INLINE void
netsnmp_agent_add_list_data(netsnmp_agent_request_info *ari,
netsnmp_data_list *node)
{
if (ari) {
if (ari->agent_data) {
netsnmp_add_list_data(&ari->agent_data, node);
} else {
ari->agent_data = node;
}
}
}
#ifndef NETSNMP_FEATURE_REMOVE_AGENT_REMOVE_LIST_DATA
NETSNMP_INLINE int
netsnmp_agent_remove_list_data(netsnmp_agent_request_info *ari,
const char * name)
{
if ((NULL == ari) || (NULL == ari->agent_data))
return 1;
return netsnmp_remove_list_node(&ari->agent_data, name);
}
#endif /* NETSNMP_FEATURE_REMOVE_AGENT_REMOVE_LIST_DATA */
NETSNMP_INLINE void *
netsnmp_agent_get_list_data(netsnmp_agent_request_info *ari,
const char *name)
{
if (ari) {
return netsnmp_get_list_data(ari->agent_data, name);
}
return NULL;
}
NETSNMP_INLINE void
netsnmp_free_agent_data_set(netsnmp_agent_request_info *ari)
{
if (ari) {
netsnmp_free_list_data(ari->agent_data);
}
}
NETSNMP_INLINE void
netsnmp_free_agent_data_sets(netsnmp_agent_request_info *ari)
{
if (ari) {
netsnmp_free_all_list_data(ari->agent_data);
}
}
NETSNMP_INLINE void
netsnmp_free_agent_request_info(netsnmp_agent_request_info *ari)
{
if (ari) {
if (ari->agent_data) {
netsnmp_free_all_list_data(ari->agent_data);
}
SNMP_FREE(ari);
}
}
const oid version_sysoid[] = { NETSNMP_SYSTEM_MIB };
const int version_sysoid_len = OID_LENGTH(version_sysoid);
#define SNMP_ADDRCACHE_SIZE 10
#define SNMP_ADDRCACHE_MAXAGE 300 /* in seconds */
enum {
SNMP_ADDRCACHE_UNUSED = 0,
SNMP_ADDRCACHE_USED = 1
};
struct addrCache {
char *addr;
int status;
struct timeval lastHitM;
};
static struct addrCache addrCache[SNMP_ADDRCACHE_SIZE];
int log_addresses = 0;
typedef struct _agent_nsap {
int handle;
netsnmp_transport *t;
void *s; /* Opaque internal session pointer. */
struct _agent_nsap *next;
} agent_nsap;
static agent_nsap *agent_nsap_list = NULL;
static netsnmp_agent_session *agent_session_list = NULL;
netsnmp_agent_session *netsnmp_processing_set = NULL;
netsnmp_agent_session *agent_delegated_list = NULL;
netsnmp_agent_session *netsnmp_agent_queued_list = NULL;
int handle_pdu(netsnmp_agent_session *asp);
int netsnmp_handle_request(netsnmp_agent_session *asp,
int status);
int check_delayed_request(netsnmp_agent_session *asp);
int handle_getnext_loop(netsnmp_agent_session *asp);
int handle_set_loop(netsnmp_agent_session *asp);
int netsnmp_remove_from_delegated(netsnmp_agent_session *asp);
int netsnmp_running = 1;
#ifndef NETSNMP_FEATURE_REMOVE_ALLOCATE_GLOBALCACHEID
static int current_globalid = 0;
int
netsnmp_allocate_globalcacheid(void)
{
return ++current_globalid;
}
#endif /* NETSNMP_FEATURE_REMOVE_ALLOCATE_GLOBALCACHEID */
int
netsnmp_get_local_cachid(netsnmp_cachemap *cache_store, int globalid)
{
while (cache_store != NULL) {
if (cache_store->globalid == globalid)
return cache_store->cacheid;
cache_store = cache_store->next;
}
return -1;
}
netsnmp_cachemap *
netsnmp_get_or_add_local_cachid(netsnmp_cachemap **cache_store,
int globalid, int localid)
{
netsnmp_cachemap *tmpp;
tmpp = SNMP_MALLOC_TYPEDEF(netsnmp_cachemap);
if (tmpp != NULL) {
if (*cache_store) {
tmpp->next = *cache_store;
*cache_store = tmpp;
} else {
*cache_store = tmpp;
}
tmpp->globalid = globalid;
tmpp->cacheid = localid;
}
return tmpp;
}
void
netsnmp_free_cachemap(netsnmp_cachemap *cache_store)
{
netsnmp_cachemap *tmpp;
while (cache_store) {
tmpp = cache_store;
cache_store = cache_store->next;
SNMP_FREE(tmpp);
}
}
typedef struct agent_set_cache_s {
/*
* match on these 2
*/
int transID;
netsnmp_session *sess;
/*
* store this info
*/
netsnmp_tree_cache *treecache;
int treecache_len;
int treecache_num;
int vbcount;
netsnmp_request_info *requests;
netsnmp_variable_list *saved_vars;
netsnmp_data_list *agent_data;
/*
* list
*/
struct agent_set_cache_s *next;
} agent_set_cache;
static agent_set_cache *Sets = NULL;
agent_set_cache *
save_set_cache(netsnmp_agent_session *asp)
{
agent_set_cache *ptr;
if (!asp || !asp->reqinfo || !asp->pdu)
return NULL;
ptr = SNMP_MALLOC_TYPEDEF(agent_set_cache);
if (ptr == NULL)
return NULL;
/*
* Save the important information
*/
DEBUGMSGTL(("verbose:asp", "asp %p reqinfo %p saved in cache (mode %d)\n",
asp, asp->reqinfo, asp->pdu->command));
ptr->transID = asp->pdu->transid;
ptr->sess = asp->session;
ptr->treecache = asp->treecache;
ptr->treecache_len = asp->treecache_len;
ptr->treecache_num = asp->treecache_num;
ptr->agent_data = asp->reqinfo->agent_data;
ptr->requests = asp->requests;
ptr->saved_vars = asp->pdu->variables; /* requests contains pointers to variables */
ptr->vbcount = asp->vbcount;
/*
* make the agent forget about what we've saved
*/
asp->treecache = NULL;
asp->reqinfo->agent_data = NULL;
asp->pdu->variables = NULL;
asp->requests = NULL;
ptr->next = Sets;
Sets = ptr;
return ptr;
}
int
get_set_cache(netsnmp_agent_session *asp)
{
agent_set_cache *ptr, *prev = NULL;
for (ptr = Sets; ptr != NULL; ptr = ptr->next) {
if (ptr->sess == asp->session && ptr->transID == asp->pdu->transid) {
/*
* remove this item from list
*/
if (prev)
prev->next = ptr->next;
else
Sets = ptr->next;
/*
* found it. Get the needed data
*/
asp->treecache = ptr->treecache;
asp->treecache_len = ptr->treecache_len;
asp->treecache_num = ptr->treecache_num;
/*
* Free previously allocated requests before overwriting by
* cached ones, otherwise memory leaks!
*/
if (asp->requests) {
/*
* I don't think this case should ever happen. Please email
* the net-snmp-coders@lists.sourceforge.net if you have
* a test case that hits this condition. -- rstory
*/
int i;
netsnmp_assert(NULL == asp->requests); /* see note above */
for (i = 0; i < asp->vbcount; i++) {
netsnmp_free_request_data_sets(&asp->requests[i]);
}
free(asp->requests);
}
/*
* If we replace asp->requests with the info from the set cache,
* we should replace asp->pdu->variables also with the cached
* info, as asp->requests contains pointers to them. And we
* should also free the current asp->pdu->variables list...
*/
if (ptr->saved_vars) {
if (asp->pdu->variables)
snmp_free_varbind(asp->pdu->variables);
asp->pdu->variables = ptr->saved_vars;
asp->vbcount = ptr->vbcount;
}
asp->requests = ptr->requests;
netsnmp_assert(NULL != asp->reqinfo);
asp->reqinfo->asp = asp;
asp->reqinfo->agent_data = ptr->agent_data;
/*
* update request reqinfo, if it's out of date.
* yyy-rks: investigate when/why sometimes they match,
* sometimes they don't.
*/
if(asp->requests && asp->requests->agent_req_info != asp->reqinfo) {
/*
* - one don't match case: agentx subagents. prev asp & reqinfo
* freed, request reqinfo ptrs not cleared.
*/
netsnmp_request_info *tmp = asp->requests;
DEBUGMSGTL(("verbose:asp",
" reqinfo %p doesn't match cached reqinfo %p\n",
asp->reqinfo, asp->requests->agent_req_info));
for(; tmp; tmp = tmp->next)
tmp->agent_req_info = asp->reqinfo;
} else if (asp->requests) {
/*
* - match case: ?
*/
DEBUGMSGTL(("verbose:asp",
" reqinfo %p matches cached reqinfo %p\n",
asp->reqinfo, asp->requests->agent_req_info));
} else {
DEBUGMSGTL(("verbose:asp", " asp->requests is NULL\n"));
}
SNMP_FREE(ptr);
return SNMP_ERR_NOERROR;
}
prev = ptr;
}
return SNMP_ERR_GENERR;
}
/* Bulkcache holds the values for the *repeating* varbinds (only),
* but ordered "by column" - i.e. the repetitions for each
* repeating varbind follow on immediately from one another,
* rather than being interleaved, as required by the protocol.
*
* So we need to rearrange the varbind list so it's ordered "by row".
*
* In the following code chunk:
* n = # non-repeating varbinds
* r = # repeating varbinds
* asp->vbcount = # varbinds in the incoming PDU
* (So asp->vbcount = n+r)
*
* repeats = Desired # of repetitions (of 'r' varbinds)
*/
NETSNMP_STATIC_INLINE void
_reorder_getbulk(netsnmp_agent_session *asp)
{
int i, n = 0, r = 0;
int repeats;
int j, k;
int all_eoMib;
netsnmp_variable_list *prev = NULL, *curr;
if (NULL == asp || NULL == asp->pdu || asp->vbcount == 0)
return;
if (asp->pdu->errstat < asp->vbcount) {
n = asp->pdu->errstat;
} else {
n = asp->vbcount;
}
if ((r = asp->vbcount - n) < 0) {
r = 0;
}
DEBUGMSGTL(("snmp_agent:bulk", "reorder n=%d, r=%d\n", n, r));
/* we do nothing if there is nothing repeated */
if (r == 0)
return;
repeats = asp->pdu->errindex;
/* Fix endOfMibView entries. */
for (i = 0; i < r; i++) {
prev = NULL;
for (j = 0; j < repeats; j++) {
curr = asp->bulkcache[i * repeats + j];
/*
* If we don't have a valid name for a given repetition
* (and probably for all the ones that follow as well),
* extend the previous result to indicate 'endOfMibView'.
* Or if the repetition already has type endOfMibView make
* sure it has the correct objid (i.e. that of the previous
* entry or that of the original request).
*/
if (curr->name_length == 0 || curr->type == SNMP_ENDOFMIBVIEW) {
if (prev == NULL) {
/* Use objid from original pdu. */
prev = asp->orig_pdu->variables;
for (k = i; prev && k > 0; k--)
prev = prev->next_variable;
}
if (prev) {
snmp_set_var_objid(curr, prev->name, prev->name_length);
snmp_set_var_typed_value(curr, SNMP_ENDOFMIBVIEW, NULL, 0);
}
}
prev = curr;
}
}
/*
* For each of the original repeating varbinds (except the last),
* go through the block of results for that varbind,
* and link each instance to the corresponding instance
* in the next block.
*/
for (i = 0; i < r - 1; i++) {
for (j = 0; j < repeats; j++) {
asp->bulkcache[i * repeats + j]->next_variable =
asp->bulkcache[(i + 1) * repeats + j];
}
}
/*
* For the last of the original repeating varbinds,
* go through that block of results, and link each
* instance to the *next* instance in the *first* block.
*
* The very last instance of this block is left untouched
* since it (correctly) points to the end of the list.
*/
for (j = 0; j < repeats - 1; j++) {
asp->bulkcache[(r - 1) * repeats + j]->next_variable =
asp->bulkcache[j + 1];
}
/*
* If we've got a full row of endOfMibViews, then we
* can truncate the result varbind list after that.
*
* Look for endOfMibView exception values in the list of
* repetitions for the first varbind, and check the
* corresponding instances for the other varbinds
* (following the next_variable links).
*
* If they're all endOfMibView too, then we can terminate
* the linked list there, and free any redundant varbinds.
*/
all_eoMib = 0;
for (i = 0; i < repeats; i++) {
if (asp->bulkcache[i]->type == SNMP_ENDOFMIBVIEW) {
all_eoMib = 1;
for (j = 1, prev=asp->bulkcache[i];
j < r;
j++, prev=prev->next_variable) {
if (prev->type != SNMP_ENDOFMIBVIEW) {
all_eoMib = 0;
break; /* Found a real value */
}
}
if (all_eoMib) {
/*
* This is indeed a full endOfMibView row.
* Terminate the list here & free the rest.
*/
snmp_free_varbind( prev->next_variable );
prev->next_variable = NULL;
break;
}
}
}
}
/* EndOfMibView replies to a GETNEXT request should according to RFC3416
* have the object ID set to that of the request. Our tree search
* algorithm will sometimes break that requirement. This function will
* fix that.
*/
NETSNMP_STATIC_INLINE void
_fix_endofmibview(netsnmp_agent_session *asp)
{
netsnmp_variable_list *vb, *ovb;
if (asp->vbcount == 0) /* Nothing to do! */
return;
for (vb = asp->pdu->variables, ovb = asp->orig_pdu->variables;
vb && ovb; vb = vb->next_variable, ovb = ovb->next_variable) {
if (vb->type == SNMP_ENDOFMIBVIEW)
snmp_set_var_objid(vb, ovb->name, ovb->name_length);
}
}
#ifndef NETSNMP_FEATURE_REMOVE_AGENT_CHECK_AND_PROCESS
/**
* This function checks for packets arriving on the SNMP port and
* processes them(snmp_read) if some are found, using the select(). If block
* is non zero, the function call blocks until a packet arrives
*
* @param block used to control blocking in the select() function, 1 = block
* forever, and 0 = don't block
*
* @return Returns a positive integer if packets were processed, and -1 if an
* error was found.
*
*/
int
agent_check_and_process(int block)
{
int numfds;
netsnmp_large_fd_set readfds;
netsnmp_large_fd_set writefds;
netsnmp_large_fd_set exceptfds;
struct timeval timeout = { LONG_MAX, 0 }, *tvp = &timeout;
int count;
int fakeblock = 0;
numfds = 0;
netsnmp_large_fd_set_init(&readfds, FD_SETSIZE);
netsnmp_large_fd_set_init(&writefds, FD_SETSIZE);
netsnmp_large_fd_set_init(&exceptfds, FD_SETSIZE);
NETSNMP_LARGE_FD_ZERO(&readfds);
NETSNMP_LARGE_FD_ZERO(&writefds);
NETSNMP_LARGE_FD_ZERO(&exceptfds);
snmp_select_info2(&numfds, &readfds, tvp, &fakeblock);
if (block != 0 && fakeblock != 0) {
/*
* There are no alarms registered, and the caller asked for blocking, so
* let select() block forever.
*/
tvp = NULL;
} else if (block != 0 && fakeblock == 0) {
/*
* The caller asked for blocking, but there is an alarm due sooner than
* LONG_MAX seconds from now, so use the modified timeout returned by
* snmp_select_info as the timeout for select().
*/
} else if (block == 0) {
/*
* The caller does not want us to block at all.
*/
timerclear(tvp);
}
#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
netsnmp_external_event_info2(&numfds, &readfds, &writefds, &exceptfds);
#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
count = netsnmp_large_fd_set_select(numfds, &readfds, &writefds, &exceptfds, tvp);
if (count > 0) {
/*
* packets found, process them
*/
#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
netsnmp_dispatch_external_events2(&count, &readfds, &writefds, &exceptfds);
#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
snmp_read2(&readfds);
} else
switch (count) {
case 0:
snmp_timeout();
break;
case -1:
if (errno != EINTR) {
snmp_log_perror("select");
}
count = -1;
goto exit;
default:
snmp_log(LOG_ERR, "select returned %d\n", count);
count = -1;
goto exit;
} /* endif -- count>0 */
/*
* see if persistent store needs to be saved
*/
snmp_store_if_needed();
/*
* Run requested alarms.
*/
run_alarms();
netsnmp_check_outstanding_agent_requests();
exit:
netsnmp_large_fd_set_cleanup(&readfds);
netsnmp_large_fd_set_cleanup(&writefds);
netsnmp_large_fd_set_cleanup(&exceptfds);
return count;
}
#endif /* NETSNMP_FEATURE_REMOVE_AGENT_CHECK_AND_PROCESS */
/*
* Set up the address cache.
*/
void
netsnmp_addrcache_initialise(void)
{
int i = 0;
for (i = 0; i < SNMP_ADDRCACHE_SIZE; i++) {
addrCache[i].addr = NULL;
addrCache[i].status = SNMP_ADDRCACHE_UNUSED;
}
}
void netsnmp_addrcache_destroy(void)
{
int i = 0;
for (i = 0; i < SNMP_ADDRCACHE_SIZE; i++) {
if (addrCache[i].status == SNMP_ADDRCACHE_USED) {
free(addrCache[i].addr);
addrCache[i].status = SNMP_ADDRCACHE_UNUSED;
}
}
}
/*
* Adds a new entry to the cache of addresses that
* have recently made connections to the agent.
* Returns 0 if the entry already exists (but updates
* the entry with a new timestamp) and 1 if the
* entry did not previously exist.
*
* Implements a simple LRU cache replacement
* policy. Uses a linear search, which should be
* okay, as long as SNMP_ADDRCACHE_SIZE remains
* relatively small.
*
* @retval 0 : updated existing entry
* @retval 1 : added new entry
*/
int
netsnmp_addrcache_add(const char *addr)
{
int oldest = -1; /* Index of the oldest cache entry */
int unused = -1; /* Index of the first free cache entry */
int i; /* Looping variable */
int rc = -1;
struct timeval now; /* What time is it now? */
struct timeval aged; /* Oldest allowable cache entry */
/*
* First get the current and oldest allowable timestamps
*/
netsnmp_get_monotonic_clock(&now);
aged.tv_sec = now.tv_sec - SNMP_ADDRCACHE_MAXAGE;
aged.tv_usec = now.tv_usec;
/*
* Now look for a place to put this thing
*/
for(i = 0; i < SNMP_ADDRCACHE_SIZE; i++) {
if (addrCache[i].status == SNMP_ADDRCACHE_UNUSED) { /* If unused */
/*
* remember this location, in case addr isn't in the cache
*/
if (unused < 0)
unused = i;
}
else { /* If used */
if ((NULL != addr) && (strcmp(addrCache[i].addr, addr) == 0)) {
/*
* found a match
*/
addrCache[i].lastHitM = now;
if (timercmp(&addrCache[i].lastHitM, &aged, <))
rc = 1; /* should have expired, so is new */
else
rc = 0; /* not expired, so is existing entry */
break;
}
else {
/*
* Used, but not this address. check if it's stale.
*/
if (timercmp(&addrCache[i].lastHitM, &aged, <)) {
/*
* Stale, reuse
*/
SNMP_FREE(addrCache[i].addr);
addrCache[i].status = SNMP_ADDRCACHE_UNUSED;
/*
* remember this location, in case addr isn't in the cache
*/
if (unused < 0)
unused = i;
}
else {
/*
* Still fresh, but a candidate for LRU replacement
*/
if (oldest < 0)
oldest = i;
else if (timercmp(&addrCache[i].lastHitM,
&addrCache[oldest].lastHitM, <))
oldest = i;
} /* fresh */
} /* used, no match */
} /* used */
} /* for loop */
if ((-1 == rc) && (NULL != addr)) {
/*
* We didn't find the entry in the cache
*/
if (unused >= 0) {
/*
* If we have a slot free anyway, use it
*/
addrCache[unused].addr = strdup(addr);
addrCache[unused].status = SNMP_ADDRCACHE_USED;
addrCache[unused].lastHitM = now;
}
else { /* Otherwise, replace oldest entry */
if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_VERBOSE))
snmp_log(LOG_INFO, "Purging address from address cache: %s",
addrCache[oldest].addr);
free(addrCache[oldest].addr);
addrCache[oldest].addr = strdup(addr);
addrCache[oldest].lastHitM = now;
}
rc = 1;
}
if ((log_addresses && (1 == rc)) ||
netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_VERBOSE)) {
snmp_log(LOG_INFO, "Received SNMP packet(s) from %s\n", addr);
}
return rc;