summaryrefslogtreecommitdiff
path: root/extra/yassl/src/log.cpp
blob: 5adbe401d7a68c7bc2ea6ee7628a9c8cb61870cf (plain)
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
/*
   Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

   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; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA  02110-1335  USA.
*/

/*  Debug logging functions
 */


#include "runtime.hpp"
#include "log.hpp"

#ifdef YASSL_LOG
    #include <time.h>
    #include <stdio.h>
    #include <string.h>
#endif



namespace yaSSL {


#ifdef YASSL_LOG

    enum { MAX_MSG = 81 };

    Log::Log(const char* str)
    {
        log_ = fopen(str, "w");
        Trace("********** Logger Attached **********");
    }


    Log::~Log()
    {
        Trace("********** Logger Detached **********");
        fclose(log_);
    }


    // Trace a message
    void Log::Trace(const char* str)
    {
        if (!log_) return;

        time_t clicks = time(0);
        char   timeStr[32];

        // get rid of newline
        strncpy(timeStr, ctime(&clicks), sizeof(timeStr));
        unsigned int len = strlen(timeStr);
        timeStr[len - 1] = 0;

        char msg[MAX_MSG];

        strncpy(msg, timeStr, sizeof(timeStr));
        strncat(msg, ":", 1);
        strncat(msg, str, MAX_MSG - sizeof(timeStr) - 2);
        strncat(msg, "\n", 1);
        msg[MAX_MSG - 1] = 0;

        fputs(msg, log_);
    }


    #if defined(_WIN32) || defined(__MACH__) || defined(__hpux__)
        typedef int socklen_t;
    #endif


    // write tcp address
    void Log::ShowTCP(socket_t fd, bool ended)
    {
        sockaddr_in peeraddr;
        socklen_t   len = sizeof(peeraddr);
        if (getpeername(fd, (sockaddr*)&peeraddr, &len) != 0)
            return;

        const char* p = reinterpret_cast<const char*>(&peeraddr.sin_addr);
        char msg[MAX_MSG];
        char number[16];
    
        if (ended)
            strncpy(msg, "yaSSL conn DONE  w/ peer ", 26);
        else
            strncpy(msg, "yaSSL conn BEGUN w/ peer ", 26);
        for (int i = 0; i < 4; ++i) {
            sprintf(number, "%u", static_cast<unsigned short>(p[i]));
            strncat(msg, number, 8);
            if (i < 3)
                strncat(msg, ".", 1);
        }
        strncat(msg, " port ", 8);
        sprintf(number, "%d", htons(peeraddr.sin_port));
        strncat(msg, number, 8);

        msg[MAX_MSG - 1] = 0;
        Trace(msg);
    }


    // log processed data
    void Log::ShowData(uint bytes, bool sent)
    {
        char msg[MAX_MSG];
        char number[16];

        if (sent)
            strncpy(msg, "Sent     ", 10); 
        else
            strncpy(msg, "Received ", 10);
        sprintf(number, "%u", bytes);
        strncat(msg, number, 8);
        strncat(msg, " bytes of application data", 27);

        msg[MAX_MSG - 1] = 0;
        Trace(msg);
    }


#else // no YASSL_LOG


    Log::Log(const char*) {}
    Log::~Log() {}
    void Log::Trace(const char*) {}
    void Log::ShowTCP(socket_t, bool) {}
    void Log::ShowData(uint, bool) {}


#endif // YASSL_LOG
} // namespace