Blame snmplib/transports/snmpUDPIPv6Domain.c

Packit Service b38f0b
/*
Packit Service b38f0b
 * Portions of this file are copyrighted by:
Packit Service b38f0b
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
Packit Service b38f0b
 * Use is subject to license terms specified in the COPYING file
Packit Service b38f0b
 * distributed with the Net-SNMP package.
Packit Service b38f0b
 */
Packit Service b38f0b
#include <net-snmp/net-snmp-config.h>
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/library/snmpUDPIPv6Domain.h>
Packit Service b38f0b
#include <net-snmp/library/system.h>
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/types.h>
Packit Service b38f0b
Packit Service b38f0b
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
Packit Service b38f0b
Packit Service b38f0b
#include <stdio.h>
Packit Service b38f0b
#include <sys/types.h>
Packit Service b38f0b
#include <ctype.h>
Packit Service b38f0b
#include <errno.h>
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_STRING_H
Packit Service b38f0b
#include <string.h>
Packit Service b38f0b
#else
Packit Service b38f0b
#include <strings.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#include <stddef.h>
Packit Service b38f0b
#if HAVE_STDLIB_H
Packit Service b38f0b
#include <stdlib.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_UNISTD_H
Packit Service b38f0b
#include <unistd.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_SYS_SOCKET_H
Packit Service b38f0b
#include <sys/socket.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#if defined(HAVE_WINSOCK_H) && !defined(mingw32)
Packit Service b38f0b
static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_NETINET_IN_H
Packit Service b38f0b
#include <netinet/in.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_ARPA_INET_H
Packit Service b38f0b
#include <arpa/inet.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_NETDB_H
Packit Service b38f0b
#include <netdb.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_NET_IF_H
Packit Service b38f0b
#include <net/if.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_DMALLOC_H
Packit Service b38f0b
#include <dmalloc.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
Packit Service b38f0b
#define SS_FAMILY ss_family
Packit Service b38f0b
#elif HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
Packit Service b38f0b
#define SS_FAMILY __ss_family
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#if defined(darwin)
Packit Service b38f0b
#include <stdint.h> /* for uint8_t */
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/types.h>
Packit Service b38f0b
#include <net-snmp/output_api.h>
Packit Service b38f0b
#include <net-snmp/config_api.h>
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/library/snmp_transport.h>
Packit Service b38f0b
#include <net-snmp/library/snmpSocketBaseDomain.h>
Packit Service b38f0b
#include <net-snmp/library/tools.h>
Packit Service b38f0b
#include <net-snmp/library/snmp_assert.h>
Packit Service b38f0b
Packit Service b38f0b
#ifndef NETSNMP_NO_SYSTEMD
Packit Service b38f0b
#include <net-snmp/library/sd-daemon.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#include "inet_ntop.h"
Packit Service b38f0b
#include "inet_pton.h"
Packit Service b38f0b
Packit Service b38f0b
oid netsnmp_UDPIPv6Domain[] = { TRANSPORT_DOMAIN_UDP_IPV6 };
Packit Service b38f0b
static netsnmp_tdomain udp6Domain;
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * Return a string representing the address in data, or else the "far end"
Packit Service b38f0b
 * address if data is NULL.  
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
static char *
Packit Service b38f0b
netsnmp_udp6_fmtaddr(netsnmp_transport *t, const void *data, int len)
Packit Service b38f0b
{
Packit Service b38f0b
    return netsnmp_ipv6_fmtaddr("UDP/IPv6", t, data, len);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * You can write something into opaque that will subsequently get passed back 
Packit Service b38f0b
 * to your send function if you like.  For instance, you might want to
Packit Service b38f0b
 * remember where a PDU came from, so that you can send a reply there...  
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
static int
Packit Service b38f0b
netsnmp_udp6_recv(netsnmp_transport *t, void *buf, int size,
Packit Service b38f0b
		  void **opaque, int *olength)
Packit Service b38f0b
{
Packit Service b38f0b
    int             rc = -1;
Packit Service b38f0b
    socklen_t       fromlen = sizeof(struct sockaddr_in6);
Packit Service b38f0b
    struct sockaddr *from;
Packit Service b38f0b
Packit Service b38f0b
    if (t != NULL && t->sock >= 0) {
Packit Service b38f0b
        from = (struct sockaddr *) malloc(sizeof(struct sockaddr_in6));
Packit Service b38f0b
        if (from == NULL) {
Packit Service b38f0b
            *opaque = NULL;
Packit Service b38f0b
            *olength = 0;
Packit Service b38f0b
            return -1;
Packit Service b38f0b
        } else {
Packit Service b38f0b
            memset(from, 0, fromlen);
Packit Service b38f0b
        }
Packit Service b38f0b
Packit Service b38f0b
	while (rc < 0) {
Packit Service b38f0b
	  rc = recvfrom(t->sock, buf, size, 0, from, &fromlen);
Packit Service b38f0b
	  if (rc < 0 && errno != EINTR) {
Packit Service b38f0b
	    break;
Packit Service b38f0b
	  }
Packit Service b38f0b
	}
Packit Service b38f0b
Packit Service b38f0b
        if (rc >= 0) {
Packit Service b38f0b
            DEBUGIF("netsnmp_udp6") {
Packit Service b38f0b
                char *str = netsnmp_udp6_fmtaddr(NULL, from, fromlen);
Packit Service b38f0b
                DEBUGMSGTL(("netsnmp_udp6",
Packit Service b38f0b
                            "recvfrom fd %d got %d bytes (from %s)\n", t->sock,
Packit Service b38f0b
                            rc, str));
Packit Service b38f0b
                free(str);
Packit Service b38f0b
            }
Packit Service b38f0b
        } else {
Packit Service b38f0b
            DEBUGMSGTL(("netsnmp_udp6", "recvfrom fd %d err %d (\"%s\")\n",
Packit Service b38f0b
			t->sock, errno, strerror(errno)));
Packit Service b38f0b
        }
Packit Service b38f0b
        *opaque = (void *) from;
Packit Service b38f0b
        *olength = sizeof(struct sockaddr_in6);
Packit Service b38f0b
    }
Packit Service b38f0b
    return rc;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
static int
Packit Service b38f0b
netsnmp_udp6_send(netsnmp_transport *t, const void *buf, int size,
Packit Service b38f0b
		  void **opaque, int *olength)
Packit Service b38f0b
{
Packit Service b38f0b
    int rc = -1;
Packit Service b38f0b
    const struct sockaddr *to = NULL;
Packit Service b38f0b
Packit Service b38f0b
    if (opaque != NULL && *opaque != NULL &&
Packit Service b38f0b
        *olength == sizeof(struct sockaddr_in6)) {
Packit Service b38f0b
        to = (const struct sockaddr *) (*opaque);
Packit Service b38f0b
    } else if (t != NULL && t->data != NULL &&
Packit Service b38f0b
               ((t->data_length == sizeof(struct sockaddr_in6)) ||
Packit Service b38f0b
                (t->data_length == sizeof(netsnmp_indexed_addr_pair)))) {
Packit Service b38f0b
        to = (const struct sockaddr *) (t->data);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    if (to != NULL && t != NULL && t->sock >= 0) {
Packit Service b38f0b
        DEBUGIF("netsnmp_udp6") {
Packit Service b38f0b
            char *str = netsnmp_udp6_fmtaddr(NULL, to,
Packit Service b38f0b
                                             sizeof(struct sockaddr_in6));
Packit Service b38f0b
            DEBUGMSGTL(("netsnmp_udp6",
Packit Service b38f0b
                        "send %d bytes from %p to %s on fd %d\n",
Packit Service b38f0b
                        size, buf, str, t->sock));
Packit Service b38f0b
            free(str);
Packit Service b38f0b
        }
Packit Service b38f0b
	while (rc < 0) {
Packit Service b38f0b
	    rc = sendto(t->sock, buf, size, 0, to,sizeof(struct sockaddr_in6));
Packit Service b38f0b
	    if (rc < 0 && errno != EINTR) {
Packit Service b38f0b
		break;
Packit Service b38f0b
	    }
Packit Service b38f0b
	}
Packit Service b38f0b
    }
Packit Service b38f0b
    return rc;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * Initialize a UDP/IPv6-based transport for SNMP.  Local is TRUE if addr is the
Packit Service b38f0b
 * local address to bind to (i.e. this is a server-type session); otherwise
Packit Service b38f0b
 * addr is the remote address to send things to.  
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_transport_init(const struct sockaddr_in6 *addr, int flags)
Packit Service b38f0b
{
Packit Service b38f0b
    netsnmp_transport *t = NULL;
Packit Service b38f0b
    int             local = flags & NETSNMP_TSPEC_LOCAL;
Packit Service b38f0b
    u_char         *addr_ptr;
Packit Service b38f0b
Packit Service b38f0b
#ifdef NETSNMP_NO_LISTEN_SUPPORT
Packit Service b38f0b
    if (local)
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
Packit Service b38f0b
Packit Service b38f0b
    if (addr == NULL || addr->sin6_family != AF_INET6) {
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
Packit Service b38f0b
    if (t == NULL) {
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    t->sock = -1;
Packit Service b38f0b
Packit Service b38f0b
    addr_ptr = netsnmp_memdup(addr, sizeof(*addr));
Packit Service b38f0b
    if (addr_ptr == NULL) {
Packit Service b38f0b
        free(t);
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (local) {
Packit Service b38f0b
        /** This is a server session. */
Packit Service b38f0b
        t->local_length = sizeof(*addr);
Packit Service b38f0b
        t->local = addr_ptr;
Packit Service b38f0b
    } else {
Packit Service b38f0b
        /** This is a client session. */
Packit Service b38f0b
        t->remote = addr_ptr;
Packit Service b38f0b
        t->remote_length = sizeof(*addr);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    DEBUGIF("netsnmp_udp6") {
Packit Service b38f0b
        char *str = netsnmp_udp6_fmtaddr(NULL, addr, sizeof(*addr));
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udp6", "open %s %s\n", local ? "local" : "remote",
Packit Service b38f0b
                    str));
Packit Service b38f0b
        free(str);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    if (!local) {
Packit Service b38f0b
        netsnmp_indexed_addr_pair *addr_pair;
Packit Service b38f0b
Packit Service b38f0b
        /*
Packit Service b38f0b
         * allocate space to save the (remote) address in the
Packit Service b38f0b
         * transport-specific data pointer for later use by netsnmp_udp_send.
Packit Service b38f0b
         */
Packit Service b38f0b
        t->data = calloc(1, sizeof(netsnmp_indexed_addr_pair));
Packit Service b38f0b
        if (NULL == t->data) {
Packit Service b38f0b
            netsnmp_transport_free(t);
Packit Service b38f0b
            return NULL;
Packit Service b38f0b
        }
Packit Service b38f0b
        t->data_length = sizeof(netsnmp_indexed_addr_pair);
Packit Service b38f0b
Packit Service b38f0b
        addr_pair = (netsnmp_indexed_addr_pair *)t->data;
Packit Service b38f0b
        memcpy(&addr_pair->remote_addr, addr, sizeof(*addr));
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * 16-bit length field, 8 byte UDP header, 40 byte IPv6 header.
Packit Service b38f0b
     */
Packit Service b38f0b
Packit Service b38f0b
    t->msgMaxSize = 0xffff - 8 - 40;
Packit Service b38f0b
    t->f_recv     = netsnmp_udp6_recv;
Packit Service b38f0b
    t->f_send     = netsnmp_udp6_send;
Packit Service b38f0b
    t->f_close    = netsnmp_socketbase_close;
Packit Service b38f0b
    t->f_accept   = NULL;
Packit Service b38f0b
    t->f_fmtaddr  = netsnmp_udp6_fmtaddr;
Packit Service b38f0b
    t->f_get_taddr = netsnmp_ipv6_get_taddr;
Packit Service b38f0b
Packit Service b38f0b
    t->domain = netsnmp_UDPIPv6Domain;
Packit Service b38f0b
    t->domain_length =
Packit Service b38f0b
        sizeof(netsnmp_UDPIPv6Domain) / sizeof(netsnmp_UDPIPv6Domain[0]);
Packit Service b38f0b
Packit Service b38f0b
    return t;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
int
Packit Service b38f0b
netsnmp_udp6_transport_bind(netsnmp_transport *t,
Packit Service b38f0b
                            const struct sockaddr_in6 *addr,
Packit Service b38f0b
                            int flags)
Packit Service b38f0b
{
Packit Service b38f0b
    int             local = flags & NETSNMP_TSPEC_LOCAL;
Packit Service b38f0b
    int             rc = 0;
Packit Service b38f0b
Packit Service b38f0b
    if (local) {
Packit Service b38f0b
#ifndef NETSNMP_NO_LISTEN_SUPPORT
Packit Service b38f0b
        /*
Packit Service b38f0b
         * This session is intended as a server, so we must bind on to the
Packit Service b38f0b
         * given IP address, which may include an interface address, or could
Packit Service b38f0b
         * be INADDR_ANY, but certainly includes a port number.
Packit Service b38f0b
         */
Packit Service b38f0b
Packit Service b38f0b
#ifdef IPV6_V6ONLY
Packit Service b38f0b
        /* Try to restrict PF_INET6 socket to IPv6 communications only. */
Packit Service b38f0b
        {
Packit Service b38f0b
            int one=1;
Packit Service b38f0b
            if (setsockopt(t->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof(one)) != 0) {
Packit Service b38f0b
                DEBUGMSGTL(("netsnmp_udp6", "couldn't set IPV6_V6ONLY to %d bytes: %s\n", one, strerror(errno)));
Packit Service b38f0b
            } 
Packit Service b38f0b
        }
Packit Service b38f0b
#endif
Packit Service b38f0b
#else /* NETSNMP_NO_LISTEN_SUPPORT */
Packit Service b38f0b
        return -1;
Packit Service b38f0b
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    DEBUGIF("netsnmp_udp6") {
Packit Service b38f0b
        char *str;
Packit Service b38f0b
        str = netsnmp_udp6_fmtaddr(NULL, addr, sizeof(*addr));
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udpbase", "binding socket: %d to %s\n",
Packit Service b38f0b
                    t->sock, str));
Packit Service b38f0b
        free(str);
Packit Service b38f0b
    }
Packit Service b38f0b
    rc = bind(t->sock, (const struct sockaddr *)addr, sizeof(*addr));
Packit Service b38f0b
    if (rc != 0) {
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udp6", "failed to bind for clientaddr: %d %s\n",
Packit Service b38f0b
                    errno, strerror(errno)));
Packit Service b38f0b
        netsnmp_socketbase_close(t);
Packit Service b38f0b
        return -1;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    return 0;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
int
Packit Service b38f0b
netsnmp_udp6_transport_socket(int flags)
Packit Service b38f0b
{
Packit Service b38f0b
    int local = flags & NETSNMP_TSPEC_LOCAL;
Packit Service b38f0b
    int sock = socket(PF_INET6, SOCK_DGRAM, 0);
Packit Service b38f0b
Packit Service b38f0b
    DEBUGMSGTL(("UDPBase", "opened socket %d as local=%d\n", sock, local));
Packit Service b38f0b
    if (sock < 0)
Packit Service b38f0b
        return -1;
Packit Service b38f0b
Packit Service b38f0b
    _netsnmp_udp_sockopt_set(sock, local);
Packit Service b38f0b
Packit Service b38f0b
    return sock;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udp6_transport_get_bound_addr(netsnmp_transport *t)
Packit Service b38f0b
{
Packit Service b38f0b
    netsnmp_indexed_addr_pair *addr_pair;
Packit Service b38f0b
    socklen_t                  local_addr_len = sizeof(addr_pair->local_addr);
Packit Service b38f0b
    int                        rc;
Packit Service b38f0b
Packit Service b38f0b
    /** only for client transports: must have data and not local */
Packit Service b38f0b
    if (NULL == t || NULL != t->local || NULL == t->data ||
Packit Service b38f0b
        t->data_length < local_addr_len) {
Packit Service b38f0b
        snmp_log(LOG_ERR, "bad parameters for get bound addr\n");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    addr_pair = (netsnmp_indexed_addr_pair *)t->data;
Packit Service b38f0b
Packit Service b38f0b
    /** get local socket address for client session */
Packit Service b38f0b
    local_addr_len = sizeof(addr_pair->local_addr);
Packit Service b38f0b
    rc = getsockname(t->sock, (struct sockaddr*)&addr_pair->local_addr,
Packit Service b38f0b
                     &local_addr_len);
Packit Service b38f0b
    netsnmp_assert(rc == 0);
Packit Service b38f0b
    DEBUGIF("netsnmp_udpbase") {
Packit Service b38f0b
        char *str = netsnmp_udp6_fmtaddr(NULL, (void *)&addr_pair->local_addr,
Packit Service b38f0b
                                         sizeof(addr_pair->local_addr));
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udpbase", "socket %d bound to %s\n",
Packit Service b38f0b
                    t->sock, str));
Packit Service b38f0b
        free(str);
Packit Service b38f0b
    }
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udpipv6base_tspec_transport(netsnmp_tdomain_spec *tspec)
Packit Service b38f0b
{
Packit Service b38f0b
    struct sockaddr_in6 addr;
Packit Service b38f0b
    int local;
Packit Service b38f0b
Packit Service b38f0b
    if (NULL == tspec)
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
Packit Service b38f0b
    local = tspec->flags & NETSNMP_TSPEC_LOCAL;
Packit Service b38f0b
Packit Service b38f0b
    /** get address from target */
Packit Service b38f0b
    if (!netsnmp_sockaddr_in6_2(&addr, tspec->target, tspec->default_target))
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
Packit Service b38f0b
    if (NULL != tspec->source) {
Packit Service b38f0b
        struct sockaddr_in6 src_addr, *srcp = &src_addr;
Packit Service b38f0b
        /** get sockaddr from source */
Packit Service b38f0b
        if (!netsnmp_sockaddr_in6_2(&src_addr, tspec->source, NULL))
Packit Service b38f0b
            return NULL;
Packit Service b38f0b
        return netsnmp_udp6_transport_with_source(&addr, local, srcp);
Packit Service b38f0b
     } else {
Packit Service b38f0b
        /** if no source and we do not want any default client address */
Packit Service b38f0b
        if (tspec->flags & NETSNMP_TSPEC_NO_DFTL_CLIENT_ADDR)
Packit Service b38f0b
            return netsnmp_udp6_transport_with_source(&addr, local,
Packit Service b38f0b
                                                             NULL);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /** no source and default client address ok */
Packit Service b38f0b
    return netsnmp_udp6_transport(&addr, local);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_transport_with_source(const struct sockaddr_in6 *addr, int local,
Packit Service b38f0b
                                   const struct sockaddr_in6 *src_addr)
Packit Service b38f0b
{
Packit Service b38f0b
    netsnmp_transport         *t = NULL;
Packit Service b38f0b
    const struct sockaddr_in6 *bind_addr;
Packit Service b38f0b
    int                        rc, flags = 0;
Packit Service b38f0b
Packit Service b38f0b
    t = netsnmp_udp6_transport_init(addr, local);
Packit Service b38f0b
    if (NULL == t)
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
Packit Service b38f0b
    if (local) {
Packit Service b38f0b
        bind_addr = addr;
Packit Service b38f0b
        flags |= NETSNMP_TSPEC_LOCAL;
Packit Service b38f0b
Packit Service b38f0b
#ifndef NETSNMP_NO_SYSTEMD
Packit Service b38f0b
        /*
Packit Service b38f0b
         * Maybe the socket was already provided by systemd...
Packit Service b38f0b
         */
Packit Service b38f0b
        t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_DGRAM, -1,
Packit Service b38f0b
                                              ntohs(addr->sin6_port));
Packit Service b38f0b
#endif
Packit Service b38f0b
    }
Packit Service b38f0b
    else
Packit Service b38f0b
        bind_addr = src_addr;
Packit Service b38f0b
Packit Service b38f0b
    if (-1 == t->sock)
Packit Service b38f0b
        t->sock = netsnmp_udp6_transport_socket(flags);
Packit Service b38f0b
    if (t->sock < 0) {
Packit Service b38f0b
        netsnmp_transport_free(t);
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * If we've been given an address to bind to, then bind to it.
Packit Service b38f0b
     * Otherwise the OS will use "something sensible".
Packit Service b38f0b
     */
Packit Service b38f0b
    if (NULL == bind_addr)
Packit Service b38f0b
        return t;
Packit Service b38f0b
Packit Service b38f0b
    rc = netsnmp_udp6_transport_bind(t, bind_addr, flags);
Packit Service b38f0b
    if (rc) {
Packit Service b38f0b
        netsnmp_transport_free(t);
Packit Service b38f0b
        t = NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
    else if (!local)
Packit Service b38f0b
        netsnmp_udp6_transport_get_bound_addr(t);
Packit Service b38f0b
Packit Service b38f0b
    return t;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * Open a UDP/IPv6-based transport for SNMP.  Local is TRUE if addr is the
Packit Service b38f0b
 * local address to bind to (i.e. this is a server-type session); otherwise
Packit Service b38f0b
 * addr is the remote address to send things to.
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_transport(const struct sockaddr_in6 *addr, int local)
Packit Service b38f0b
{
Packit Service b38f0b
    if (!local) {
Packit Service b38f0b
        const char *client_socket;
Packit Service b38f0b
        client_socket = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
Packit Service b38f0b
                                              NETSNMP_DS_LIB_CLIENT_ADDR);
Packit Service b38f0b
        if (client_socket) {
Packit Service b38f0b
            struct sockaddr_in6 client_addr;
Packit Service 650b7d
            if(netsnmp_sockaddr_in6_2(&client_addr, client_socket, NULL)) {
Packit Service b38f0b
                return netsnmp_udp6_transport_with_source(addr, local,
Packit Service b38f0b
                                                          &client_addr);
Packit Service b38f0b
            }
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
    return netsnmp_udp6_transport_with_source(addr, local, NULL);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
/*
Packit Service b38f0b
 * The following functions provide the "com2sec6" configuration token
Packit Service b38f0b
 * functionality for compatibility.
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
#define EXAMPLE_NETWORK       "NETWORK"
Packit Service b38f0b
#define EXAMPLE_COMMUNITY     "COMMUNITY"
Packit Service b38f0b
Packit Service b38f0b
typedef struct com2Sec6Entry_s {
Packit Service b38f0b
    const char     *secName;
Packit Service b38f0b
    const char     *contextName;
Packit Service b38f0b
    struct com2Sec6Entry_s *next;
Packit Service b38f0b
    struct in6_addr network;
Packit Service b38f0b
    struct in6_addr mask;
Packit Service b38f0b
    int             negate;
Packit Service b38f0b
    const char      community[1];
Packit Service b38f0b
} com2Sec6Entry;
Packit Service b38f0b
Packit Service b38f0b
static com2Sec6Entry  *com2Sec6List = NULL, *com2Sec6ListLast = NULL;
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
NETSNMP_STATIC_INLINE int
Packit Service b38f0b
create_com2Sec6Entry(const struct addrinfo* const run,
Packit Service b38f0b
                     const struct in6_addr* const mask,
Packit Service b38f0b
                     const char* const secName,
Packit Service b38f0b
                     const size_t secNameLen,
Packit Service b38f0b
                     const char* const contextName,
Packit Service b38f0b
                     const size_t contextNameLen,
Packit Service b38f0b
                     const char* const community,
Packit Service b38f0b
                     const size_t communityLen,
Packit Service b38f0b
                     int negate,
Packit Service b38f0b
                     com2Sec6Entry** const begin,
Packit Service b38f0b
                     com2Sec6Entry** const end)
Packit Service b38f0b
{
Packit Service b38f0b
    const struct sockaddr_in6 * const run_addr =
Packit Service b38f0b
        (const struct sockaddr_in6*)run->ai_addr;
Packit Service b38f0b
    int i;
Packit Service b38f0b
Packit Service b38f0b
    /* Check that the network and mask are consistent. */
Packit Service b38f0b
    for (i = 0; i < 16; ++i) {
Packit Service b38f0b
        if (run_addr->sin6_addr.s6_addr[i] & ~mask->s6_addr[i]) {
Packit Service b38f0b
            config_perror("source/mask mismatch");
Packit Service b38f0b
            return 1;
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    {
Packit Service b38f0b
        char buf1[INET6_ADDRSTRLEN];
Packit Service b38f0b
        char buf2[INET6_ADDRSTRLEN];
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udp6_parse_security",
Packit Service b38f0b
                    "<\"%s\", %s/%s> => \"%s\"\n",
Packit Service b38f0b
                    community,
Packit Service b38f0b
                    inet_ntop(AF_INET6, &run_addr->sin6_addr,
Packit Service b38f0b
                              buf1, sizeof(buf1)),
Packit Service b38f0b
                    inet_ntop(AF_INET6, mask, buf2, sizeof(buf2)),
Packit Service b38f0b
                    secName));
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    {
Packit Service b38f0b
        /* Allocate all the needed chunks */
Packit Service b38f0b
        void * const v =
Packit Service b38f0b
            malloc(offsetof(com2Sec6Entry, community) + communityLen +
Packit Service b38f0b
                   secNameLen + contextNameLen);
Packit Service b38f0b
Packit Service b38f0b
        com2Sec6Entry* const e = (com2Sec6Entry*)v;
Packit Service b38f0b
        char *last = ((char*)v) + offsetof(com2Sec6Entry, community);
Packit Service b38f0b
Packit Service b38f0b
        if (v == NULL) {
Packit Service b38f0b
            config_perror("memory error");
Packit Service b38f0b
            return 1;
Packit Service b38f0b
        }
Packit Service b38f0b
Packit Service b38f0b
        memcpy(last, community, communityLen);
Packit Service b38f0b
        last += communityLen;
Packit Service b38f0b
Packit Service b38f0b
        memcpy(last, secName, secNameLen);
Packit Service b38f0b
        e->secName = last;
Packit Service b38f0b
        last += secNameLen;
Packit Service b38f0b
Packit Service b38f0b
        if (contextNameLen) {
Packit Service b38f0b
            memcpy(last, contextName, contextNameLen);
Packit Service b38f0b
            e->contextName = last;
Packit Service b38f0b
        } else
Packit Service b38f0b
            e->contextName = last - 1;
Packit Service b38f0b
Packit Service b38f0b
        memcpy(&e->network, &run_addr->sin6_addr, sizeof(struct in6_addr));
Packit Service b38f0b
        memcpy(&e->mask, mask, sizeof(struct in6_addr));
Packit Service b38f0b
Packit Service b38f0b
        e->negate = negate;
Packit Service b38f0b
        e->next = NULL;
Packit Service b38f0b
        if (*end != NULL) {
Packit Service b38f0b
            (*end)->next = e;
Packit Service b38f0b
            *end = e;
Packit Service b38f0b
        } else {
Packit Service b38f0b
            *end = *begin = e;
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
    return 0;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udp6_parse_security(const char *token, char *param)
Packit Service b38f0b
{
Packit Service b38f0b
    /** copy_nword does null term, so we need vars of max size + 2. */
Packit Service b38f0b
    /** (one for null, one to detect param too long */
Packit Service b38f0b
    char            secName[VACMSTRINGLEN]; /* == VACM_MAX_STRING + 2 */
Packit Service b38f0b
    size_t          secNameLen;
Packit Service b38f0b
    char            contextName[VACMSTRINGLEN];
Packit Service b38f0b
    size_t          contextNameLen;
Packit Service b38f0b
    char            community[COMMUNITY_MAX_LEN + 2];/* overflow + null char */
Packit Service b38f0b
    size_t          communityLen;
Packit Service b38f0b
    char            source[301]; /* !(1)+dns-name(253)+/(1)+mask(45)+\0(1) */
Packit Service b38f0b
    char            *sourcep;
Packit Service b38f0b
    struct in6_addr mask;
Packit Service b38f0b
    int             negate;
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * Get security, source address/netmask and community strings.
Packit Service b38f0b
     */
Packit Service b38f0b
Packit Service b38f0b
    param = copy_nword( param, secName, sizeof(secName));
Packit Service b38f0b
    if (strcmp(secName, "-Cn") == 0) {
Packit Service b38f0b
        if (!param) {
Packit Service b38f0b
            config_perror("missing CONTEXT_NAME parameter");
Packit Service b38f0b
            return;
Packit Service b38f0b
        }
Packit Service b38f0b
        param = copy_nword( param, contextName, sizeof(contextName));
Packit Service b38f0b
        contextNameLen = strlen(contextName);
Packit Service b38f0b
        if (contextNameLen > VACM_MAX_STRING) {
Packit Service b38f0b
            config_perror("context name too long");
Packit Service b38f0b
            return;
Packit Service b38f0b
        }
Packit Service b38f0b
        if (!param) {
Packit Service b38f0b
            config_perror("missing NAME parameter");
Packit Service b38f0b
            return;
Packit Service b38f0b
        }
Packit Service b38f0b
        ++contextNameLen; /* null termination */
Packit Service b38f0b
        param = copy_nword( param, secName, sizeof(secName));
Packit Service b38f0b
    } else {
Packit Service b38f0b
        contextNameLen = 0;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    secNameLen = strlen(secName);
Packit Service b38f0b
    if (secNameLen == 0) {
Packit Service b38f0b
        config_perror("empty NAME parameter");
Packit Service b38f0b
        return;
Packit Service b38f0b
    } else if (secNameLen > VACM_MAX_STRING) {
Packit Service b38f0b
        config_perror("security name too long");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    ++secNameLen; /* null termination */
Packit Service b38f0b
Packit Service b38f0b
    if (!param) {
Packit Service b38f0b
        config_perror("missing SOURCE parameter");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    param = copy_nword( param, source, sizeof(source));
Packit Service b38f0b
    if (source[0] == '\0') {
Packit Service b38f0b
        config_perror("empty SOURCE parameter");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (strncmp(source, EXAMPLE_NETWORK, strlen(EXAMPLE_NETWORK)) == 0) {
Packit Service b38f0b
        config_perror("example config NETWORK not properly configured");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    if (!param) {
Packit Service b38f0b
        config_perror("missing COMMUNITY parameter");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    param = copy_nword( param, community, sizeof(community));
Packit Service b38f0b
    if (community[0] == '\0') {
Packit Service b38f0b
        config_perror("empty COMMUNITY parameter");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    communityLen = strlen(community);
Packit Service b38f0b
    if (communityLen > COMMUNITY_MAX_LEN) {
Packit Service b38f0b
        config_perror("community name too long");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
    ++communityLen; /* null termination */
Packit Service b38f0b
    if (communityLen == sizeof(EXAMPLE_COMMUNITY) &&
Packit Service b38f0b
        memcmp(community, EXAMPLE_COMMUNITY, sizeof(EXAMPLE_COMMUNITY)) == 0) {
Packit Service b38f0b
        config_perror("example config COMMUNITY not properly configured");
Packit Service b38f0b
        return;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /* Possible mask cases
Packit Service b38f0b
     * "default" <=> 0::0/0
Packit Service b38f0b
     * <hostname>[/] <=> <hostname>/128
Packit Service b38f0b
     * <hostname>/number <=> <hostname>/number
Packit Service b38f0b
     * <hostname>/<mask> <=> <hostname>/<mask>
Packit Service b38f0b
     */
Packit Service b38f0b
    {
Packit Service b38f0b
        /* Deal with the "default" case first. */
Packit Service b38f0b
        const int isdefault = strcmp(source, "default") == 0;
Packit Service b38f0b
Packit Service b38f0b
        if (isdefault) {
Packit Service b38f0b
            memset(mask.s6_addr, '\0', sizeof(mask.s6_addr));
Packit Service b38f0b
            negate = 0;
Packit Service b38f0b
            sourcep = NULL;    /* gcc gets confused about sourcep being used */
Packit Service b38f0b
        } else {
Packit Service b38f0b
            if (*source == '!') {
Packit Service b38f0b
               negate = 1;
Packit Service b38f0b
               sourcep = source + 1;
Packit Service b38f0b
            } else {
Packit Service b38f0b
               negate = 0;
Packit Service b38f0b
               sourcep = source;
Packit Service b38f0b
            }
Packit Service b38f0b
Packit Service b38f0b
            /* Split the source/netmask parts */
Packit Service b38f0b
            char *strmask = strchr(sourcep, '/');
Packit Service b38f0b
            if (strmask != NULL)
Packit Service b38f0b
                /* Mask given. */
Packit Service b38f0b
                *strmask++ = '\0';
Packit Service b38f0b
Packit Service b38f0b
            /* Try to interpret the mask */
Packit Service b38f0b
            if (strmask == NULL || *strmask == '\0') {
Packit Service b38f0b
                /* No mask was given. Assume /128 */
Packit Service b38f0b
                memset(mask.s6_addr, '\xff', sizeof(mask.s6_addr));
Packit Service b38f0b
            } else {
Packit Service b38f0b
                /* Try to interpret mask as a "number of 1 bits". */
Packit Service b38f0b
                char* cp;
Packit Service b38f0b
                long masklength = strtol(strmask, &cp, 10);
Packit Service b38f0b
                if (*cp == '\0') {
Packit Service b38f0b
                    if (0 <= masklength && masklength <= 128) {
Packit Service b38f0b
                        const int j = masklength / 8;
Packit Service b38f0b
                        const int jj = masklength % 8;
Packit Service b38f0b
                        memset(mask.s6_addr, '\xff', j);
Packit Service b38f0b
                        if (j < 16) {
Packit Service b38f0b
                            mask.s6_addr[j] = ((uint8_t)~0U << (8 - jj));
Packit Service b38f0b
                            memset(mask.s6_addr + j + 1, '\0', 15 - j);
Packit Service b38f0b
                        }
Packit Service b38f0b
                    } else {
Packit Service b38f0b
                        config_perror("bad mask length");
Packit Service b38f0b
                        return;
Packit Service b38f0b
                    }
Packit Service b38f0b
                }
Packit Service b38f0b
                /* Try to interpret mask numerically. */
Packit Service b38f0b
                else if (inet_pton(AF_INET6, strmask, &mask) != 1) {
Packit Service b38f0b
                    config_perror("bad mask");
Packit Service b38f0b
                    return;
Packit Service b38f0b
                }
Packit Service b38f0b
            }
Packit Service b38f0b
        }
Packit Service b38f0b
Packit Service b38f0b
        {
Packit Service b38f0b
            struct sockaddr_in6 pton_addr;
Packit Service b38f0b
            struct addrinfo hints, *res = NULL;
Packit Service b38f0b
            memset(&hints, '\0', sizeof(hints));
Packit Service b38f0b
Packit Service b38f0b
            /* First check if default, otherwise try to parse as a numeric
Packit Service b38f0b
             * address, if that also fails try to lookup the address */
Packit Service b38f0b
            if (isdefault) {
Packit Service b38f0b
                memset(&pton_addr.sin6_addr.s6_addr, '\0',
Packit Service b38f0b
                       sizeof(struct in6_addr));
Packit Service b38f0b
            } else if (inet_pton(AF_INET6, sourcep, &pton_addr.sin6_addr) != 1) {
Packit Service b38f0b
                /* Nope, wasn't a numeric address. Must be a hostname. */
Packit Service b38f0b
#if HAVE_GETADDRINFO
Packit Service b38f0b
                int             gai_error;
Packit Service b38f0b
Packit Service b38f0b
                hints.ai_family = AF_INET6;
Packit Service b38f0b
                hints.ai_socktype = SOCK_DGRAM;
Packit Service b38f0b
                gai_error = netsnmp_getaddrinfo(sourcep, NULL, &hints, &res;;
Packit Service b38f0b
                if (gai_error != 0) {
Packit Service b38f0b
                    config_perror(gai_strerror(gai_error));
Packit Service b38f0b
                    return;
Packit Service b38f0b
                }
Packit Service b38f0b
#else
Packit Service b38f0b
                config_perror("getaddrinfo() not available");
Packit Service b38f0b
                return;
Packit Service b38f0b
#endif
Packit Service b38f0b
            }
Packit Service b38f0b
            if (res == NULL) {
Packit Service b38f0b
                hints.ai_addrlen = sizeof(pton_addr);
Packit Service b38f0b
                hints.ai_addr = (struct sockaddr*)&pton_addr;
Packit Service b38f0b
                hints.ai_next = NULL;
Packit Service b38f0b
                res = &hints;
Packit Service b38f0b
            }
Packit Service b38f0b
Packit Service b38f0b
            {
Packit Service b38f0b
                struct addrinfo *run;
Packit Service b38f0b
                int    failed = 0;
Packit Service b38f0b
                com2Sec6Entry *begin = NULL, *end = NULL;
Packit Service b38f0b
Packit Service b38f0b
                for (run = res; run && !failed; run = run->ai_next)
Packit Service b38f0b
                    failed =
Packit Service b38f0b
                        create_com2Sec6Entry(run, &mask,
Packit Service b38f0b
                                             secName, secNameLen,
Packit Service b38f0b
                                             contextName, contextNameLen,
Packit Service b38f0b
                                             community, communityLen, negate,
Packit Service b38f0b
                                             &begin, &end;;
Packit Service b38f0b
Packit Service b38f0b
                if (failed) {
Packit Service b38f0b
                    /* Free eventually allocated chunks */
Packit Service b38f0b
                    while (begin) {
Packit Service b38f0b
                        end = begin;
Packit Service b38f0b
                        begin = begin->next;
Packit Service b38f0b
                        free(end);
Packit Service b38f0b
                    }
Packit Service b38f0b
                } else if (com2Sec6ListLast != NULL) {
Packit Service b38f0b
                    com2Sec6ListLast->next = begin;
Packit Service b38f0b
                    com2Sec6ListLast = end;
Packit Service b38f0b
                } else {
Packit Service b38f0b
                    com2Sec6List = begin;
Packit Service b38f0b
                    com2Sec6ListLast = end;
Packit Service b38f0b
                }
Packit Service b38f0b
            }
Packit Service b38f0b
#if HAVE_GETADDRINFO
Packit Service b38f0b
            if (res != &hints)
Packit Service b38f0b
                freeaddrinfo(res);
Packit Service b38f0b
#endif
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udp6_com2Sec6List_free(void)
Packit Service b38f0b
{
Packit Service b38f0b
    com2Sec6Entry  *e = com2Sec6List;
Packit Service b38f0b
    while (e != NULL) {
Packit Service b38f0b
        com2Sec6Entry  *tmp = e;
Packit Service b38f0b
        e = e->next;
Packit Service b38f0b
        free(tmp);
Packit Service b38f0b
    }
Packit Service b38f0b
    com2Sec6List = com2Sec6ListLast = NULL;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
#endif /* support for community based SNMP */
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udp6_agent_config_tokens_register(void)
Packit Service b38f0b
{
Packit Service b38f0b
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
    register_app_config_handler("com2sec6", netsnmp_udp6_parse_security,
Packit Service b38f0b
                                netsnmp_udp6_com2Sec6List_free,
Packit Service b38f0b
                                "[-Cn CONTEXT] secName IPv6-network-address[/netmask] community");
Packit Service b38f0b
#endif /* support for community based SNMP */
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * Return 0 if there are no com2sec entries, or return 1 if there ARE com2sec
Packit Service b38f0b
 * entries.  On return, if a com2sec entry matched the passed parameters,
Packit Service b38f0b
 * then *secName points at the appropriate security name, or is NULL if the
Packit Service b38f0b
 * parameters did not match any com2sec entry.
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
int
Packit Service b38f0b
netsnmp_udp6_getSecName(void *opaque, int olength,
Packit Service b38f0b
                        const char *community,
Packit Service b38f0b
                        int community_len,
Packit Service b38f0b
                        const char **secName, const char **contextName)
Packit Service b38f0b
{
Packit Service b38f0b
    const com2Sec6Entry *c;
Packit Service b38f0b
    struct sockaddr_in6 *from = (struct sockaddr_in6 *) opaque;
Packit Service b38f0b
    char           *ztcommunity = NULL;
Packit Service b38f0b
    char            str6[INET6_ADDRSTRLEN];
Packit Service b38f0b
Packit Service b38f0b
    if (secName != NULL) {
Packit Service b38f0b
        *secName = NULL;  /* Haven't found anything yet */
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * Special case if there are NO entries (as opposed to no MATCHING
Packit Service b38f0b
     * entries).
Packit Service b38f0b
     */
Packit Service b38f0b
Packit Service b38f0b
    if (com2Sec6List == NULL) {
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udp6_getSecName", "no com2sec entries\n"));
Packit Service b38f0b
        return 0;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * If there is no IPv6 source address, then there can be no valid security
Packit Service b38f0b
     * name.
Packit Service b38f0b
     */
Packit Service b38f0b
Packit Service b38f0b
    if (opaque == NULL || olength != sizeof(struct sockaddr_in6)
Packit Service b38f0b
        || from->sin6_family != PF_INET6) {
Packit Service b38f0b
        DEBUGMSGTL(("netsnmp_udp6_getSecName",
Packit Service b38f0b
                    "no IPv6 source address in PDU?\n"));
Packit Service b38f0b
        return 1;
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    ztcommunity = (char *) malloc(community_len + 1);
Packit Service b38f0b
    if (ztcommunity != NULL) {
Packit Service b38f0b
        memcpy(ztcommunity, community, community_len);
Packit Service b38f0b
        ztcommunity[community_len] = '\0';
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    inet_ntop(AF_INET6, &from->sin6_addr, str6, sizeof(str6));
Packit Service b38f0b
    DEBUGMSGTL(("netsnmp_udp6_getSecName", "resolve <\"%s\", %s>\n",
Packit Service b38f0b
                ztcommunity ? ztcommunity : "<malloc error>", str6));
Packit Service b38f0b
Packit Service b38f0b
    for (c = com2Sec6List; c != NULL; c = c->next) {
Packit Service b38f0b
        {
Packit Service b38f0b
            char buf1[INET6_ADDRSTRLEN];
Packit Service b38f0b
            char buf2[INET6_ADDRSTRLEN];
Packit Service b38f0b
            DEBUGMSGTL(("netsnmp_udp6_getSecName",
Packit Service b38f0b
                        "compare <\"%s\", %s/%s>", c->community,
Packit Service b38f0b
                        inet_ntop(AF_INET6, &c->network, buf1, sizeof(buf1)),
Packit Service b38f0b
                        inet_ntop(AF_INET6, &c->mask, buf2, sizeof(buf2))));
Packit Service b38f0b
        }
Packit Service b38f0b
        if ((community_len == (int)strlen(c->community)) &&
Packit Service b38f0b
            (memcmp(community, c->community, community_len) == 0)) {
Packit Service b38f0b
            int i, ok = 1;
Packit Service b38f0b
            for (i = 0; ok && i < 16; ++i)
Packit Service b38f0b
                if ((from->sin6_addr.s6_addr[i] & c->mask.s6_addr[i]) !=
Packit Service b38f0b
                    c->network.s6_addr[i])
Packit Service b38f0b
                    ok = 0;
Packit Service b38f0b
            if (ok) {
Packit Service b38f0b
                DEBUGMSG(("netsnmp_udp6_getSecName", "... SUCCESS\n"));
Packit Service b38f0b
                if (c->negate) {
Packit Service b38f0b
                   /*
Packit Service b38f0b
                    * If we matched a negative entry, then we are done - claim that we
Packit Service b38f0b
                    * matched nothing.
Packit Service b38f0b
                    */
Packit Service b38f0b
                   DEBUGMSG(("netsnmp_udp6_getSecName", "... <negative entry>\n"));
Packit Service b38f0b
                   break;
Packit Service b38f0b
                }
Packit Service b38f0b
                if (secName != NULL) {
Packit Service b38f0b
                    *secName = c->secName;
Packit Service b38f0b
                    *contextName = c->contextName;
Packit Service b38f0b
                }
Packit Service b38f0b
                break;
Packit Service b38f0b
            }
Packit Service b38f0b
        }
Packit Service b38f0b
        else {
Packit Service b38f0b
            DEBUGMSG(("netsnmp_udp6_getSecName", "... nope\n"));
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    if (ztcommunity != NULL) {
Packit Service b38f0b
        free(ztcommunity);
Packit Service b38f0b
    }
Packit Service b38f0b
    return 1;
Packit Service b38f0b
}
Packit Service b38f0b
#endif /* support for community based SNMP */
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_create_tstring(const char *str, int local,
Packit Service b38f0b
			    const char *default_target)
Packit Service b38f0b
{
Packit Service b38f0b
    struct sockaddr_in6 addr;
Packit Service b38f0b
Packit Service b38f0b
    if (netsnmp_sockaddr_in6_2(&addr, str, default_target)) {
Packit Service b38f0b
        return netsnmp_udp6_transport(&addr, local);
Packit Service b38f0b
    } else {
Packit Service b38f0b
        return NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_create_tspec(netsnmp_tdomain_spec *tspec)
Packit Service b38f0b
{
Packit Service b38f0b
    netsnmp_transport *t = netsnmp_udpipv6base_tspec_transport(tspec);
Packit Service b38f0b
    return t;
Packit Service b38f0b
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
/*
Packit Service b38f0b
 * See:
Packit Service b38f0b
 * 
Packit Service b38f0b
 * http://www.ietf.org/internet-drafts/draft-ietf-ops-taddress-mib-01.txt
Packit Service b38f0b
 * 
Packit Service b38f0b
 * (or newer equivalent) for details of the TC which we are using for
Packit Service b38f0b
 * the mapping here.  
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
netsnmp_transport *
Packit Service b38f0b
netsnmp_udp6_create_ostring(const void *o, size_t o_len, int local)
Packit Service b38f0b
{
Packit Service b38f0b
    struct sockaddr_in6 sin6;
Packit Service b38f0b
Packit Service b38f0b
    if (netsnmp_ipv6_ostring_to_sockaddr(&sin6, o, o_len))
Packit Service b38f0b
        return netsnmp_udp6_transport(&sin6, local);
Packit Service b38f0b
    return NULL;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udpipv6_ctor(void)
Packit Service b38f0b
{
Packit Service b38f0b
    udp6Domain.name = netsnmp_UDPIPv6Domain;
Packit Service b38f0b
    udp6Domain.name_length = sizeof(netsnmp_UDPIPv6Domain) / sizeof(oid);
Packit Service b38f0b
    udp6Domain.f_create_from_tstring     = NULL;
Packit Service b38f0b
    udp6Domain.f_create_from_tstring_new = netsnmp_udp6_create_tstring;
Packit Service b38f0b
    udp6Domain.f_create_from_tspec       = netsnmp_udp6_create_tspec;
Packit Service b38f0b
    udp6Domain.f_create_from_ostring     = netsnmp_udp6_create_ostring;
Packit Service b38f0b
    udp6Domain.prefix = (const char**)calloc(5, sizeof(char *));
Packit Service b38f0b
    udp6Domain.prefix[0] = "udp6";
Packit Service b38f0b
    udp6Domain.prefix[1] = "ipv6";
Packit Service b38f0b
    udp6Domain.prefix[2] = "udpv6";
Packit Service b38f0b
    udp6Domain.prefix[3] = "udpipv6";
Packit Service b38f0b
Packit Service b38f0b
    netsnmp_tdomain_register(&udp6Domain);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
#else
Packit Service b38f0b
Packit Service b38f0b
#ifdef NETSNMP_DLL
Packit Service b38f0b
/* need this hook for win32 MSVC++ DLL build */
Packit Service b38f0b
void
Packit Service b38f0b
netsnmp_udp6_agent_config_tokens_register(void)
Packit Service b38f0b
{ }
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */
Packit Service b38f0b