• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 25520917686

07 May 2026 08:41PM UTC coverage: 56.793% (-0.001%) from 56.794%
25520917686

push

github

web-flow
chore: format #2106 (#2108)

39 of 67 new or added lines in 4 files covered. (58.21%)

10 existing lines in 5 files now uncovered.

12917 of 22744 relevant lines covered (56.79%)

302090.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.03
/trick_source/sim_services/VariableServer/VariableServer.cpp
1

2
#include "trick/VariableServer.hh"
3

4
#include "trick/Message_proto.hh"
5
#include "trick/message_proto.h"
6
#include "trick/tc_proto.h"
7

8
#include <iostream>
9
#include <netdb.h>
10

11
Trick::VariableServer * the_vs ;
12

13
Trick::VariableServer::VariableServer()
216✔
14
    : enabled(false)
15
    , info_msg(false)
16
    , log(false)
17
    , allow_connections(false)
18
    , bypass_ip_check(false)
216✔
19
{
20
    the_vs = this ;
216✔
21
    pthread_mutex_init(&map_mutex, NULL);
216✔
22
}
216✔
23

24
Trick::VariableServer::~VariableServer() {
216✔
25
    the_vs = NULL;
216✔
26
}
216✔
27

28
void Trick::VariableServer::shutdownConnections() {
187✔
29
    listen_thread.shutdownConnections();
187✔
30
}
187✔
31

32
std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServer& vs) {
1✔
33
    std::map < pthread_t , VariableServerSessionThread * >::iterator it ;
1✔
34

35
    s << "{\"variable_server_connections\":[\n";
1✔
36
    int count = 0;
1✔
37
    int n_connections = (int)vs.var_server_threads.size();
1✔
38
    for ( it = vs.var_server_threads.begin() ; it != vs.var_server_threads.end() ; ++it ) {
1✔
39
        s << "{\n";
×
40
        s << *(*it).second;
×
41
        s << "}";
×
42
        if ((n_connections-count)>1) {
×
43
            s << "," ;
×
44
        }
45
        s << "\n";
×
46
        count ++;
×
47
    }
48
    s << "]}" << std::endl;
1✔
49
    return s;
1✔
50
}
51

52
bool Trick::VariableServer::get_enabled() {
2✔
53
    return enabled ;
2✔
54
}
55

56
void Trick::VariableServer::set_enabled(bool on_off) {
27✔
57
    if (!enabled && on_off)
27✔
58
    {
59
        message_publish(MSG_INFO,
27✔
60
            "Trick VariableServer: Enabling the Variable Server. See Trick documentation for associated security "
61
            "concerns.\n");
62
    }
63

64
    enabled = on_off ;
27✔
65
}
27✔
66

67
bool Trick::VariableServer::get_info_msg() {
15✔
68
    return info_msg ;
15✔
69
}
70

71
void Trick::VariableServer::set_var_server_info_msg_on() {
1✔
72
    info_msg = true;
1✔
73
    for ( auto& session_it : var_server_sessions ) {
1✔
74
        session_it.second->set_info_message(info_msg);
×
75
    }
76
}
1✔
77

78
void Trick::VariableServer::set_var_server_info_msg_off() {
1✔
79
    info_msg = false;
1✔
80
    for ( auto& session_it : var_server_sessions ) {
1✔
81
        session_it.second->set_info_message(info_msg);
×
82
    }
83
}
1✔
84

85
bool Trick::VariableServer::get_log() {
14✔
86
    return log ;
14✔
87
}
88

89
bool Trick::VariableServer::get_session_log() {
14✔
90
    return session_log ;
14✔
91
}
92

93
void Trick::VariableServer::set_var_server_log_on() {
2✔
94
    log = true;
2✔
95
    // turn log on for all current vs clients
96
    for ( auto& session_it : var_server_sessions ) {
3✔
97
        session_it.second->set_log(true);
1✔
98
    }
99
}
2✔
100

101
void Trick::VariableServer::set_var_server_log_off() {
1✔
102
    log = false;
1✔
103
    // turn log off for all current vs clients
104
    for ( auto& session_it : var_server_sessions ) {
2✔
105
        session_it.second->set_log(false);
1✔
106
    }
107
}
1✔
108

