summaryrefslogtreecommitdiff
path: root/chromium/components/cast_channel/logger.cc
blob: 171830da261297ab6b8cfd2ba04a7f82d7af702c (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cast_channel/logger.h"

#include <stdint.h>

#include <string>
#include <utility>

#include "base/strings/string_util.h"
#include "components/cast_channel/cast_auth_util.h"
#include "components/cast_channel/cast_socket.h"
#include "net/base/net_errors.h"

namespace cast_channel {

using net::IPEndPoint;

namespace {

ChallengeReplyError AuthErrorToChallengeReplyError(
    AuthResult::ErrorType error_type) {
  switch (error_type) {
    case AuthResult::ERROR_NONE:
      return ChallengeReplyError::NONE;
    case AuthResult::ERROR_PEER_CERT_EMPTY:
      return ChallengeReplyError::PEER_CERT_EMPTY;
    case AuthResult::ERROR_WRONG_PAYLOAD_TYPE:
      return ChallengeReplyError::WRONG_PAYLOAD_TYPE;
    case AuthResult::ERROR_NO_PAYLOAD:
      return ChallengeReplyError::NO_PAYLOAD;
    case AuthResult::ERROR_PAYLOAD_PARSING_FAILED:
      return ChallengeReplyError::PAYLOAD_PARSING_FAILED;
    case AuthResult::ERROR_MESSAGE_ERROR:
      return ChallengeReplyError::MESSAGE_ERROR;
    case AuthResult::ERROR_NO_RESPONSE:
      return ChallengeReplyError::NO_RESPONSE;
    case AuthResult::ERROR_FINGERPRINT_NOT_FOUND:
      return ChallengeReplyError::FINGERPRINT_NOT_FOUND;
    case AuthResult::ERROR_CERT_PARSING_FAILED:
      return ChallengeReplyError::CERT_PARSING_FAILED;
    case AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA:
      return ChallengeReplyError::CERT_NOT_SIGNED_BY_TRUSTED_CA;
    case AuthResult::ERROR_CANNOT_EXTRACT_PUBLIC_KEY:
      return ChallengeReplyError::CANNOT_EXTRACT_PUBLIC_KEY;
    case AuthResult::ERROR_SIGNED_BLOBS_MISMATCH:
      return ChallengeReplyError::SIGNED_BLOBS_MISMATCH;
    case AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG:
      return ChallengeReplyError::TLS_CERT_VALIDITY_PERIOD_TOO_LONG;
    case AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE:
      return ChallengeReplyError::TLS_CERT_VALID_START_DATE_IN_FUTURE;
    case AuthResult::ERROR_TLS_CERT_EXPIRED:
      return ChallengeReplyError::TLS_CERT_EXPIRED;
    case AuthResult::ERROR_CRL_INVALID:
      return ChallengeReplyError::CRL_INVALID;
    case AuthResult::ERROR_CERT_REVOKED:
      return ChallengeReplyError::CERT_REVOKED;
    case AuthResult::ERROR_SENDER_NONCE_MISMATCH:
      return ChallengeReplyError::SENDER_NONCE_MISMATCH;
    case AuthResult::ERROR_SIGNATURE_EMPTY:
      return ChallengeReplyError::SIGNATURE_EMPTY;
    case AuthResult::ERROR_DIGEST_UNSUPPORTED:
      return ChallengeReplyError::DIGEST_UNSUPPORTED;
    default:
      NOTREACHED();
      return ChallengeReplyError::NONE;
  }
}

}  // namespace

LastError::LastError()
    : channel_event(ChannelEvent::UNKNOWN),
      challenge_reply_error(ChallengeReplyError::NONE),
      net_return_value(net::OK) {}

LastError::~LastError() {}

Logger::Logger() {
  // Logger may not be necessarily be created on the IO thread, but logging
  // happens exclusively there.
  DETACH_FROM_THREAD(thread_checker_);
}

Logger::~Logger() {}

void Logger::LogSocketEventWithRv(int channel_id,
                                  ChannelEvent channel_event,
                                  int rv) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  MaybeSetLastError(channel_id, channel_event, rv, ChallengeReplyError::NONE);
}

void Logger::LogSocketChallengeReplyEvent(int channel_id,
                                          const AuthResult& auth_result) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  MaybeSetLastError(channel_id, ChannelEvent::AUTH_CHALLENGE_REPLY, net::OK,
                    AuthErrorToChallengeReplyError(auth_result.error_type));
}

LastError Logger::GetLastError(int channel_id) const {
  const auto it = last_errors_.find(channel_id);
  return it != last_errors_.end() ? it->second : LastError();
}

void Logger::ClearLastError(int channel_id) {
  last_errors_.erase(channel_id);
}

void Logger::MaybeSetLastError(int channel_id,
                               ChannelEvent channel_event,
                               int rv,
                               ChallengeReplyError challenge_reply_error) {
  auto it = last_errors_.find(channel_id);
  if (it == last_errors_.end())
    last_errors_[channel_id] = LastError();

  LastError* last_error = &last_errors_[channel_id];
  if (rv < net::ERR_IO_PENDING) {
    last_error->net_return_value = rv;
    last_error->channel_event = channel_event;
  }

  if (challenge_reply_error != ChallengeReplyError::NONE) {
    last_error->challenge_reply_error = challenge_reply_error;
    last_error->channel_event = channel_event;
  }
}

}  // namespace cast_channel