#############################################################  -*- c -*-
## generic include for XXX. Do not use directly.
##
## $Id: mfd-interface.m2c,v 1.43.2.2 2005/01/03 00:50:29 rstory Exp $
########################################################################
@if $m2c_mark_boundary == 1@
/** START code generated by $RCSfile: mfd-interface.m2c,v $ $Revision: 1.43.2.2 $ */
@end@
########################################################################
##
########################################################################
@open ${name}_interface.h@
@eval $hack = "Id"@
/*
 * Note: this file originally auto-generated by mib2c using
 *       version $Revision: 1.43.2.2 $ of $RCSfile: mfd-interface.m2c,v $
 *
 * $$hack:$
 */
/** @defgroup interface: Routines to interface to Net-SNMP
 *
 * \warning This code should not be modified, called directly,
 *          or used to interpret functionality. It is subject to
 *          change at any time.
 * 
 * @{
 */
@include m2c-internal-warning.m2i@
##
@eval $m2c_save = "$name"@
@eval $name = "${name}_INTERFACE"@
@include generic-header-top.m2i@
@eval $name = $m2c_save@

#include "${name}.h"

/* ********************************************************************
 * Table declarations
 */
@foreach $table table@
@    include m2c_setup_table.m2i@

/* PUBLIC interface initialization routine */
void _${context}_initialize_interface(${context}_registration_ptr user_ctx,
                                    u_long flags);

@   if $m2c_data_allocate == 1@
@      eval $m2c_tmp = "${context}_data *"@
@      if $m2c_data_init == 1@
@         eval $m2c_tmp = "$m2c_tmp, void *"@
@      @end@
@   elsif $m2c_data_init == 1@
@      eval $m2c_tmp = "void *"@
@   else@
@      eval $m2c_tmp = "void"@
@   end@
    ${context}_rowreq_ctx * ${context}_allocate_rowreq_ctx($m2c_tmp);
void ${context}_release_rowreq_ctx(${context}_rowreq_ctx *rowreq_ctx);

int ${context}_index_to_oid(netsnmp_index *oid_idx,
                            ${context}_mib_index *mib_idx);
int ${context}_index_from_oid(netsnmp_index *oid_idx,
                              ${context}_mib_index *mib_idx);

/*
 * access to certain internals. use with caution!
 */
void ${context}_valid_columns_set(netsnmp_column_info *vc);

@end@ # for each
@eval $m2c_save = "$name"@
@eval $name = "${name}_INTERFACE"@
@include generic-header-bottom.m2i@
@eval $name = $m2c_save@
########################################################################
##
@open ${name}_interface.c@
/*
 * Note: this file originally auto-generated by mib2c using
 *       version $Revision: 1.43.2.2 $ of $RCSfile: mfd-interface.m2c,v $ 
 *
 * $$hack:$
 */
@include m2c-internal-warning.m2i@

@include generic-source-includes.m2i@

#include <net-snmp/library/container.h>

#include "${name}_interface.h"

@eval $m2c_processing_type = 'i'@
@foreach $table table@
@   include m2c_setup_table.m2i@
@   include details-table.m2i@
########################################################################
typedef struct ${context}_interface_ctx_s {

   netsnmp_container              *container;
   netsnmp_cache                  *cache; /* optional cache */

   ${context}_registration_ptr      user_ctx;
   
   netsnmp_table_registration_info  tbl_info;

   netsnmp_baby_steps_access_methods access_multiplexer;

} ${context}_interface_ctx;

static ${context}_interface_ctx ${context}_if_ctx;

static void _${context}_container_init(
    ${context}_interface_ctx *if_ctx);


static Netsnmp_Node_Handler _mfd_${context}_pre_request;
static Netsnmp_Node_Handler _mfd_${context}_post_request;
static Netsnmp_Node_Handler _mfd_${context}_object_lookup;
static Netsnmp_Node_Handler _mfd_${context}_get_values;
@   if $m2c_table_settable@
static Netsnmp_Node_Handler _mfd_${context}_check_objects;
static Netsnmp_Node_Handler _mfd_${context}_undo_setup;
static Netsnmp_Node_Handler _mfd_${context}_set_values;
static Netsnmp_Node_Handler _mfd_${context}_undo_cleanup;
static Netsnmp_Node_Handler _mfd_${context}_undo_values;
static Netsnmp_Node_Handler _mfd_${context}_commit;
static Netsnmp_Node_Handler _mfd_${context}_undo_commit;
@      if $m2c_irreversible_commit == 1@
static Netsnmp_Node_Handler _mfd_${context}_irreversible_commit;
@      end@
@      if $m2c_table_dependencies == 1@
static Netsnmp_Node_Handler _mfd_${context}_check_dependencies;
@      end@

NETSNMP_STATIC_INLINE int _${context}_undo_column( ${context}_rowreq_ctx *rowreq_ctx,
                                                   netsnmp_variable_list *var, int column );
