Re-import of fprobe-ulog
[fprobe-ulog.git] / src / libipulog / libipulog.c
1 /* 
2  * libipulog.c, $Revision: 1.1.2.1 $
3  *
4  * netfilter ULOG userspace library.
5  *
6  * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
7  *
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
11  *
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.
16  *
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
20  *
21  * This library is still under development, so be aware of sudden interface
22  * changes
23  *
24  * $Id: libipulog.c,v 1.1.2.1 2004/08/13 21:03:10 sla Exp $
25  */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <net/if.h>
32 #include <libipulog/libipulog.h>
33
34 struct ipulog_handle
35 {
36         int fd;
37         u_int8_t blocking;
38         struct sockaddr_nl local;
39         struct sockaddr_nl peer;
40         struct nlmsghdr* last_nlhdr;
41 };
42
43 /* internal */
44
45
46 int ipulog_errno = IPULOG_ERR_NONE;
47
48 struct ipulog_errmap_t 
49 {
50         int errcode;
51         char *message;
52 } ipulog_errmap[] = 
53 {
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" },
65 };
66
67 static ssize_t 
68 ipulog_netlink_recvfrom(const struct ipulog_handle *h,
69                         unsigned char *buf, size_t len)
70 {
71         int addrlen, status;
72         struct nlmsghdr *nlh;
73         
74         if (len < sizeof(struct nlmsgerr)) {
75                 ipulog_errno = IPULOG_ERR_RECVBUF;
76                 return -1; 
77         }
78         addrlen = sizeof(h->peer);
79         status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&h->peer,      
80                         &addrlen);
81         if (status < 0) 
82         {
83                 ipulog_errno = IPULOG_ERR_RECV;
84                 return status;
85         }
86         if (addrlen != sizeof (h->peer))
87         {
88                 ipulog_errno = IPULOG_ERR_RECV;
89                 return -1;
90         }       
91         if (status == 0)
92         {
93                 ipulog_errno = IPULOG_ERR_NLEOF;
94                 return -1;
95         }
96         nlh = (struct nlmsghdr *)buf;
97         if (nlh->nlmsg_flags & MSG_TRUNC || status > len)
98         {
99                 ipulog_errno = IPULOG_ERR_TRUNC;
100                 return -1;
101         }
102         return status;
103 }
104
105 /* public */
106
107 char *ipulog_strerror(int errcode)
108 {
109         if (errcode < 0 || errcode > IPULOG_MAXERR)
110                 errcode = IPULOG_ERR_IMPL;
111         return ipulog_errmap[errcode].message;
112 }
113
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)
116 {
117         if (group < 1 || group > 32)
118         {
119                 ipulog_errno = IPULOG_ERR_INVGR;
120                 return 0;
121         }
122         return (1 << (group - 1));
123 }
124
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)
128 {
129         struct ipulog_handle *h;
130         int status;
131
132         h = (struct ipulog_handle *) malloc(sizeof(struct ipulog_handle));
133         if (h == NULL)
134         {
135                 ipulog_errno = IPULOG_ERR_HANDLE;
136                 return NULL;
137         }
138         memset(h, 0, sizeof(struct ipulog_handle));
139         h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NFLOG);
140         if (h->fd == -1)
141         {
142                 ipulog_errno = IPULOG_ERR_SOCKET;
143                 close(h->fd);
144                 free(h);
145                 return NULL;
146         }
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));
152         if (status == -1)
153         {
154                 ipulog_errno = IPULOG_ERR_BIND;
155                 close(h->fd);
156                 free(h);
157                 return NULL;
158         }
159         memset(&h->peer, 0, sizeof(struct sockaddr_nl));
160         h->peer.nl_family = AF_NETLINK;
161         h->peer.nl_pid = 0;
162         h->peer.nl_groups = gmask;
163
164         status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
165                             sizeof(rcvbufsize));
166         if (status == -1)
167         {
168                 ipulog_errno = IPULOG_ERR_RECVBUF;
169                 close(h->fd);
170                 free(h);
171                 return NULL;
172         }
173
174         return h;
175
176
177 /* destroy a ipulog handle */
178 void ipulog_destroy_handle(struct ipulog_handle *h)
179 {
180         close(h->fd);
181         free(h);
182 }
183
184 #if 0
185 int ipulog_set_mode()
186 {
187 }
188 #endif
189
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)
193 {
194         return ipulog_netlink_recvfrom(h, buf, len);
195 }
196
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, 
201                                      size_t len)
202 {
203         struct nlmsghdr *nlh;
204         size_t remain_len;
205
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;
211         }
212         
213         if (!h->last_nlhdr) {
214                 /* fist message in buffer */
215                 nlh = (struct nlmsghdr *) buf;
216                 if (!NLMSG_OK(nlh, len)) {
217                         /* ERROR */
218                         ipulog_errno = IPULOG_ERR_INVNL;
219                         return NULL;
220                 }
221         } else {
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;
228                         return NULL;
229                 }
230
231                 /* calculate remaining lenght from lasthdr to end of buffer */
232                 remain_len = (len - 
233                                 ((unsigned char *)h->last_nlhdr - buf));
234                 nlh = NLMSG_NEXT(h->last_nlhdr, remain_len);
235         }
236
237         h->last_nlhdr = nlh;
238
239         return NLMSG_DATA(nlh);
240 }
241
242 /* print a human readable description of the last error to stderr */
243 void ipulog_perror(const char *s)
244 {
245         if (s)
246                 fputs(s, stderr);
247         else
248                 fputs("ERROR", stderr);
249         if (ipulog_errno)
250                 fprintf(stderr, ": %s", ipulog_strerror(ipulog_errno));
251         if (errno)
252                 fprintf(stderr, ": %s", strerror(errno));
253         fputc('\n', stderr);
254 }
255