14 /* log session to file stuff ... */
17 enum { L_CLOSED
, L_OPENING
, L_OPEN
, L_ERROR
} state
;
19 Filename currlogfilename
;
24 static void xlatlognam(Filename
*d
, Filename s
, char *hostname
, struct tm
*tm
);
27 * Internal wrapper function which must be called for _all_ output
28 * to the log file. It takes care of opening the log file if it
29 * isn't open, buffering data if it's in the process of being
30 * opened asynchronously, etc.
32 static void logwrite(struct LogContext
*ctx
, void *data
, int len
)
35 * In state L_CLOSED, we call logfopen, which will set the state
36 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
37 * those three _after_ processing L_CLOSED.
39 if (ctx
->state
== L_CLOSED
)
42 if (ctx
->state
== L_OPENING
) {
43 bufchain_add(&ctx
->queue
, data
, len
);
44 } else if (ctx
->state
== L_OPEN
) {
46 if (fwrite(data
, 1, len
, ctx
->lgfp
) < len
) {
49 /* Log state is L_ERROR so this won't cause a loop */
50 logevent(ctx
->frontend
,
51 "Disabled writing session log due to error while writing");
53 } /* else L_ERROR, so ignore the write */
57 * Convenience wrapper on logwrite() which printf-formats the
60 static void logprintf(struct LogContext
*ctx
, const char *fmt
, ...)
66 data
= dupvprintf(fmt
, ap
);
69 logwrite(ctx
, data
, strlen(data
));
74 * Flush any open log file.
76 void logflush(void *handle
) {
77 struct LogContext
*ctx
= (struct LogContext
*)handle
;
78 if (ctx
->cfg
.logtype
> 0)
79 if (ctx
->state
== L_OPEN
)
83 static void logfopen_callback(void *handle
, int mode
)
85 struct LogContext
*ctx
= (struct LogContext
*)handle
;
86 char buf
[256], *event
;
91 ctx
->state
= L_ERROR
; /* disable logging */
93 fmode
= (mode
== 1 ?
"ab" : "wb");
94 ctx
->lgfp
= f_open(ctx
->currlogfilename
, fmode
, FALSE
);
101 if (ctx
->state
== L_OPEN
) {
102 /* Write header line into log file. */
104 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", &tm
);
105 logprintf(ctx
, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
106 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf
);
109 event
= dupprintf("%s session log (%s mode) to file: %s",
110 ctx
->state
== L_ERROR ?
111 (mode
== 0 ?
"Disabled writing" : "Error writing") :
112 (mode
== 1 ?
"Appending" : "Writing new"),
113 (ctx
->cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
114 ctx
->cfg
.logtype
== LGTYP_DEBUG ?
"raw" :
115 ctx
->cfg
.logtype
== LGTYP_PACKETS ?
"SSH packets" :
116 ctx
->cfg
.logtype
== LGTYP_SSHRAW ?
"SSH raw data" :
118 filename_to_str(&ctx
->currlogfilename
));
119 logevent(ctx
->frontend
, event
);
123 * Having either succeeded or failed in opening the log file,
124 * we should write any queued data out.
126 assert(ctx
->state
!= L_OPENING
); /* make _sure_ it won't be requeued */
127 while (bufchain_size(&ctx
->queue
)) {
130 bufchain_prefix(&ctx
->queue
, &data
, &len
);
131 logwrite(ctx
, data
, len
);
132 bufchain_consume(&ctx
->queue
, len
);
137 * Open the log file. Takes care of detecting an already-existing
138 * file and asking the user whether they want to append, overwrite
141 void logfopen(void *handle
)
143 struct LogContext
*ctx
= (struct LogContext
*)handle
;
147 /* Prevent repeat calls */
148 if (ctx
->state
!= L_CLOSED
)
151 if (!ctx
->cfg
.logtype
)
156 /* substitute special codes in file name */
157 xlatlognam(&ctx
->currlogfilename
, ctx
->cfg
.logfilename
,ctx
->cfg
.host
, &tm
);
159 ctx
->lgfp
= f_open(ctx
->currlogfilename
, "r", FALSE
); /* file already present? */
162 if (ctx
->cfg
.logxfovr
!= LGXF_ASK
) {
163 mode
= ((ctx
->cfg
.logxfovr
== LGXF_OVR
) ?
2 : 1);
165 mode
= askappend(ctx
->frontend
, ctx
->currlogfilename
,
166 logfopen_callback
, ctx
);
168 mode
= 2; /* create == overwrite */
171 ctx
->state
= L_OPENING
;
173 logfopen_callback(ctx
, mode
); /* open the file */
176 void logfclose(void *handle
)
178 struct LogContext
*ctx
= (struct LogContext
*)handle
;
183 ctx
->state
= L_CLOSED
;
187 * Log session traffic.
189 void logtraffic(void *handle
, unsigned char c
, int logmode
)
191 struct LogContext
*ctx
= (struct LogContext
*)handle
;
192 if (ctx
->cfg
.logtype
> 0) {
193 if (ctx
->cfg
.logtype
== logmode
)
194 logwrite(ctx
, &c
, 1);
199 * Log an Event Log entry. Used in SSH packet logging mode; this is
200 * also as convenient a place as any to put the output of Event Log
201 * entries to stderr when a command-line tool is in verbose mode.
202 * (In particular, this is a better place to put it than in the
203 * front ends, because it only has to be done once for all
204 * platforms. Platforms which don't have a meaningful stderr can
205 * just avoid defining FLAG_STDERR.
207 void log_eventlog(void *handle
, const char *event
)
209 struct LogContext
*ctx
= (struct LogContext
*)handle
;
210 if ((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)) {
211 fprintf(stderr
, "%s\n", event
);
214 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
217 if (ctx
->cfg
.logtype
!= LGTYP_PACKETS
&&
218 ctx
->cfg
.logtype
!= LGTYP_SSHRAW
)
220 logprintf(ctx
, "Event Log: %s\r\n", event
);
226 * If n_blanks != 0, blank or omit some parts.
227 * Set of blanking areas must be in increasing order.
229 void log_packet(void *handle
, int direction
, int type
,
230 char *texttype
, const void *data
, int len
,
231 int n_blanks
, const struct logblank_t
*blanks
,
232 const unsigned long *seq
)
234 struct LogContext
*ctx
= (struct LogContext
*)handle
;
235 char dumpdata
[80], smalldata
[5];
236 int p
= 0, b
= 0, omitted
= 0;
237 int output_pos
= 0; /* NZ if pending output in dumpdata */
239 if (!(ctx
->cfg
.logtype
== LGTYP_SSHRAW
||
240 (ctx
->cfg
.logtype
== LGTYP_PACKETS
&& texttype
)))
246 logprintf(ctx
, "%s packet #0x%lx, type %d / 0x%02x (%s)\r\n",
247 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
248 *seq
, type
, type
, texttype
);
250 logprintf(ctx
, "%s packet type %d / 0x%02x (%s)\r\n",
251 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
252 type
, type
, texttype
);
255 logprintf(ctx
, "%s raw data\r\n",
256 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing");
260 * Output a hex/ASCII dump of the packet body, blanking/omitting
261 * parts as specified.
266 /* Move to a current entry in the blanking array. */
267 while ((b
< n_blanks
) &&
268 (p
>= blanks
[b
].offset
+ blanks
[b
].len
))
270 /* Work out what type of blanking to apply to
272 blktype
= PKTLOG_EMIT
; /* default */
273 if ((b
< n_blanks
) &&
274 (p
>= blanks
[b
].offset
) &&
275 (p
< blanks
[b
].offset
+ blanks
[b
].len
))
276 blktype
= blanks
[b
].type
;
278 /* If we're about to stop omitting, it's time to say how
279 * much we omitted. */
280 if ((blktype
!= PKTLOG_OMIT
) && omitted
) {
281 logprintf(ctx
, " (%d byte%s omitted)\r\n",
282 omitted
, (omitted
==1?
"":"s"));
286 /* (Re-)initialise dumpdata as necessary
287 * (start of row, or if we've just stopped omitting) */
288 if (!output_pos
&& !omitted
)
289 sprintf(dumpdata
, " %08x%*s\r\n", p
-(p
%16), 1+3*16+2+16, "");
291 /* Deal with the current byte. */
292 if (blktype
== PKTLOG_OMIT
) {
296 if (blktype
== PKTLOG_BLANK
) {
298 sprintf(smalldata
, "XX");
299 } else { /* PKTLOG_EMIT */
300 c
= ((unsigned char *)data
)[p
];
301 sprintf(smalldata
, "%02x", c
);
303 dumpdata
[10+2+3*(p
%16)] = smalldata
[0];
304 dumpdata
[10+2+3*(p
%16)+1] = smalldata
[1];
305 dumpdata
[10+1+3*16+2+(p
%16)] = (isprint(c
) ? c
: '.');
306 output_pos
= (p
%16) + 1;
311 /* Flush row if necessary */
312 if (((p
% 16) == 0) || (p
== len
) || omitted
) {
314 strcpy(dumpdata
+ 10+1+3*16+2+output_pos
, "\r\n");
315 logwrite(ctx
, dumpdata
, strlen(dumpdata
));
324 logprintf(ctx
, " (%d byte%s omitted)\r\n",
325 omitted
, (omitted
==1?
"":"s"));
329 void *log_init(void *frontend
, Config
*cfg
)
331 struct LogContext
*ctx
= snew(struct LogContext
);
333 ctx
->state
= L_CLOSED
;
334 ctx
->frontend
= frontend
;
335 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
336 bufchain_init(&ctx
->queue
);
340 void log_free(void *handle
)
342 struct LogContext
*ctx
= (struct LogContext
*)handle
;
345 bufchain_clear(&ctx
->queue
);
349 void log_reconfig(void *handle
, Config
*cfg
)
351 struct LogContext
*ctx
= (struct LogContext
*)handle
;
354 if (!filename_equal(ctx
->cfg
.logfilename
, cfg
->logfilename
) ||
355 ctx
->cfg
.logtype
!= cfg
->logtype
)
356 reset_logging
= TRUE
;
358 reset_logging
= FALSE
;
363 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
370 * translate format codes into time/date strings
371 * and insert them into log file name
373 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
375 static void xlatlognam(Filename
*dest
, Filename src
,
376 char *hostname
, struct tm
*tm
) {
379 char buffer
[FILENAME_MAX
];
380 int len
= sizeof(buffer
)-1;
385 s
= filename_to_str(&src
);
388 /* Let (bufp, len) be the string to append. */
389 bufp
= buf
; /* don't usually override this */
394 if (*s
) switch (c
= *s
++, tolower((unsigned char)c
)) {
396 size
= strftime(buf
, sizeof(buf
), "%Y", tm
);
399 size
= strftime(buf
, sizeof(buf
), "%m", tm
);
402 size
= strftime(buf
, sizeof(buf
), "%d", tm
);
405 size
= strftime(buf
, sizeof(buf
), "%H%M%S", tm
);
423 memcpy(d
, bufp
, size
);
429 *dest
= filename_from_str(buffer
);