1 /* $NetBSD: evt.c,v 1.4 2005/11/21 14:20:29 manu Exp $ */ 2 3 /* Id: evt.c,v 1.2.4.1 2005/09/26 17:49:38 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 52 struct evtlist evtlist = TAILQ_HEAD_INITIALIZER(evtlist); 53 int evtlist_len = 0; 54 55 void 56 evt_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 /* If we are above the limit, don't record anything */ 67 if (evtlist_len > EVTLIST_MAX) { 68 plog(LLV_DEBUG, LOCATION, NULL, 69 "Cannot record event: event queue overflowed\n"); 70 return; 71 } 72 73 /* If we hit the limit, record an overflow event instead */ 74 if (evtlist_len == EVTLIST_MAX) { 75 plog(LLV_ERROR, LOCATION, NULL, 76 "Cannot record event: event queue overflow\n"); 77 src = NULL; 78 dst = NULL; 79 type = EVTT_OVERFLOW; 80 optdata = NULL; 81 } 82 83 len = sizeof(*evtdump); 84 if (optdata) 85 len += optdata->l; 86 87 if ((evtdump = racoon_malloc(len)) == NULL) { 88 plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n", 89 strerror(errno)); 90 return; 91 } 92 93 if ((evt = racoon_malloc(sizeof(*evt))) == NULL) { 94 plog(LLV_ERROR, LOCATION, NULL, "Cannot record event: %s\n", 95 strerror(errno)); 96 racoon_free(evtdump); 97 return; 98 } 99 100 if (src) 101 memcpy(&evtdump->src, src, sysdep_sa_len(src)); 102 if (dst) 103 memcpy(&evtdump->dst, dst, sysdep_sa_len(dst)); 104 evtdump->len = len; 105 evtdump->type = type; 106 time(&evtdump->timestamp); 107 108 if (optdata) 109 memcpy(evtdump + 1, optdata->v, optdata->l); 110 111 evt->dump = evtdump; 112 TAILQ_INSERT_TAIL(&evtlist, evt, next); 113 114 evtlist_len++; 115 116 return; 117 } 118 119 struct evtdump * 120 evt_pop(void) { 121 struct evtdump *evtdump; 122 struct evt *evt; 123 124 if ((evt = TAILQ_FIRST(&evtlist)) == NULL) 125 return NULL; 126 127 evtdump = evt->dump; 128 TAILQ_REMOVE(&evtlist, evt, next); 129 racoon_free(evt); 130 evtlist_len--; 131 132 return evtdump; 133 } 134 135 vchar_t * 136 evt_dump(void) { 137 struct evtdump *evtdump; 138 vchar_t *buf = NULL; 139 140 if ((evtdump = evt_pop()) != NULL) { 141 if ((buf = vmalloc(evtdump->len)) == NULL) { 142 plog(LLV_ERROR, LOCATION, NULL, 143 "evt_dump failed: %s\n", strerror(errno)); 144 return NULL; 145 } 146 memcpy(buf->v, evtdump, evtdump->len); 147 racoon_free(evtdump); 148 } 149 150 return buf; 151 } 152