tizen 2.3 release
[kernel/api/system-resource.git] / src / network / nl-helper.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /**
21  *  @file nl-helper.c
22  *  @desc Common netlink helper function
23  *
24  *  Created on: Jun 25, 2012
25  */
26
27 #include "nl-helper.h"
28 #include "trace.h"
29
30 #include <unistd.h>
31 #include <linux/rtnetlink.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /**
36  * create_netlink(): Create netlink socket and returns it.
37  * Returns: Created socket on success and -1 on failure.
38  */
39 int create_netlink(int protocol, uint32_t groups)
40 {
41         /**
42         * TODO it's one socket, in future make set of sockets
43         * unique for protocol and groups
44         */
45         int sock;
46         sock = socket(PF_NETLINK, SOCK_RAW, protocol);
47         if (sock < 0)
48                 return -EINVAL;
49
50         struct sockaddr_nl src_addr = { 0, };
51
52         src_addr.nl_family = AF_NETLINK;
53         src_addr.nl_groups = groups;
54
55         if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
56                 close(sock);
57                 return -1;
58         }
59
60         return sock;
61 }
62
63 void fill_attribute_list(struct rtattr **atb, const int max_len,
64         struct rtattr *rt_na, int rt_len)
65 {
66         int i = 0;
67         while (RTA_OK(rt_na, rt_len)) {
68                 if (rt_na->rta_type <= max_len)
69                         atb[rt_na->rta_type] = rt_na;
70
71                 rt_na = RTA_NEXT(rt_na, rt_len);
72                 ++i;
73                 if (i >= max_len)
74                         break;
75         }
76 }
77
78 /* read netlink message from socket
79  * return opaque pointer to genl structure */
80
81 #ifdef CONFIG_DATAUSAGE_NFACCT
82 int read_netlink(int sock, void *buf, size_t len)
83 {
84         ssize_t ret;
85         struct sockaddr_nl addr;
86         struct iovec iov = {
87                 .iov_base       = buf,
88                 .iov_len        = len,
89         };
90         struct msghdr msg = {
91                 .msg_name       = &addr,
92                 .msg_namelen    = sizeof(struct sockaddr_nl),
93                 .msg_iov        = &iov,
94                 .msg_iovlen     = 1,
95                 .msg_control    = NULL,
96                 .msg_controllen = 0,
97                 .msg_flags      = 0,
98         };
99         ret = recvmsg(sock, &msg, 0);
100         if (ret == -1)
101                 return ret;
102
103         if (msg.msg_flags & MSG_TRUNC) {
104                 errno = ENOSPC;
105                 return -1;
106         }
107
108         if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
109                 errno = EINVAL;
110                 return -1;
111         }
112
113         return ret;
114 }
115 #else
116 int read_netlink(int sock, void *buf, size_t len)
117 {
118         int ans_len;
119         struct genl *ans = buf;
120
121         ans_len = recv(sock, ans, len, MSG_DONTWAIT);
122         if (ans_len < 0)
123                 return 0;
124
125         if (ans->n.nlmsg_type == NLMSG_ERROR)
126                 return 0;
127
128         if (!NLMSG_OK((&ans->n), ans_len))
129                 return 0;
130
131         return ans_len;
132 }
133 #endif