diff options
author | Thomas Graf <tgr@lsx.localdomain> | 2008-05-15 18:29:08 (GMT) |
---|---|---|
committer | Thomas Graf <tgr@lsx.localdomain> | 2008-05-15 18:29:08 (GMT) |
commit | e1cc1b859e81e7b0645d8183b555a470909655e1 (patch) | |
tree | 518cd03ce63d408743686bd050e4204241cacbd0 /src | |
parent | 5e732ad075e54ceb9f9e94fadbcec2e83d971b52 (diff) | |
download | libnl-e1cc1b859e81e7b0645d8183b555a470909655e1.zip libnl-e1cc1b859e81e7b0645d8183b555a470909655e1.tar.gz libnl-e1cc1b859e81e7b0645d8183b555a470909655e1.tar.bz2 |
utility functions for address tools
Diffstat (limited to 'src')
-rw-r--r-- | src/addr-utils.c | 87 | ||||
-rw-r--r-- | src/addr-utils.h | 25 |
2 files changed, 112 insertions, 0 deletions
diff --git a/src/addr-utils.c b/src/addr-utils.c new file mode 100644 index 0000000..3c8ee4d --- /dev/null +++ b/src/addr-utils.c @@ -0,0 +1,87 @@ +/* + * src/addr-utils.c Address Helpers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch> + */ + +#include "addr-utils.h" + +void parse_family(struct rtnl_addr *addr, char *arg) +{ + int family; + + if ((family = nl_str2af(arg)) != AF_UNSPEC) + rtnl_addr_set_family(addr, family); +} + +void parse_local(struct rtnl_addr *addr, char *arg) +{ + struct nl_addr *a; + int err; + + a = nlt_addr_parse(arg, rtnl_addr_get_family(addr)); + if ((err = rtnl_addr_set_local(addr, a)) < 0) + fatal(err, "Unable to set local address: %s", + nl_geterror(err)); + + nl_addr_put(a); +} + +void parse_dev(struct rtnl_addr *addr, struct nl_cache *link_cache, char *arg) +{ + int ival; + + if (!(ival = rtnl_link_name2i(link_cache, arg))) + fatal(ENOENT, "Link \"%s\" does not exist", arg); + + rtnl_addr_set_ifindex(addr, ival); +} + +void parse_label(struct rtnl_addr *addr, char *arg) +{ + int err; + + if ((err = rtnl_addr_set_label(addr, arg)) < 0) + fatal(err, "Unable to set address label: %s", nl_geterror(err)); +} + +void parse_peer(struct rtnl_addr *addr, char *arg) +{ + struct nl_addr *a; + int err; + + a = nlt_addr_parse(arg, rtnl_addr_get_family(addr)); + if ((err = rtnl_addr_set_peer(addr, a)) < 0) + fatal(err, "Unable to set peer address: %s", nl_geterror(err)); + + nl_addr_put(a); +} + +void parse_scope(struct rtnl_addr *addr, char *arg) +{ + int ival; + + if ((ival = rtnl_str2scope(arg)) < 0) + fatal(EINVAL, "Unknown address scope \"%s\"", arg); + + rtnl_addr_set_scope(addr, ival); +} + +void parse_broadcast(struct rtnl_addr *addr, char *arg) +{ + struct nl_addr *a; + int err; + + a = nlt_addr_parse(arg, rtnl_addr_get_family(addr)); + if ((err = rtnl_addr_set_broadcast(addr, a)) < 0) + fatal(err, "Unable to set broadcast address: %s", + nl_geterror(err)); + + nl_addr_put(a); +} + diff --git a/src/addr-utils.h b/src/addr-utils.h new file mode 100644 index 0000000..0a4db23 --- /dev/null +++ b/src/addr-utils.h @@ -0,0 +1,25 @@ +/* + * src/addr-utils.h Address Helpers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch> + */ + +#ifndef __ADDR_UTILS_H_ +#define __ADDR_UTILS_H_ + +#include "utils.h" + +extern void parse_family(struct rtnl_addr *, char *); +extern void parse_local(struct rtnl_addr *, char *); +extern void parse_dev(struct rtnl_addr *, struct nl_cache *, char *); +extern void parse_label(struct rtnl_addr *, char *); +extern void parse_peer(struct rtnl_addr *, char *); +extern void parse_scope(struct rtnl_addr *, char *); +extern void parse_broadcast(struct rtnl_addr *, char *); + +#endif |