2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/ioctl.h>
30 #include <catta/malloc.h>
32 #include <catta/log.h>
37 CattaNetlinkCallback callback;
42 const CattaPoll *poll_api;
46 int catta_netlink_work(CattaNetlink *nl, int block) {
53 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
57 iov.iov_base = nl->buffer;
58 iov.iov_len = nl->buffer_length;
64 smsg.msg_control = cred_msg;
65 smsg.msg_controllen = sizeof(cred_msg);
66 smsg.msg_flags = (block ? 0 : MSG_DONTWAIT);
68 if ((bytes = recvmsg(nl->fd, &smsg, 0)) < 0) {
69 if (errno == EAGAIN || errno == EINTR)
72 catta_log_error(__FILE__": recvmsg() failed: %s", strerror(errno));
76 cmsg = CMSG_FIRSTHDR(&smsg);
78 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
79 catta_log_warn("No sender credentials received, ignoring data.");
83 cred = (struct ucred*) CMSG_DATA(cmsg);
88 p = (struct nlmsghdr *) nl->buffer;
92 for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
93 if (!NLMSG_OK(p, (size_t) bytes)) {
94 catta_log_warn(__FILE__": packet truncated");
98 nl->callback(nl, p, nl->userdata);
104 static void socket_event(CattaWatch *w, int fd, CATTA_GCC_UNUSED CattaWatchEvent event, void *userdata) {
105 CattaNetlink *nl = userdata;
109 assert(fd == nl->fd);
111 catta_netlink_work(nl, 0);
114 CattaNetlink *catta_netlink_new(const CattaPoll *poll_api, uint32_t groups, void (*cb) (CattaNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
117 struct sockaddr_nl addr;
118 CattaNetlink *nl = NULL;
123 if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
124 catta_log_error(__FILE__": socket(PF_NETLINK): %s", strerror(errno));
128 memset(&addr, 0, sizeof(addr));
129 addr.nl_family = AF_NETLINK;
130 addr.nl_groups = groups;
131 addr.nl_pid = 0; // use 0 instead of getpid() to allow multiple instances of catta in one process
133 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
134 catta_log_error(__FILE__": bind(): %s", strerror(errno));
138 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
139 catta_log_error(__FILE__": SO_PASSCRED: %s", strerror(errno));
143 if (!(nl = catta_new(CattaNetlink, 1))) {
144 catta_log_error(__FILE__": catta_new() failed.");
148 nl->poll_api = poll_api;
152 nl->userdata = userdata;
154 if (!(nl->buffer = catta_new(uint8_t, nl->buffer_length = 64*1024))) {
155 catta_log_error(__FILE__": catta_new() failed.");
159 if (!(nl->watch = poll_api->watch_new(poll_api, fd, CATTA_WATCH_IN, socket_event, nl))) {
160 catta_log_error(__FILE__": Failed to create watch.");
172 catta_free(nl->buffer);
179 void catta_netlink_free(CattaNetlink *nl) {
183 nl->poll_api->watch_free(nl->watch);
188 catta_free(nl->buffer);
192 int catta_netlink_send(CattaNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq) {
196 m->nlmsg_seq = nl->seq++;
197 m->nlmsg_flags |= NLM_F_ACK;
199 if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
200 catta_log_error(__FILE__": send(): %s", strerror(errno));
205 *ret_seq = m->nlmsg_seq;