Fixed some svace issues
[platform/core/connectivity/stc-manager.git] / src / helper / helper-nl.h
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __STC_HELPER_NL_H__
18 #define __STC_HELPER_NL_H__
19
20 //#include "app-stat.h"
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <sys/socket.h>
25 #include <linux/netlink.h>
26 #include <linux/genetlink.h>
27 #include <linux/rtnetlink.h>
28
29 #define NLA_BUF_MAX 65560       /*(65 * 1024) - used in tc_common,
30                                   we'll do the same */
31
32 /*TODO: move to common place and rewrite because it's from TC*/
33 #define NLMSG_TAIL(nmsg) \
34         ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
35
36 /*TODO remove unused code */
37 typedef struct {
38         struct nlmsghdr n;
39         struct tcmsg t;
40         char buf[NLA_BUF_MAX];
41 } rt_param;
42
43 void put_attr(rt_param *arg, int type, const void *data, int data_len);
44
45 /*
46  * Generic macros for dealing with netlink sockets. Might be duplicated
47  * elsewhere. It is recommended that commercial grade applications use
48  * libnl or libnetlink and use the interfaces provided by the library
49  */
50 #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
51 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
52 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
53
54 #define NETLINK_BUF_SIZE 16536
55
56 enum nfnl_acct_msg_types {
57         NFNL_MSG_ACCT_NEW,
58         NFNL_MSG_ACCT_GET,
59         NFNL_MSG_ACCT_GET_CTRZERO,
60         NFNL_MSG_ACCT_DEL,
61         NFNL_MSG_ACCT_MAX
62 };
63
64 enum nfnl_acct_type {
65         NFACCT_UNSPEC,
66         NFACCT_NAME,
67         NFACCT_PKTS,
68         NFACCT_BYTES,
69         NFACCT_USE,
70         NFACCT_FLAGS,
71         NFACCT_QUOTA,
72         NFACCT_FILTER,
73         __NFACCT_MAX
74 };
75
76 enum nfnl_attr_filter_type {
77         NFACCT_FILTER_ATTR_UNSPEC,
78         NFACCT_FILTER_ATTR_MASK,
79         NFACCT_FILTER_ATTR_VALUE,
80         __NFACCT_FILTER_ATTR_MAX
81 };
82
83 #define NFACCT_MAX (__NFACCT_MAX - 1)
84
85 struct genl {
86         struct nlmsghdr n;
87         struct genlmsghdr g;
88         char buf[NETLINK_BUF_SIZE];
89 };
90
91 struct netlink_serialization_params {
92         int direction;
93         struct genl *ans;
94         struct counter_arg *carg;
95         int (*eval_attr)(struct rtattr *attr_list[__NFACCT_MAX],
96                          void *user_data);
97         int (*post_eval_attr)(void *user_data);
98 };
99
100 typedef struct {
101         void (*deserialize_answer)(struct netlink_serialization_params *params);
102         void (*finalize)(struct netlink_serialization_params *params);
103         struct netlink_serialization_params params;
104 } netlink_serialization_command;
105
106 int create_netlink(int protocol, uint32_t groups);
107 int read_netlink(int sock, void *buf, size_t len);
108
109 void fill_attribute_list(struct rtattr **atb, const int max_len,
110                          struct rtattr *rt_na, int rt_len);
111
112 #endif  /* __STC_HELPER_NL_H__ */