summaryrefslogtreecommitdiff
path: root/src/components/connection_handler/src/connection.cc
blob: 84904724ad035608a3a01570b15a18497d182aaf (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * Copyright (c) 2018, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <limits.h>

#include <algorithm>

#include "connection_handler/connection.h"
#include "connection_handler/connection_handler.h"
#include "protocol_handler/protocol_packet.h"
#include "utils/helpers.h"
#include "utils/logger.h"
#include "utils/macro.h"

#ifdef ENABLE_SECURITY
#include "security_manager/security_manager.h"
#include "security_manager/ssl_context.h"
#endif  // ENABLE_SECURITY

/**
 * \namespace connection_handler
 * \brief SmartDeviceLink ConnectionHandler namespace.
 */
namespace connection_handler {

SDL_CREATE_LOG_VARIABLE("ConnectionHandler")

Service* Session::FindService(
    const protocol_handler::ServiceType& service_type) {
  ServiceList::iterator service_it =
      std::find(service_list.begin(), service_list.end(), service_type);
  if (service_it != service_list.end()) {
    return &(*service_it);
  }
  return NULL;
}

const Service* Session::FindService(
    const protocol_handler::ServiceType& service_type) const {
  ServiceList::const_iterator service_it =
      std::find(service_list.begin(), service_list.end(), service_type);
  if (service_it != service_list.end()) {
    return &(*service_it);
  }
  return NULL;
}

const bool Session::final_message_sent() const {
  return final_message_sent_;
}

void Session::set_final_message_sent(const bool final_message_sent) {
  final_message_sent_ = final_message_sent;
}

Connection::Connection(ConnectionHandle connection_handle,
                       DeviceHandle connection_device_handle,
                       ConnectionHandler* connection_handler,
                       uint32_t heartbeat_timeout)
    : connection_handler_(connection_handler)
    , connection_handle_(connection_handle)
    , connection_device_handle_(connection_device_handle)
    , primary_connection_handle_(0)
    , heartbeat_timeout_(heartbeat_timeout) {
  SDL_LOG_AUTO_TRACE();
  DCHECK(connection_handler_);

  heartbeat_monitor_ = new HeartBeatMonitor(heartbeat_timeout_, this);
  heart_beat_monitor_thread_ =
      threads::CreateThread("HeartBeatMonitor", heartbeat_monitor_);
  heart_beat_monitor_thread_->Start();
}

Connection::~Connection() {
  SDL_LOG_AUTO_TRACE();
  heart_beat_monitor_thread_->Stop(threads::Thread::kThreadSoftStop);
  delete heartbeat_monitor_;
  threads::DeleteThread(heart_beat_monitor_thread_);

  // Before clearing out the session_map_, we must remove all sessions
  // associated with this Connection from the SessionConnectionMap.

  // NESTED LOCK: make sure to lock session_map_lock_ then ConnectionHandler's
  // session_connection_map_lock_ptr_ (which will be taken in RemoveSession).
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.begin();
  while (session_it != session_map_.end()) {
    SDL_LOG_INFO("Removed Session ID "
                 << static_cast<int>(session_it->first)
                 << " from Session/Connection Map in Connection Destructor");
    connection_handler_->RemoveSession(session_it->first);
    ++session_it;
  }

  session_map_.clear();
}

uint32_t Connection::AddNewSession(
    const transport_manager::ConnectionUID connection_handle) {
  SDL_LOG_AUTO_TRACE();

  // NESTED LOCK: make sure to lock session_map_lock_ then ConnectionHandler's
  // session_connection_map_lock_ptr_ (which will be taken in AddSession)
  sync_primitives::AutoLock lock(session_map_lock_);

  // Even though we have our own SessionMap, we use the Connection Handler's
  // SessionConnectionMap to generate a session ID. We want to make sure that
  // session IDs are globally unique, and not only unique within a Connection.
  const uint32_t session_id =
      connection_handler_->AddSession(connection_handle);
  if (session_id > 0) {
    Session& new_session = session_map_[session_id];
    new_session.protocol_version = ::protocol_handler::PROTOCOL_VERSION_2;
    new_session.service_list.push_back(
        Service(protocol_handler::kRpc, connection_handle));
    new_session.service_list.push_back(
        Service(protocol_handler::kBulk, connection_handle));
  }

  return session_id;
}

uint32_t Connection::RemoveSession(uint8_t session_id) {
  SDL_LOG_AUTO_TRACE();

  // Again, a NESTED lock, but it follows the rules.
  sync_primitives::AutoLock lock(session_map_lock_);

  if (!connection_handler_->RemoveSession(session_id)) {
    return 0;
  }

  SessionMap::iterator it = session_map_.find(session_id);
  if (session_map_.end() == it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return 0;
  }
  heartbeat_monitor_->RemoveSession(session_id);
  session_map_.erase(session_id);

  return session_id;
}

void Connection::OnFinalMessageCallback(uint8_t session_id) {
  SDL_LOG_AUTO_TRACE();

  sync_primitives::AutoLock lock(session_map_lock_);

  SessionMap::iterator it = session_map_.find(session_id);
  if (session_map_.end() == it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return;
  }

  it->second.set_final_message_sent(true);
}

bool Connection::AddNewService(uint8_t session_id,
                               protocol_handler::ServiceType service_type,
                               const bool request_protection,
                               transport_manager::ConnectionUID connection_id,
                               std::string* err_reason) {
  // Ignore wrong services
  if (protocol_handler::kControl == service_type ||
      protocol_handler::kInvalidServiceType == service_type) {
    SDL_LOG_WARN("Wrong service " << static_cast<int>(service_type));
    if (err_reason) {
      *err_reason = "Wrong service type " + std::to_string(service_type);
    }
    return false;
  }

  SDL_LOG_DEBUG("Add service " << service_type << " for session "
                               << static_cast<uint32_t>(session_id)
                               << " using connection ID "
                               << static_cast<uint32_t>(connection_id));
  sync_primitives::AutoLock lock(session_map_lock_);

  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_it == session_map_.end()) {
    SDL_LOG_WARN("Session not found in this connection!");
    if (err_reason) {
      *err_reason = "Session " + std::to_string(session_id) +
                    " not found for connection " +
                    std::to_string(connection_id);
    }
    return false;
  }
  Session& session = session_it->second;

  if (session.protocol_version <= protocol_handler::PROTOCOL_VERSION_2 &&
      helpers::
          Compare<protocol_handler::ServiceType, helpers::EQ, helpers::ONE>(
              service_type,
              protocol_handler::ServiceType::kAudio,
              protocol_handler::ServiceType::kMobileNav)) {
    SDL_LOG_WARN(
        "Audio and video services are disallowed for protocol version "
        "2 or lower");
    return false;
  }

  Service* service = session.FindService(service_type);
  // if service already exists
  if (service) {
#ifdef ENABLE_SECURITY
    if (!request_protection) {
      SDL_LOG_WARN("Session " << static_cast<int>(session_id)
                              << " already has unprotected service "
                              << static_cast<int>(service_type));
      if (err_reason) {
        *err_reason = "Session " + std::to_string(session_id) +
                      " already has an unprotected service of type " +
                      std::to_string(service_type);
      }
      return false;
    }
    if (service->is_protected_) {
      SDL_LOG_WARN("Session " << static_cast<int>(session_id)
                              << " already has protected service "
                              << static_cast<int>(service_type));
      if (err_reason) {
        *err_reason = "Session " + std::to_string(session_id) +
                      " already has a protected service of type " +
                      std::to_string(service_type);
      }
      return false;
    }
    // For unproteced service could be start protection
    return true;
#else
    // Service already exists
    return false;
#endif  // ENABLE_SECURITY
  }
  // id service is not exists
  session.service_list.push_back(Service(service_type, connection_id));
  return true;
}

inline bool is_incorrect_for_remove_service(
    const protocol_handler::ServiceType service_type) {
  return
      // Control type is internal part of session
      protocol_handler::kControl == service_type ||
      // RPC and bulk service is necessary part of session
      protocol_handler::kRpc == service_type ||
      protocol_handler::kBulk == service_type ||
      // Invalid service is not part of session
      protocol_handler::kInvalidServiceType == service_type;
}

bool Connection::RemoveService(uint8_t session_id,
                               protocol_handler::ServiceType service_type) {
  // Ignore wrong and required for Session services
  if (is_incorrect_for_remove_service(service_type)) {
    SDL_LOG_WARN("Could not remove service " << static_cast<int>(service_type));
    return false;
  }
  sync_primitives::AutoLock lock(session_map_lock_);

  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return false;
  }

  ServiceList& service_list = session_it->second.service_list;
  ServiceList::iterator service_it =
      find(service_list.begin(), service_list.end(), service_type);
  if (service_list.end() == service_it) {
    SDL_LOG_WARN("Session " << session_id
                            << " didn't established"
                               " service "
                            << service_type);
    return false;
  }
  service_list.erase(service_it);
  return true;
}

