Re: [PATCH iproute2 v2 2/2] tipc: refactor bearer identification

2016-08-17 Thread Stephen Hemminger
On Mon, 15 Aug 2016 10:24:32 +0200
Richard Alpe  wrote:

> Introduce a generic function (nl_add_bearer_name()) that identifies a
> bearer and adds it to an existing netlink message. This reduces code
> complexity and makes the code a little bit easier to maintain.
> 
> Signed-off-by: Richard Alpe 

Thanks for keeping TIPC support up to date.
Applied.


[PATCH iproute2 v2 2/2] tipc: refactor bearer identification

2016-08-15 Thread Richard Alpe
Introduce a generic function (nl_add_bearer_name()) that identifies a
bearer and adds it to an existing netlink message. This reduces code
complexity and makes the code a little bit easier to maintain.

Signed-off-by: Richard Alpe 
---
 tipc/bearer.c | 313 --
 tipc/cmdl.h   |   6 ++
 2 files changed, 114 insertions(+), 205 deletions(-)

diff --git a/tipc/bearer.c b/tipc/bearer.c
index 30b54d9..05dabe6 100644
--- a/tipc/bearer.c
+++ b/tipc/bearer.c
@@ -29,7 +29,7 @@
 static void _print_bearer_opts(void)
 {
fprintf(stderr,
-   "\nOPTIONS\n"
+   "OPTIONS\n"
" priority  - Bearer link priority\n"
" tolerance - Bearer link tolerance\n"
" window- Bearer link window\n");
@@ -44,43 +44,27 @@ static void _print_bearer_media(void)
" eth   - Ethernet\n");
 }
 
-static void cmd_bearer_enable_l2_help(struct cmdl *cmdl)
+static void cmd_bearer_enable_l2_help(struct cmdl *cmdl, char *media)
 {
fprintf(stderr,
-   "Usage: %s bearer enable media MEDIA device DEVICE [OPTIONS]\n"
+   "Usage: %s bearer enable media %s device DEVICE [OPTIONS]\n"
"\nOPTIONS\n"
" domain DOMAIN - Discovery domain\n"
" priority PRIORITY - Bearer priority\n",
-   cmdl->argv[0]);
+   cmdl->argv[0], media);
 }
 
-static void cmd_bearer_enable_udp_help(struct cmdl *cmdl)
+static void cmd_bearer_enable_udp_help(struct cmdl *cmdl, char *media)
 {
fprintf(stderr,
-   "Usage: %s bearer enable media udp name NAME localip IP 
[OPTIONS]\n"
+   "Usage: %s bearer enable media %s name NAME localip IP 
[OPTIONS]\n"
"\nOPTIONS\n"
" domain DOMAIN - Discovery domain\n"
" priority PRIORITY - Bearer priority\n"
" localport PORT- Local UDP port (default 6118)\n"
" remoteip IP   - Remote IP address\n"
" remoteport IP - Remote UDP port (default 6118)\n",
-   cmdl->argv[0]);
-}
-
-static int enable_l2_bearer(struct nlmsghdr *nlh, struct opt *opts,
-   struct cmdl *cmdl)
-{
-   struct opt *opt;
-   char id[TIPC_MAX_BEARER_NAME];
-
-   if (!(opt = get_opt(opts, "device"))) {
-   fprintf(stderr, "error: missing bearer device\n");
-   return -EINVAL;
-   }
-   snprintf(id, sizeof(id), "eth:%s", opt->val);
-   mnl_attr_put_strz(nlh, TIPC_NLA_BEARER_NAME, id);
-
-   return 0;
+   cmdl->argv[0], media);
 }
 
 static int get_netid_cb(const struct nlmsghdr *nlh, void *data)
@@ -123,8 +107,8 @@ static int generate_multicast(short af, char *buf, int 
bufsize)
return 0;
 }
 
-static int enable_udp_bearer(struct nlmsghdr *nlh, struct opt *opts,
-struct cmdl *cmdl)
+static int nl_add_udp_enable_opts(struct nlmsghdr *nlh, struct opt *opts,
+ struct cmdl *cmdl)
 {
int err;
struct opt *opt;
@@ -134,7 +118,6 @@ static int enable_udp_bearer(struct nlmsghdr *nlh, struct 
opt *opts,
char *remport = "6118";
char *locip = NULL;
char *remip = NULL;
-   char name[TIPC_MAX_BEARER_NAME];
struct addrinfo *loc = NULL;
struct addrinfo *rem = NULL;
struct addrinfo hints = {
@@ -142,22 +125,9 @@ static int enable_udp_bearer(struct nlmsghdr *nlh, struct 
opt *opts,
.ai_socktype = SOCK_DGRAM
};
 
-   if (help_flag) {
-   cmd_bearer_enable_udp_help(cmdl);
-   /* TODO find a better error code? */
-   return -EINVAL;
-   }
-
-   if (!(opt = get_opt(opts, "name"))) {
-   fprintf(stderr, "error, udp bearer name missing\n");
-   cmd_bearer_enable_udp_help(cmdl);
-   return -EINVAL;
-   }
-   snprintf(name, sizeof(name), "udp:%s", opt->val);
-
if (!(opt = get_opt(opts, "localip"))) {
fprintf(stderr, "error, udp bearer localip missing\n");
-   cmd_bearer_enable_udp_help(cmdl);
+   cmd_bearer_enable_udp_help(cmdl, "udp");
return -EINVAL;
}
locip = opt->val;
@@ -197,8 +167,6 @@ static int enable_udp_bearer(struct nlmsghdr *nlh, struct 
opt *opts,
return -EINVAL;
}
 
-   mnl_attr_put_strz(nlh, TIPC_NLA_BEARER_NAME, name);
-
nest = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER_UDP_OPTS);
mnl_attr_put(nlh, TIPC_NLA_UDP_LOCAL, loc->ai_addrlen, loc->ai_addr);
mnl_attr_put(nlh, TIPC_NLA_UDP_REMOTE, rem->ai_addrlen, rem->ai_addr);
@@ -210,6 +178,50 @@ static int enable_udp_bearer(struct nlmsghdr *nlh, struct 
opt *opts,
return 0;
 }