libnl  3.2.3
/build/buildd/libnl3-3.2.3/src/lib/cls.c
00001 /*
00002  * src/lib/cls.c        CLI Classifier Helpers
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup cli
00014  * @defgroup cli_cls Classifiers
00015  * @{
00016  */
00017 
00018 #include <netlink/cli/utils.h>
00019 #include <netlink/cli/cls.h>
00020 #include <netlink/route/cls/ematch.h>
00021 
00022 struct rtnl_cls *nl_cli_cls_alloc(void)
00023 {
00024         struct rtnl_cls *cls;
00025 
00026         if (!(cls = rtnl_cls_alloc()))
00027                 nl_cli_fatal(ENOMEM, "Unable to allocate classifier object");
00028 
00029         return cls;
00030 }
00031 
00032 struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex,
00033                                         uint32_t parent)
00034 {
00035         struct nl_cache *cache;
00036         int err;
00037 
00038         if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0)
00039                 nl_cli_fatal(err, "Unable to allocate classifier cache: %s",
00040                              nl_geterror(err));
00041 
00042         return cache;
00043 }
00044 
00045 void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg)
00046 {
00047         int proto;
00048 
00049         if ((proto = nl_str2ether_proto(arg)) < 0)
00050                 nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg);
00051 
00052         rtnl_cls_set_protocol(cls, proto);
00053 }
00054 
00055 struct rtnl_ematch_tree *nl_cli_cls_parse_ematch(struct rtnl_cls *cls, char *arg)
00056 {
00057         struct rtnl_ematch_tree *tree;
00058         char *errstr = NULL;
00059         int err;
00060 
00061         if ((err = rtnl_ematch_parse_expr(arg, &errstr, &tree)) < 0)
00062                 nl_cli_fatal(err, "Unable to parse ematch expression: %s",
00063                                   errstr);
00064         
00065         if (errstr)
00066                 free(errstr);
00067 
00068         return tree;
00069 }
00070 
00071 /** @} */