Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / extensions / ulogd_OPRINT.c
1 /* ulogd_MAC.c, Version $Revision: 5252 $
2  *
3  * ulogd output target for logging to a file 
4  *
5  * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 
9  *  as published by the Free Software Foundation
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * $Id: ulogd_OPRINT.c 5252 2005-02-14 16:12:49Z laforge $
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ulogd/ulogd.h>
28 #include <ulogd/conffile.h>
29
30 #ifndef ULOGD_OPRINT_DEFAULT
31 #define ULOGD_OPRINT_DEFAULT    "/var/log/ulogd.pktlog"
32 #endif
33
34 #define NIPQUAD(addr) \
35         ((unsigned char *)&addr)[0], \
36         ((unsigned char *)&addr)[1], \
37         ((unsigned char *)&addr)[2], \
38         ((unsigned char *)&addr)[3]
39
40 #define HIPQUAD(addr) \
41         ((unsigned char *)&addr)[3], \
42         ((unsigned char *)&addr)[2], \
43         ((unsigned char *)&addr)[1], \
44         ((unsigned char *)&addr)[0]
45
46 static FILE *of = NULL;
47
48 static int _output_print(ulog_iret_t *res)
49 {
50         ulog_iret_t *ret;
51         
52         fprintf(of, "===>PACKET BOUNDARY\n");
53         for (ret = res; ret; ret = ret->cur_next) {
54                 fprintf(of,"%s=", ret->key);
55                 switch (ret->type) {
56                         case ULOGD_RET_STRING:
57                                 fprintf(of, "%s\n", (char *) ret->value.ptr);
58                                 break;
59                         case ULOGD_RET_BOOL:
60                         case ULOGD_RET_INT8:
61                         case ULOGD_RET_INT16:
62                         case ULOGD_RET_INT32:
63                                 fprintf(of, "%d\n", ret->value.i32);
64                                 break;
65                         case ULOGD_RET_UINT8:
66                         case ULOGD_RET_UINT16:
67                         case ULOGD_RET_UINT32:
68                                 fprintf(of, "%u\n", ret->value.ui32);
69                                 break;
70                         case ULOGD_RET_IPADDR:
71                                 fprintf(of, "%u.%u.%u.%u\n", 
72                                         HIPQUAD(ret->value.ui32));
73                                 break;
74                         case ULOGD_RET_NONE:
75                                 fprintf(of, "<none>");
76                                 break;
77                 }
78         }
79         return 0;
80 }
81
82 static config_entry_t outf_ce = { 
83         .key = "file", 
84         .type = CONFIG_TYPE_STRING, 
85         .options = CONFIG_OPT_NONE,
86         .u = { .string = ULOGD_OPRINT_DEFAULT }
87 };
88
89 static void sighup_handler_print(int signal)
90 {
91
92         switch (signal) {
93         case SIGHUP:
94                 ulogd_log(ULOGD_NOTICE, "PKTLOG: reopening logfile\n");
95                 fclose(of);
96                 of = fopen(outf_ce.u.string, "a");
97                 if (!of) {
98                         ulogd_log(ULOGD_FATAL, "can't open PKTLOG: %s\n",
99                                 strerror(errno));
100                         exit(2);
101                 }
102                 break;
103         default:
104                 break;
105         }
106 }
107
108 static int oprint_init(void)
109 {
110 #ifdef DEBUG
111         of = stdout;
112 #else
113         config_parse_file("OPRINT", &outf_ce);
114
115         of = fopen(outf_ce.u.string, "a");
116         if (!of) {
117                 ulogd_log(ULOGD_FATAL, "can't open PKTLOG: %s\n", 
118                         strerror(errno));
119                 exit(2);
120         }               
121 #endif
122         return 0;
123 }
124
125 static void oprint_fini(void)
126 {
127         if (of != stdout)
128                 fclose(of);
129
130         return;
131 }
132
133 static ulog_output_t oprint_op = {
134         .name = "oprint", 
135         .output = &_output_print, 
136         .signal = &sighup_handler_print,
137         .init = &oprint_init,
138         .fini = &oprint_fini,
139 };
140
141 void _init(void)
142 {
143         register_output(&oprint_op);
144 }