~ubuntu-branches/ubuntu/feisty/tcpreen/feisty

« back to all changes in this revision

Viewing changes to logs/log.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Kleineidam
  • Date: 2006-05-02 21:24:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060502212400-v5m70qcaqwdmt2bg
Tags: 1.4.3-0.1
* NMU, with permission from maintainer.
* New upstream release (Closes: #269639, #211956)
* Standards version 3.7.0.0
* Use debhelper v5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * log.cpp - log files handling functions
 
3
 * $Id: log.cpp,v 1.3 2004/06/05 15:15:17 rdenisc Exp $
 
4
 */
 
5
 
 
6
/***********************************************************************
 
7
 *  Copyright (C) 2002-2004 Remi Denis-Courmont.                       *
 
8
 *  This program is free software; you can redistribute and/or modify  *
 
9
 *  it under the terms of the GNU General Public License as published  *
 
10
 *  by the Free Software Foundation; version 2 of the license.         *
 
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.               *
 
15
 *  See the 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, you can get it from:              *
 
19
 *  http://www.gnu.org/copyleft/gpl.html                               *
 
20
 ***********************************************************************/
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include <config.h>
 
24
#endif
 
25
 
 
26
#include "secstdio.h"
 
27
#include <string.h>
 
28
 
 
29
#include "log.h"
 
30
 
 
31
 
 
32
/*** DataLog class implementation ***/
 
33
DataLog::~DataLog (void)
 
34
{
 
35
        if ((out != NULL) && close_out)
 
36
                fclose (out);
 
37
}
 
38
 
 
39
 
 
40
void
 
41
DataLog::Connect (const char *, const char *,
 
42
                        const char *, const char *)
 
43
{
 
44
}
 
45
 
 
46
 
 
47
int
 
48
DataLog::WriteServerData (const void *, int length, int)
 
49
{
 
50
        return length;
 
51
}
 
52
 
 
53
 
 
54
int
 
55
DataLog::WriteClientData (const void *, int length, int)
 
56
{
 
57
        return length;
 
58
}
 
59
 
 
60
 
 
61
void
 
62
DataLog::ShutdownServer (void)
 
63
{
 
64
}
 
65
 
 
66
 
 
67
void
 
68
DataLog::ShutdownClient (void)
 
69
{
 
70
}
 
71
 
 
72
 
 
73
/*** DataLogList implementation ***/
 
74
int
 
75
DataLogList::AddLog (DataLog *log)
 
76
{
 
77
        if (log == NULL)
 
78
                return -1;
 
79
        
 
80
        struct listnode *node;
 
81
 
 
82
        try
 
83
        {
 
84
                node = new listnode;
 
85
        }
 
86
        catch (...)
 
87
        {
 
88
                return -1;
 
89
        }
 
90
 
 
91
        node->log = log;
 
92
        node->next = head;
 
93
        head = node;
 
94
        return 0;
 
95
}
 
96
 
 
97
 
 
98
DataLogList::~DataLogList (void)
 
99
{
 
100
        for (struct listnode *node = head; node != NULL; node = head)
 
101
        {
 
102
                head = node->next;
 
103
                delete node->log;
 
104
                delete node;
 
105
        }
 
106
}
 
107
 
 
108
 
 
109
int
 
110
DataLogList::WriteServerData (const void *data, int length, int oob)
 
111
{
 
112
        for (struct listnode *node = head; node != NULL; node = node->next)
 
113
                if (node->log->WriteServerData (data, length, oob) != length)
 
114
                        return -1;
 
115
 
 
116
        return length;
 
117
}
 
118
 
 
119
 
 
120
int
 
121
DataLogList::WriteClientData (const void *data, int length, int oob)
 
122
{
 
123
        for (struct listnode *node = head; node != NULL; node = node->next)
 
124
                if (node->log->WriteClientData (data, length, oob) != length)
 
125
                        return -1;
 
126
 
 
127
        return length;
 
128
}
 
129
 
 
130
 
 
131
void
 
132
DataLogList::ShutdownServer (void)
 
133
{
 
134
        for (struct listnode *node = head; node != NULL; node = node->next)
 
135
                node->log->ShutdownServer ();
 
136
}
 
137
 
 
138
 
 
139
void
 
140
DataLogList::ShutdownClient (void)
 
141
{
 
142
        for (struct listnode *node = head; node != NULL; node = node->next)
 
143
                node->log->ShutdownClient ();
 
144
}
 
145
 
 
146
 
 
147
void
 
148
DataLogList::Connect (const char *server, const char *service,
 
149
                        const char *client, const char *port)
 
150
{
 
151
        for (struct listnode *node = head; node != NULL; node = node->next)
 
152
                node->log->Connect (server, service, client, port);
 
153
}
 
154
 
 
155
 
 
156
/*** DataLogListMaker implementation ***/
 
157
int
 
158
DataLogListMaker::AddLogMaker (DataLog *(*maker) (void), FILE *stream)
 
159
{
 
160
        struct listnode *node;
 
161
 
 
162
        try
 
163
        {
 
164
                node = new struct listnode;
 
165
        }
 
166
        catch (...)
 
167
        {
 
168
                return -1;
 
169
        }
 
170
 
 
171
        node->maker = maker;
 
172
        node->next = head;
 
173
        node->basename = NULL;
 
174
        node->stream = stream;
 
175
        head = node;
 
176
        return 0;
 
177
}
 
178
 
 
179
 
 
180
int
 
181
DataLogListMaker::AddLogMaker (DataLog *(*maker) (void), const char *prefix)
 
182
{
 
183
        struct listnode *node;
 
184
 
 
185
        try
 
186
        {
 
187
                node = new struct listnode;
 
188
        }
 
189
        catch (...)
 
190
        {
 
191
                return -1;
 
192
        }
 
193
 
 
194
        node->maker = maker;
 
195
        node->next = head;
 
196
 
 
197
        node->basename = strdup (prefix);
 
198
        if (node->basename == NULL)
 
199
        {
 
200
                delete node;
 
201
                return -1;
 
202
        }
 
203
 
 
204
        node->stream = NULL;
 
205
        head = node;
 
206
        return 0;
 
207
}
 
208
 
 
209
 
 
210
DataLogListMaker::~DataLogListMaker (void)
 
211
{
 
212
        for (struct listnode *node = head; node != NULL; node = head)
 
213
        {
 
214
                head = node->next;
 
215
                if (node->basename != NULL)
 
216
                        delete node->basename;
 
217
                delete node;
 
218
        }
 
219
}
 
220
 
 
221
 
 
222
DataLogList *
 
223
DataLogListMaker::MakeLogList (const char *addr, const char *port) const
 
224
{
 
225
        DataLogList *list;
 
226
 
 
227
        try
 
228
        {
 
229
                list = new DataLogList;
 
230
        }
 
231
        catch (...)
 
232
        {
 
233
                return NULL;
 
234
        }
 
235
 
 
236
        // Creates a log for each maker in the list
 
237
        for (struct listnode *node = head; node != NULL; node = node->next)
 
238
        {
 
239
                DataLog *log;
 
240
 
 
241
                try
 
242
                {
 
243
                        log = node->maker ();
 
244
                }
 
245
                catch (...)
 
246
                {
 
247
                        delete list;
 
248
                        return NULL;
 
249
                }
 
250
 
 
251
                if (log != NULL)
 
252
                {
 
253
                        // Adds a log in the list
 
254
                        if (list->AddLog (log))
 
255
                        {
 
256
                                delete log;
 
257
                                delete list;
 
258
                                return NULL;
 
259
                        }
 
260
 
 
261
                        // Attaches a stream to the new log
 
262
                        if (node->stream != NULL)
 
263
                                log->SetStream (node->stream, 0);
 
264
                        else
 
265
                        {       // Do we need more abstraction?
 
266
                                const char *prefix = node->basename;
 
267
                                if (prefix == NULL)
 
268
                                        prefix = "";
 
269
                                if (addr == NULL)
 
270
                                        addr = "";
 
271
                                if (port == NULL)
 
272
                                        port = "";
 
273
 
 
274
                                char *filename;
 
275
 
 
276
                                int len = strlen (prefix) + strlen (addr)
 
277
                                                + strlen (port) + 3;
 
278
 
 
279
                                try
 
280
                                {
 
281
                                        filename = new char[len];
 
282
                                }
 
283
                                catch (...)
 
284
                                {
 
285
                                        delete list;
 
286
                                        return NULL;
 
287
                                }
 
288
                                snprintf (filename, len, "%s-%s-%s",
 
289
                                                prefix, addr, port);
 
290
 
 
291
                                FILE *stream = secure_fopen (filename, "a");
 
292
                                delete filename;
 
293
 
 
294
                                if (stream == NULL)
 
295
                                {
 
296
                                        delete list;
 
297
                                        return NULL;
 
298
                                }
 
299
                                log->SetStream (stream);
 
300
                        }
 
301
                }
 
302
        }
 
303
 
 
304
        return list;
 
305
}