2 * libipulog.c, $Revision: 1.1.2.1 $
4 * netfilter ULOG userspace library.
6 * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This library is still under development, so be aware of sudden interface
24 * $Id: libipulog.c,v 1.1.2.1 2004/08/13 21:03:10 sla Exp $
32 #include <libipulog/libipulog.h>
38 struct sockaddr_nl local;
39 struct sockaddr_nl peer;
40 struct nlmsghdr* last_nlhdr;
46 int ipulog_errno = IPULOG_ERR_NONE;
48 struct ipulog_errmap_t
54 { IPULOG_ERR_NONE, "No error" },
55 { IPULOG_ERR_IMPL, "Not implemented yet" },
56 { IPULOG_ERR_HANDLE, "Unable to create netlink handle" },
57 { IPULOG_ERR_SOCKET, "Unable to create netlink socket" },
58 { IPULOG_ERR_BIND, "Unable to bind netlink socket" },
59 { IPULOG_ERR_RECVBUF, "Receive buffer size invalid" },
60 { IPULOG_ERR_RECV, "Error during netlink receive" },
61 { IPULOG_ERR_NLEOF, "Received EOF on netlink socket" },
62 { IPULOG_ERR_TRUNC, "Receive message truncated" },
63 { IPULOG_ERR_INVGR, "Invalid group specified" },
64 { IPULOG_ERR_INVNL, "Invalid netlink message" },
68 ipulog_netlink_recvfrom(const struct ipulog_handle *h,
69 unsigned char *buf, size_t len)
74 if (len < sizeof(struct nlmsgerr)) {
75 ipulog_errno = IPULOG_ERR_RECVBUF;
78 addrlen = sizeof(h->peer);
79 status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&h->peer,
83 ipulog_errno = IPULOG_ERR_RECV;
86 if (addrlen != sizeof (h->peer))
88 ipulog_errno = IPULOG_ERR_RECV;
93 ipulog_errno = IPULOG_ERR_NLEOF;
96 nlh = (struct nlmsghdr *)buf;
97 if (nlh->nlmsg_flags & MSG_TRUNC || status > len)
99 ipulog_errno = IPULOG_ERR_TRUNC;
107 char *ipulog_strerror(int errcode)
109 if (errcode < 0 || errcode > IPULOG_MAXERR)
110 errcode = IPULOG_ERR_IMPL;
111 return ipulog_errmap[errcode].message;
114 /* convert a netlink group (1-32) to a group_mask suitable for create_handle */
115 u_int32_t ipulog_group2gmask(u_int32_t group)
117 if (group < 1 || group > 32)
119 ipulog_errno = IPULOG_ERR_INVGR;
122 return (1 << (group - 1));
125 /* create a ipulog handle for the reception of packets sent to gmask */
126 struct ipulog_handle *ipulog_create_handle(u_int32_t gmask,
127 u_int32_t rcvbufsize)
129 struct ipulog_handle *h;
132 h = (struct ipulog_handle *) malloc(sizeof(struct ipulog_handle));
135 ipulog_errno = IPULOG_ERR_HANDLE;
138 memset(h, 0, sizeof(struct ipulog_handle));
139 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NFLOG);
142 ipulog_errno = IPULOG_ERR_SOCKET;
147 memset(&h->local, 0, sizeof(struct sockaddr_nl));
148 h->local.nl_family = AF_NETLINK;
149 h->local.nl_pid = getpid();
150 h->local.nl_groups = gmask;
151 status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
154 ipulog_errno = IPULOG_ERR_BIND;
159 memset(&h->peer, 0, sizeof(struct sockaddr_nl));
160 h->peer.nl_family = AF_NETLINK;
162 h->peer.nl_groups = gmask;
164 status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
168 ipulog_errno = IPULOG_ERR_RECVBUF;
177 /* destroy a ipulog handle */
178 void ipulog_destroy_handle(struct ipulog_handle *h)
185 int ipulog_set_mode()
190 /* do a BLOCKING read on an ipulog handle */
191 ssize_t ipulog_read(struct ipulog_handle *h, unsigned char *buf,
192 size_t len, int timeout)
194 return ipulog_netlink_recvfrom(h, buf, len);
197 /* get a pointer to the actual start of the ipulog packet,
198 use this to strip netlink header */
199 ulog_packet_msg_t *ipulog_get_packet(struct ipulog_handle *h,
200 const unsigned char *buf,
203 struct nlmsghdr *nlh;
206 /* if last header in handle not inside this buffer,
207 * drop reference to last header */
208 if ((unsigned char *)h->last_nlhdr > (buf + len) ||
209 (unsigned char *)h->last_nlhdr < buf) {
210 h->last_nlhdr = NULL;
213 if (!h->last_nlhdr) {
214 /* fist message in buffer */
215 nlh = (struct nlmsghdr *) buf;
216 if (!NLMSG_OK(nlh, len)) {
218 ipulog_errno = IPULOG_ERR_INVNL;
222 /* we are in n-th part of multilink message */
223 if (h->last_nlhdr->nlmsg_type == NLMSG_DONE ||
224 !(h->last_nlhdr->nlmsg_flags & NLM_F_MULTI)) {
225 /* if last part in multilink message,
226 * or no multipart message at all: return */
227 h->last_nlhdr = NULL;
231 /* calculate remaining lenght from lasthdr to end of buffer */
233 ((unsigned char *)h->last_nlhdr - buf));
234 nlh = NLMSG_NEXT(h->last_nlhdr, remain_len);
239 return NLMSG_DATA(nlh);
242 /* print a human readable description of the last error to stderr */
243 void ipulog_perror(const char *s)
248 fputs("ERROR", stderr);
250 fprintf(stderr, ": %s", ipulog_strerror(ipulog_errno));
252 fprintf(stderr, ": %s", strerror(errno));