File: binlog.c

package info (click to toggle)
binkd 1.1a-99-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,452 kB
  • sloc: ansic: 22,949; makefile: 943; perl: 369; sh: 325
file content (216 lines) | stat: -rw-r--r-- 7,198 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*--------------------------------------------------------------------*/
/*       B i n L o g . c                                              */
/*                                                                    */
/*       Part of BinkD project                                        */
/*       Binary log implementation                                    */
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/*       Copyright (c) 1997 by Fydodor Ustinov                        */
/*                             FIDONet 2:5020/79                      */
/*       FrontDoor stat (c) 1997 A&T Software                         */
/*                             2:5023/8@fidonet                       */
/*                                                                    */
/*  This program is  free software;  you can  redistribute it and/or  */
/*  modify it  under  the terms of the GNU General Public License as  */ 
/*  published  by the  Free Software Foundation; either version 2 of  */
/*  the License, or (at your option) any later version. See COPYING.  */
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*/
/*                        System include files                        */
/*--------------------------------------------------------------------*/

#include <stdio.h>

/*--------------------------------------------------------------------*/
/*                        Local include files                         */
/*--------------------------------------------------------------------*/

#include "sys.h"
#include "readcfg.h"
#include "iphdr.h"
#include "protoco2.h"
#include "binlog.h"
#include "tools.h"
#include "sem.h"

/* Write 16-bit integer to file in intel bytes order */
static int fput16(u16 arg, FILE *file)
{
	if (fputc(arg & 0xff, file) == EOF) return EOF;
	return fputc(arg >> 8, file);
}

/* Write 32-bit integer to file in intel bytes order */
static int fput32(u32 arg, FILE *file)
{
	if (fput16((u16)(arg & 0xffff), file) == EOF) return EOF;
	return fput16((u16)(arg/0x10000), file);
}

/*--------------------------------------------------------------------*/
/*    static void TLogStat (char*, STATE*, char*, int)                */
/*                                                                    */
/*    Add record to T-Mail style binary log.                          */
/*--------------------------------------------------------------------*/

static void TLogStat (int status, STATE *state, char *binlogpath, int tzoff)
{
	struct {
		u16    fZone;
		u16    fNet;
		u16    fNode;
		u16    fPoint;
		u32    fSTime;
		u32    fLTime;
		u32    fBReceive;
		u32    fBSent;
		u8     fFReceive;
		u8     fFSent;
		u16    fStatus;
	} TS;

	FILE *fl;

	if (binlogpath[0]) {
		TS.fStatus = 0;

		if (state->to) {
			TS.fZone = (u16)state->to->fa.z;
			TS.fNet = (u16)state->to->fa.net;
			TS.fNode = (u16)state->to->fa.node;
			TS.fPoint = (u16)state->to->fa.p;
			TS.fStatus = 1;
		} else if (state->fa) {
			TS.fZone = (u16)state->fa->z;
			TS.fNet = (u16)state->fa->net;
			TS.fNode = (u16)state->fa->node;
			TS.fPoint = (u16)state->fa->p;
			TS.fStatus = 2;
		} else {
			TS.fZone = 0;
			TS.fNet = 0;
			TS.fNode = 0;
			TS.fPoint = 0;
			TS.fStatus = 0;
		}
		TS.fBReceive = (u32)state->bytes_rcvd;
		TS.fBSent = (u32)state->bytes_sent;
		TS.fFReceive = (u8)state->files_rcvd;
		TS.fFSent = (u8)state->files_sent;
		TS.fSTime = (u32)(state->start_time + tz_off(state->start_time, tzoff)*60);
		TS.fLTime = (u32)(safe_time() - state->start_time);
		if (status) {
			TS.fStatus |= 3;
		}
		LockSem(&blsem);
		if ((fl = fopen(binlogpath,"ab")) != NULL) {
			/* fwrite(&TS, sizeof(TS), 1, fl); */
			/* FIXME: check retcode and set original file size
			 * if write failed */
			fput16(TS.fZone,     fl);
			fput16(TS.fNet,      fl);
			fput16(TS.fNode,     fl);
			fput16(TS.fPoint,    fl);
			fput32(TS.fSTime,    fl);
			fput32(TS.fLTime,    fl);
			fput32(TS.fBReceive, fl);
			fput32(TS.fBSent,    fl);
			fputc (TS.fFReceive, fl);
			fputc (TS.fFSent,    fl);
			fput16(TS.fStatus,   fl);
			fclose(fl);
			ReleaseSem(&blsem);
		} else {
			ReleaseSem(&blsem);
			Log(1,"unable to open binary log file `%s'",binlogpath);
		}
	}

}