@if $m2c_table_row_creation == 1@
NETSNMP_STATIC_INLINE int _${context}_check_indexes(${context}_rowreq_ctx * rowreq_ctx);
@end@

@   end@ # writable
@   if ("$m2c_data_context" eq "generated") && (($m2c_undo_embed == 0) || ($m2c_data_allocate == 1))@
${context}_data *${context}_allocate_data(void);

@   end@
/**
 * @internal
 * Initialize the table $context 
 *    (Define its contents and how it's structured)
 */
void
_${context}_initialize_interface(${context}_registration_ptr reg_ptr,  u_long flags)
{
    netsnmp_baby_steps_access_methods *access_multiplexer =
        &${context}_if_ctx.access_multiplexer;
    netsnmp_table_registration_info *tbl_info = &${context}_if_ctx.tbl_info;
    netsnmp_handler_registration *reginfo;
    netsnmp_mib_handler *handler;
    int    mfd_modes = 0;

    DEBUGMSGTL(("internal:${context}:_${context}_initialize_interface","called\n"));


    /*************************************************
     *
     * save interface context for ${context}
     */
    /*
     * Setting up the table's definition
     */
    netsnmp_table_helper_add_indexes(tbl_info,
    @foreach $tabledx index@
                                  $tabledx.type, /** index: $tabledx */
    @end@
                             0);

    /*  Define the minimum and maximum accessible columns.  This
        optimizes retrival. */
    tbl_info->min_column = $context.uc_MIN_COL;
    tbl_info->max_column = $context.uc_MAX_COL;

    /*
     * save users context
     */
    ${context}_if_ctx.user_ctx = reg_ptr;

    /*
     * call data access initialization code
     */
    ${context}_init_data(reg_ptr);

    /*
     * set up the container
     */
    _${context}_container_init(&${context}_if_ctx);
    if (NULL == ${context}_if_ctx.container) {
        snmp_log(LOG_ERR,"could not initialize container for ${context}\n");
        return;
    }
    
    /*
     * access_multiplexer: REQUIRED wrapper for get request handling
     */
    access_multiplexer->object_lookup = _mfd_${context}_object_lookup;
    access_multiplexer->get_values = _mfd_${context}_get_values;

    /*
     * no wrappers yet
     */
    access_multiplexer->pre_request = _mfd_${context}_pre_request;
    access_multiplexer->post_request = _mfd_${context}_post_request;

##
@   if $m2c_table_settable@

    /*
     * REQUIRED wrappers for set request handling
     */
    access_multiplexer->object_syntax_checks = _mfd_${context}_check_objects;
    access_multiplexer->undo_setup = _mfd_${context}_undo_setup;
    access_multiplexer->undo_cleanup = _mfd_${context}_undo_cleanup;
    access_multiplexer->set_values = _mfd_${context}_set_values;
    access_multiplexer->undo_sets = _mfd_${context}_undo_values;

    /*
     * no wrappers yet
     */
    access_multiplexer->commit = _mfd_${context}_commit;
    access_multiplexer->undo_commit = _mfd_${context}_undo_commit;
@   if $m2c_irreversible_commit == 1@
    access_multiplexer->irreversible_commit = _mfd_${context}_irreversible_commit;
@   end@
##
@      if $m2c_table_dependencies == 1@
    
    /*
     * REQUIRED for tables with dependencies
     */
    access_multiplexer->consistency_checks = _mfd_${context}_check_dependencies;
@      end@
@   end@ # writable

    /*************************************************
     *
     * Create a registration, save our reg data, register table.
     */
    DEBUGMSGTL(("$name:init_$context",
                "Registering $context as a mibs-for-dummies table.\n"));		 
    handler = netsnmp_baby_steps_access_multiplexer_get(access_multiplexer);
    reginfo = netsnmp_handler_registration_create("${context}", handler,
                                                  ${context}_oid,
                                                  ${context}_oid_size,
                                                  HANDLER_CAN_BABY_STEP |
@if $m2c_table_settable == 1@
                                                  HANDLER_CAN_RWRITE
@else@
                                                  HANDLER_CAN_RONLY
@end@
                                                  );
    if(NULL == reginfo) {
        snmp_log(LOG_ERR,"error registering table ${context}\n");
        return;
    }
    reginfo->my_reg_void = &${context}_if_ctx;

    /*************************************************
     *
     * set up baby steps handler, create it and inject it
     */
    if( access_multiplexer->object_lookup )
        mfd_modes |= BABY_STEP_OBJECT_LOOKUP;
    if( access_multiplexer->set_values )
        mfd_modes |= BABY_STEP_SET_VALUES;
    if( access_multiplexer->irreversible_commit )
        mfd_modes |= BABY_STEP_IRREVERSIBLE_COMMIT;
    if( access_multiplexer->object_syntax_checks )
        mfd_modes |= BABY_STEP_CHECK_OBJECT;

    if( access_multiplexer->pre_request )
        mfd_modes |= BABY_STEP_PRE_REQUEST;
    if( access_multiplexer->post_request )
        mfd_modes |= BABY_STEP_POST_REQUEST;
    
    if( access_multiplexer->undo_setup )
        mfd_modes |= BABY_STEP_UNDO_SETUP;
    if( access_multiplexer->undo_cleanup )
        mfd_modes |= BABY_STEP_UNDO_CLEANUP;
    if( access_multiplexer->undo_sets )
        mfd_modes |= BABY_STEP_UNDO_SETS;
    
    if( access_multiplexer->row_creation )
        mfd_modes |= BABY_STEP_ROW_CREATE;
    if( access_multiplexer->consistency_checks )
        mfd_modes |= BABY_STEP_CHECK_CONSISTENCY;
    if( access_multiplexer->commit )
        mfd_modes |= BABY_STEP_COMMIT;
    if( access_multiplexer->undo_commit )
        mfd_modes |= BABY_STEP_UNDO_COMMIT;
    
    handler = netsnmp_baby_steps_handler_get(mfd_modes);
    netsnmp_inject_handler(reginfo, handler);

    /*************************************************
     *
     * inject row_merge helper with prefix rootoid_len + 2 (entry.col)
     */
    handler = netsnmp_get_row_merge_handler(reginfo->rootoid_len + 2);
    netsnmp_inject_handler(reginfo, handler);

    /*************************************************
     *
     * inject container_table helper
     */
    handler =
        netsnmp_container_table_handler_get(tbl_info,
                                            ${context}_if_ctx.container,
                                            TABLE_CONTAINER_KEY_NETSNMP_INDEX);
    netsnmp_inject_handler( reginfo, handler );

    /*************************************************
     *
     * inject cache helper
     */
    if(NULL != ${context}_if_ctx.cache) {
        handler = netsnmp_cache_handler_get(${context}_if_ctx.cache);
        netsnmp_inject_handler( reginfo, handler );
    }

    /*
     * register table
     */
    netsnmp_register_table(reginfo, tbl_info);
} /* _${context}_initialize_interface */

