Date: Mon, 16 Jun 2014 17:58:04 GMT From: shonali@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r269631 - soc2014/shonali/head/contrib/bsnmp/snmpd Message-ID: <201406161758.s5GHw4PS015765@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: shonali Date: Mon Jun 16 17:58:03 2014 New Revision: 269631 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=269631 Log: Completed ipv6 implementation of udp (trans_udpv6.c) and added the implementation to main Added: soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c Deleted: soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udp_ipv6.c Modified: soc2014/shonali/head/contrib/bsnmp/snmpd/main.c Modified: soc2014/shonali/head/contrib/bsnmp/snmpd/main.c ============================================================================== --- soc2014/shonali/head/contrib/bsnmp/snmpd/main.c Mon Jun 16 17:43:28 2014 (r269630) +++ soc2014/shonali/head/contrib/bsnmp/snmpd/main.c Mon Jun 16 17:58:03 2014 (r269631) @@ -233,6 +233,7 @@ /* transports */ extern const struct transport_def udp_trans; extern const struct transport_def lsock_trans; +extern const struct transport_def udpv6_trans; struct transport_list transport_list = TAILQ_HEAD_INITIALIZER(transport_list); @@ -1819,6 +1820,8 @@ syslog(LOG_WARNING, "cannot start UDP transport"); if (lsock_trans.start() != SNMP_ERR_NOERROR) syslog(LOG_WARNING, "cannot start LSOCK transport"); + if (udpv6_trans.start() != SNMP_ERR_NOERROR) + syslog(LOG_WARNING, "cannot start UDPv6 transport"); #ifdef USE_LIBBEGEMOT if (debug.evdebug > 0) Added: soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c Mon Jun 16 17:58:03 2014 (r269631) @@ -0,0 +1,333 @@ +/* + * Copyright (c) 2003 + * Fraunhofer Institute for Open Communication Systems (FhG Fokus). + * All rights reserved. + * + * Author: Harti Brandt <harti@freebsd.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Begemot: bsnmp/snmpd/trans_udpv6.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $ + * + * UDP transport + */ +#include <sys/types.h> +#include <sys/queue.h> + +#include <stdlib.h> +#include <syslog.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include "snmpmod.h" +#include "snmpd.h" +#include "trans_udpv6.h" +#include "tree.h" +#include "oid.h" + +static int udpv6_start(void); +static int udpv6_stop(int); +static void udpv6_close_port(struct tport *); +static int udpv6_init_port(struct tport *); +static ssize_t udpv6_send(struct tport *, const u_char *, size_t, + const struct sockaddr *, size_t); + +/* exported */ +const struct transport_def udpv6_trans = { + "udpv6", + OIDX_begemotSnmpdTransIpv6Udp, + udpv6_start, + udpv6_stop, + udpv6_close_port, + udpv6_init_port, + udpv6_send +}; +static struct transport *my_trans; + +static int +udpv6_start(void) +{ + return (trans_register(&udpv6_trans, &my_trans)); +} + +static int +udpv6_stop(int force __unused) +{ + if (my_trans != NULL) + if (trans_unregister(my_trans) != 0) + return (SNMP_ERR_GENERR); + return (SNMP_ERR_NOERROR); +} + +/* + * A UDP port is ready + */ +static void +udpv6_input(int fd __unused, void *udata) +{ + struct udpv6_port *p = udata; + + p->input.peerlen = sizeof(p->ret); + snmpd_input(&p->input, &p->tport); +} + +/* + * Create a UDP socket and bind it to the given port + */ +static int +udpv6_init_port(struct tport *tp) +{ + struct udpv6_port *p = (struct udpv6_port *)tp; + struct sockaddr_in6 addr; + void *ptr; + u_int32_t ip[4]; + const int on = 1; + char str[INET6_ADDRSTRLEN]; + + if ((p->input.fd = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { + syslog(LOG_ERR, "creating UDP socket: %m"); + return (SNMP_ERR_RES_UNAVAIL); + } + + /* Need to check - modify ip so as to store ipv6 */ + ip[0] = htonl((p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) | + p->addr[3]); + ip[1] = htonl((p->addr[4] << 24) | (p->addr[5] << 16) | (p->addr[6] << 8) | + p->addr[7]); + ip[2] = htonl((p->addr[8] << 24) | (p->addr[9] << 16) | (p->addr[10] << 8) | + p->addr[11]); + ip[3] = htonl((p->addr[12] << 24) | (p->addr[13] << 16) | (p->addr[14] << 8) | + p->addr[15]); + + ptr = ip; + + % Need to check - can use getaddrinfo instead to fill up addr structure % + memset(&addr, 0, sizeof(addr)); + addr.sin6_addr.s_addr = *ptr; + addr.sin6_port = htons(p->port); + addr.sin6_family = AF_INET6; + addr.sin_len = sizeof(addr); + if (addr.sin6_addr.s_addr == IN6ADDR_ANY && + setsockopt(p->input.fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, + sizeof(on)) == -1) { + syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m"); + close(p->input.fd); + p->input.fd = -1; + return (SNMP_ERR_GENERR); + } + if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) { + if (errno == EADDRNOTAVAIL) { + close(p->input.fd); + p->input.fd = -1; + return (SNMP_ERR_INCONS_NAME); + } + inet_ntop(AF_INET6, &(addr.sin_addr), str, INET6_ADDRSTRLEN) + syslog(LOG_ERR, "bind: %s:%u %m", str, + p->port); + close(p->input.fd); + p->input.fd = -1; + return (SNMP_ERR_GENERR); + } + if ((p->input.id = fd_select(p->input.fd, udp_input, + p, NULL)) == NULL) { + close(p->input.fd); + p->input.fd = -1; + return (SNMP_ERR_GENERR); + } + return (SNMP_ERR_NOERROR); +} + +/* + * Create a new SNMP Port object and start it, if we are not + * in initialization mode. The arguments are in host byte order. + */ +static int +udpv6_open_port(u_int8_t *addr, u_int32_t udpv6_port, struct udpv6_port **pp) +{ + struct udpv6_port *port; + int i, err; + + if (udpv6_port > 0xffff) + return (SNMP_ERR_NO_CREATION); + if ((port = malloc(sizeof(*port))) == NULL) + return (SNMP_ERR_GENERR); + memset(port, 0, sizeof(*port)); + + /* initialize common part */ + /* bug here - port should be two bytes - check if this must be changed for ipv6 module */ + + port->tport.index.len = 17; + + for(i = 0; i < 16; i++) { + port->tport.index.subs[i] = addr[i]; + port->addr[i] = addr[i]; + } + + port->tport.index.subs[16] = udpv6_port; + port->port = udpv6_port; + + port->input.fd = -1; + port->input.id = NULL; + port->input.stream = 0; + port->input.cred = 0; + port->input.peer = (struct sockaddr *)&port->ret; + port->input.peerlen = sizeof(port->ret); + + trans_insert_port(my_trans, &port->tport); + + if (community != COMM_INITIALIZE && + (err = udpv6_init_port(&port->tport)) != SNMP_ERR_NOERROR) { + udpv6_close_port(&port->tport); + return (err); + } + *pp = port; + return (SNMP_ERR_NOERROR); +} + +/* + * Close an SNMP port + */ +static void +udpv6_close_port(struct tport *tp) +{ + struct udpv6_port *port = (struct udpv6_port *)tp; + + snmpd_input_close(&port->input); + trans_remove_port(tp); + free(port); +} + +/* + * Send something + */ +static ssize_t +udpv6_send(struct tport *tp, const u_char *buf, size_t len, + const struct sockaddr *addr, size_t addrlen) +{ + struct udpv6_port *p = (struct udpv6_port *)tp; + + return (sendto(p->input.fd, buf, len, 0, addr, addrlen)); +} + +/* + * Port table + */ +int +op_snmp_port(struct snmp_context *ctx, struct snmp_value *value, + u_int sub, u_int iidx, enum snmp_op op) +{ + asn_subid_t which = value->var.subs[sub-1]; + struct udpv6_port *p; + u_int8_t addr[16]; + u_int32_t port; + + switch (op) { + + case SNMP_OP_GETNEXT: + if ((p = (struct udpv6_port *)trans_next_port(my_trans, + &value->var, sub)) == NULL) + return (SNMP_ERR_NOSUCHNAME); + index_append(&value->var, sub, &p->tport.index); + break; + + case SNMP_OP_GET: + if ((p = (struct udpv6_port *)trans_find_port(my_trans, + &value->var, sub)) == NULL) + return (SNMP_ERR_NOSUCHNAME); + break; + + case SNMP_OP_SET: + p = (struct udpv6_port *)trans_find_port(my_trans, + &value->var, sub); + ctx->scratch->int1 = (p != NULL); + + if (which != LEAF_begemotSnmpdPortStatus) + abort(); + if (!TRUTH_OK(value->v.integer)) + return (SNMP_ERR_WRONG_VALUE); + + ctx->scratch->int2 = TRUTH_GET(value->v.integer); + + if (ctx->scratch->int2) { + /* open an SNMP port */ + if (p != NULL) + /* already open - do nothing */ + return (SNMP_ERR_NOERROR); + + if (index_decode(&value->var, sub, iidx, addr, &port)) + return (SNMP_ERR_NO_CREATION); + return (udpv6_open_port(addr, port, &p)); + + } else { + /* close SNMP port - do in commit */ + } + return (SNMP_ERR_NOERROR); + + case SNMP_OP_ROLLBACK: + p = (struct udpv6_port *)trans_find_port(my_trans, + &value->var, sub); + if (ctx->scratch->int1 == 0) { + /* did not exist */ + if (ctx->scratch->int2 == 1) { + /* created */ + if (p != NULL) + udpv6_close_port(&p->tport); + } + } + return (SNMP_ERR_NOERROR); + + case SNMP_OP_COMMIT: + p = (struct udpv6_port *)trans_find_port(my_trans, + &value->var, sub); + if (ctx->scratch->int1 == 1) { + /* did exist */ + if (ctx->scratch->int2 == 0) { + /* delete */ + if (p != NULL) + udpv6_close_port(&p->tport); + } + } + return (SNMP_ERR_NOERROR); + + default: + abort(); + } + + /* + * Come here to fetch the value + */ + switch (which) { + + case LEAF_begemotSnmpdPortStatus: + value->v.integer = 1; + break; + + default: + abort(); + } + + return (SNMP_ERR_NOERROR); +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201406161758.s5GHw4PS015765>