Debian Codegraph @

agent/mibgroup/udp-mib/data_access/udp_endpoint_linux.c

raw
1 /*
2 * udpEndpointTable MIB architecture support
3 */
4
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-features.h>
7 #include <net-snmp/net-snmp-includes.h>
8
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <net-snmp/library/file_utils.h>
11 #include <net-snmp/library/text_utils.h>
12
13 #include <net-snmp/data_access/ipaddress.h>
14 #include <net-snmp/data_access/udp_endpoint.h>
15
16 #include "udp-mib/udpEndpointTable/udpEndpointTable_constants.h"
17 #include "mibgroup/util_funcs/get_pid_from_inode.h"
18 #include "udp_endpoint_private.h"
19
20 #include <fcntl.h>
21 #include <stdint.h>
22
23 netsnmp_feature_require(text_utils);
24 netsnmp_feature_child_of(udp_endpoint_all, libnetsnmpmibs);
25 netsnmp_feature_child_of(udp_endpoint_writable, udp_endpoint_all);
26
27 static int _load4(netsnmp_container *container, u_int flags);
28 #if defined (NETSNMP_ENABLE_IPV6)
29 static int _load6(netsnmp_container *container, u_int flags);
30 #endif
31
32 /*
33 * initialize arch specific storage
34 *
35 * @retval 0: success
36 * @retval <0: error
37 */
38 #ifndef NETSNMP_FEATURE_REMOVE_UDP_ENDPOINT_WRITABLE
39 int
40 netsnmp_arch_udp_endpoint_entry_init(netsnmp_udp_endpoint_entry *entry)
41 {
42 /*
43 * init
44 */
45 return 0;
46 }
47
48 /*
49 * cleanup arch specific storage
50 */
51 void
52 netsnmp_arch_udp_endpoint_entry_cleanup(netsnmp_udp_endpoint_entry *entry)
53 {
54 /*
55 * cleanup
56 */
57 }
58
59 /*
60 * copy arch specific storage
61 */
62 int
63 netsnmp_arch_udp_endpoint_entry_copy(netsnmp_udp_endpoint_entry *lhs,
64 netsnmp_udp_endpoint_entry *rhs)
65 {
66 return 0;
67 }
68
69 /*
70 * delete an entry
71 */
72 int
73 netsnmp_arch_udp_endpoint_delete(netsnmp_udp_endpoint_entry *entry)
74 {
75 if (NULL == entry)
76 return -1;
77 /** xxx-rks:9 udp_endpoint delete not implemented */
78 return -1;
79 }
80 #endif /* NETSNMP_FEATURE_REMOVE_UDP_ENDPOINT_WRITABLE */
81
82
83 /**
84 *
85 * @retval 0 no errors
86 * @retval !0 errors
87 */
88 int
89 netsnmp_arch_udp_endpoint_container_load(netsnmp_container *container,
90 u_int load_flags )
91 {
92 int rc = 0;
93
94 /* Setup the pid_from_inode table, and fill it.*/
95 netsnmp_get_pid_from_inode_init();
96
97 rc = _load4(container, load_flags);
98 if(rc < 0) {
99 u_int flags = NETSNMP_ACCESS_UDP_ENDPOINT_FREE_KEEP_CONTAINER;
100 netsnmp_access_udp_endpoint_container_free(container, flags);
101 return rc;
102 }
103
104 #if defined (NETSNMP_ENABLE_IPV6)
105 rc = _load6(container, load_flags);
106 if(rc < 0) {
107 u_int flags = NETSNMP_ACCESS_UDP_ENDPOINT_FREE_KEEP_CONTAINER;
108 netsnmp_access_udp_endpoint_container_free(container, flags);
109 return rc;
110 }
111 #endif
112
113 return 0;
114 }
115
116 /**
117 * @internal
118 * process token value index line
119 */
120 static int
121 _process_line_udp_ep(netsnmp_line_info *line_info, void *mem,
122 struct netsnmp_line_process_info_s* lpi)
123 {
124 netsnmp_udp_endpoint_entry *ep = (netsnmp_udp_endpoint_entry *)mem;
125 char *ptr, *sep;
126 u_char *u_ptr;
127 size_t u_ptr_len, offset, len;
128 unsigned long long inode;
129 size_t count = 0;
130
131 /*
132 * skip 'sl'
133 */
134 ptr = skip_not_white(line_info->start);
135 if (NULL == ptr) {
136 DEBUGMSGTL(("access:udp_endpoint", "no sl '%s'\n",
137 line_info->start));
138 return PMLP_RC_MEMORY_UNUSED;
139 }
140 ptr = skip_white(ptr);
141 if (NULL == ptr) {
142 DEBUGMSGTL(("text:util:tvi", "no space after sl '%s'\n",
143 line_info->start));
144 return PMLP_RC_MEMORY_UNUSED;
145 }
146
147 /*
148 * get local address. ignore error on hex conversion, since that
149 * function doesn't like the ':' between address and port. check the
150 * offset to see if it worked. May need to flip string too.
151 */
152 u_ptr = ep->loc_addr;
153 u_ptr_len = sizeof(ep->loc_addr);
154 sep = strchr(ptr, ':');
155 if (NULL == sep) {
156 DEBUGMSGTL(("text:util:tvi", "no ':' '%s'\n",
157 line_info->start));
158 return PMLP_RC_MEMORY_UNUSED;
159 }
160 len = (sep - ptr);
161 if (-1 == netsnmp_addrstr_hton(ptr, len)) {
162 DEBUGMSGTL(("text:util:tvi", "bad length %d for loc addr '%s'\n",
163 (int)u_ptr_len, line_info->start));
164 return PMLP_RC_MEMORY_UNUSED;
165 }
166 offset = 0;
167 netsnmp_hex_to_binary(&u_ptr, &u_ptr_len, &offset, 0, ptr, NULL);
168 if ((4 != offset) && (16 != offset)) {
169 DEBUGMSGTL(("text:util:tvi", "bad offset %d for loc addr '%s'\n",
170 (int)offset, line_info->start));
171 return PMLP_RC_MEMORY_UNUSED;
172 }
173 ep->loc_addr_len = offset;
174 ptr += (offset * 2);
175 ++ptr; /* skip ':' */
176
177 /*
178 * get local port
179 */
180 ep->loc_port = strtol(ptr, &ptr, 16);
181 ptr = skip_white(ptr);
182
183 /*
184 * get remote address. ignore error on hex conversion, since that
185 * function doesn't like the ':' between address and port. check the
186 * offset to see if it worked. May need to flip string too.
187 */
188 u_ptr = ep->rmt_addr;
189 u_ptr_len = sizeof(ep->rmt_addr);
190 sep = strchr(ptr, ':');
191 if (NULL == sep) {
192 DEBUGMSGTL(("text:util:tvi", "no ':' '%s'\n",
193 line_info->start));
194 return PMLP_RC_MEMORY_UNUSED;
195 }
196 len = (sep - ptr);
197 if (-1 == netsnmp_addrstr_hton(ptr, len)) {
198 DEBUGMSGTL(("text:util:tvi", "bad length %d for rmt addr '%s'\n",
199 (int)u_ptr_len, line_info->start));
200 return PMLP_RC_MEMORY_UNUSED;
201 }
202 offset = 0;
203 netsnmp_hex_to_binary(&u_ptr, &u_ptr_len, &offset, 0, ptr, NULL);
204 if ((4 != offset) && (16 != offset)) {
205 DEBUGMSGTL(("text:util:tvi", "bad offset %d for rmt addr '%s'\n",
206 (int)offset, line_info->start));
207 return PMLP_RC_MEMORY_UNUSED;
208 }
209 ep->rmt_addr_len = offset;
210 ptr += (offset * 2);
211 ++ptr; /* skip ':' */
212
213 /*
214 * get remote port
215 */
216 ep->rmt_port = strtol(ptr, &ptr, 16);
217 ptr = skip_white(ptr);
218
219 /*
220 * get state too
221 */
222 ep->state = strtol(ptr, &ptr, 16);
223
224 /*
225 * Use inode as instance value.
226 */
227 while (count != 5) {
228 ptr = skip_white(ptr);
229 ptr = skip_not_white(ptr);
230 count++;
231 }
232 inode = strtoull(ptr, &ptr, 0);
233 ep->instance = (u_int)inode;
234
235 /*
236 * get the pid also
237 */
238 ep->pid = netsnmp_get_pid_from_inode(inode);
239
240 ep->index = (uintptr_t)(lpi->user_context);
241 lpi->user_context = (void*)((char*)(lpi->user_context) + 1);
242
243 ep->oid_index.oids = &ep->index;
244 ep->oid_index.len = 1;
245
246 return PMLP_RC_MEMORY_USED;
247 }
248
249 /**
250 *
251 * @retval 0 no errors
252 * @retval !0 errors
253 */
254 static int
255 _load4(netsnmp_container *container, u_int load_flags)
256 {
257 netsnmp_file *fp;
258 netsnmp_line_process_info lpi;
259
260 if (NULL == container)
261 return -1;
262
263 /*
264 * allocate file resources
265 */
266 fp = netsnmp_file_fill(NULL, "/proc/net/udp" , O_RDONLY, 0, 0);
267 if (NULL == fp) /** msg already logged */
268 return -2;
269
270 memset(&lpi, 0x0, sizeof(lpi));
271 lpi.mem_size = sizeof(netsnmp_udp_endpoint_entry);
272 lpi.process = _process_line_udp_ep;
273 lpi.user_context = (void*)0;
274
275 container = netsnmp_file_text_parse(fp, container, PM_USER_FUNCTION,
276 0, &lpi);
277 netsnmp_file_release(fp);
278 return (NULL == container);
279 }
280
281 #if defined (NETSNMP_ENABLE_IPV6)
282 /**
283 *
284 * @retval 0 no errors
285 * @retval !0 errors
286 */
287 static int
288 _load6(netsnmp_container *container, u_int load_flags)
289 {
290 netsnmp_file *fp;
291 netsnmp_line_process_info lpi;
292
293 if (NULL == container)
294 return -1;
295
296 /*
297 * allocate file resources
298 */
299 fp = netsnmp_file_fill(NULL, "/proc/net/udp6" , O_RDONLY, 0, 0);
300 if (NULL == fp) /** msg already logged */
301 return -2;
302
303 memset(&lpi, 0x0, sizeof(lpi));
304 lpi.mem_size = sizeof(netsnmp_udp_endpoint_entry);
305 lpi.process = _process_line_udp_ep;
306 lpi.user_context = (void*)CONTAINER_SIZE(container);
307
308 container = netsnmp_file_text_parse(fp, container, PM_USER_FUNCTION,
309 0, &lpi);
310 netsnmp_file_release(fp);
311 return (NULL == container);
312 }
313 #endif /* NETSNMP_ENABLE_IPV6 */