109
void Trick::VariableServer::set_var_server_session_log_on() {
1✔
110
    session_log = true;
1✔
111
    // turn log on for all current vs clients
112
    for ( auto& session_it : var_server_sessions ) {
2✔
113
        session_it.second->set_session_log(true);
1✔
114
    }
115
}
1✔
116

117
void Trick::VariableServer::set_var_server_session_log_off() {
1✔
118
    session_log = false;
1✔
119
    // turn log off for all current vs clients
120
    for ( auto& session_it : var_server_sessions ) {
2✔
121
        session_it.second->set_session_log(false);
1✔
122
    }
123
}
1✔
124

125
const char * Trick::VariableServer::get_hostname() {
×
126
    return listen_thread.get_hostname();
×
127
}
128

129
Trick::VariableServerListenThread & Trick::VariableServer::get_listen_thread() {
190✔
130
    return listen_thread ;
190✔
131
}
132

133
void Trick::VariableServer::add_vst(pthread_t in_thread_id, VariableServerSessionThread * in_vst) {
14✔
134
    pthread_mutex_lock(&map_mutex) ;
14✔
135
    var_server_threads[in_thread_id] = in_vst ;
14✔
136
    pthread_mutex_unlock(&map_mutex) ;
14✔
137
}
14✔
138

139
void Trick::VariableServer::add_session(pthread_t in_thread_id, VariableServerSession * in_session) {
17✔
140
    pthread_mutex_lock(&map_mutex) ;
17✔
141
    var_server_sessions[in_thread_id] = in_session ;
17✔
142
    pthread_mutex_unlock(&map_mutex) ;
17✔
143
}
17✔
144

145
Trick::VariableServerSessionThread * Trick::VariableServer::get_vst(pthread_t thread_id) {
11✔
146
    std::map < pthread_t , Trick::VariableServerSessionThread * >::iterator it ;
11✔
147
    Trick::VariableServerSessionThread * ret = NULL ;
11✔
148
    pthread_mutex_lock(&map_mutex) ;
11✔
149
    it = var_server_threads.find(thread_id) ;
11✔
150
    if ( it != var_server_threads.end() ) {
11✔
151
        ret = (*it).second ;
5✔
152
    }
153
    pthread_mutex_unlock(&map_mutex) ;
11✔
154
    return ret ;
11✔
155
}
156

157
Trick::VariableServerSession * Trick::VariableServer::get_session(pthread_t thread_id) {
1,076✔
158
    Trick::VariableServerSession * ret = NULL ;
1,076✔
159
    pthread_mutex_lock(&map_mutex) ;
1,076✔
160
    auto it = var_server_sessions.find(thread_id) ;
1,076✔
161
    if ( it != var_server_sessions.end() ) {
1,076✔
162
        ret = (*it).second ;
38✔
163
    }
164
    pthread_mutex_unlock(&map_mutex) ;
1,076✔
165
    return ret ;
1,076✔
166
}
167

168
void Trick::VariableServer::delete_vst(pthread_t thread_id) {
14✔
169
    pthread_mutex_lock(&map_mutex) ;
14✔
170
    var_server_threads.erase(thread_id) ;
14✔
171
    pthread_mutex_unlock(&map_mutex) ;
14✔
172
}
14✔
173

174
void Trick::VariableServer::delete_session(pthread_t thread_id) {
12✔
175
    pthread_mutex_lock(&map_mutex) ;
12✔
176
    var_server_sessions.erase(thread_id) ;
12✔
177
    pthread_mutex_unlock(&map_mutex) ;
12✔
178
}
12✔
179

180
void Trick::VariableServer::set_copy_data_job( Trick::JobData * in_job ) {
187✔
181
    copy_data_job = in_job ;
187✔
182
}
187✔
183

184
void Trick::VariableServer::set_copy_and_write_freeze_job( Trick::JobData * in_job ) {
187✔
185
    copy_and_write_freeze_job = in_job ;
187✔
186
}
187✔
187

188
bool Trick::VariableServer::set_allow_connections(const bool& b)
223✔
189
{
190
    if (b)
223✔
191
    {
192
        message_publish(MSG_WARNING, "Trick VariableServer: Enabling Variable Server Connectivity\n");
36✔
193
        add_ip("127.0.0.1");
36✔
194
    }
195

196
    return allow_connections = b;
223✔
197
}
198

199
bool Trick::VariableServer::get_allow_connections() { return allow_connections; }
8✔
200

