Hello net-snmp coders, I would like to propose null terminated string "magic tie" helper function implementation: netsnmp_register_str_instance().
The C source code for null terminated strings can be then automagically
generated from MIB by mib2c.tie.conf, see attachments.
netsnmp_register_str_instance() behaves like netsnmp_register_int_instance()
function, but should receive pointer to netsnmp_ntstr_t structure instead of
int pointer, no API functions were modified.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//new structure to be added
typedef struct netsnmp_ntstr_s {
char * val; //pointer to your null terminated string
size_t max_size; //maximum string size (allocated or statically
defined)
} netsnmp_ntstr_t;
//new instance and handler functions to be added to agent/helpers/instance.c
int
netsnmp_register_str_instance(const char *name,
oid * reg_oid, size_t reg_oid_len,
netsnmp_ntstr_t *it, Netsnmp_Node_Handler *
subhandler)
{
int i=netsnmp_register_instance(
get_reg(name, "str_handler", reg_oid, reg_oid_len, (void *)
it,
HANDLER_CAN_RWRITE, netsnmp_instance_str_handler,
subhandler , NULL )
);
return i;
}
int
netsnmp_instance_str_handler(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
netsnmp_ntstr_t *nt_string = (netsnmp_ntstr_t *)handler->myvoid;
char *it = nt_string->val;
char *it_save=NULL;
size_t it_len=0;
size_t max_it_len=nt_string->max_size;
DEBUGMSGTL(("netsnmp_instance_str_handler", "Got request: %d\n",
reqinfo->mode));
switch (reqinfo->mode) {
/*
* data requests
*/
case MODE_GET:
/*
*/
if(it)
it_len=strlen(it);
else
it_len=0;
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
it, it_len);
break;
/*
* SET requests. Should only get here if registered RWRITE
*/
case MODE_SET_RESERVE1:
if (requests->requestvb->type != ASN_OCTET_STR)
netsnmp_set_request_error(reqinfo, requests,
SNMP_ERR_WRONGTYPE);
break;
case MODE_SET_RESERVE2:
/*
* store old info for undo later
*/
if(it)
it_len=strlen(it);
else
it_len=0;
memdup((char **) & it_save, it, it_len);
if (it_save == NULL) {
netsnmp_set_request_error(reqinfo, requests,
SNMP_ERR_RESOURCEUNAVAILABLE);
return SNMP_ERR_NOERROR;
}
netsnmp_request_add_list_data(requests,
netsnmp_create_data_list
(INSTANCE_HANDLER_NAME, it_save,
free));
break;
case MODE_SET_ACTION:
/*
* update current
*/
DEBUGMSGTL(("testhandler", "updated str %s -> %s\n", it,
requests->requestvb->val.string));
strncpy(it, requests->requestvb->val.string, max_it_len-1);
it[max_it_len-1]='\0';
break;
case MODE_SET_UNDO:
strcpy(it, ((char *) netsnmp_request_get_list_data(requests,
INSTANCE_HANDLER_NAME)));
break;
case MODE_SET_COMMIT:
case MODE_SET_FREE:
/*
* nothing to do
*/
break;
}
if (handler->next && handler->next->access_method)
return netsnmp_call_next_handler(handler, reginfo, reqinfo,
requests);
return SNMP_ERR_NOERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I have been looking for "magic tie" helper for null terminated strings in
net-snmp API but haven't found one. Am I missing some point? Do you think it
could be helpful for someone else?
Best regards,
Eugene
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.tie.conf
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "ntstr_example.h"
extern int exampleInt;
extern char exampleString[255];
netsnmp_ntstr_t exampleString_nt = {
.val = (char *)exampleString,
.max_size = sizeof(exampleString)
};
/** Initializes the ntstr_example module */
void
init_ntstr_example(void)
{
static oid exampleInt_oid[] =
{ 1, 3, 6, 1, 4, 1, 32415, 2, 1, 1 };
static oid exampleString_oid[] = { 1, 3, 6, 1, 4, 1, 32415, 2, 1, 5 };
DEBUGMSGTL(("ntstr_example", "Initializing\n"));
netsnmp_register_int_instance("exampleInt",
exampleInt_oid,
OID_LENGTH(exampleInt_oid),
&exampleInt, NULL);
netsnmp_register_str_instance("exampleString",
exampleString_oid,
OID_LENGTH(exampleString_oid),
&exampleString_nt, NULL);
}
mib2c.tie.conf
Description: Binary data
------------------------------------------------------------------------------ SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada. The future of the web can't happen without you. Join us at MIX09 to help pave the way to the Next Web now. Learn more and register at http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________ Net-snmp-coders mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
