diff options
author | ru <ru@FreeBSD.org> | 2003-12-17 12:40:34 +0000 |
---|---|---|
committer | ru <ru@FreeBSD.org> | 2003-12-17 12:40:34 +0000 |
commit | 1df45259591c8233ac65b0031774b50f3b791df4 (patch) | |
tree | cb79deb87f430caf11f013eff4540c587f790642 /sys/netgraph/ng_parse.c | |
parent | 6c49c65d276d185a38c2dffb97067fbb1faf4b60 (diff) | |
download | FreeBSD-src-1df45259591c8233ac65b0031774b50f3b791df4.zip FreeBSD-src-1df45259591c8233ac65b0031774b50f3b791df4.tar.gz |
Made the Ethernet address parse type standard.
OK'ed by: archie
Diffstat (limited to 'sys/netgraph/ng_parse.c')
-rw-r--r-- | sys/netgraph/ng_parse.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/sys/netgraph/ng_parse.c b/sys/netgraph/ng_parse.c index cff5fcb..58389e8 100644 --- a/sys/netgraph/ng_parse.c +++ b/sys/netgraph/ng_parse.c @@ -48,6 +48,8 @@ #include <sys/malloc.h> #include <sys/ctype.h> +#include <net/ethernet.h> + #include <netinet/in.h> #include <netgraph/ng_message.h> @@ -999,6 +1001,62 @@ const struct ng_parse_type ng_parse_ipaddr_type = { }; /************************************************************************ + ETHERNET ADDRESS TYPE + ************************************************************************/ + +static int +ng_enaddr_parse(const struct ng_parse_type *type, + const char *s, int *const off, const u_char *const start, + u_char *const buf, int *const buflen) +{ + char *eptr; + u_long val; + int i; + + if (*buflen < ETHER_ADDR_LEN) + return (ERANGE); + for (i = 0; i < ETHER_ADDR_LEN; i++) { + val = strtoul(s + *off, &eptr, 16); + if (val > 0xff || eptr == s + *off) + return (EINVAL); + buf[i] = (u_char)val; + *off = (eptr - s); + if (i < ETHER_ADDR_LEN - 1) { + if (*eptr != ':') + return (EINVAL); + (*off)++; + } + } + *buflen = ETHER_ADDR_LEN; + return (0); +} + +static int +ng_enaddr_unparse(const struct ng_parse_type *type, + const u_char *data, int *off, char *cbuf, int cbuflen) +{ + int len; + + len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x", + data[*off], data[*off + 1], data[*off + 2], + data[*off + 3], data[*off + 4], data[*off + 5]); + if (len >= cbuflen) + return (ERANGE); + *off += ETHER_ADDR_LEN; + return (0); +} + +const struct ng_parse_type ng_parse_enaddr_type = { + NULL, + NULL, + NULL, + ng_enaddr_parse, + ng_enaddr_unparse, + NULL, + 0 +}; + +/************************************************************************ BYTE ARRAY TYPE ************************************************************************/ |