uint8_t Connection::RemoveSecondaryServices(
    transport_manager::ConnectionUID secondary_connection_handle,
    std::list<protocol_handler::ServiceType>& removed_services_list) {
  SDL_LOG_AUTO_TRACE();

  uint8_t found_session_id = 0;
  sync_primitives::AutoLock lock(session_map_lock_);

  SDL_LOG_INFO("RemoveSecondaryServices looking for services on Connection ID "
               << static_cast<int>(secondary_connection_handle));

  // Walk the SessionMap in the primary connection, and for each
  // Session, we walk its ServiceList, looking for all the services
  // that were running on the now-closed Secondary Connection.
  for (SessionMap::iterator session_it = session_map_.begin();
       session_map_.end() != session_it;
       ++session_it) {
    SDL_LOG_INFO("RemoveSecondaryServices found session ID "
                 << static_cast<int>(session_it->first));

    // Now, for each session, walk the its ServiceList, looking for services
    // that were using secondary)_connection_handle. If we find such a service,
    // set session_found and break out of the outer loop.
    ServiceList& service_list = session_it->second.service_list;
    ServiceList::iterator service_it = service_list.begin();
    for (; service_it != service_list.end();) {
      SDL_LOG_INFO("RemoveSecondaryServices found service ID "
                   << static_cast<int>(service_it->service_type));
      if (service_it->connection_id == secondary_connection_handle) {
        found_session_id = session_it->first;

        SDL_LOG_INFO("RemoveSecondaryServices removing Service "
                     << static_cast<int>(service_it->service_type)
                     << " in session " << static_cast<int>(found_session_id));

        removed_services_list.push_back(service_it->service_type);
        service_it = service_list.erase(service_it);
      } else {
        ++service_it;
      }
    }

    // If we found a session that had services running on the secondary
    // connection, we're done.
    if (found_session_id != 0) {
      break;
    }
  }

  return found_session_id;
}

