summaryrefslogtreecommitdiff
path: root/src/socket.c
blob: 4fe795ae7b6b3b4b9a745ae2e7f5ef4675241d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * (C) 2008-2010 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <libmnl/libmnl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>

struct mnl_socket {
	int 			fd;
	struct sockaddr_nl	addr;
};

/**
 * mnl_socket_get_fd - obtain file descriptor from netlink socket
 * @nl: netlink socket obtained via mnl_socket_open()
 *
 * This function returns the file descriptor of a given netlink socket.
 */
int mnl_socket_get_fd(const struct mnl_socket *nl)
{
	return nl->fd;
}

/**
 * mnl_socket_get_portid - obtain Netlink PortID from netlink socket
 * @nl: netlink socket obtained via mnl_socket_open()
 *
 * This function returns the Netlink PortID of a given netlink socket.
 * It's a common mistake to assume that this PortID equals the process ID
 * which is not always true. This is the case if you open more than one
 * socket that is binded to the same Netlink subsystem.
 */
unsigned int mnl_socket_get_portid(const struct mnl_socket *nl)
{
	return nl->addr.nl_pid;
}

/**
 * mnl_socket_open - open a netlink socket
 * @unit: the netlink socket unit (see NETLINK_* constants)
 *
 * On error, it returns -1 and errno is appropriately set. Otherwise, it
 * returns a valid pointer to the mnl_socket structure.
 */
struct mnl_socket *mnl_socket_open(int unit)
{
	struct mnl_socket *nl;

	nl = calloc(sizeof(struct mnl_socket), 1);
	if (nl == NULL)
		return NULL;

	nl->fd = socket(AF_NETLINK, SOCK_RAW, unit);
	if (nl->fd == -1) {
		free(nl);
		return NULL;
	}

	return nl;
}

/**
 * mnl_socket_bind - bind netlink socket
 * @nl: netlink socket obtained via mnl_socket_open()
 * @groups: the group of message you're interested in
 * @pid: the port ID you want to use (use zero for automatic selection)
 *
 * On error, this function returns -1 and errno is appropriately set. On
 * success, 0 is returned.
 */
int mnl_socket_bind(struct mnl_socket *nl, int groups, int pid)
{
	int ret;
	socklen_t addr_len;

	nl->addr.nl_family = AF_NETLINK;
	nl->addr.nl_groups = groups;
	nl->addr.nl_pid = pid;

	ret = bind(nl->fd, (struct sockaddr *) &nl->addr, sizeof (nl->addr));
	if (ret < 0)
		return ret;

	addr_len = sizeof(nl->addr);
	ret = getsockname(nl->fd, (struct sockaddr *) &nl->addr, &addr_len);
	if (ret < 0)	
		return ret;

	if (addr_len != sizeof(nl->addr)) {
		errno = EINVAL;
		return -1;
	}
	if (nl->addr.nl_family != AF_NETLINK) {
		errno = EINVAL;
		return -1;
	}
	return 0;
}

/**
 * mnl_socket_sendto - send a netlink message of a certain size
 * @nl: netlink socket obtained via mnl_socket_open()
 * @buf: buffer containing the netlink message to be sent
 * @bufsiz: number of bytes in the buffer that you want to send
 *
 * On error, it returns -1 and errno is appropriately set. Otherwise, it 
 * returns the number of bytes sent.
 */
int mnl_socket_sendto(const struct mnl_socket *nl, const void *buf, size_t len)
{
	struct sockaddr_nl snl = {
		.nl_family = AF_NETLINK
	};
	return sendto(nl->fd, buf, len, 0, 
		      (struct sockaddr *) &snl, sizeof(snl));
}

/**
 * mnl_socket_sendmsg - send a netlink message of a certain size
 * @nl: netlink socket obtained via mnl_socket_open()
 * @msg: pointer to struct msghdr (must be initialized appropriately)
 * @flags: flags passed to sendmsg()
 *
 * On error, it returns -1 and errno is appropriately set. Otherwise, it 
 * returns the number of bytes sent.
 */
int mnl_socket_sendmsg(const struct mnl_socket *nl,
		       const struct msghdr *msg, int flags)
{
	return sendmsg(nl->fd, msg, flags);
}

