/*							-- DO NOT EDIT --
 * This file has been generated by smidump
 * version 0.3.1 for the scli package.
 *
 * Derived from TUNNEL-MIB:
 *   The MIB module for management of IP Tunnels, independent of
 *   the specific encapsulation scheme in use.
 *
 * Revision 1999-08-24 12:00:
 *   Initial version, published as RFC 2667.
 *
 * $Id: tunnel-mib.c,v 1.14 2002/03/27 13:48:23 schoenw Exp $
 */

#include "tunnel-mib.h"

GSnmpEnum const tunnel_mib_enums_tunnelIfEncapsMethod[] = {
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_OTHER,   "other" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_DIRECT,  "direct" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_GRE,     "gre" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_MINIMAL, "minimal" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_L2TP,    "l2tp" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_PPTP,    "pptp" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_L2F,     "l2f" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_UDP,     "udp" },
    { TUNNEL_MIB_TUNNELIFENCAPSMETHOD_ATMP,    "atmp" },
    { 0, NULL }
};

GSnmpEnum const tunnel_mib_enums_tunnelIfSecurity[] = {
    { TUNNEL_MIB_TUNNELIFSECURITY_NONE,  "none" },
    { TUNNEL_MIB_TUNNELIFSECURITY_IPSEC, "ipsec" },
    { TUNNEL_MIB_TUNNELIFSECURITY_OTHER, "other" },
    { 0, NULL }
};

GSnmpEnum const tunnel_mib_enums_tunnelConfigEncapsMethod[] = {
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_OTHER,   "other" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_DIRECT,  "direct" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_GRE,     "gre" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_MINIMAL, "minimal" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_L2TP,    "l2tp" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_PPTP,    "pptp" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_L2F,     "l2f" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_UDP,     "udp" },
    { TUNNEL_MIB_TUNNELCONFIGENCAPSMETHOD_ATMP,    "atmp" },
    { 0, NULL }
};

GSnmpEnum const tunnel_mib_enums_tunnelConfigStatus[] = {
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_ACTIVE,        "active" },
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_NOTINSERVICE,  "notInService" },
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_NOTREADY,      "notReady" },
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_CREATEANDGO,   "createAndGo" },
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_CREATEANDWAIT, "createAndWait" },
    { TUNNEL_MIB_TUNNELCONFIGSTATUS_DESTROY,       "destroy" },
    { 0, NULL }
};


static guint16 tunnelIfLocalAddress_constraints[] = {4, 4, 0, 0};
static guint16 tunnelIfRemoteAddress_constraints[] = {4, 4, 0, 0};
static guint32 tunnelIfHopLimit_constraints[] = {0L, 255L, 0, 0};
static guint32 tunnelIfTOS_constraints[] = {-2L, 63L, 0, 0};
static guint32 tunnelConfigIfIndex_constraints[] = {0L, 2147483647L, 0, 0};


static guint32 const tunnelIfEntry_oid[] = {1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 1, 1};

static GSnmpAttribute tunnelIfEntry_attr[] = {
    { 1, G_SNMP_IPADDRESS,
      TUNNEL_MIB_TUNNELIFLOCALADDRESS, "tunnelIfLocalAddress",
       tunnelIfLocalAddress_constraints,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfLocalAddress),
      0,
      0 },
    { 2, G_SNMP_IPADDRESS,
      TUNNEL_MIB_TUNNELIFREMOTEADDRESS, "tunnelIfRemoteAddress",
       tunnelIfRemoteAddress_constraints,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfRemoteAddress),
      0,
      0 },
    { 3, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELIFENCAPSMETHOD, "tunnelIfEncapsMethod",
       NULL,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfEncapsMethod),
      0,
      0 },
    { 4, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELIFHOPLIMIT, "tunnelIfHopLimit",
       tunnelIfHopLimit_constraints,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfHopLimit),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 5, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELIFSECURITY, "tunnelIfSecurity",
       NULL,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfSecurity),
      0,
      0 },
    { 6, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELIFTOS, "tunnelIfTOS",
       tunnelIfTOS_constraints,
      G_STRUCT_OFFSET(tunnel_mib_tunnelIfEntry_t, tunnelIfTOS),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 0, 0, 0, NULL }
};

static guint32 const tunnelConfigEntry_oid[] = {1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 2, 1};

