-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg.c
505 lines (415 loc) · 13.7 KB
/
msg.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
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
/*
* Copyright (c) 2010-2011, Red Hat, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND RED HAT, INC. DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RED HAT, INC. BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Author: Jan Friesse <[email protected]>
*/
#include <err.h>
#include <stdio.h>
#include <string.h>
#include "logging.h"
#include "msg.h"
#include "omping.h"
#include "tlv.h"
#include "util.h"
/*
* Create answer message from query message. orig_msg is pointer to buffer with query message
* with orig_msg_len length (only used bytes, not buffer size). new_msg is pointer to buffer where
* to store result message. new_msg_len is size of buffer. ttl is value of TTL option. server_tstamp
* is boolean variable and if set, server timestamp option is added to message.
*
* All options from original messages are copied without changing order. Only exceptions are Server
* Info, Multicast Prefix, Session ID, TTL and Server Timestamp, which are not copied.
*
* Returned value is size of new message or 0 on fail (mostly because msg_len
* is smaller then needed). If success, new message is always at least 1 bytes long.
*/
size_t
msg_answer_create(const char *orig_msg, size_t orig_msg_len, char *new_msg, size_t new_msg_len,
uint8_t ttl, int server_tstamp)
{
struct tlv_iterator tlv_iter;
enum tlv_opt_type opt_type;
size_t pos;
pos = 0;
new_msg[pos++] = (unsigned char)MSG_TYPE_ANSWER;
memset(&tlv_iter, 0, sizeof(tlv_iter));
tlv_iter_init(orig_msg, orig_msg_len, &tlv_iter);
while (tlv_iter_next(&tlv_iter) != -1) {
opt_type = tlv_iter_get_type(&tlv_iter);
if (opt_type != TLV_OPT_TYPE_SERVER_INFO &&
opt_type != TLV_OPT_TYPE_MCAST_PREFIX &&
opt_type != TLV_OPT_TYPE_SES_ID &&
opt_type != TLV_OPT_TYPE_TTL &&
opt_type != TLV_OPT_TYPE_SERVER_TSTAMP) {
tlv_iter_item_copy(&tlv_iter, new_msg, new_msg_len, &pos);
}
}
if (tlv_add_ttl(new_msg, new_msg_len, &pos, ttl) == -1)
goto small_buf_err;
if (server_tstamp) {
if (tlv_add_server_tstamp(new_msg, new_msg_len, &pos) == -1)
goto small_buf_err;
}
return (pos);
small_buf_err:
return (0);
}
/*
* Decode message. Decoded message is stored in msg_decoded structure.
*/
void
msg_decode(const char *msg, size_t msg_len, struct msg_decoded *decoded)
{
char debug_str[128];
struct tlv_iterator tlv_iter;
size_t pos;
uint32_t u32, u32_2;
uint16_t tlv_len;
uint16_t u16;
uint8_t u8;
memset(decoded, 0, sizeof(struct msg_decoded));
decoded->msg_type = (enum msg_type)msg[0];
DEBUG2_PRINTF("Message type %c (0x%X)", decoded->msg_type, decoded->msg_type);
tlv_iter_init(msg, msg_len, &tlv_iter);
while (tlv_iter_next(&tlv_iter) == 0) {
tlv_len = tlv_iter_get_len(&tlv_iter);
if (logging_get_verbose() >= LOGGING_LEVEL_DEBUG2) {
snprintf(debug_str, sizeof(debug_str), "%u:%s:%u:",
tlv_iter_get_type(&tlv_iter),
tlv_opt_type_to_str(tlv_iter_get_type(&tlv_iter)), tlv_len);
}
switch (tlv_iter_get_type(&tlv_iter)) {
case TLV_OPT_TYPE_VERSION:
if (tlv_len == 1) {
memcpy(&u8, tlv_iter_get_data(&tlv_iter), sizeof(u8));
decoded->version = u8;
DEBUG2_PRINTF("%s%u", debug_str, u8);
} else {
DEBUG2_PRINTF("%slen != 1", debug_str);
}
break;
case TLV_OPT_TYPE_CLIENT_ID:
if (tlv_len > 0) {
decoded->client_id_len = tlv_len;
decoded->client_id = tlv_iter_get_data(&tlv_iter);
DEBUG2_HEXDUMP(debug_str, decoded->client_id, tlv_len);
} else {
DEBUG2_PRINTF("%slen <= 0", debug_str);
}
break;
case TLV_OPT_TYPE_SEQ_NUM:
if (tlv_len == 4) {
decoded->seq_num_isset = 1;
memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
u32 = ntohl(u32);
decoded->seq_num = u32;
DEBUG2_PRINTF("%s%u", debug_str, u32);
} else {
DEBUG2_PRINTF("%slen != 4", debug_str);
}
break;
case TLV_OPT_TYPE_CLIENT_TSTAMP:
if (tlv_len == 8) {
memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
u32 = ntohl(u32);
decoded->client_tstamp.tv_sec = u32;
memcpy(&u32_2, tlv_iter_get_data(&tlv_iter) + sizeof(u32),
sizeof(u32_2));
u32_2 = ntohl(u32_2);
decoded->client_tstamp.tv_usec = u32_2;
decoded->client_tstamp_isset = 1;
DEBUG2_PRINTF("%s(%u,%u)", debug_str, u32, u32_2);
} else {
DEBUG2_PRINTF("%slen != 8", debug_str);
}
break;
case TLV_OPT_TYPE_MCAST_GRP:
if (tlv_len > 2) {
memcpy(&u16, tlv_iter_get_data(&tlv_iter), sizeof(u16));
u16 = ntohs(u16);
if ((u16 == AF_IANA_IP && tlv_len == 6) ||
(u16 == AF_IANA_IP6 && tlv_len == 18)) {
decoded->mcast_grp_len = tlv_len;
decoded->mcast_grp = tlv_iter_get_data(&tlv_iter);
DEBUG2_PRINTF("%sAF %u", debug_str, u16);
} else {
DEBUG2_PRINTF("%sbad AF %u or len", debug_str, u16);
}
} else {
DEBUG2_PRINTF("%slen <= 2", debug_str);
}
break;
case TLV_OPT_TYPE_OPT_REQUEST:
if (tlv_len > 1 && (tlv_len % 2 == 0)) {
for (pos = 0; pos < (uint16_t)(tlv_len / 2); pos++) {
memcpy(&u16, tlv_iter_get_data(&tlv_iter) + pos * 2,
sizeof(u16));
u16 = ntohs(u16);
switch (u16) {
case TLV_OPT_TYPE_SERVER_INFO:
decoded->request_opt_server_info = 1;
DEBUG2_PRINTF("%s%zu opt %u", debug_str, pos, u16);
break;
case TLV_OPT_TYPE_SERVER_TSTAMP:
decoded->request_opt_server_tstamp = 1;
DEBUG2_PRINTF("%s%zu opt %u", debug_str, pos, u16);
break;
default:
DEBUG2_PRINTF("%s%zu unknown opt %u", debug_str,
pos, u16);
break;
}
}
} else {
DEBUG2_PRINTF("%slen <= 1 || (tlv_len %%2 != 0)", debug_str);
}
break;
case TLV_OPT_TYPE_SERVER_INFO:
if (tlv_len > 0) {
decoded->server_info = tlv_iter_get_data(&tlv_iter);
decoded->server_info_len = tlv_len;
DEBUG2_HEXDUMP(debug_str, decoded->server_info, tlv_len);
} else {
DEBUG2_PRINTF("%slen <= 0", debug_str);
}
break;
case TLV_OPT_TYPE_TTL:
if (tlv_len == 1) {
memcpy(&u8, tlv_iter_get_data(&tlv_iter), sizeof(u8));
decoded->ttl = u8;
DEBUG2_PRINTF("%s%u", debug_str, u8);
} else {
DEBUG2_PRINTF("%slen != 1", debug_str);
}
break;
case TLV_OPT_TYPE_MCAST_PREFIX:
if (tlv_len > 2) {
memcpy(&u16, tlv_iter_get_data(&tlv_iter), sizeof(u16));
u16 = ntohs(u16);
if (u16 == AF_IANA_IP || u16 == AF_IANA_IP6) {
decoded->mcast_prefix_isset = 1;
DEBUG2_PRINTF("%sAF %u", debug_str, u16);
} else {
DEBUG2_PRINTF("%sbad AF %u", debug_str, u16);
}
} else {
DEBUG2_PRINTF("%slen <= 2", debug_str);
}
break;
case TLV_OPT_TYPE_SES_ID:
if (tlv_len > 0) {
decoded->ses_id_len = tlv_len;
decoded->ses_id = tlv_iter_get_data(&tlv_iter);
DEBUG2_HEXDUMP(debug_str, decoded->ses_id, tlv_len);
} else {
DEBUG2_PRINTF("%slen <= 0", debug_str);
}
break;
case TLV_OPT_TYPE_SERVER_TSTAMP:
if (tlv_len == 8) {
memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
u32 = ntohl(u32);
decoded->server_tstamp.tv_sec = u32;
memcpy(&u32_2, tlv_iter_get_data(&tlv_iter) + sizeof(u32),
sizeof(u32_2));
u32_2 = ntohl(u32_2);
decoded->server_tstamp.tv_usec = u32_2;
decoded->server_tstamp_isset = 1;
DEBUG2_PRINTF("%s(%u,%u)", debug_str, u32, u32_2);
} else {
DEBUG2_PRINTF("%slen != 8", debug_str);
}
break;
default:
DEBUG2_PRINTF("%s", debug_str);
break;
}
}
}
/*
* Checks if msg contains prefix of mcast_addr. msg is message buffer, msg_len is size of buffer.
*
* Returned value is 0 if msg doesn't contain prefix, otherwise 1
*/
int
msg_has_prefix(const char *msg, size_t msg_len, const struct sockaddr_storage *mcast_addr)
{
struct tlv_iterator tlv_iter;
int has_prefix;
has_prefix = 0;
tlv_iter_init(msg, msg_len, &tlv_iter);
while (tlv_iter_next(&tlv_iter) == 0) {
if (tlv_iter_get_type(&tlv_iter) == TLV_OPT_TYPE_MCAST_PREFIX) {
if (tlv_iter_pref_eq(&tlv_iter, mcast_addr)) {
has_prefix = 1;
break;
}
}
}
return (has_prefix);
}
/*
* Create init message. msg is pointer to buffer where to store result message. msg_len is size of
* buffer. mcast_addr is required multicast address. client_id is client ID to store in message with
* length client_id_len.
*
* Returned value is size of new message or 0 on fail (mostly because msg_len
* is smaller then needed). If success, new message is always at least 1 bytes long.
*/
size_t
msg_init_create(char *msg, size_t msg_len, int req_si, const struct sockaddr_storage *mcast_addr,
const char *client_id, size_t client_id_len)
{
size_t pos;
uint16_t u16;
pos = 0;
if (client_id == NULL) {
return (0);
}
msg[pos++] = (unsigned char)MSG_TYPE_INIT;
if (tlv_add_version(msg, msg_len, &pos) == -1)
goto small_buf_err;
if (tlv_add(msg, msg_len, &pos, TLV_OPT_TYPE_CLIENT_ID, client_id_len, client_id) == -1)
goto small_buf_err;
if (req_si) {
u16 = TLV_OPT_TYPE_SERVER_INFO;
if (tlv_add_opt_request(msg, msg_len, &pos, &u16, 1) == -1)
goto small_buf_err;
}
if (tlv_add_mcast_prefix(msg, msg_len, &pos, mcast_addr) == -1)
goto small_buf_err;
return (pos);
small_buf_err:
return (0);
}
/*
* Create query message. msg is pointer to buffer where to store result message. msg_len is size
* of buffer. mcast_addr is required multicast group address. server_tstamp is boolean to decide if
* to include Option request option with server time stamp. client_id is Client ID with length
* client_id_len. session_id with session_id_len is similar, but for Session ID.
*
* Returned value is size of new message or 0 on fail (mostly because msg_len
* is smaller then needed). If success, new message is always at least 1 bytes long.
*/
size_t
msg_query_create(char *msg, size_t msg_len, const struct sockaddr_storage *mcast_addr,
uint32_t seq_num, int server_tstamp, const char *client_id, size_t client_id_len,
const char *session_id, size_t session_id_len)
{
size_t pos;
uint16_t u16;
pos = 0;
msg[pos++] = (unsigned char)MSG_TYPE_QUERY;
if (tlv_add_version(msg, msg_len, &pos) == -1)
goto small_buf_err;
if (client_id) {
if (tlv_add(msg, msg_len, &pos, TLV_OPT_TYPE_CLIENT_ID, client_id_len,
client_id) == -1) {
goto small_buf_err;
}
}
if (tlv_add_seq_num(msg, msg_len, &pos, seq_num) == -1)
goto small_buf_err;
if (tlv_add_client_tstamp(msg, msg_len, &pos) == -1)
goto small_buf_err;
if (tlv_add_mcast_grp(msg, msg_len, &pos, mcast_addr) == -1)
goto small_buf_err;
if (server_tstamp) {
u16 = TLV_OPT_TYPE_SERVER_TSTAMP;
if (tlv_add_opt_request(msg, msg_len, &pos, &u16, 1) == -1)
goto small_buf_err;
}
if (tlv_add(msg, msg_len, &pos, TLV_OPT_TYPE_SES_ID, session_id_len, session_id) == -1)
goto small_buf_err;
return (pos);
small_buf_err:
return (0);
}
/*
* Create response message. msg is pointer to buffer where to store result message. msg_len is size
* of buffer. msg_decoded is decoded init message used for some informations (like client id, ...).
* mcast_grp and mcast_prefix are booleans used for decision, if to add Multicast Group and/or
* Multicast Prefix options. mcast_addr is value for mcast_grp and/or mcast_prefix. If none of this
* options is/are required, mcasr_addr can be set to NULL. ulticast address. session_id is
* session ID of client.
*
* Returned value is size of new message or 0 on fail (mostly because msg_len
* is smaller then needed). If success, new message is always at least 1 bytes long.
*/
size_t
msg_response_create(char *msg, size_t msg_len, const struct msg_decoded *msg_decoded,
int mcast_grp, int mcast_prefix, const struct sockaddr_storage *mcast_addr,
const char *session_id, size_t session_id_len)
{
size_t pos;
pos = 0;
msg[pos++] = (unsigned char)MSG_TYPE_RESPONSE;
if (tlv_add_version(msg, msg_len, &pos) == -1)
goto small_buf_err;
if (msg_decoded->client_id) {
if (tlv_add(msg, msg_len, &pos, TLV_OPT_TYPE_CLIENT_ID, msg_decoded->client_id_len,
msg_decoded->client_id) == -1)
goto small_buf_err;
}
if (msg_decoded->seq_num_isset) {
if (tlv_add_seq_num(msg, msg_len, &pos, msg_decoded->seq_num) == -1)
goto small_buf_err;
}
if (mcast_grp) {
if (tlv_add_mcast_grp(msg, msg_len, &pos, mcast_addr) == -1)
goto small_buf_err;
}
if (msg_decoded->request_opt_server_info) {
if (tlv_add_server_info(msg, msg_len, &pos, PROGRAM_SERVER_INFO) == -1)
goto small_buf_err;
}
if (mcast_prefix) {
if (tlv_add_mcast_prefix(msg, msg_len, &pos, mcast_addr) == -1)
goto small_buf_err;
}
if (session_id) {
if (tlv_add(msg, msg_len, &pos, TLV_OPT_TYPE_SES_ID, session_id_len,
session_id) == -1) {
goto small_buf_err;
}
}
return (pos);
small_buf_err:
return (0);
}
/*
* Update Server Timestamp option in message to current time stamp. msg is pointer to buffer with
* message and msg_len is length of message (without unused space).
* Function returns 0 on success, otherwise -1.
*/
int
msg_update_server_tstamp(char *msg, size_t msg_len)
{
struct tlv_iterator tlv_iter;
size_t pos;
memset(&tlv_iter, 0, sizeof(tlv_iter));
tlv_iter_init(msg, msg_len, &tlv_iter);
while (tlv_iter_next(&tlv_iter) != -1) {
if (tlv_iter_get_type(&tlv_iter) == TLV_OPT_TYPE_SERVER_TSTAMP) {
pos = tlv_iter.pos;
if (tlv_add_server_tstamp(msg, msg_len, &pos) == -1)
goto add_tstamp_err;
}
}
return (0);
add_tstamp_err:
return (-1);
}