void
${context}_valid_columns_set(netsnmp_column_info *vc)
{
    ${context}_if_ctx.tbl_info.valid_columns = vc;
} /* ${context}_valid_columns_set */

@include generic-table-indexes-to-oid.m2i@
@include generic-table-indexes-from-oid.m2i@

########################################################################
##
@    if (($m2c_data_allocate == 1) || ($m2c_undo_embed == 0)) && ("$m2c_data_context" eq "generated")@
@        eval $m2c_gda_todo_supress = 1@ # no todo comments
@        include generic-data-allocate.m2i@
@    end@
########################################################################
/* *********************************************************************
 * @internal
 * allocate resources for a ${context}_rowreq_ctx
 */
${context}_rowreq_ctx *
@if $m2c_data_allocate == 1@
@   eval $m2c_tmp = "${context}_data *data"@
@   if $m2c_data_init == 1@
@     eval $m2c_tmp = "$m2c_tmp, void *user_init_ctx"@
@   end@
@elsif $m2c_data_init == 1@
@   eval $m2c_tmp = "void *user_init_ctx"@
@else@
@   eval $m2c_tmp = "void"@
@end@
${context}_allocate_rowreq_ctx($m2c_tmp)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  SNMP_MALLOC_TYPEDEF(${context}_rowreq_ctx);

    DEBUGMSGTL(("internal:${context}:${context}_allocate_rowreq_ctx","called\n"));

    if(NULL == rowreq_ctx) {
        snmp_log(LOG_ERR,"Couldn't allocate memory for a "
                 "${context}_rowreq_ctx.\n");
    }
@if $m2c_data_allocate == 1@
    else {
        if(NULL != data) {
@   if $m2c_data_transient == 0@
            /*
             * for non-transient data, track when we got data from user
             */
            rowreq_ctx->rowreq_flags |= MFD_ROW_DATA_FROM_USER;
@   end@
            rowreq_ctx->data = data;
        }
        else if (NULL == (rowreq_ctx->data = ${context}_allocate_data())) {
            SNMP_FREE(rowreq_ctx);
            return NULL;
        }
    }

    /*
     * undo context will be allocated when needed (in *_undo_setup)
     */
@end@

    rowreq_ctx->oid_idx.oids = rowreq_ctx->oid_tmp;

    rowreq_ctx->${context}_data_list = NULL;
    rowreq_ctx->${context}_reg = ${context}_if_ctx.user_ctx;

@if $m2c_data_init == 1@
    if(SNMPERR_SUCCESS !=
        ${context}_rowreq_ctx_init(rowreq_ctx, user_init_ctx)) {
       ${context}_release_rowreq_ctx(rowreq_ctx);
       rowreq_ctx = NULL;
    }
@end@

    return rowreq_ctx;
} /* ${context}_allocate_rowreq_ctx */

/*
 * @internal
 * release resources for a ${context}_rowreq_ctx
 */
