evt.c revision 1.1.1.2
1/*	$NetBSD: evt.c,v 1.1.1.2 2005/02/23 14:54:14 manu Exp $	*/
2
3/* Id: evt.c,v 1.2 2004/11/29 23:30:39 manubsd Exp */
4
5/*
6 * Copyright (C) 2004 Emmanuel Dreyfus
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "config.h"
35
36#include <errno.h>
37#include <string.h>
38#include <stdio.h>
39#include <time.h>
40#include <unistd.h>
41#include <stdlib.h>
42#include <sys/queue.h>
43#include <sys/socket.h>
44
45#include "vmbuf.h"
46#include "plog.h"
47#include "misc.h"
48#include "gcmalloc.h"
49#include "evt.h"
50
51
52struct evtlist evtlist = TAILQ_HEAD_INITIALIZER(evtlist);
53int evtlist_len = 0;
54
55void
56evt_push(src, dst, type, optdata)
57	struct sockaddr *src;
58	struct sockaddr *dst;
59	int type;
60	vchar_t *optdata;
61{
62	struct evtdump *evtdump;
63	struct evt *evt;
64	size_t len;
65
66	len = sizeof(*evtdump);
67	if (optdata)
68		len += optdata->l;
69
70	if ((evtdump = racoon_malloc(len)) == NULL) {
71		plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n",
72		    strerror(errno));
73		return;
74	}
75
76	if ((evt = racoon_malloc(sizeof(*evt))) == NULL) {
77		plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n",
78		    strerror(errno));
79		racoon_free(evtdump);
80		return;
81	}
82
83	if (src)
84		memcpy(&evtdump->src, src, sysdep_sa_len(src));
85	if (dst)
86		memcpy(&evtdump->dst, dst, sysdep_sa_len(dst));
87	evtdump->len = len;
88	evtdump->type = type;
89	time(&evtdump->timestamp);
90
91	if (optdata)
92		memcpy(evtdump + 1, optdata->v, optdata->l);
93
94	evt->dump = evtdump;
95	TAILQ_INSERT_TAIL(&evtlist, evt, next);
96
97	if (evtlist_len++ == EVTLIST_MAX)
98		evt_push(NULL, NULL, EVTT_OVERFLOW, NULL);
99
100	return;
101}
102
103struct evtdump *
104evt_pop(void) {
105	struct evtdump *evtdump;
106	struct evt *evt;
107
108	if ((evt = TAILQ_FIRST(&evtlist)) == NULL)
109		return NULL;
110
111	evtdump = evt->dump;
112	TAILQ_REMOVE(&evtlist, evt, next);
113	racoon_free(evt);
114	evtlist_len--;
115
116	return evtdump;
117}
118
119vchar_t *
120evt_dump(void) {
121	struct evtdump *evtdump;
122	vchar_t *buf = NULL;
123
124	if ((evtdump = evt_pop()) != NULL) {
125		if ((buf = vmalloc(evtdump->len)) == NULL) {
126			plog(LLV_ERROR, LOCATION, NULL,
127			    "evt_dump failed: %s\n", strerror(errno));
128			return NULL;
129		}
130		memcpy(buf->v, evtdump, evtdump->len);
131		racoon_free(evtdump);
132	}
133
134	return buf;
135}
136