/src/net-snmp/snmplib/transports/snmpAliasDomain.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <net-snmp/net-snmp-config.h> |
2 | | |
3 | | #include <net-snmp/types.h> |
4 | | #include <net-snmp/library/snmpAliasDomain.h> |
5 | | |
6 | | #include <stdio.h> |
7 | | #include <sys/types.h> |
8 | | #include <errno.h> |
9 | | |
10 | | #ifdef HAVE_STRING_H |
11 | | #include <string.h> |
12 | | #else |
13 | | #include <strings.h> |
14 | | #endif |
15 | | #ifdef HAVE_STDLIB_H |
16 | | #include <stdlib.h> |
17 | | #endif |
18 | | #ifdef HAVE_UNISTD_H |
19 | | #include <unistd.h> |
20 | | #endif |
21 | | |
22 | | #include <net-snmp/net-snmp-includes.h> |
23 | | #include <net-snmp/types.h> |
24 | | #include <net-snmp/output_api.h> |
25 | | #include <net-snmp/utilities.h> |
26 | | #include <net-snmp/config_api.h> |
27 | | |
28 | | const oid netsnmp_snmpALIASDomain[] = { 1,3,6,1,4,1,8072,3,3,7 }; |
29 | | static netsnmp_tdomain aliasDomain; |
30 | | |
31 | | /* simple storage mechanism */ |
32 | | static netsnmp_data_list *alias_memory = NULL; |
33 | | |
34 | | #ifdef HAVE_DMALLOC_H |
35 | | static void free_wrapper(void * p) |
36 | | { |
37 | | free(p); |
38 | | } |
39 | | #else |
40 | 0 | #define free_wrapper free |
41 | | #endif |
42 | | |
43 | | /* An alias parser */ |
44 | | void |
45 | 0 | parse_alias_config(const char *token, char *line) { |
46 | 0 | char aliasname[SPRINT_MAX_LEN]; |
47 | 0 | char transportdef[SPRINT_MAX_LEN]; |
48 | | /* copy the first word (the alias) out and then assume the rest is |
49 | | transport */ |
50 | 0 | line = copy_nword(line, aliasname, sizeof(aliasname)); |
51 | 0 | line = copy_nword(line, transportdef, sizeof(transportdef)); |
52 | 0 | if (line) |
53 | 0 | config_perror("more data than expected"); |
54 | 0 | netsnmp_data_list_add_node(&alias_memory, |
55 | 0 | netsnmp_create_data_list(aliasname, |
56 | 0 | strdup(transportdef), |
57 | 0 | &free_wrapper)); |
58 | 0 | } |
59 | | |
60 | | void |
61 | 6.12k | free_alias_config(void) { |
62 | 6.12k | netsnmp_free_all_list_data(alias_memory); |
63 | 6.12k | alias_memory = NULL; |
64 | 6.12k | } |
65 | | |
66 | | /* |
67 | | * Open a ALIAS-based transport for SNMP. Local is TRUE if addr is the local |
68 | | * address to bind to (i.e. this is a server-type session); otherwise addr is |
69 | | * the remote address to send things to. |
70 | | */ |
71 | | |
72 | | netsnmp_transport * |
73 | | netsnmp_alias_create_tstring(const char *str, int local, |
74 | | const char *default_target) |
75 | 0 | { |
76 | 0 | const char *aliasdata; |
77 | |
|
78 | 0 | aliasdata = (const char*)netsnmp_get_list_data(alias_memory, str); |
79 | 0 | if (!aliasdata) { |
80 | 0 | snmp_log(LOG_ERR, "No alias found for %s\n", str); |
81 | 0 | return NULL; |
82 | 0 | } |
83 | | |
84 | 0 | return netsnmp_tdomain_transport(aliasdata,local,default_target); |
85 | 0 | } |
86 | | |
87 | | |
88 | | |
89 | | netsnmp_transport * |
90 | | netsnmp_alias_create_ostring(const void *o, size_t o_len, int local) |
91 | 0 | { |
92 | 0 | fprintf(stderr, "make ostring\n"); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | | |
96 | | void |
97 | | netsnmp_alias_ctor(void) |
98 | 3.82k | { |
99 | 3.82k | aliasDomain.name = netsnmp_snmpALIASDomain; |
100 | 3.82k | aliasDomain.name_length = OID_LENGTH(netsnmp_snmpALIASDomain); |
101 | 3.82k | aliasDomain.prefix = calloc(2, sizeof(char *)); |
102 | 3.82k | if (!aliasDomain.prefix) { |
103 | 0 | snmp_log(LOG_ERR, "calloc() failed - out of memory\n"); |
104 | 0 | return; |
105 | 0 | } |
106 | 3.82k | aliasDomain.prefix[0] = "alias"; |
107 | | |
108 | 3.82k | aliasDomain.f_create_from_tstring_new = netsnmp_alias_create_tstring; |
109 | 3.82k | aliasDomain.f_create_from_ostring = netsnmp_alias_create_ostring; |
110 | | |
111 | 3.82k | netsnmp_tdomain_register(&aliasDomain); |
112 | | |
113 | 3.82k | register_config_handler("snmp", "alias", parse_alias_config, |
114 | 3.82k | free_alias_config, "NAME TRANSPORT_DEFINITION"); |
115 | 3.82k | } |