summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraschrijver <aschrijver@cvs.openbsd.org>2009-01-03 18:41:42 +0000
committeraschrijver <aschrijver@cvs.openbsd.org>2009-01-03 18:41:42 +0000
commit104499dc802fcab431a01cb9f7b2a4422cc9db0c (patch)
tree207cfdc2f6c1262f3b7b4c1fb9cbfba941aeb525
parent3bbf2365f7fd283a7af940177e2268c33b0a4e38 (diff)
Sync with ypldap. Add ber_{add,get}_enumerated.
OK reyk@
-rw-r--r--usr.sbin/snmpd/ber.c55
-rw-r--r--usr.sbin/snmpd/ber.h4
2 files changed, 57 insertions, 2 deletions
diff --git a/usr.sbin/snmpd/ber.c b/usr.sbin/snmpd/ber.c
index e3944690b7f..fba0b25c3a9 100644
--- a/usr.sbin/snmpd/ber.c
+++ b/usr.sbin/snmpd/ber.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ber.c,v 1.15 2008/06/29 16:00:22 ragge Exp $ */
+/* $OpenBSD: ber.c,v 1.16 2009/01/03 18:41:41 aschrijver Exp $ */
/*
* Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net>
@@ -153,6 +153,35 @@ ber_add_set(struct ber_element *prev)
}
struct ber_element *
+ber_add_enumerated(struct ber_element *prev, long long val)
+{
+ struct ber_element *elm;
+ u_int i, len = 0;
+ u_char cur, last = 0;
+
+ if ((elm = ber_get_element(BER_TYPE_ENUMERATED)) == NULL)
+ return NULL;
+
+ elm->be_numeric = val;
+
+ for (i = 0; i < sizeof(long long); i++) {
+ cur = val & 0xff;
+ if (cur != 0 && cur != 0xff)
+ len = i;
+ if ((cur == 0 && last & 0x80) ||
+ (cur == 0xff && (last & 0x80) == 0))
+ len = i;
+ val >>= 8;
+ last = cur;
+ }
+ elm->be_len = len + 1;
+
+ ber_link_elements(prev, elm);
+
+ return elm;
+}
+
+struct ber_element *
ber_add_integer(struct ber_element *prev, long long val)
{
struct ber_element *elm;
@@ -191,6 +220,17 @@ ber_get_integer(struct ber_element *elm, long long *n)
return 0;
}
+int
+ber_get_enumerated(struct ber_element *elm, long long *n)
+{
+ if (elm->be_encoding != BER_TYPE_ENUMERATED)
+ return -1;
+
+ *n = elm->be_numeric;
+ return 0;
+}
+
+
struct ber_element *
ber_add_boolean(struct ber_element *prev, int bool)
{
@@ -514,6 +554,10 @@ ber_printf_elements(struct ber_element *ber, char *fmt, ...)
e = va_arg(ap, struct ber_element *);
ber_link_elements(ber, e);
break;
+ case 'E':
+ i = va_arg(ap, long long);
+ ber = ber_add_enumerated(ber, i);
+ break;
case 'i':
i = va_arg(ap, long long);
ber = ber_add_integer(ber, i);
@@ -602,6 +646,12 @@ ber_scanf_elements(struct ber_element *ber, char *fmt, ...)
*e = ber;
ret++;
continue;
+ case 'E':
+ i = va_arg(ap, long long *);
+ if (ber_get_enumerated(ber, i) == -1)
+ goto fail;
+ ret++;
+ break;
case 'i':
i = va_arg(ap, long long *);
if (ber_get_integer(ber, i) == -1)
@@ -614,6 +664,9 @@ ber_scanf_elements(struct ber_element *ber, char *fmt, ...)
goto fail;
ret++;
break;
+ case 'S':
+ ret++;
+ break;
case 's':
s = va_arg(ap, char **);
if (ber_get_string(ber, s) == -1)
diff --git a/usr.sbin/snmpd/ber.h b/usr.sbin/snmpd/ber.h
index c0358c0dd71..907a6c5971a 100644
--- a/usr.sbin/snmpd/ber.h
+++ b/usr.sbin/snmpd/ber.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ber.h,v 1.6 2008/03/18 16:57:58 reyk Exp $ */
+/* $OpenBSD: ber.h,v 1.7 2009/01/03 18:41:41 aschrijver Exp $ */
/*
* Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net>
@@ -90,6 +90,8 @@ struct ber_element *ber_add_sequence(struct ber_element *);
struct ber_element *ber_add_set(struct ber_element *);
struct ber_element *ber_add_integer(struct ber_element *, long long);
int ber_get_integer(struct ber_element *, long long *);
+struct ber_element *ber_add_enumerated(struct ber_element *, long long);
+int ber_get_enumerated(struct ber_element *, long long *);
struct ber_element *ber_add_boolean(struct ber_element *, int);
int ber_get_boolean(struct ber_element *, int *);
struct ber_element *ber_add_string(struct ber_element *, const char *);