/*--------------------------------------------------------------------*/
/*    static void FDLogStat (STATE*, char*, char*, int)               */
/*                                                                    */
/*    Add record to FrontDoor-style binary log.                       */
/*--------------------------------------------------------------------*/

static void FDLogStat (STATE *state, char *fdinhist, char *fdouthist, int tzoff)
{
	struct
	{
		u16    Zone;
		u16    Net;
		u16    Node;
		u16    Point;
		char   Domain[16];
		u32    TimeStart;
		u32    TimeEnd;
		char   StationName[32];
		char   StationLoc[40];
		u32    Received;
		u32    Sent;
		u32    Cost;
	} std;

	FILE *fp;
	time_t t;

	if (!state->fa || ((state->to && !fdouthist[0]) || (!state->to && !fdinhist[0])))
            return; /* nothing to do */

	t = safe_time();
	std.TimeStart = (u32)(state->start_time + tz_off(state->start_time, tzoff)*60);
	std.TimeEnd = (u32)(t + tz_off(t, tzoff)*60);
	std.Zone = (u16)state->fa->z;
	std.Net = (u16)state->fa->net;
	std.Node = (u16)state->fa->node;
	std.Point = (u16)state->fa->p;
	strnzcpy (std.Domain, state->fa->domain, sizeof (std.Domain));
	strnzcpy (std.StationName, state->sysname, sizeof (std.StationName));
	strnzcpy (std.StationLoc, state->location, sizeof (std.StationLoc));
	std.Received = (u32)(state->bytes_rcvd);
	std.Sent = (u32)(state->bytes_sent);
	std.Cost = 0; /* Let it be free :) */

	LockSem(&blsem);
	if ((fp = fopen ( state->to ? fdouthist : fdinhist, "ab" )) != NULL)
	{
		/* fwrite ( &std, (size_t) sizeof(std), (size_t) 1, fp); */
		/* FIXME: check retcode and set original file size
		 * if write failed */
		fput16(std.Zone,       fp);
		fput16(std.Net,        fp);
		fput16(std.Node,       fp);
		fput16(std.Point,      fp);
		fwrite(std.Domain,     sizeof(std.Domain), 1, fp);
		fput32(std.TimeStart,  fp);
		fput32(std.TimeEnd,    fp);
		fwrite(std.StationName,sizeof(std.StationName), 1, fp);
		fwrite(std.StationLoc, sizeof(std.StationLoc),  1, fp);
		fput32(std.Received,   fp);
		fput32(std.Sent,       fp);
		fput32(std.Cost,       fp);
		fclose( fp );
		ReleaseSem(&blsem);
	}
	else
	{
		ReleaseSem(&blsem);
		Log (1, "failed to write to %s", (state->to ? fdouthist : fdinhist));
	}
}

/*--------------------------------------------------------------------*/
/*    void BinLogStat (char*, STATE*, BINKD_CONFIG*)                  */
/*                                                                    */
/*    Add record to binary logs.                                      */
/*--------------------------------------------------------------------*/

void BinLogStat (int status, STATE *state, BINKD_CONFIG *config)
{
	TLogStat (status, state, config->binlogpath, config->tzoff);
	FDLogStat (state, config->fdinhist, config->fdouthist, config->tzoff);
}