void
${context}_release_rowreq_ctx(${context}_rowreq_ctx *rowreq_ctx)
{
    DEBUGMSGTL(("internal:${context}:${context}_release_rowreq_ctx","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
@if $m2c_data_init == 1@
    ${context}_rowreq_ctx_cleanup(rowreq_ctx);
@end@

@if $m2c_data_allocate == 1@
@   if $m2c_data_transient == 0@
    /*
     * for non-transient data, don't free data we got from the user
     */
    if ((rowreq_ctx->data) &&
        !(rowreq_ctx->rowreq_flags & MFD_ROW_DATA_FROM_USER))
@   else@
    if (rowreq_ctx->data)
@   end@ # // transient
        ${context}_release_data(rowreq_ctx->data);
 
@end@ 
@if $m2c_undo_embed == 0@
    if(rowreq_ctx->undo)
        ${context}_release_data(rowreq_ctx->undo);
 
@end@ 
    /*
     * free index oid pointer
     */
    if(rowreq_ctx->oid_idx.oids != rowreq_ctx->oid_tmp)
        free(rowreq_ctx->oid_idx.oids);

    SNMP_FREE(rowreq_ctx);
} /* ${context}_release_rowreq_ctx */

########################################################################
##
/**
 * @internal
 * wrapper
 */
static int
_mfd_${context}_pre_request(netsnmp_mib_handler *handler,
                            netsnmp_handler_registration *reginfo,
                            netsnmp_agent_request_info *agtreq_info,
                            netsnmp_request_info *requests)
{
    int rc = ${context}_pre_request(${context}_if_ctx.user_ctx);
    if (MFD_SUCCESS != rc) {
        /*
         * nothing we can do about it but log it
         */
        DEBUGMSGTL(("internal:${context}","error %d from "
                    "${context}_pre_request\n", rc));
        netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
    }
    
    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_pre_request */

/**
 * @internal
 * wrapper
 */
static int
_mfd_${context}_post_request(netsnmp_mib_handler *handler,
                             netsnmp_handler_registration *reginfo,
                             netsnmp_agent_request_info *agtreq_info,
                             netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx;
    int rc = ${context}_post_request(${context}_if_ctx.user_ctx);
    if (MFD_SUCCESS != rc) {
        /*
         * nothing we can do about it but log it
         */
        DEBUGMSGTL(("internal:${context}","error %d from "
                    "${context}_post_request\n", rc));
    }
    
    /*
     * if there are no errors, check for and handle row creation/deletion
     */
    rc = netsnmp_check_requests_error(requests);
    if ((SNMP_ERR_NOERROR == rc) &&
        (NULL !=
         (rowreq_ctx = netsnmp_container_table_row_extract(requests)))) {
        if (rowreq_ctx->rowreq_flags & MFD_ROW_CREATED) {
            rowreq_ctx->rowreq_flags &= ~MFD_ROW_CREATED;
            CONTAINER_INSERT(${context}_if_ctx.container, rowreq_ctx);
        }
        else if (rowreq_ctx->rowreq_flags & MFD_ROW_DELETED) {
            CONTAINER_REMOVE(${context}_if_ctx.container, rowreq_ctx);
            ${context}_release_rowreq_ctx(rowreq_ctx);
        }
    }

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_post_request */

########################################################################
##
/**
 * @internal
 * wrapper
 */
static int
_mfd_${context}_object_lookup(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    
    DEBUGMSGTL(("internal:${context}:_mfd_${context}_object_lookup","called\n"));

    /*
     * get our context from mfd
     * ${context}_interface_ctx *if_ctx =
     *             (${context}_interface_ctx *)reginfo->my_reg_void;
     */

    if(NULL == rowreq_ctx) {
@   if $m2c_table_row_creation == 0@
        netsnmp_request_set_error_all(requests, SNMP_ERR_NOCREATION);
@   else@
        int rc = MFD_SUCCESS;
        netsnmp_table_request_info *tblreq_info;
        netsnmp_index oid_idx;
        ${context}_mib_index mib_idx;

        tblreq_info = netsnmp_extract_table_info(requests);
        if(NULL == tblreq_info) {
            snmp_log(LOG_ERR, "request had no table info\n");
            return MFD_ERROR;
        }

        /*
         * try to parse oid
         */
        oid_idx.oids = tblreq_info->index_oid;
        oid_idx.len = tblreq_info->index_oid_len;
        rc = ${context}_index_from_oid(&oid_idx, &mib_idx);
        if(MFD_SUCCESS != rc) {
            DEBUGMSGT(("$context", "error parsing index\n"));
            netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
        }
        else {
            /*
             * allocate new context
             */
@   eval $m2c_tmp = ""@
@   if ($m2c_data_allocate == 1) || ($m2c_data_init == 1)@
@      eval $m2c_tmp = "NULL"@
@      if ($m2c_data_allocate == 1) && ($m2c_data_init == 1)@
@         eval $m2c_tmp = "$m2c_tmp, NULL"@
@      @end@
@   end@
            rowreq_ctx = ${context}_allocate_rowreq_ctx($m2c_tmp);
            if (NULL == rowreq_ctx)
                return MFD_ERROR; /* msg already logged */

            memcpy(&rowreq_ctx->tbl_idx, &mib_idx, sizeof(mib_idx));

            /*
             * check indexes
             */
            rc = _${context}_check_indexes(rowreq_ctx);
            if(MFD_SUCCESS != rc) {
                netsnmp_assert((rc == SNMP_ERR_NOCREATION) ||
                               (rc == SNMP_ERR_INCONSISTENTNAME));
                netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
                ${context}_release_rowreq_ctx(rowreq_ctx);
            }
            else {
                rowreq_ctx->rowreq_flags |= MFD_ROW_CREATED;
                netsnmp_container_table_row_insert(requests, rowreq_ctx);
            }
        }
@   end@ // row creation
    }
    else {
        ${context}_row_prep(rowreq_ctx);
    }

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_object_lookup */

########################################################################
##
/***********************************************************************
 *
 * GET processing
 *
 ***********************************************************************/
/*
 * @internal
 * Retrieve the value for a particular column
 */
NETSNMP_STATIC_INLINE int
_${context}_get_column( ${context}_rowreq_ctx *rowreq_ctx,
                       netsnmp_variable_list *var, int column )
{
    int rc = SNMPERR_SUCCESS;
    
    DEBUGMSGTL(("internal:${context}:_mfd_${context}_get_column","called\n"));


    netsnmp_assert(NULL != rowreq_ctx);

    switch(column) {
@   foreach $node internalindex@
@      include m2c_setup_node.m2i@
@      if $node.accessible == 1@

    /* (INDEX) $m2c_node_summary */
    case COLUMN_$node.uc:
    var->type = $node.type;
@         if $m2c_node_needlength == 1@
    /*
     * NOTE: val_len is in bytes, ${node}_len might not be (e.g. oids)
     */
        if (var->val_len < (rowreq_ctx->tbl_idx.${node}_len *
                            sizeof(rowreq_ctx->tbl_idx.${node}[0]))) {
           var->val.string = malloc(rowreq_ctx->tbl_idx.${node}_len *
                                    sizeof(rowreq_ctx->tbl_idx.${node}[0]));
        }
        var->val_len = rowreq_ctx->tbl_idx.${node}_len * sizeof(rowreq_ctx->tbl_idx.${node}[0]);
        memcpy( var->val.string, rowreq_ctx->tbl_idx.$node, var->val_len );
@         else@
        var->val_len = sizeof($m2c_decl);
        (*var->val.integer) = rowreq_ctx->tbl_idx.$node;
@         end@
        break;
@      end@ ## accessible
@   end@ ## index
@   foreach $node nonindex@
@      include m2c_setup_node.m2i@
@      if $node.accessible == 1@

    /* $m2c_node_summary */
    case COLUMN_$node.uc:
@         if $m2c_node_needlength == 0@
    var->val_len = sizeof($m2c_decl);
@         end@
    var->type = $node.type;
rc = ${node}_get(rowreq_ctx, $m2c_node_var_ref );
        break;
 @      end@ # accessible
@   end@ # for each column

     default:
         snmp_log(LOG_ERR,"unknown column %d in _${context}_get_column\n", column);
         break;
    }

    return rc;
} /* _${context}_get_column */

########################################################################
##
int
_mfd_${context}_get_values(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    netsnmp_table_request_info * tri;
    u_char                     * old_string;
    void                      (*dataFreeHook)(void *);
    int                        rc;

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_get_values","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
    for(;requests; requests = requests->next) {
        /*
         * save old pointer, so we can free it if replaced
         */
        old_string = requests->requestvb->val.string;
        dataFreeHook = requests->requestvb->dataFreeHook;
        if(NULL == requests->requestvb->val.string) {
            requests->requestvb->val.string = requests->requestvb->buf;
            requests->requestvb->val_len = sizeof(requests->requestvb->buf);
        }
        else if(requests->requestvb->buf == requests->requestvb->val.string) {
            if(requests->requestvb->val_len != sizeof(requests->requestvb->buf))
                requests->requestvb->val_len = sizeof(requests->requestvb->buf);
        }

        /*
         * get column data
         */
        tri = netsnmp_extract_table_info(requests);
        if(NULL == tri)
            continue;
        
        rc = _${context}_get_column(rowreq_ctx, requests->requestvb, tri->colnum);
        if(rc) {
            if(MFD_SKIP == rc) {
                requests->requestvb->type = ASN_PRIV_RETRY;
                rc = SNMP_ERR_NOERROR;
            }
        }
        else if (NULL == requests->requestvb->val.string) {
            snmp_log(LOG_ERR,"NULL varbind data pointer!\n");
            rc = SNMP_ERR_GENERR;
        }
        if(rc)
            netsnmp_request_set_error(requests, SNMP_VALIDATE_ERR(rc));

        /*
         * if the buffer wasn't used previously for the old data (i.e. it
         * was allcoated memory)  and the get routine replaced the pointer,
         * we need to free the previous pointer.
         */
        if(old_string && (old_string != requests->requestvb->buf) &&
           (requests->requestvb->val.string != old_string)) {
            if(dataFreeHook)
                (*dataFreeHook)(old_string);
            else
                free(old_string);
        }
    } /* for results */

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_get_values */

########################################################################
##
/***********************************************************************
 *
 * SET processing
 *
 ***********************************************************************/

@if $m2c_table_settable == 0@
/*
 * NOT APPLICABLE (per MIB or user setting)
 */
@else@
/*----------------------------------------------------------------------
 *
 * SET: Syntax checks
 *
 *---------------------------------------------------------------------*/
/*
 * @internal
 * Check the syntax for a particular column
 */
NETSNMP_STATIC_INLINE int
_${context}_check_column( ${context}_rowreq_ctx *rowreq_ctx,
                         netsnmp_variable_list *var, int column )
{
    int rc = SNMPERR_SUCCESS;
    
    DEBUGMSGTL(("internal:${context}:_${context}_check_column","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    switch(column) {
@   foreach $node internalindex@
@      include m2c_setup_node.m2i@
@      if $node.settable == 1@

    /* (INDEX) $m2c_node_summary */
    case COLUMN_$node.uc:
        return SNMP_ERR_NOTWRITABLE; /* can not change index of active row */
        break;
@      end@ ## settable
@   end@ ## index
@   foreach $node nonindex@
@      include m2c_setup_node.m2i@
@      if $node.settable == 0@
@          next@
@      end@

    /* $m2c_node_summary */
    case COLUMN_$node.uc:
@      include node-varbind-validate.m2i@
    if(SNMPERR_SUCCESS == rc) {
        rc = ${node}_check_value( rowreq_ctx, $m2c_node_var_val );
        if((MFD_SUCCESS != rc) && (MFD_NOT_VALID_EVER != rc) &&
           (MFD_NOT_VALID_NOW != rc)) {
            snmp_log(LOG_ERR, "bad rc %d from ${node}_check_value\n", rc);
            rc = SNMP_ERR_GENERR;
        }
    }
        break;
@   end@ # for each nonindex

        default: /** We shouldn't get here */
            rc = SNMP_ERR_GENERR;
            snmp_log(LOG_ERR, "unknown column %d in _${context}_check_column\n", column);
    }

    return rc;
} /* _${context}_check_column */

##----------------------------------------------------------------------
int
_mfd_${context}_check_objects(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    netsnmp_table_request_info * tri;
    int                          rc;

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_check_objects","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
    for(;requests; requests = requests->next) {

        /*
         * get column number from table request info, and check that column
         */
        tri = netsnmp_extract_table_info(requests);
        if(NULL == tri)
            continue;

        rc = _${context}_check_column(rowreq_ctx, requests->requestvb, tri->colnum);
        if(rc) {
            netsnmp_request_set_error(requests, SNMP_VALIDATE_ERR(rc));
            break;
        }

    } /* for results */

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_check_objects */

##----------------------------------------------------------------------
@if $m2c_table_row_creation == 1@
NETSNMP_STATIC_INLINE int
_${context}_check_indexes(${context}_rowreq_ctx * rowreq_ctx)
{
    int                          rc;

    DEBUGMSGTL(("internal:${context}:_${context}_check_indexes","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

##
@if $m2c_table_external_indexes != 0@
    /*
     * check that the corresponding row exists
     */
@   foreach $node externalindex@
@      include m2c_setup_node.m2i@

    /* (INDEX) $m2c_node_summary */
    rc = ${context}_${node}_check_index( rowreq_ctx );
    if(MFD_SUCCESS != rc)
        return SNMP_ERR_NOCREATION;
@   end@ # for each nonindex

@end@ # external index
@   foreach $node internalindex@
@      include m2c_setup_node.m2i@

    /* (INDEX) $m2c_node_summary */
        rc = ${node}_check_index( rowreq_ctx );
       if(MFD_SUCCESS != rc)
           return SNMP_ERR_NOCREATION;
@   end@ # for each internalindex

    /*
     * if individual parts look ok, check them as a whole
     */
    return ${context}_validate_index( rowreq_ctx->${context}_reg, rowreq_ctx );
} /* _${context}_check_indexes */
@end@ # $m2c_table_row_creation


@      if $m2c_table_dependencies == 1@
/*----------------------------------------------------------------------
 *
 * SET: check dependencies
 *
 *---------------------------------------------------------------------*/
/*
 * @internal
 * Check dependencies wrapper
 */
static int
_mfd_${context}_check_dependencies(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    DEBUGMSGTL(("internal:${context}:_mfd_${context}_check_dependencies","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    rc = ${context}_check_dependencies(rowreq_ctx);
    if(rc){
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_check_dependencies\n", rc));
        netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
    }

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_check_dependencies */

@end@ // dependencies
/*----------------------------------------------------------------------
 *
 * SET: Undo setup
 *
 *---------------------------------------------------------------------*/
/*
 * @internal
 * Set the value for a particular column
 */
NETSNMP_STATIC_INLINE int
_${context}_undo_setup_column( ${context}_rowreq_ctx *rowreq_ctx, int column )
{
    int rc = SNMPERR_SUCCESS;
    
    DEBUGMSGTL(("internal:${context}:_${context}_undo_setup_column","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    switch(column) {
@   foreach $node nonindex@
@      include m2c_setup_node.m2i@
@      if $node.settable == 1@

    /* $m2c_node_summary */
    case COLUMN_$node.uc:
        rowreq_ctx->column_set_flags |= FLAG_$node.uc;
        rc = ${node}_undo_setup(rowreq_ctx );
        break;
 @      end@ # settable
@   end@ # for each column

     default:
         snmp_log(LOG_ERR,"unknown column %d in _${context}_undo_setup_column\n", column);
         break;
    }

    return rc;
} /* _${context}_undo_setup_column */


##----------------------------------------------------------------------
/**
 * @internal
 * undo setup
 */
int
_mfd_${context}_undo_setup(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_undo_setup","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

@if $m2c_undo_embed == 0@
    /*
     * allocate undo context
     */
    rowreq_ctx->undo = ${context}_allocate_data();
    if(NULL == rowreq_ctx->undo) {
        /** msg already logged */
        netsnmp_request_set_error_all(requests, SNMP_ERR_RESOURCEUNAVAILABLE);
        return SNMP_ERR_NOERROR;
    }

@end@
    /*
     * row undo setup
     */
    rowreq_ctx->column_set_flags = 0;
    rc = ${context}_undo_setup(rowreq_ctx);
    if (MFD_SUCCESS != rc)  {
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_undo_setup\n", rc));
        netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
    }
    else {
        /*
         * column undo setup
         */
        netsnmp_table_request_info * tri;
        for(;requests; requests = requests->next) {
            /*
             * set column data
             */
            tri = netsnmp_extract_table_info(requests);
            if(NULL == tri)
                continue;
            
            rc = _${context}_undo_setup_column(rowreq_ctx, tri->colnum);
            if(MFD_SUCCESS != rc)  {
                DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                            "${context}_undo_setup_column\n", rc));
                netsnmp_set_request_error(agtreq_info, requests, SNMP_VALIDATE_ERR(rc));
            }
        } /* for results */
    }
    
    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_undo_setup */

/**
 * @internal
 * undo setup
 */
int
_mfd_${context}_undo_cleanup(netsnmp_mib_handler *handler,
                             netsnmp_handler_registration *reginfo,
                             netsnmp_agent_request_info *agtreq_info,
                             netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    int rc;

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_undo_cleanup","called\n"));

    /*
     * failed row create in early stages has no rowreq_ctx
     */
    if (NULL == rowreq_ctx)
        return MFD_SUCCESS;

    /*
     * call user cleanup
     */
    rc = ${context}_undo_cleanup(rowreq_ctx);
    if (MFD_SUCCESS != rc) {
        /*
         * nothing we can do about it but log it
         */
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_undo_cleanup\n", rc));
    }

@if $m2c_undo_embed == 0@
    /*
     * release undo context, if needed
     */
    if(rowreq_ctx->undo) {
         ${context}_release_data(rowreq_ctx->undo);
         rowreq_ctx->undo = NULL;
    }

@end@
    /*
     * clear set flags
     */
    rowreq_ctx->column_set_flags = 0;

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_undo_cleanup */

/*----------------------------------------------------------------------
 *
 * SET: Set values
 *
 *---------------------------------------------------------------------*/
/*
 * @internal
 * Set the value for a particular column
 */
NETSNMP_STATIC_INLINE int
_${context}_set_column( ${context}_rowreq_ctx *rowreq_ctx,
                       netsnmp_variable_list *var, int column )
{
    int rc = SNMPERR_SUCCESS;
    
    DEBUGMSGTL(("internal:${context}:_${context}_set_column","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    switch(column) {
@   foreach $node nonindex@
@      include m2c_setup_node.m2i@
@      if $node.settable == 1@

    /* $m2c_node_summary */
    case COLUMN_$node.uc:
@         if $m2c_node_needlength == 0@
        if(var->val_len != sizeof($m2c_decl)) {
            rc = SNMP_ERR_WRONGLENGTH;
            snmp_log(LOG_ERR,
                     "varbind size of %d does not match expected size %d\n",
                     var->val_len, sizeof($m2c_decl));
            break;
        }
@         end@
        rowreq_ctx->column_set_flags |= FLAG_$node.uc;
        rc = ${node}_set(rowreq_ctx, $m2c_node_var_val );
        break;
 @      end@ # settable
@   end@ # for each column

     default:
         snmp_log(LOG_ERR,"unknown column %d in _${context}_set_column\n", column);
         break;
    }
    
    return rc;
} /* _${context}_set_column */

########################################################################
##
int
_mfd_${context}_set_values(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    netsnmp_table_request_info * tri;
    int                          rc = SNMP_ERR_NOERROR;

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_set_values","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
    rowreq_ctx->column_set_flags = 0;
    for(;requests; requests = requests->next) {
        /*
         * set column data
         */
        tri = netsnmp_extract_table_info(requests);
        if(NULL == tri)
            continue;
        
        rc = _${context}_set_column(rowreq_ctx,
                                    requests->requestvb, tri->colnum);
        if(MFD_SUCCESS != rc)  {
            DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                        "${context}_set_column\n", rc));
            netsnmp_set_request_error(agtreq_info, requests, SNMP_VALIDATE_ERR(rc));
        }
    } /* for results */

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_set_values */

/*----------------------------------------------------------------------
 *
 * SET: commit
 *
 *---------------------------------------------------------------------*/
/**
 * @internal
 * commit the values
 */
int
_mfd_${context}_commit(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_commit","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
    rc = ${context}_commit(rowreq_ctx);
    if (MFD_SUCCESS != rc) {
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_commit\n", rc));
        netsnmp_request_set_error_all(requests, SNMP_VALIDATE_ERR(rc));
    }

    return SNMP_ERR_NOERROR;
}

int
_mfd_${context}_undo_commit(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_undo_commit","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    rc = ${context}_undo_commit(rowreq_ctx);
    if (MFD_SUCCESS != rc) {
        /*
         * nothing we can do about it but log it
         */
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_undo_commit\n", rc));
    }

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_commit */

/*----------------------------------------------------------------------
 *
 * SET: Undo
 *
 *---------------------------------------------------------------------*/
/**
 * @internal
 * undo the value for a particular column
 */
NETSNMP_STATIC_INLINE int
_${context}_undo_column( ${context}_rowreq_ctx *rowreq_ctx,
                       netsnmp_variable_list *var, int column )
{
    int rc = SNMPERR_SUCCESS;
    
    DEBUGMSGTL(("internal:${context}:_${context}_undo_column","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    switch(column) {
@   foreach $node nonindex@
@      include m2c_setup_node.m2i@
@      if $node.settable == 1@

    /* $m2c_node_summary */
    case COLUMN_$node.uc:
        rc = ${node}_undo(rowreq_ctx);
        break;
 @      end@ # settable
@   end@ # for each column

     default:
         snmp_log(LOG_ERR,"unknown column %d in _${context}_undo_column\n", column);
         break;
    }

    return rc;
} /* _${context}_undo_column */

########################################################################
##
int
_mfd_${context}_undo_values(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    netsnmp_table_request_info * tri;

    DEBUGMSGTL(("internal:${context}:_mfd_${context}_undo_values","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);
    
    for(;requests; requests = requests->next) {
        /*
         * set column data
         */
        tri = netsnmp_extract_table_info(requests);
        if(NULL == tri)
            continue;
        
        rc = _${context}_undo_column(rowreq_ctx, requests->requestvb,
                                     tri->colnum);
        if (MFD_SUCCESS != rc) {
            /*
             * nothing we can do about it but log it
             */
            DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                        "${context}_undo_column\n", rc));
        }
    } /* for results */

    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_undo_values */

@   if $m2c_irreversible_commit == 1@
/*----------------------------------------------------------------------
 *
 * SET: irreversible commit
 *
 *---------------------------------------------------------------------*/
/**
 * @internal
 * commit irreversible actions
 */
int
_mfd_${context}_irreversible_commit(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *agtreq_info,
                         netsnmp_request_info *requests)
{
    int                    rc;
    ${context}_rowreq_ctx *rowreq_ctx =
                  netsnmp_container_table_row_extract(requests);
    
    DEBUGMSGTL(("internal:${context}:_mfd_${context}_irreversible:commit","called\n"));
   
    netsnmp_assert(NULL != rowreq_ctx);
    
    rc = ${context}_irreversible_commit(rowreq_ctx);
    if (MFD_SUCCESS != rc) {
        netsnmp_request_set_error_all(requests, SNMP_ERR_COMMITFAILED);
        DEBUGMSGTL(("verbose:${context}:mfd","error %d from "
                    "${context}_irreversible_commit\n", rc));
    }
    
    return SNMP_ERR_NOERROR;
} /* _mfd_${context}_irreversible_commit */

@end@
@end@ # settable
/***********************************************************************
 *
 * DATA ACCESS
 *
 ***********************************************************************/
@    include mfd-access-${m2c_table_access}-defines.m2i@
@end@ # foreach table
##
########################################################################
@if $m2c_mark_boundary == 1@
/** END code generated by $RCSfile: mfd-interface.m2c,v $ $Revision: 1.43.2.2 $ */
@end@