static GSnmpAttribute tunnelConfigEntry_attr[] = {
    { 5, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELCONFIGIFINDEX, "tunnelConfigIfIndex",
       tunnelConfigIfIndex_constraints,
      G_STRUCT_OFFSET(tunnel_mib_tunnelConfigEntry_t, tunnelConfigIfIndex),
      0,
      0 },
    { 6, G_SNMP_INTEGER32,
      TUNNEL_MIB_TUNNELCONFIGSTATUS, "tunnelConfigStatus",
       NULL,
      G_STRUCT_OFFSET(tunnel_mib_tunnelConfigEntry_t, tunnelConfigStatus),
      0,
      GSNMP_ATTR_FLAG_WRITABLE },
    { 0, 0, 0, NULL }
};


tunnel_mib_tunnelIfEntry_t *
tunnel_mib_new_tunnelIfEntry()
{
    tunnel_mib_tunnelIfEntry_t *tunnelIfEntry;

    tunnelIfEntry = (tunnel_mib_tunnelIfEntry_t *) g_malloc0(sizeof(tunnel_mib_tunnelIfEntry_t) + sizeof(gpointer));
    return tunnelIfEntry;
}

static int
unpack_tunnelIfEntry(GSnmpVarBind *vb, tunnel_mib_tunnelIfEntry_t *tunnelIfEntry)
{
    guint8 idx = 13;

    if (vb->id_len < idx) return -1;
    tunnelIfEntry->ifIndex = vb->id[idx++];
    if (vb->id_len > idx) return -1;
    return 0;
}

static int
pack_tunnelIfEntry(guint32 *base, gint32 ifIndex)
{
    guint8 idx = 13;

    base[idx++] = ifIndex;
    return idx;
}

static tunnel_mib_tunnelIfEntry_t *
assign_tunnelIfEntry(GSList *vbl)
{
    tunnel_mib_tunnelIfEntry_t *tunnelIfEntry;
    char *p;

    tunnelIfEntry = tunnel_mib_new_tunnelIfEntry();
    if (! tunnelIfEntry) {
        return NULL;
    }

    p = (char *) tunnelIfEntry + sizeof(tunnel_mib_tunnelIfEntry_t);
    * (GSList **) p = vbl;

    if (unpack_tunnelIfEntry((GSnmpVarBind *) vbl->data, tunnelIfEntry) < 0) {
        g_warning("%s: invalid instance identifier", "tunnelIfEntry");
        g_free(tunnelIfEntry);
        return NULL;
    }

    gsnmp_attr_assign(vbl, tunnelIfEntry_oid, sizeof(tunnelIfEntry_oid)/sizeof(guint32),
                      tunnelIfEntry_attr, tunnelIfEntry);

    return tunnelIfEntry;
}

void
tunnel_mib_get_tunnelIfTable(GSnmpSession *s, tunnel_mib_tunnelIfEntry_t ***tunnelIfEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    GSList *row;
    int i;
    static guint32 base[] = {1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 1, 1, 0};

    *tunnelIfEntry = NULL;

    gsnmp_attr_get(s, &in, base, 13, 12, tunnelIfEntry_attr, mask);

    out = gsnmp_gettable(s, in);
    /* g_snmp_vbl_free(in); */

    if (out) {
        *tunnelIfEntry = (tunnel_mib_tunnelIfEntry_t **) g_malloc0((g_slist_length(out) + 1) * sizeof(tunnel_mib_tunnelIfEntry_t *));
        if (! *tunnelIfEntry) {
            s->error_status = G_SNMP_ERR_INTERNAL;
            g_snmp_vbl_free(out);
            return;
        }
        for (row = out, i = 0; row; row = g_slist_next(row), i++) {
            (*tunnelIfEntry)[i] = assign_tunnelIfEntry(row->data);
        }
    }
}

