Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / extensions / ulogd_LOCAL.c
1 /*  ulogd_LOCAL.c, Version 0.3
2  *
3  *  ulogd interpreter plugin for: - local time of packet
4  *                                - hostname of localhost
5  *
6  *  (C) 2001-2002 by Florent AIDE <faide@alphacent.com>
7  *  with the help of Moez MKADMI <moez.mka@voila.fr>
8  *  shamelessly ripped from Harald Welte
9  *
10  *  2002 extended by Martin Kaehmer <teg@mompl.org>
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License version 2 
14  *  as published by the Free Software Foundation
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  *
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/time.h>
32 #include <ulogd/ulogd.h>
33
34 #ifdef DEBUG_LOCAL
35 #define DEBUGP(x) ulogd_log(ULOGD_DEBUG, x)
36 #else
37 #define DEBUGP(format, args...)
38 #endif
39
40
41 static char hostname[255];
42
43 static ulog_iret_t *_interp_local(ulog_interpreter_t *ip,
44                                   ulog_packet_msg_t *pkt)
45 {
46         struct timeval tv;
47         ulog_iret_t *ret = ip->result;
48
49         /* Get date */
50         gettimeofday(&tv, NULL);
51
52         /* put date */
53         ret[0].value.ui32 = (unsigned long) tv.tv_sec;
54         ret[0].flags |= ULOGD_RETF_VALID;
55
56         ret[1].value.ptr = hostname;
57         ret[1].flags |= ULOGD_RETF_VALID;
58
59         return ret;
60 }
61
62 static ulog_iret_t local_rets[] = {
63         { .type = ULOGD_RET_UINT32, 
64           .flags = ULOGD_RETF_NONE, 
65           .key = "local.time",
66         },
67         { .type = ULOGD_RET_STRING, 
68           .flags = ULOGD_RETF_NONE, 
69           .key = "local.hostname",
70         },
71 };
72
73 static ulog_interpreter_t local_ip[] = { 
74     { NULL, "local", 0, &_interp_local, 2, local_rets },
75     { NULL, "", 0, NULL, 0, NULL },
76 };
77
78 static void _local_reg_ip(void)
79 {
80         ulog_interpreter_t *ip = local_ip;
81         ulog_interpreter_t *p;
82
83         for (p = ip; p->interp; p++)
84                 register_interpreter(p);
85 }
86
87 void _init(void)
88 {
89         /* get hostname */
90         char *tmp;
91         if (gethostname(hostname, sizeof(hostname)) < 0) {
92                 ulogd_log(ULOGD_FATAL, "can't gethostname(): %s\n",
93                           strerror(errno));
94                 exit(2);
95         }
96         /* strip off everything after first '.' */
97         if ((tmp = strchr(hostname, '.')))
98                 *tmp = '\0';
99
100         _local_reg_ip();
101 }