201
bool Trick::VariableServer::set_bypass_ip_check(const bool& b)
30✔
202
{
203
    if (b)
30✔
204
    {
205
        message_publish(MSG_WARNING,
30✔
206
            "Trick VariableServer:\nBYPASSING IP SECURITY CHECK.\nANYONE ON THE NETWORK CAN CONNECT TO YOUR PYTHON "
207
            "INTERPRETER!\nYOU BETTER KNOW WHAT YOU'RE DOING!\n");
208
    }
209

210
    return bypass_ip_check = b;
30✔
211
}
212

213
bool Trick::VariableServer::get_bypass_ip_check() { return bypass_ip_check; }
14✔
214

NEW
215
const std::set<std::string>& Trick::VariableServer::get_ip_allowlist() { return ip_allowlist; }
×
216

217
void Trick::VariableServer::add_ip(const std::string& ip)
42✔
218
{
219
    std::string msg = "Trick VariableServer: Adding " + ip + " to the allowlist.\n";
84✔
220
    message_publish(MSG_INFO, msg.c_str());
42✔
221

222
    ip_allowlist.insert(ip);
42✔
223
}
42✔
224

NEW
225
void Trick::VariableServer::remove_ip(const std::string& ip)
×
226
{
NEW
227
    std::string msg = "Trick VariableServer: Removing " + ip + " from the allowlist.\n";
×
NEW
228
    message_publish(MSG_INFO, msg.c_str());
×
229

NEW
230
    ip_allowlist.erase(ip);
×
231
}
×
232

NEW
233
bool Trick::VariableServer::check_ip(const std::string& ip)
×
234
{
235
    bool valid = false;
×
NEW
236
    for (std::set<std::string>::iterator it = ip_allowlist.begin(); it != ip_allowlist.end(); ++it)
×
237
    {
UNCOV
238
        std::string entry = *it;
×
239
        size_t slash;
240

241
        struct in_addr addr;
NEW
242
        if (inet_pton(AF_INET, ip.c_str(), &addr) != 1)
×
243
        {
NEW
244
            return false;
×
245
        }
246
        uint32_t ipVal = ntohl(addr.s_addr);
×
247

NEW
248
        if ((slash = entry.find('/')) != std::string::npos)
×
249
        {
250
            std::string network = entry.substr(0, slash);
×
NEW
251
            int prefix          = std::stoi(entry.substr(slash + 1));
×
252

NEW
253
            if (inet_pton(AF_INET, network.c_str(), &addr) != 1)
×
254
            {
UNCOV
255
                continue;
×
256
            }
257
            uint32_t netVal = ntohl(addr.s_addr);
×
258

259
            uint32_t mask = (prefix == 0) ? 0 : (~0u << (32 - prefix));
×
260

261
            valid = (ipVal & mask) == (netVal & mask);
×
262
        }
263
        else
264
        {
UNCOV
265
            valid = (ip == entry);
×
266
        }
267

NEW
268
        if (valid)
×
NEW
269
            break;
×
270
    }
271

272
    return valid;
×
273
}
274

275
void Trick::VariableServer::resolve_hostname()
6✔
276
{
277
    if (enabled)
6✔
278
    {
279
        // Add your network IPs to the allowlist
280
        char hostname[256];
281
        if (gethostname(hostname, sizeof(hostname)) != 0)
6✔
282
        {
UNCOV
283
            return;
×
284
        }
285

286
        struct addrinfo hints { }, *res, *p;
6✔
287
        hints.ai_family   = AF_INET;
6✔
288
        hints.ai_socktype = SOCK_STREAM;
6✔
289

290
        int status = getaddrinfo(hostname, nullptr, &hints, &res);
6✔
291
        if (status != 0)
6✔
292
        {
UNCOV
293
            return;
×
294
        }
295

296
        char ipstr[INET6_ADDRSTRLEN];
297
        for (p = res; p != nullptr; p = p->ai_next)
12✔
298
        {
299
            if (p->ai_family == AF_INET)
6✔
300
            {
301
                struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
6✔
302
                inet_ntop(p->ai_family, &(ipv4->sin_addr), ipstr, sizeof(ipstr));
6✔
303
                add_ip(ipstr);
6✔
304
            }
305
        }
306

307
        freeaddrinfo(res);
6✔
308
    }
309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc