| 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 |
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 |
#define free_wrapper free |
| 41 |
#endif |
| 42 |
|
| 43 |
/* An alias parser */ |
| 44 |
void |
| 45 |
parse_alias_config(const char *token, char *line) { |
| 46 |
char aliasname[SPRINT_MAX_LEN]; |
| 47 |
char transportdef[SPRINT_MAX_LEN]; |
| 48 |
/* copy the first word (the alias) out and then assume the rest is |
| 49 |
transport */ |
| 50 |
line = copy_nword(line, aliasname, sizeof(aliasname)); |
| 51 |
line = copy_nword(line, transportdef, sizeof(transportdef)); |
| 52 |
if (line) |
| 53 |
config_perror("more data than expected"); |
| 54 |
netsnmp_data_list_add_node(&alias_memory, |
| 55 |
netsnmp_create_data_list(aliasname, |
| 56 |
strdup(transportdef), |
| 57 |
&free_wrapper)); |
| 58 |
} |
| 59 |
|
| 60 |
void |
| 61 |
free_alias_config(void) { |
| 62 |
netsnmp_free_all_list_data(alias_memory); |
| 63 |
alias_memory = NULL; |
| 64 |
} |
| 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 |
{ |
| 76 |
const char *aliasdata; |
| 77 |
|
| 78 |
aliasdata = (const char*)netsnmp_get_list_data(alias_memory, str); |
| 79 |
if (!aliasdata) { |
| 80 |
snmp_log(LOG_ERR, "No alias found for %s\n", str); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
return netsnmp_tdomain_transport(aliasdata,local,default_target); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
netsnmp_transport * |
| 90 |
netsnmp_alias_create_ostring(const void *o, size_t o_len, int local) |
| 91 |
{ |
| 92 |
fprintf(stderr, "make ostring\n"); |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
netsnmp_alias_ctor(void) |
| 98 |
{ |
| 99 |
aliasDomain.name = netsnmp_snmpALIASDomain; |
| 100 |
aliasDomain.name_length = sizeof(netsnmp_snmpALIASDomain) / sizeof(oid); |
| 101 |
aliasDomain.prefix = (const char **)calloc(2, sizeof(char *)); |
| 102 |
aliasDomain.prefix[0] = "alias"; |
| 103 |
|
| 104 |
aliasDomain.f_create_from_tstring_new = netsnmp_alias_create_tstring; |
| 105 |
aliasDomain.f_create_from_ostring = netsnmp_alias_create_ostring; |
| 106 |
|
| 107 |
netsnmp_tdomain_register(&aliasDomain); |
| 108 |
|
| 109 |
register_config_handler("snmp", "alias", parse_alias_config, |
| 110 |
free_alias_config, "NAME TRANSPORT_DEFINITION"); |
| 111 |
} |