#ifdef ENABLE_SECURITY
int Connection::SetSSLContext(uint8_t session_id,
                              security_manager::SSLContext* context) {
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_it == session_map_.end()) {
    SDL_LOG_WARN("Session not found in this connection!");
    return security_manager::SecurityManager::ERROR_INTERNAL;
  }
  Session& session = session_it->second;
  session.ssl_context = context;
  return security_manager::SecurityManager::ERROR_SUCCESS;
}

security_manager::SSLContext* Connection::GetSSLContext(
    const uint8_t session_id,
    const protocol_handler::ServiceType& service_type) const {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::const_iterator session_it = session_map_.find(session_id);
  if (session_it == session_map_.end()) {
    SDL_LOG_WARN("Session not found in this connection!");
    return NULL;
  }
  const Session& session = session_it->second;
  // for control services return current SSLContext value
  if (protocol_handler::kControl == service_type)
    return session.ssl_context;
  const Service* service = session.FindService(service_type);
  if (!service) {
    SDL_LOG_WARN("Service not found in this session!");
    return NULL;
  }
  if (!service->is_protected_)
    return NULL;
  SDL_LOG_TRACE("SSLContext is " << session.ssl_context);
  return session.ssl_context;
}

void Connection::SetProtectionFlag(
    const uint8_t session_id,
    const protocol_handler::ServiceType& service_type) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_it == session_map_.end()) {
    SDL_LOG_WARN("Session not found in this connection!");
    return;
  }
  Session& session = session_it->second;
  Service* service = session.FindService(service_type);
  if (!service) {
    SDL_LOG_WARN("Service not found in this session!");
    return;
  }
  service->is_protected_ = true;
  // Rpc and bulk shall be protected as one service
  if (service->service_type == protocol_handler::kRpc) {
    Service* service_bulk = session.FindService(protocol_handler::kBulk);
    DCHECK(service_bulk);
    service_bulk->is_protected_ = true;
  } else if (service->service_type == protocol_handler::kBulk) {
    Service* service_rpc = session.FindService(protocol_handler::kRpc);
    DCHECK(service_rpc);
    service_rpc->is_protected_ = true;
  }
}

