socsvn commit: r269873 - soc2014/shonali/head/contrib/bsnmp/snmpd
shonali at FreeBSD.org
shonali at FreeBSD.org
Sun Jun 22 17:16:14 UTC 2014
Author: shonali
Date: Sun Jun 22 17:16:12 2014
New Revision: 269873
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=269873
Log:
Added ipv6 support to config.c and export.c. Made corrections in trans_udpv6.c
Modified:
soc2014/shonali/head/contrib/bsnmp/snmpd/config.c
soc2014/shonali/head/contrib/bsnmp/snmpd/export.c
soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c
Modified: soc2014/shonali/head/contrib/bsnmp/snmpd/config.c
==============================================================================
--- soc2014/shonali/head/contrib/bsnmp/snmpd/config.c Sun Jun 22 16:58:29 2014 (r269872)
+++ soc2014/shonali/head/contrib/bsnmp/snmpd/config.c Sun Jun 22 17:16:12 2014 (r269873)
@@ -152,6 +152,7 @@
static char strval[_POSIX2_LINE_MAX];
static size_t strvallen;
static int token;
+static int addr_type;
/* error return */
static jmp_buf errjmp[4];
@@ -779,10 +780,10 @@
{
struct addrinfo hints, *res;
int error;
- struct sockaddr_in *sain;
+ struct sockaddr_storage *sain;
memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
@@ -792,13 +793,48 @@
if (res == NULL)
report("%s: unknown hostname", host);
- sain = (struct sockaddr_in *)(void *)res->ai_addr;
- sain->sin_addr.s_addr = ntohl(sain->sin_addr.s_addr);
- ip[0] = sain->sin_addr.s_addr >> 24;
- ip[1] = sain->sin_addr.s_addr >> 16;
- ip[2] = sain->sin_addr.s_addr >> 8;
- ip[3] = sain->sin_addr.s_addr >> 0;
-
+ if(res->ai_family == AF_INET) {
+ addr_type = AF_INET;
+ sain = (struct sockaddr_in *)(void *)res->ai_addr;
+ sain->sin_addr.s_addr = ntohl(sain->sin_addr.s_addr);
+
+ ip[0] = sain->sin_addr.s_addr >> 24;
+ ip[1] = sain->sin_addr.s_addr >> 16;
+ ip[2] = sain->sin_addr.s_addr >> 8;
+ ip[3] = sain->sin_addr.s_addr >> 0;
+ }
+ else if(res->ai_family == AF_INET6) {
+ addr_type = AF_INET6;
+ sain = (struct sockaddr_in6 *)(void *)res->ai_addr;
+ u_int32_t tmp[4];
+ tmp[0] = htonl((sain->sin6_addr.s6_addr[0] << 24) | (sain->sin6_addr.s6_addr[1] << 16) | (sain->sin6_addr.s6_addr[2] << 8) | sain->sin6_addr.s6_addr[3]);
+ tmp[1] = htonl((sain->sin6_addr.s6_addr[4] << 24) | (sain->sin6_addr.s6_addr[5] << 16) | (sain->sin6_addr.s6_addr[6] << 8) | sain->sin6_addr.s6_addr[7]);
+ tmp[2] = htonl((sain->sin6_addr.s6_addr[8] << 24) | (sain->sin6_addr.s6_addr[9] << 16) | (sain->sin6_addr.s6_addr[10] << 8) | sain->sin6_addr.s6_addr[11]);
+ tmp[3] = htonl((sain->sin6_addr.s6_addr[12] << 24) | (sain->sin6_addr.s6_addr[13] << 16) | (sain->sin6_addr.s6_addr[14] << 8) | sain->sin6_addr.s6_addr[15]);
+
+ sain->sin6_addr.s6_addr = *tmp;
+ inet_ntop(AF_INET6, &(sain.sin6_addr), *ip, INET6_ADDRSTRLEN)
+
+ /* incase inet_ntop doesn't work as expected
+ ip[0] = sain->sin6_addr.s6_addr >> 120;
+ ip[1] = sain->sin6_addr.s6_addr >> 112;
+ ip[2] = sain->sin6_addr.s6_addr >> 104;
+ ip[3] = sain->sin6_addr.s6_addr >> 96;
+ ip[4] = sain->sin6_addr.s6_addr >> 88;
+ ip[5] = sain->sin6_addr.s6_addr >> 80;
+ ip[6] = sain->sin6_addr.s6_addr >> 72;
+ ip[7] = sain->sin6_addr.s6_addr >> 64;
+ ip[8] = sain->sin6_addr.s6_addr >> 56;
+ ip[9] = sain->sin6_addr.s6_addr >> 48;
+ ip[10] = sain->sin6_addr.s6_addr >> 40;
+ ip[11] = sain->sin6_addr.s6_addr >> 32;
+ ip[12] = sain->sin6_addr.s6_addr >> 24;
+ ip[13] = sain->sin6_addr.s6_addr >> 16;
+ ip[14] = sain->sin6_addr.s6_addr >> 8;
+ ip[15] = sain->sin6_addr.s6_addr >> 0;
+ */
+ }
+
freeaddrinfo(res);
}
@@ -850,11 +886,20 @@
} else if (token == TOK_HOST) {
gethost(strval, ip);
- if (oid->len + 4 > ASN_MAXOIDLEN)
+ if (oid->len + 16 > ASN_MAXOIDLEN)
report("index too long");
- for (i = 0; i < 4; i++)
- oid->subs[oid->len++] = ip[i];
- gettoken();
+ if (addr_type == AF_INET){
+ for (i = 0; i < 4; i++)
+ oid->subs[oid->len++] = ip[i];
+ gettoken();
+ }
+
+ if (addr_type == AF_INET6){
+ for (i = 0; i < 16; i++)
+ oid->subs[oid->len++] = ip[i];
+ gettoken();
+ }
+
} else
report("bad token in index");
}
@@ -970,27 +1015,55 @@
{
int i;
u_char ip[4];
-
+
if (token == TOK_NUM) {
/* numerical address */
i = 0;
for (;;) {
+ if (!isxdigit(numval)) {
if (numval >= 256)
report("ip address part too large");
value->v.ipaddress[i++] = numval;
- if (i == 4)
+ if (i == 4)
break;
if (gettoken() != '.')
report("expecting '.' in ip address");
+ }
+ else {
+ value->v.ipaddress[i++] = numval;
+ if (i == 16)
+ break;
+ if (gettoken() != ':')
+ report("expecting ':' in ip address");
+ }
+
}
gettoken();
+ if (i == 4) {
+ struct in_addr dst;
+ if (!inet_pton(AF_INET,value->v.ipaddress[i++],(void *)&dst))
+ report("ip address not valid");
+ }
+ else if (i == 16) {
+ struct in6_addr dst;
+ if (!inet_pton(AF_INET,value->v.ipaddress[i++],(void *)&dst))
+ report("ipv6 address not valid");
+
+ }
} else if (token == TOK_HOST) {
/* host name */
gethost(strval, ip);
- for (i = 0; i < 4; i++)
- value->v.ipaddress[i] = ip[i];
- gettoken();
+ if (addr_type == AF_INET){
+ for (i = 0; i < 4; i++)
+ value->v.ipaddress[i] = ip[i];
+ gettoken();
+ }
+ if (addr_type == AF_INET6){
+ for (i = 0; i < 16; i++)
+ value->v.ipaddress[i] = ip[i];
+ gettoken();
+ }
} else
report("bad ip address syntax");
Modified: soc2014/shonali/head/contrib/bsnmp/snmpd/export.c
==============================================================================
--- soc2014/shonali/head/contrib/bsnmp/snmpd/export.c Sun Jun 22 16:58:29 2014 (r269872)
+++ soc2014/shonali/head/contrib/bsnmp/snmpd/export.c Sun Jun 22 17:16:12 2014 (r269873)
@@ -152,7 +152,8 @@
*/
int
ip_save(struct snmp_value *value, struct snmp_context *ctx, u_char *valp)
-{
+{
+ if (sizeof(*valp)== 4) {
ctx->scratch->int1 = (valp[0] << 24) | (valp[1] << 16) | (valp[2] << 8)
| valp[3];
@@ -160,6 +161,30 @@
valp[1] = value->v.ipaddress[1];
valp[2] = value->v.ipaddress[2];
valp[3] = value->v.ipaddress[3];
+ }
+
+ if (sizeof(*valp)== 16) {
+ ctx->scratch->int1 = (valp[0] << 120) | (valp[1] << 112) | (valp[2] << 104) | (valp[3] << 96) | (valp[4] << 88) | (valp[5] << 80) | (valp[6] << 72) | (valp[7] << 64) | (valp[8] << 56) |
+ (valp[9] << 48) | (valp[10] << 40) | (valp[11] << 32) | (valp[12] << 24) | (valp[13] << 16) | (valp[14] << 8) | valp[15];
+
+ valp[0] = value->v.ipaddress[0];
+ valp[1] = value->v.ipaddress[1];
+ valp[2] = value->v.ipaddress[2];
+ valp[3] = value->v.ipaddress[3];
+ valp[4] = value->v.ipaddress[4];
+ valp[5] = value->v.ipaddress[5];
+ valp[6] = value->v.ipaddress[6];
+ valp[7] = value->v.ipaddress[7];
+ valp[8] = value->v.ipaddress[8];
+ valp[9] = value->v.ipaddress[9];
+ valp[10] = value->v.ipaddress[10];
+ valp[11] = value->v.ipaddress[11];
+ valp[12] = value->v.ipaddress[12];
+ valp[13] = value->v.ipaddress[13];
+ valp[14] = value->v.ipaddress[14];
+ valp[15] = value->v.ipaddress[15];
+
+ }
return (0);
}
@@ -169,11 +194,30 @@
*/
void
ip_rollback(struct snmp_context *ctx, u_char *valp)
-{
- valp[0] = ctx->scratch->int1 >> 24;
- valp[1] = ctx->scratch->int1 >> 16;
- valp[2] = ctx->scratch->int1 >> 8;
- valp[3] = ctx->scratch->int1;
+{ if (sizeof(*valp)== 4) {
+ valp[0] = ctx->scratch->int1 >> 24;
+ valp[1] = ctx->scratch->int1 >> 16;
+ valp[2] = ctx->scratch->int1 >> 8;
+ valp[3] = ctx->scratch->int1;
+ }
+ if (sizeof(*valp)== 16) {
+ valp[0] = ctx->scratch->int1 >> 120;
+ valp[1] = ctx->scratch->int1 >> 112;
+ valp[2] = ctx->scratch->int1 >> 104;
+ valp[3] = ctx->scratch->int1 >> 96;
+ valp[4] = ctx->scratch->int1 >> 88;
+ valp[5] = ctx->scratch->int1 >> 80;
+ valp[6] = ctx->scratch->int1 >> 72;
+ valp[7] = ctx->scratch->int1 >> 64;
+ valp[8] = ctx->scratch->int1 >> 56;
+ valp[9] = ctx->scratch->int1 >> 48;
+ valp[10] = ctx->scratch->int1 >> 40;
+ valp[11] = ctx->scratch->int1 >> 32;
+ valp[12] = ctx->scratch->int1 >> 24;
+ valp[13] = ctx->scratch->int1 >> 16;
+ valp[14] = ctx->scratch->int1 >> 8;
+ valp[15] = ctx->scratch->int1 >> 0;
+ }
}
/*
@@ -190,10 +234,31 @@
int
ip_get(struct snmp_value *value, u_char *valp)
{
+ if (sizeof(*valp)== 4) {
value->v.ipaddress[0] = valp[0];
value->v.ipaddress[1] = valp[1];
value->v.ipaddress[2] = valp[2];
value->v.ipaddress[3] = valp[3];
+ }
+ if (sizeof(*valp)== 16) {
+ value->v.ipaddress[0] = valp[0];
+ value->v.ipaddress[1] = valp[1];
+ value->v.ipaddress[2] = valp[2];
+ value->v.ipaddress[3] = valp[3];
+ value->v.ipaddress[4] = valp[4];
+ value->v.ipaddress[5] = valp[5];
+ value->v.ipaddress[6] = valp[6];
+ value->v.ipaddress[7] = valp[7];
+ value->v.ipaddress[8] = valp[8];
+ value->v.ipaddress[9] = valp[9];
+ value->v.ipaddress[10] = valp[10];
+ value->v.ipaddress[11] = valp[11];
+ value->v.ipaddress[12] = valp[12];
+ value->v.ipaddress[13] = valp[13];
+ value->v.ipaddress[14] = valp[14];
+ value->v.ipaddress[15] = valp[15];
+
+ }
return (SNMP_ERR_NOERROR);
}
@@ -316,14 +381,21 @@
u_int8_t *pval;
u_int i;
- if (sub + 4 > oid->len)
+ if (sub + 16 > oid->len)
goto err;
pval = va_arg(ap, u_int8_t *);
+ if (sizeof(*pval)== 4) {
for (i = 0; i < 4; i++) {
if (oid->subs[sub] > 0xff)
goto err;
pval[i] = oid->subs[sub++];
}
+ }
+ if (sizeof(*pval)== 16) {
+ for (i = 0; i < 16; i++) {
+ pval[i] = oid->subs[sub++];
+ }
+ }
break;
}
Modified: soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c
==============================================================================
--- soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c Sun Jun 22 16:58:29 2014 (r269872)
+++ soc2014/shonali/head/contrib/bsnmp/snmpd/trans_udpv6.c Sun Jun 22 17:16:12 2014 (r269873)
@@ -102,7 +102,6 @@
{
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];
@@ -112,24 +111,18 @@
return (SNMP_ERR_RES_UNAVAIL);
}
- 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]);
+ 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_addr.s6_addr = *ip;
addr.sin6_port = htons(p->port);
addr.sin6_family = AF_INET6;
addr.sin_len = sizeof(addr);
- if (addr.sin6_addr.s_addr == IN6ADDR_ANY &&
+ if (addr.sin6_addr.s6_addr == IN6ADDR_ANY &&
setsockopt(p->input.fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof(on)) == -1) {
syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
@@ -143,7 +136,7 @@
p->input.fd = -1;
return (SNMP_ERR_INCONS_NAME);
}
- inet_ntop(AF_INET6, &(addr.sin_addr), str, INET6_ADDRSTRLEN)
+ inet_ntop(AF_INET6, &(addr.sin6_addr), str, INET6_ADDRSTRLEN)
syslog(LOG_ERR, "bind: %s:%u %m", str,
p->port);
close(p->input.fd);
More information about the svn-soc-all
mailing list