/**
 * mnl_socket_recvfrom - receive a netlink message
 * @nl: netlink socket obtained via mnl_socket_open()
 * @buf: buffer that you want to use to store the netlink message
 * @bufsiz: size of the buffer passed to store the netlink message
 *
 * On error, it returns -1 and errno is appropriately set. If errno is set
 * to ENOSPC, it means that the buffer that you have passed to store the
 * netlink message is small so you have received a truncated message. Make
 * sure your program set a buffer big enough to store the netlink message.
 */
int mnl_socket_recvfrom(const struct mnl_socket *nl, void *buf, size_t bufsiz)
{
	int ret;
	struct sockaddr_nl addr;
	struct iovec iov = {
		.iov_base	= buf,
		.iov_len	= bufsiz,
	};
	struct msghdr msg = {
		.msg_name	= (void *)&addr,
		.msg_namelen	= sizeof(struct sockaddr_nl),
		.msg_iov	= &iov,
		.msg_iovlen	= 1,
		.msg_control	= NULL,
		.msg_controllen	= 0,
		.msg_flags	= 0,
	};
	ret = recvmsg(nl->fd, &msg, 0);
	if (ret == -1)
		return ret;

	if (msg.msg_flags & MSG_TRUNC) {
		errno = ENOSPC;
		return -1;
	}
	if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
		errno = EINVAL;
		return -1;
	}
	return ret;
}

/**
 * mnl_socket_recvmsg- receive a netlink message
 * @nl: netlink socket obtained via mnl_socket_open()
 * @msg: pointer to struct msghdr (must be initialized appropriately)
 * @flags: flags passed to recvmsg()
 *
 * On error, this function returns -1 and errno is appropriately set.
 * On sucess, this function returns the number of bytes received.
 */
int mnl_socket_recvmsg(const struct mnl_socket *nl,
		       struct msghdr *msg, int flags)
{
	return recvmsg(nl->fd, msg, flags);
}

/**
 * mnl_socket_close - close a given netlink socket
 * @nl: netlink socket obtained via mnl_socket_open()
 *
 * On error, this function returns -1 and errno is appropriately set.
 * On success, it returns 0.
 */
int mnl_socket_close(struct mnl_socket *nl)
{
	int ret = close(nl->fd);
	free(nl);
	nl = NULL;
	return ret;
}

/**
 * mnl_socket_setsockopt - set Netlink socket option
 * @nl: netlink socket obtained via mnl_socket_open()
 * @type: type of Netlink socket options
 * @buf: the buffer that contains the data about this option
 * @len: the size of the buffer passed
 *
 * This function allows you to set some Netlink socket option. As of writing
 * this, the existing options are:
 *
 * #define NETLINK_ADD_MEMBERSHIP  1
 * #define NETLINK_DROP_MEMBERSHIP 2
 * #define NETLINK_PKTINFO         3
 * #define NETLINK_BROADCAST_ERROR 4
 * #define NETLINK_NO_ENOBUFS      5
 *
 * In the early days, Netlink only supported 32 groups expressed in a
 * 32-bits mask. However, since 2.6.14, Netlink may have up to 2^32 multicast
 * groups but you have to use setsockopt() with NETLINK_ADD_MEMBERSHIP to
 * join a given multicast group. This function internally calls setsockopt()
 * to join a given netlink multicast group. You can still use mnl_bind()
 * and the 32-bit mask to join a set of Netlink multicast groups.
 *
 * On error, this function returns -1 and errno is appropriately set.
 */
int mnl_socket_setsockopt(const struct mnl_socket *nl, int type,
			  void *buf, socklen_t len)
{
	return setsockopt(nl->fd, SOL_NETLINK, type, buf, len);
}

/**
 * mnl_socket_getsockopt - get a Netlink socket option
 * @nl: netlink socket obtained via mnl_socket_open()
 * @type: type of Netlink socket options
 * @buf: pointer to the buffer to store the value of this option
 * @len: size of the information written in the buffer
 *
 * On error, this function returns -1 and errno is appropriately set.
 */
int mnl_socket_getsockopt(const struct mnl_socket *nl, int type,
			  void *buf, socklen_t *len)
{
	return getsockopt(nl->fd, SOL_NETLINK, type, buf, len);
}