-
Notifications
You must be signed in to change notification settings - Fork 34
/
telemetry.c
266 lines (234 loc) · 7.8 KB
/
telemetry.c
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
/*
* IoT Hub Raspberry Pi C - Microsoft Sample Code - Copyright (c) 2017 - Licensed MIT
*/
#include "./telemetry.h"
static struct utsname platform;
static const char *PATH = "https://dc.services.visualstudio.com/v2/track";
static const char *IKEY = "0823bae8-a3b8-4fd5-80e5-f7272a2377a9";
static const char *EVENT = "AIEVENT";
static const char *LANGUAGE = "C";
static const char *DEVICE = "RaspberryPi";
static const char *MCU = "STM32F412";
static const char *MAC_TEMPLATE = "%02X-%02X-%02X-%02X-%02X-%02X";
static const int HASH_LEN = 65;
static const char *BODY_TEMPLATE_FULL =
"{"
"\"data\": {"
"\"baseType\": \"EventData\","
"\"baseData\": {"
"\"properties\": {"
"\"language\": \"%s\","
"\"device\": \"%s\","
"\"mcu\": \"%s\","
"\"message\": \"%s\","
"\"mac\": \"%s\","
"\"iothub\": \"%s\","
"\"osType\": \"%s\","
"\"osPlatform\": \"%s\","
"\"osRelease\": \"%s\""
"},"
"\"name\": \"%s\""
"}"
"},"
"\"time\": \"%s\","
"\"name\": \"%s\","
"\"iKey\": \"%s\""
"}";
static const char *BODY_TEMPLATE_MIN =
"{"
"\"data\": {"
"\"baseType\": \"EventData\","
"\"baseData\": {"
"\"properties\": {"
"\"language\": \"C\","
"\"device\": \"RaspberryPi\""
"},"
"\"name\": \"%s\""
"}"
"},"
"\"time\": \"%s\","
"\"name\": \"%s\","
"\"iKey\": \"%s\","
"\"tags\": {"
"\"ai.location.ip\": \"0.0.0.0\""
"}"
"}";
static const char *PROMPT_TEXT = "\nMicrosoft would like to collect data about how users use Azure IoT " \
"samples and some problems they encounter. Microsoft uses this information to improve "\
"our tooling experience. Participation is voluntary and when you choose to participate " \
"your device automatically sends information to Microsoft about how you use Azure IoT "\
"samples. If you want to change this setting after first time, please delete the "\
"telemetry.config file and restart the program. "\
"\n\nSelect y to enable data collection (y/n, default is y). ";
static int enabled = 0;
void initial_telemetry()
{
FILE *file;
file = fopen("telemetry.config", "r");
if (file != NULL)
{
char config = (char)fgetc(file);
enabled = config == 'y';
fclose(file);
}
else
{
printf("%s", PROMPT_TEXT);
char answer = getchar();
file = fopen("telemetry.config", "w");
if (answer == 'n')
{
fputc('n', file);
send_telemetry_data_without_sensitive_info("no");
enabled = 0;
}
else
{
fputc('y', file);
send_telemetry_data_without_sensitive_info("yes");
enabled = 1;
}
fclose(file);
}
}
void get_mac_address_hash(char outputBuffer[], int len)
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
snprintf(s.ifr_name, sizeof(s.ifr_name), "eth0");
if (0 == ioctl(fd, SIOCGIFHWADDR, &s))
{
char mac[18];
snprintf(mac, sizeof(mac), MAC_TEMPLATE, (unsigned char)s.ifr_addr.sa_data[0],
(unsigned char)s.ifr_addr.sa_data[1], (unsigned char)s.ifr_addr.sa_data[2],
(unsigned char)s.ifr_addr.sa_data[3], (unsigned char)s.ifr_addr.sa_data[4],
(unsigned char)s.ifr_addr.sa_data[5]);
mac[17] = '\n';
sha256(mac, outputBuffer, len);
}
close(fd);
}
void sha256(const char *str, char outputBuffer[], int len)
{
if (str == NULL)
{
outputBuffer[0] = '\0';
return;
}
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str, strlen(str));
SHA256_Final(hash, &sha256);
int i = 0;
for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
snprintf(outputBuffer + (i * 2), len, "%02x", hash[i]);
}
outputBuffer[HASH_LEN - 1] = 0;
}
void exec_command(char command [], char result [])
{
FILE * pipe = popen(command, "r");
if (pipe == NULL)
{
result = "unknown";
return;
}
fgets(result, LINE_BUFSIZE, pipe);
if (strlen(result) == 0)
{
result = "unknown";
}
}
int substr_count(const char * str, const char * substr)
{
const char *tmp = str;
int count = 0;
while (tmp = strstr(tmp, substr))
{
count++;
tmp += strlen(substr);
}
return count;
}
void send_telemetry_data_without_sensitive_info(const char *event)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, PATH);
time_t t = time(NULL);
char *cur_time = ctime(&t);
int tlen = strlen(cur_time) - 1;
cur_time[tlen] = 0;
int str_placeholder_count = substr_count(BODY_TEMPLATE_MIN, "%s");
int size = strlen(BODY_TEMPLATE_MIN) + strlen(IKEY) - strlen("%s") * str_placeholder_count
+ strlen(EVENT) + strlen(event) + tlen + 1;
char *data = (char *)malloc(size * sizeof(char));
if (data != NULL)
{
snprintf(data, size, BODY_TEMPLATE_MIN, event, cur_time, EVENT, IKEY);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
// hide curl output
FILE *devnull = fopen("/dev/null", "w+");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
res = curl_easy_perform(curl);
fclose(devnull);
curl_easy_cleanup(curl);
free(data);
}
}
}
void send_telemetry_data(const char *iotHubName, const char *event, const char *message)
{
if (!enabled)
{
return;
}
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, PATH);
char hash_mac[HASH_LEN];
unsigned char hash_iothub_name[HASH_LEN];
get_mac_address_hash(hash_mac, sizeof(hash_mac));
sha256(iotHubName, hash_iothub_name, sizeof(hash_iothub_name));
time_t t = time(NULL);
char *cur_time = ctime(&t);
int tlen = strlen(cur_time) - 1;
cur_time[tlen] = 0;
uname(&platform);
char os_release[LINE_BUFSIZE];
char os_platform[LINE_BUFSIZE];
const char prefix[] = "ID_LIKE=";
exec_command("/usr/bin/lsb_release -r| grep -oP \'\\d\\.\\d\' | tr -d \'\\r\\n\'", os_release);
exec_command("cat /etc/os-release | grep -oP \'^ID_LIKE=.*$\' | tr -d \'\\r\\n\'", os_platform);
int str_placeholder_count = substr_count(BODY_TEMPLATE_FULL, "%s");
int platform_str_offset = strlen(prefix);
int size = strlen(BODY_TEMPLATE_FULL) + strlen(LANGUAGE) + strlen(DEVICE) + strlen(MCU) + strlen(EVENT)
+ strlen(IKEY) - strlen("%s") * str_placeholder_count + strlen(hash_iothub_name)
+ strlen(hash_mac) + strlen(platform.sysname) + strlen(os_platform + platform_str_offset)
+ strlen(os_release) + strlen(message) + strlen(event) + tlen + 1;
char *data = (char *)malloc(size * sizeof(char));
if (data != NULL)
{
snprintf(data, size, BODY_TEMPLATE_FULL, LANGUAGE, DEVICE, MCU, message, hash_mac, hash_iothub_name,
platform.sysname, os_platform + platform_str_offset, os_release, event, cur_time, EVENT, IKEY);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
// hide curl output
FILE *devnull = fopen("/dev/null", "w+");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
res = curl_easy_perform(curl);
fclose(devnull);
curl_easy_cleanup(curl);
free(data);
}
}
}