Made the Ethernet address parse type standard.

OK'ed by:	archie
This commit is contained in:
Ruslan Ermilov 2003-12-17 12:40:34 +00:00
parent 7304a833fb
commit 8c7e4101f8
5 changed files with 69 additions and 67 deletions

View File

@ -77,7 +77,6 @@
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_bridge.h>
#include <netgraph/ng_ether.h>
#ifdef NG_SEPARATE_MALLOC
MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node ");
@ -172,7 +171,7 @@ ng_bridge_getTableLength(const struct ng_parse_type *type,
/* Parse type for struct ng_bridge_host_ary */
static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
= NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
static const struct ng_parse_type ng_bridge_host_type = {
&ng_parse_struct_type,
&ng_bridge_host_type_fields
@ -287,9 +286,6 @@ static struct ng_type ng_bridge_typestruct = {
};
NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
/* Depend on ng_ether so we can use the Ethernet parse type */
MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
/******************************************************************
NETGRAPH NODE METHODS
******************************************************************/

View File

@ -109,19 +109,6 @@ static ng_rcvdata_t ng_ether_rcvdata;
static ng_disconnect_t ng_ether_disconnect;
static int ng_ether_mod_event(module_t mod, int event, void *data);
/* Parse type for an Ethernet address */
static ng_parse_t ng_enaddr_parse;
static ng_unparse_t ng_enaddr_unparse;
const struct ng_parse_type ng_ether_enaddr_type = {
NULL,
NULL,
NULL,
ng_enaddr_parse,
ng_enaddr_unparse,
NULL, /* no such thing as a "default" EN address */
0
};
/* List of commands and how to convert arguments to/from ASCII */
static const struct ng_cmdlist ng_ether_cmdlist[] = {
{
@ -143,13 +130,13 @@ static const struct ng_cmdlist ng_ether_cmdlist[] = {
NGM_ETHER_GET_ENADDR,
"getenaddr",
NULL,
&ng_ether_enaddr_type
&ng_parse_enaddr_type
},
{
NGM_ETHER_COOKIE,
NGM_ETHER_SET_ENADDR,
"setenaddr",
&ng_ether_enaddr_type,
&ng_parse_enaddr_type,
NULL
},
{
@ -653,48 +640,6 @@ ng_ether_disconnect(hook_p hook)
return (0);
}
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);
}
/******************************************************************
INITIALIZATION
******************************************************************/

View File

@ -65,10 +65,5 @@ enum {
NGM_ETHER_SET_AUTOSRC, /* enable/disable src addr override */
};
#ifdef _KERNEL
/* Ethernet address parse type */
extern const struct ng_parse_type ng_ether_enaddr_type;
#endif /* _KERNEL */
#endif /* _NETGRAPH_NG_ETHER_H_ */

View File

@ -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>
@ -998,6 +1000,62 @@ const struct ng_parse_type ng_parse_ipaddr_type = {
ng_int32_getAlign
};
/************************************************************************
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
************************************************************************/

View File

@ -441,6 +441,14 @@ extern const struct ng_parse_type ng_parse_hint64_type;
*/
extern const struct ng_parse_type ng_parse_ipaddr_type;
/*
* ETHERNET ADDRESS TYPE
*
* Default value: None
* Additional info: None required
*/
extern const struct ng_parse_type ng_parse_enaddr_type;
/*
* VARIABLE LENGTH BYTE ARRAY TYPE
*