void
tunnel_mib_get_tunnelIfEntry(GSnmpSession *s, tunnel_mib_tunnelIfEntry_t **tunnelIfEntry, gint32 ifIndex, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, tunnelIfEntry_oid, sizeof(tunnelIfEntry_oid));
    len = pack_tunnelIfEntry(base, ifIndex);
    if (len < 0) {
        g_warning("%s: invalid index values", "tunnelIfEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    *tunnelIfEntry = NULL;

    gsnmp_attr_get(s, &in, base, len, 12, tunnelIfEntry_attr, mask);

    out = g_snmp_session_sync_get(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        if (s->error_status != G_SNMP_ERR_NOERROR) {
            g_snmp_vbl_free(out);
            return;
        }
        *tunnelIfEntry = assign_tunnelIfEntry(out);
    }
}

void
tunnel_mib_set_tunnelIfEntry(GSnmpSession *s, tunnel_mib_tunnelIfEntry_t *tunnelIfEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, tunnelIfEntry_oid, sizeof(tunnelIfEntry_oid));
    len = pack_tunnelIfEntry(base, tunnelIfEntry->ifIndex);
    if (len < 0) {
        g_warning("%s: invalid index values", "tunnelIfEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    gsnmp_attr_set(s, &in, base, len, 12, tunnelIfEntry_attr, mask, tunnelIfEntry);

    out = g_snmp_session_sync_set(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        g_snmp_vbl_free(out);
    }
}

void
tunnel_mib_free_tunnelIfEntry(tunnel_mib_tunnelIfEntry_t *tunnelIfEntry)
{
    GSList *vbl;
    char *p;

    if (tunnelIfEntry) {
        p = (char *) tunnelIfEntry + sizeof(tunnel_mib_tunnelIfEntry_t);
        vbl = * (GSList **) p;
        g_snmp_vbl_free(vbl);
        g_free(tunnelIfEntry);
    }
}

void
tunnel_mib_free_tunnelIfTable(tunnel_mib_tunnelIfEntry_t **tunnelIfEntry)
{
    int i;

    if (tunnelIfEntry) {
        for (i = 0; tunnelIfEntry[i]; i++) {
            tunnel_mib_free_tunnelIfEntry(tunnelIfEntry[i]);
        }
        g_free(tunnelIfEntry);
    }
}

tunnel_mib_tunnelConfigEntry_t *
tunnel_mib_new_tunnelConfigEntry()
{
    tunnel_mib_tunnelConfigEntry_t *tunnelConfigEntry;

    tunnelConfigEntry = (tunnel_mib_tunnelConfigEntry_t *) g_malloc0(sizeof(tunnel_mib_tunnelConfigEntry_t) + sizeof(gpointer));
    return tunnelConfigEntry;
}

static int
unpack_tunnelConfigEntry(GSnmpVarBind *vb, tunnel_mib_tunnelConfigEntry_t *tunnelConfigEntry)
{
    guint8 idx = 13;
    guint16 i, len;

    len = 4;
    if (vb->id_len < idx + len) return -1;
    if (len != 4) return -1;
    if (vb->id_len < idx + len) return -1;
    for (i = 0; i < len; i++) {
        tunnelConfigEntry->tunnelConfigLocalAddress[i] = vb->id[idx++];
    }
    len = 4;
    if (vb->id_len < idx + len) return -1;
    if (len != 4) return -1;
    if (vb->id_len < idx + len) return -1;
    for (i = 0; i < len; i++) {
        tunnelConfigEntry->tunnelConfigRemoteAddress[i] = vb->id[idx++];
    }
    if (vb->id_len < idx) return -1;
    tunnelConfigEntry->tunnelConfigEncapsMethod = vb->id[idx++];
    if (vb->id_len < idx) return -1;
    tunnelConfigEntry->tunnelConfigID = vb->id[idx++];
    if (vb->id_len > idx) return -1;
    return 0;
}

static int
pack_tunnelConfigEntry(guint32 *base, guchar *tunnelConfigLocalAddress, guchar *tunnelConfigRemoteAddress, gint32 tunnelConfigEncapsMethod, gint32 tunnelConfigID)
{
    guint8 idx = 13;
    guint16 i, len;

    len = 4;
    if (len != 4) return -1;
    for (i = 0; i < len; i++) {
        base[idx++] = tunnelConfigLocalAddress[i];
        if (idx >= 128) return -1;
    }
    len = 4;
    if (len != 4) return -1;
    for (i = 0; i < len; i++) {
        base[idx++] = tunnelConfigRemoteAddress[i];
        if (idx >= 128) return -1;
    }
    base[idx++] = tunnelConfigEncapsMethod;
    base[idx++] = tunnelConfigID;
    return idx;
}

static tunnel_mib_tunnelConfigEntry_t *
assign_tunnelConfigEntry(GSList *vbl)
{
    tunnel_mib_tunnelConfigEntry_t *tunnelConfigEntry;
    char *p;

    tunnelConfigEntry = tunnel_mib_new_tunnelConfigEntry();
    if (! tunnelConfigEntry) {
        return NULL;
    }

    p = (char *) tunnelConfigEntry + sizeof(tunnel_mib_tunnelConfigEntry_t);
    * (GSList **) p = vbl;

    if (unpack_tunnelConfigEntry((GSnmpVarBind *) vbl->data, tunnelConfigEntry) < 0) {
        g_warning("%s: invalid instance identifier", "tunnelConfigEntry");
        g_free(tunnelConfigEntry);
        return NULL;
    }

    gsnmp_attr_assign(vbl, tunnelConfigEntry_oid, sizeof(tunnelConfigEntry_oid)/sizeof(guint32),
                      tunnelConfigEntry_attr, tunnelConfigEntry);

    return tunnelConfigEntry;
}

void
tunnel_mib_get_tunnelConfigTable(GSnmpSession *s, tunnel_mib_tunnelConfigEntry_t ***tunnelConfigEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    GSList *row;
    int i;
    static guint32 base[] = {1, 3, 6, 1, 2, 1, 10, 131, 1, 1, 2, 1, 0};

    *tunnelConfigEntry = NULL;

    gsnmp_attr_get(s, &in, base, 13, 12, tunnelConfigEntry_attr, mask);

    out = gsnmp_gettable(s, in);
    /* g_snmp_vbl_free(in); */

    if (out) {
        *tunnelConfigEntry = (tunnel_mib_tunnelConfigEntry_t **) g_malloc0((g_slist_length(out) + 1) * sizeof(tunnel_mib_tunnelConfigEntry_t *));
        if (! *tunnelConfigEntry) {
            s->error_status = G_SNMP_ERR_INTERNAL;
            g_snmp_vbl_free(out);
            return;
        }
        for (row = out, i = 0; row; row = g_slist_next(row), i++) {
            (*tunnelConfigEntry)[i] = assign_tunnelConfigEntry(row->data);
        }
    }
}

void
tunnel_mib_get_tunnelConfigEntry(GSnmpSession *s, tunnel_mib_tunnelConfigEntry_t **tunnelConfigEntry, guchar *tunnelConfigLocalAddress, guchar *tunnelConfigRemoteAddress, gint32 tunnelConfigEncapsMethod, gint32 tunnelConfigID, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, tunnelConfigEntry_oid, sizeof(tunnelConfigEntry_oid));
    len = pack_tunnelConfigEntry(base, tunnelConfigLocalAddress, tunnelConfigRemoteAddress, tunnelConfigEncapsMethod, tunnelConfigID);
    if (len < 0) {
        g_warning("%s: invalid index values", "tunnelConfigEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    *tunnelConfigEntry = NULL;

    gsnmp_attr_get(s, &in, base, len, 12, tunnelConfigEntry_attr, mask);

    out = g_snmp_session_sync_get(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        if (s->error_status != G_SNMP_ERR_NOERROR) {
            g_snmp_vbl_free(out);
            return;
        }
        *tunnelConfigEntry = assign_tunnelConfigEntry(out);
    }
}

void
tunnel_mib_set_tunnelConfigEntry(GSnmpSession *s, tunnel_mib_tunnelConfigEntry_t *tunnelConfigEntry, gint mask)
{
    GSList *in = NULL, *out = NULL;
    guint32 base[128];
    gint8 len;

    memset(base, 0, sizeof(base));
    memcpy(base, tunnelConfigEntry_oid, sizeof(tunnelConfigEntry_oid));
    len = pack_tunnelConfigEntry(base, tunnelConfigEntry->tunnelConfigLocalAddress, tunnelConfigEntry->tunnelConfigRemoteAddress, tunnelConfigEntry->tunnelConfigEncapsMethod, tunnelConfigEntry->tunnelConfigID);
    if (len < 0) {
        g_warning("%s: invalid index values", "tunnelConfigEntry");
        s->error_status = G_SNMP_ERR_INTERNAL;
        return;
    }

    gsnmp_attr_set(s, &in, base, len, 12, tunnelConfigEntry_attr, mask, tunnelConfigEntry);

    out = g_snmp_session_sync_set(s, in);
    g_snmp_vbl_free(in);
    if (out) {
        g_snmp_vbl_free(out);
    }
}

void
tunnel_mib_free_tunnelConfigEntry(tunnel_mib_tunnelConfigEntry_t *tunnelConfigEntry)
{
    GSList *vbl;
    char *p;

    if (tunnelConfigEntry) {
        p = (char *) tunnelConfigEntry + sizeof(tunnel_mib_tunnelConfigEntry_t);
        vbl = * (GSList **) p;
        g_snmp_vbl_free(vbl);
        g_free(tunnelConfigEntry);
    }
}

void
tunnel_mib_free_tunnelConfigTable(tunnel_mib_tunnelConfigEntry_t **tunnelConfigEntry)
{
    int i;

    if (tunnelConfigEntry) {
        for (i = 0; tunnelConfigEntry[i]; i++) {
            tunnel_mib_free_tunnelConfigEntry(tunnelConfigEntry[i]);
        }
        g_free(tunnelConfigEntry);
    }
}