#endif  // ENABLE_SECURITY

bool Connection::SessionServiceExists(
    const uint8_t session_id,
    const protocol_handler::ServiceType& service_type) const {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);

  SessionMap::const_iterator session_it = session_map_.find(session_id);
  if (session_it == session_map_.end()) {
    SDL_LOG_WARN("Session not found in this connection!");
    return false;
  }

  const Session& session = session_it->second;
  return session.FindService(service_type);
}

ConnectionHandle Connection::connection_handle() const {
  return connection_handle_;
}

DeviceHandle Connection::connection_device_handle() const {
  return connection_device_handle_;
}

const SessionMap Connection::session_map() const {
  sync_primitives::AutoLock lock(session_map_lock_);
  return session_map_;
}

void Connection::CloseSession(uint8_t session_id) {
  {
    sync_primitives::AutoLock lock(session_map_lock_);

    SessionMap::iterator session_it = session_map_.find(session_id);
    if (session_it == session_map_.end()) {
      return;
    }
  }

  connection_handler_->CloseSession(
      connection_handle_, session_id, connection_handler::kCommon);
}

void Connection::UpdateProtocolVersionSession(uint8_t session_id,
                                              uint8_t protocol_version) {
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return;
  }
  Session& session = session_it->second;
  session.protocol_version = protocol_version;
  if (session.full_protocol_version.major_version_ !=
      session.protocol_version) {
    session.full_protocol_version =
        utils::SemanticVersion(protocol_version, 0, 0);
  }
}

void Connection::UpdateProtocolVersionSession(
    uint8_t session_id, const utils::SemanticVersion& full_protocol_version) {
  SDL_LOG_AUTO_TRACE();

  if (!full_protocol_version.isValid()) {
    SDL_LOG_WARN("Invalid version: " << full_protocol_version.toString());
    return;
  }

  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return;
  }

  Session& session = session_it->second;
  session.protocol_version =
      static_cast<uint8_t>(full_protocol_version.major_version_);
  session.full_protocol_version = full_protocol_version;
}

bool Connection::SupportHeartBeat(uint8_t session_id) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return false;
  }
  Session& session = session_it->second;
  return (
      (session.protocol_version >= ::protocol_handler::PROTOCOL_VERSION_3) &&
      (0 != heartbeat_timeout_));
}

bool Connection::ProtocolVersion(uint8_t session_id,
                                 uint8_t& protocol_version) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return false;
  }
  protocol_version = (session_it->second).protocol_version;
  return true;
}

bool Connection::ProtocolVersion(
    uint8_t session_id, utils::SemanticVersion& full_protocol_version) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock lock(session_map_lock_);
  SessionMap::iterator session_it = session_map_.find(session_id);
  if (session_map_.end() == session_it) {
    SDL_LOG_WARN("Session not found in this connection!");
    return false;
  }
  full_protocol_version = (session_it->second).full_protocol_version;
  return true;
}

ConnectionHandle Connection::primary_connection_handle() const {
  return primary_connection_handle_;
}

void Connection::SetPrimaryConnectionHandle(
    ConnectionHandle primary_connection_handle) {
  primary_connection_handle_ = primary_connection_handle;
}

void Connection::StartHeartBeat(uint8_t session_id) {
  heartbeat_monitor_->AddSession(session_id);
}

void Connection::SendHeartBeat(uint8_t session_id) {
  connection_handler_->SendHeartBeat(connection_handle_, session_id);
}

void Connection::KeepAlive(uint8_t session_id) {
  heartbeat_monitor_->KeepAlive(session_id);
}

void Connection::SetHeartBeatTimeout(uint32_t timeout, uint8_t session_id) {
  heartbeat_monitor_->set_heartbeat_timeout_milliseconds(timeout, session_id);
}

}  // namespace connection_handler