Merge "Return errors to caller" into tizen_5.5
[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 #include "stc-manager.h"
30
31 #define NLA_BUF_MAX 65560       /*(65 * 1024) - used in tc_common,
32                                   we'll do the same */
33
34 /*TODO: move to common place and rewrite because it's from TC*/
35 #define NLMSG_TAIL(nmsg) \
36         ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
37
38 /*TODO remove unused code */
39 typedef struct {
40         struct nlmsghdr n;
41         struct tcmsg t;
42         char buf[NLA_BUF_MAX];
43 } rt_param;
44
45 void put_attr(rt_param *arg, int type, const void *data, int data_len);
46
47 /*
48  * Generic macros for dealing with netlink sockets. Might be duplicated
49  * elsewhere. It is recommended that commercial grade applications use
50  * libnl or libnetlink and use the interfaces provided by the library
51  */
52 #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
53 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
54 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
55
56 #define NETLINK_BUF_SIZE 16536
57
58 enum nfnl_acct_msg_types {
59         NFNL_MSG_ACCT_NEW,
60         NFNL_MSG_ACCT_GET,
61         NFNL_MSG_ACCT_GET_CTRZERO,
62         NFNL_MSG_ACCT_DEL,
63         NFNL_MSG_ACCT_MAX
64 };
65
66 enum nfnl_acct_type {
67         NFACCT_UNSPEC,
68         NFACCT_NAME,
69         NFACCT_PKTS,
70         NFACCT_BYTES,
71         NFACCT_USE,
72         NFACCT_FLAGS,
73         NFACCT_QUOTA,
74         NFACCT_FILTER,
75         __NFACCT_MAX
76 };
77
78 enum nfnl_attr_filter_type {
79         NFACCT_FILTER_ATTR_UNSPEC,
80         NFACCT_FILTER_ATTR_MASK,
81         NFACCT_FILTER_ATTR_VALUE,
82         __NFACCT_FILTER_ATTR_MAX
83 };
84
85 #define NFACCT_MAX (__NFACCT_MAX - 1)
86
87 struct genl {
88         struct nlmsghdr n;
89         struct genlmsghdr g;
90         char buf[NETLINK_BUF_SIZE];
91 };
92
93 struct netlink_serialization_params {
94         int direction;
95         struct genl *ans;
96         struct counter_arg *carg;
97         int (*eval_attr)(struct rtattr *attr_list[__NFACCT_MAX],
98                          void *user_data);
99         int (*post_eval_attr)(void *user_data);
100 };
101
102 typedef struct {
103         void (*deserialize_answer)(struct netlink_serialization_params *params);
104         void (*finalize)(struct netlink_serialization_params *params);
105         struct netlink_serialization_params params;
106 } netlink_serialization_command;
107
108 int create_netlink(int protocol, uint32_t groups);
109 int read_netlink(int sock, void *buf, size_t len);
110
111 void fill_attribute_list(struct rtattr **atb, const int max_len,
112                          struct rtattr *rt_na, int rt_len);
113
114 #endif  /* __STC_HELPER_NL_H__ */