Skip to content

Commit f63f8fd

Browse files
Add compatibility mode with Device Client 1.9 and Greengrass Secure Tunneling Component (#140)
* fix compatibility with Device Client 1.9
1 parent d3150e0 commit f63f8fd

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

src/LocalproxyConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ namespace aws {
100100
* The end point will store either source listening or destination service depends on the mode of local proxy.
101101
*/
102102
std::unordered_map<std::string, std::string> serviceId_to_endpoint_map;
103+
103104
/**
104105
* A flag to judge if v2 local proxy needs to fallback to communicate using v1 local proxy message format.
105106
* v1 local proxy format fallback will be enabled when a tunnel is opened with no or 1 service id.

src/TcpAdapterProxy.cpp

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,12 @@ namespace aws { namespace iot { namespace securedtunneling {
253253
{
254254
if (tac.serviceId_to_tcp_server_map.find(service_id) == tac.serviceId_to_tcp_server_map.end())
255255
{
256-
BOOST_LOG_SEV(log, debug) << "No serviceId_to_tcp_server mapping for service_id: " << service_id;
257-
return connection_ptr;
256+
if (tac.serviceId_to_tcp_server_map.find(tac.adapter_config.serviceId_to_endpoint_map.cbegin()->first) == tac.serviceId_to_tcp_server_map.end())
257+
{
258+
BOOST_LOG_SEV(log, debug) << "No serviceId_to_tcp_server mapping for service_id: " << service_id;
259+
return connection_ptr;
260+
}
261+
service_id = tac.adapter_config.serviceId_to_endpoint_map.cbegin()->first;;
258262
}
259263
tcp_server::pointer server = tac.serviceId_to_tcp_server_map[service_id];
260264
BOOST_LOG_SEV(log, trace) << "num active connections for service id " << service_id << ": " << server->connectionId_to_tcp_connection_map.size();
@@ -376,6 +380,7 @@ namespace aws { namespace iot { namespace securedtunneling {
376380
BOOST_LOG_SEV(this->log, trace) << "Post-reset TCP drain complete. Closing TCP socket for service id " << service_id << " connection id " << connection_id;
377381
BOOST_LOG_SEV(this->log, info) << "Disconnected from: " << connection_to_reset->socket().remote_endpoint();
378382
connection_to_reset->socket_.close();
383+
delete_tcp_socket(tac, service_id, connection_id);
379384
*tcp_write_buffer_drain_complete = true;
380385
if (*web_socket_write_buffer_drain_complete)
381386
{
@@ -515,8 +520,9 @@ namespace aws { namespace iot { namespace securedtunneling {
515520
tcp_connection::pointer socket_connection = get_tcp_connection(tac, service_id, connection_id);
516521

517522
// if simultaneous connections are not enabled, then send a stream reset
518-
if (tac.adapter_config.is_v2_message_format)
523+
if (tac.adapter_config.is_v2_message_format || tac.adapter_config.is_v1_message_format)
519524
{
525+
BOOST_LOG_SEV(log, info) << "simultaneous connections are not enabled, sending stream reset";
520526
socket_connection->after_send_message = std::bind(&tcp_adapter_proxy::setup_tcp_socket, this, std::ref(tac), service_id);
521527
tac.serviceId_to_control_message_handler_map[service_id] = std::bind(&tcp_adapter_proxy::ignore_message_and_stop, this, std::ref(tac), std::placeholders::_1);
522528
async_send_stream_reset(tac, service_id, connection_id);
@@ -600,8 +606,16 @@ namespace aws { namespace iot { namespace securedtunneling {
600606

601607
BOOST_LOG_SEV(log, debug) << "Sending stream start, setting new stream ID to: " << new_stream_id << ", service id: " << service_id;
602608

609+
if (tac.adapter_config.is_v1_message_format)
610+
{
611+
outgoing_message.set_serviceid("");
612+
}
613+
else
614+
{
615+
outgoing_message.set_serviceid(service_id);
616+
}
617+
603618
outgoing_message.set_type(Message_Type_STREAM_START);
604-
outgoing_message.set_serviceid(service_id);
605619
outgoing_message.set_streamid(new_stream_id);
606620
outgoing_message.set_connectionid(connection_id);
607621
outgoing_message.set_ignorable(false);
@@ -638,8 +652,16 @@ namespace aws { namespace iot { namespace securedtunneling {
638652
}
639653
std::int32_t stream_id = tac.serviceId_to_streamId_map[service_id];
640654

655+
if (tac.adapter_config.is_v1_message_format)
656+
{
657+
outgoing_message.set_serviceid("");
658+
}
659+
else
660+
{
661+
outgoing_message.set_serviceid(service_id);
662+
}
663+
641664
outgoing_message.set_type(Message_Type_CONNECTION_START);
642-
outgoing_message.set_serviceid(service_id);
643665
outgoing_message.set_streamid(stream_id);
644666
outgoing_message.set_connectionid(connection_id);
645667
outgoing_message.set_ignorable(false);
@@ -657,12 +679,20 @@ namespace aws { namespace iot { namespace securedtunneling {
657679
return;
658680
}
659681

682+
if (tac.adapter_config.is_v1_message_format)
683+
{
684+
outgoing_message.set_serviceid("");
685+
}
686+
else
687+
{
688+
outgoing_message.set_serviceid(service_id);
689+
}
690+
660691
// NOTE: serviceIds -> streamId mapping will be updated when send/receive stream start, no action needed now.
661692
std::int32_t stream_id = tac.serviceId_to_streamId_map[service_id];
662693
outgoing_message.set_type(Message_Type_STREAM_RESET);
663-
outgoing_message.set_serviceid(service_id);
664694
outgoing_message.set_streamid(stream_id);
665-
outgoing_message.set_connectionid(0);
695+
outgoing_message.set_connectionid(connection_id);
666696
outgoing_message.set_ignorable(false);
667697
outgoing_message.clear_payload();
668698
async_send_message(tac, outgoing_message, service_id, connection_id);
@@ -677,10 +707,19 @@ namespace aws { namespace iot { namespace securedtunneling {
677707
BOOST_LOG_SEV(log, warning) << "No stream id mapping found for service id " << service_id << " . Skip connection reset.";
678708
return;
679709
}
710+
711+
if (tac.adapter_config.is_v1_message_format)
712+
{
713+
outgoing_message.set_serviceid("");
714+
}
715+
else
716+
{
717+
outgoing_message.set_serviceid(service_id);
718+
}
719+
680720
// NOTE: serviceIds -> streamId mapping will be updated when send/receive stream start, no action needed now.
681721
std::int32_t stream_id = tac.serviceId_to_streamId_map[service_id];
682722
outgoing_message.set_type(Message_Type_CONNECTION_RESET);
683-
outgoing_message.set_serviceid(service_id);
684723
outgoing_message.set_streamid(stream_id);
685724
outgoing_message.set_connectionid(connection_id);
686725
outgoing_message.set_ignorable(false);
@@ -1073,7 +1112,7 @@ namespace aws { namespace iot { namespace securedtunneling {
10731112
// backward compatibility: set connection id to 1 if first received a message with no connection id (id value will be 0)
10741113
if (!connection_id)
10751114
{
1076-
connection_id = 1;
1115+
BOOST_LOG_SEV(log, info) << "reverting to v2 message format";
10771116
tac.adapter_config.is_v2_message_format = true;
10781117
}
10791118
string service_id = message.serviceid();
@@ -1305,6 +1344,7 @@ namespace aws { namespace iot { namespace securedtunneling {
13051344
// Remove empty string map and put new mapping
13061345
tac.adapter_config.serviceId_to_endpoint_map.erase("");
13071346
tac.adapter_config.serviceId_to_endpoint_map[service_id] = endpoint;
1347+
13081348
BOOST_LOG_SEV(log, info) << "Updated port mapping for v1 format: ";
13091349
for (auto m : tac.adapter_config.serviceId_to_endpoint_map)
13101350
{
@@ -1328,7 +1368,7 @@ namespace aws { namespace iot { namespace securedtunneling {
13281368
// backward compatibility: set connection id to 1 if first received a message with no connection id (id value will be 0)
13291369
if (!connection_id)
13301370
{
1331-
connection_id = 1;
1371+
BOOST_LOG_SEV(log, info) << "reverting to v2 message format";
13321372
tac.adapter_config.is_v2_message_format = true;
13331373
}
13341374
string service_id = message.serviceid();
@@ -1431,7 +1471,7 @@ namespace aws { namespace iot { namespace securedtunneling {
14311471
// backward compatibility: set connection id to 1 if first received a message with no connection id (id value will be 0)
14321472
if (!connection_id)
14331473
{
1434-
connection_id = 1;
1474+
BOOST_LOG_SEV(log, info) << "reverting to v2 message format";
14351475
tac.adapter_config.is_v2_message_format = true;
14361476
}
14371477
/**
@@ -1562,7 +1602,7 @@ namespace aws { namespace iot { namespace securedtunneling {
15621602
// backward compatibility: set connection id to 1 if first received a message with no connection id (id value will be 0)
15631603
if (!connection_id)
15641604
{
1565-
connection_id = 1;
1605+
BOOST_LOG_SEV(log, info) << "reverting to v2 message format";
15661606
tac.adapter_config.is_v2_message_format = true;
15671607
}
15681608
tcp_connection::pointer connection = get_tcp_connection(tac, service_id, connection_id);
@@ -1762,8 +1802,17 @@ namespace aws { namespace iot { namespace securedtunneling {
17621802
throw proxy_exception((boost::format("No streamId exists for the service Id %1%") % service_id).str());
17631803
}
17641804
BOOST_LOG_SEV(log, debug) << "Prepare to send data message: service id: " << service_id << " stream id: " << tac.serviceId_to_streamId_map[service_id] << " connection id: " << connection_id;
1805+
1806+
if (tac.adapter_config.is_v1_message_format)
1807+
{
1808+
outgoing_message.set_serviceid("");
1809+
}
1810+
else
1811+
{
1812+
outgoing_message.set_serviceid(service_id);
1813+
}
1814+
17651815
// Construct outgoing message
1766-
outgoing_message.set_serviceid(service_id);
17671816
outgoing_message.set_streamid(tac.serviceId_to_streamId_map[service_id]);
17681817
outgoing_message.set_connectionid(connection_id);
17691818
size_t const send_size = std::min<std::size_t>(GET_SETTING(settings, MESSAGE_MAX_PAYLOAD_SIZE),
@@ -1988,9 +2037,10 @@ namespace aws { namespace iot { namespace securedtunneling {
19882037
uint32_t new_connection_id = ++server->highest_connection_id;
19892038

19902039
// backward compatibility: set connection id to 1 if simultaneous connections is not enabled
1991-
if (tac.adapter_config.is_v2_message_format)
2040+
if (tac.adapter_config.is_v2_message_format || tac.adapter_config.is_v1_message_format)
19922041
{
1993-
new_connection_id = 1;
2042+
BOOST_LOG_SEV(log, info) << "Falling back to older protocol, setting new connection id to 0";
2043+
new_connection_id = 0;
19942044
}
19952045
BOOST_LOG_SEV(log, info) << "creating tcp connection id " << new_connection_id;
19962046

@@ -2009,7 +2059,7 @@ namespace aws { namespace iot { namespace securedtunneling {
20092059
server->connectionId_to_tcp_connection_map[new_connection_id]->socket() = std::move(new_socket);
20102060
BOOST_LOG_SEV(log, info) << "Accepted tcp connection on port " << server->connectionId_to_tcp_connection_map[new_connection_id]->socket().local_endpoint().port() << " from " << server->connectionId_to_tcp_connection_map[new_connection_id]->socket().remote_endpoint();
20112061

2012-
if (is_first_connection)
2062+
if (is_first_connection || tac.adapter_config.is_v1_message_format || tac.adapter_config.is_v2_message_format)
20132063
{
20142064
async_send_stream_start(tac, service_id, new_connection_id);
20152065
}

0 commit comments

Comments
 (0)