12 /* log session to file stuff ... */
13 static FILE *lgfp
= NULL
;
14 static char timdatbuf
[20];
15 static char currlogfilename
[FILENAME_MAX
];
17 static void xlatlognam(char *d
, char *s
, char *hostname
, struct tm
*tm
);
20 * Log session traffic.
22 void logtraffic(unsigned char c
, int logmode
)
24 if (cfg
.logtype
> 0) {
25 if (cfg
.logtype
== logmode
) {
26 /* deferred open file from pgm start? */
36 * Log an Event Log entry (used in SSH packet logging mode).
38 void log_eventlog(char *event
)
40 if (cfg
.logtype
!= LGTYP_PACKETS
)
45 fprintf(lgfp
, "Event Log: %s\n", event
);
51 void log_packet(int direction
, int type
, char *texttype
, void *data
, int len
)
54 char dumpdata
[80], smalldata
[5];
56 if (cfg
.logtype
!= LGTYP_PACKETS
)
61 fprintf(lgfp
, "%s packet type %d / 0x%02x (%s)\n",
62 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
63 type
, type
, texttype
);
64 for (i
= 0; i
< len
; i
+= 16) {
65 sprintf(dumpdata
, " %08x%*s\n", i
, 1+3*16+2+16, "");
66 for (j
= 0; j
< 16 && i
+j
< len
; j
++) {
67 int c
= ((unsigned char *)data
)[i
+j
];
68 sprintf(smalldata
, "%02x", c
);
69 dumpdata
[10+2+3*j
] = smalldata
[0];
70 dumpdata
[10+2+3*j
+1] = smalldata
[1];
71 dumpdata
[10+1+3*16+2+j
] = (isprint(c
) ? c
: '.');
73 strcpy(dumpdata
+ 10+1+3*16+2+j
, "\n");
74 fputs(dumpdata
, lgfp
);
80 /* open log file append/overwrite mode */
88 /* Prevent repeat calls */
94 sprintf(writemod
, "wb"); /* default to rewrite */
99 /* substitute special codes in file name */
100 xlatlognam(currlogfilename
,cfg
.logfilename
,cfg
.host
, &tm
);
102 lgfp
= fopen(currlogfilename
, "r"); /* file already present? */
106 i
= askappend(currlogfilename
);
108 writemod
[0] = 'a'; /* set append mode */
109 else if (i
== 0) { /* cancelled */
111 cfg
.logtype
= 0; /* disable logging */
116 lgfp
= fopen(currlogfilename
, writemod
);
117 if (lgfp
) { /* enter into event log */
118 /* --- write header line into log file */
119 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
120 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", &tm
);
122 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
124 sprintf(buf
, "%s session log (%s mode) to file: ",
125 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
126 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
127 cfg
.logtype
== LGTYP_DEBUG ?
"raw" :
128 cfg
.logtype
== LGTYP_PACKETS ?
"SSH packets" : "<ukwn>"));
129 /* Make sure we do not exceed the output buffer size */
130 strncat(buf
, currlogfilename
, 128);
131 buf
[strlen(buf
)] = '\0';
145 * translate format codes into time/date strings
146 * and insert them into log file name
148 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
150 static void xlatlognam(char *d
, char *s
, char *hostname
, struct tm
*tm
) {
153 char *ds
= d
; /* save start pos. */
154 int len
= FILENAME_MAX
-1;
157 /* Let (bufp, len) be the string to append. */
158 bufp
= buf
; /* don't usually override this */
162 if (*s
) switch (c
= *s
++, tolower(c
)) {
164 size
= strftime(buf
, sizeof(buf
), "%Y", tm
);
167 size
= strftime(buf
, sizeof(buf
), "%m", tm
);
170 size
= strftime(buf
, sizeof(buf
), "%d", tm
);
173 size
= strftime(buf
, sizeof(buf
), "%H%M%S", tm
);
191 memcpy(d
, bufp
, size
);