Debian Codegraph @

agent/mibgroup/sctp-mib/sctpScalars_linux.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4
5 #include "sctpScalars_common.h"
6
7 #include <stdio.h>
8 #include <errno.h>
9
10 #define PROC_PREFIX "/proc"
11 #define PROC_RTO_MIN PROC_PREFIX "/sys/net/sctp/rto_min"
12 #define PROC_RTO_MAX PROC_PREFIX "/sys/net/sctp/rto_max"
13 #define PROC_RTO_INITIAL PROC_PREFIX "/sys/net/sctp/rto_initial"
14 #define PROC_VAL_COOKIE_LIFE PROC_PREFIX "/sys/net/sctp/valid_cookie_life"
15 #define PROC_MAX_INIT_RETR PROC_PREFIX "/sys/net/sctp/max_init_retransmits"
16 #define PROC_STATS PROC_PREFIX "/net/sctp/snmp"
17
18 /**
19 * Reads one integer value from a file.
20 */
21 static int
22 load_uint_file(const char *filename, u_int * value)
23 {
24 FILE *f;
25 int ret;
26
27 f = fopen(filename, "r");
28 if (f == NULL) {
29 DEBUGMSGTL(("sctp:scalars:arch:load", "Cannot read file %s\n",
30 filename));
31 return -1;
32 }
33
34 ret = fscanf(f, "%u", value);
35 if (ret != 1) {
36 DEBUGMSGTL(("sctp:scalars:arch:load", "Malformed file %s\n",
37 filename));
38 fclose(f);
39 return -2;
40 }
41
42 fclose(f);
43 return 0;
44 }
45
46 void
47 netsnmp_access_sctp_stats_arch_init(void)
48 {
49 }
50
51 int
52 netsnmp_access_sctp_stats_arch_load(netsnmp_sctp_stats * sctp_stats)
53 {
54 FILE *f;
55 char line[100];
56 unsigned long long value;
57 char *delimiter;
58 int ret = 0;
59
60 DEBUGMSGTL(("sctp:scalars:stats:arch_load",
61 "netsnmp_access_sctp_stats_arch_load called"));
62
63 memset(sctp_stats, 0, sizeof(netsnmp_sctp_stats));
64 f = fopen(PROC_STATS, "r");
65 if (f == NULL)
66 return -1;
67
68 while (fgets(line, sizeof(line), f) != NULL) {
69 DEBUGMSGTL(("sctp:scalars:stats:arch_load", "read: %s", line));
70
71 delimiter = strchr(line, '\t');
72 if (delimiter == NULL) {
73 DEBUGMSGTL(("sctp:scalars:stats:arch_load",
74 "Malformed line, cannot find '\\t'!\n"));
75 fclose(f);
76 return -1;
77 }
78 errno = 0;
79 value = strtoull(delimiter + 1, NULL, 10);
80 if (errno != 0) {
81 DEBUGMSGTL(("sctp:scalars:stats:arch_load",
82 "Malformed value!'\n"));
83 fclose(f);
84 return -1;
85 }
86
87 if (line[4] == 'C' && line[8] == 'E')
88 sctp_stats->curr_estab = value;
89 else if (line[4] == 'A' && line[10] == 'E')
90 sctp_stats->active_estabs = value;
91 else if (line[4] == 'P' && line[11] == 'E')
92 sctp_stats->passive_estabs = value;
93 else if (line[4] == 'A' && line[5] == 'b')
94 sctp_stats->aborteds = value;
95 else if (line[4] == 'S' && line[5] == 'h')
96 sctp_stats->shutdowns = value;
97 else if (line[4] == 'O' && line[9] == 'B')
98 sctp_stats->out_of_blues = value;
99 else if (line[6] == 'e')
100 sctp_stats->checksum_errors = value;
101 else if (line[4] == 'O') {
102 if (line[7] == 'C') {
103 sctp_stats->out_ctrl_chunks.low = value & 0xffffffff;
104 sctp_stats->out_ctrl_chunks.high = value >> 32;
105 } else if (line[7] == 'O') {
106 sctp_stats->out_order_chunks.low = value & 0xffffffff;
107 sctp_stats->out_order_chunks.high = value >> 32;
108 } else if (line[7] == 'U') {
109 sctp_stats->out_unorder_chunks.low = value & 0xffffffff;
110 sctp_stats->out_unorder_chunks.high = value >> 32;
111 } else if (line[7] == 'S') {
112 sctp_stats->out_sctp_packs.low = value & 0xffffffff;
113 sctp_stats->out_sctp_packs.high = value >> 32;
114 } else
115 ret = -1;
116 } else if (line[4] == 'I') {
117 if (line[6] == 'C') {
118 sctp_stats->in_ctrl_chunks.low = value & 0xffffffff;
119 sctp_stats->in_ctrl_chunks.high = value >> 32;
120 } else if (line[6] == 'O') {
121 sctp_stats->in_order_chunks.low = value & 0xffffffff;
122 sctp_stats->in_order_chunks.high = value >> 32;
123 } else if (line[6] == 'U') {
124 sctp_stats->in_unorder_chunks.low = value & 0xffffffff;
125 sctp_stats->in_unorder_chunks.high = value >> 32;
126 } else if (line[6] == 'S') {
127 sctp_stats->in_sctp_packs.low = value & 0xffffffff;
128 sctp_stats->in_sctp_packs.high = value >> 32;
129 } else {
130 ret = -1;
131 }
132 } else if (line[4] == 'F' && line[8] == 'U') {
133 sctp_stats->frag_usr_msgs.low = value & 0xffffffff;
134 sctp_stats->frag_usr_msgs.high = value >> 32;
135 } else if (line[4] == 'R') {
136 sctp_stats->reasm_usr_msgs.low = value & 0xffffffff;
137 sctp_stats->reasm_usr_msgs.high = value >> 32;
138 } else if (line[4] == 'T') {
139 } else {
140 ret = -1;
141 }
142
143 if (ret < 0) {
144 DEBUGMSGTL(("sctp:scalars:stats:arch_load",
145 "Unknown entry: %s\n", line));
146 ret = 0;
147 }
148 }
149
150 sctp_stats->discontinuity_time = 0;
151 fclose(f);
152 return 0;
153 }
154
155 void
156 netsnmp_access_sctp_params_arch_init(void)
157 {
158 }
159
160 int
161 netsnmp_access_sctp_params_arch_load(netsnmp_sctp_params * sctp_params)
162 {
163 int ret;
164 DEBUGMSGTL(("sctp:scalars:params:arch_load",
165 "netsnmp_access_sctp_params_arch_load called"));
166
167 sctp_params->rto_algorithm = NETSNMP_SCTP_ALGORITHM_OTHER;
168
169 ret = load_uint_file(PROC_RTO_MIN, &sctp_params->rto_min);
170 if (ret < 0)
171 return ret;
172
173 ret = load_uint_file(PROC_RTO_MAX, &sctp_params->rto_max);
174 if (ret < 0)
175 return ret;
176
177 ret = load_uint_file(PROC_RTO_INITIAL, &sctp_params->rto_initial);
178 if (ret < 0)
179 return ret;
180
181 sctp_params->max_assocs = -1; /* dynamic allocation of associations */
182
183 ret = load_uint_file(PROC_VAL_COOKIE_LIFE,
184 &sctp_params->val_cookie_life);
185 if (ret < 0)
186 return ret;
187
188 ret = load_uint_file(PROC_MAX_INIT_RETR, &sctp_params->max_init_retr);
189 if (ret < 0)
190 return ret;
191
192 return 0;
193 }