Skip to content

Commit 937d68f

Browse files
authored
Merge pull request #8645 from mtomczykmobica/ONME-3733
Detect xinetd service pattern and generate correct pattern (ONME-3733)
2 parents 14eadfa + 5eabfab commit 937d68f

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

TESTS/netsocket/tcp/tcpsocket_recv_100k.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,34 @@ static nsapi_error_t _tcpsocket_connect_to_chargen_srv(TCPSocket &sock)
5858
* \param offset Start pattern from offset
5959
* \param len Length of pattern to generate.
6060
*/
61-
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len)
61+
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len, bool is_xinetd)
6262
{
63+
const int row_size = 74; // Number of chars in single row
64+
const int row_count = 95; // Number of rows in pattern after which pattern start from beginning
65+
const int chars_scope = is_xinetd ? 93 : 95; // Number of chars from ASCII table used in pattern
66+
const char first_char = is_xinetd ? '!' : ' '; // First char from ASCII table used in pattern
6367
while (len--) {
64-
if (offset % 74 == 72) {
68+
if (offset % row_size == (row_size - 2)) {
6569
*buf++ = '\r';
66-
} else if (offset % 74 == 73) {
70+
} else if (offset % row_size == (row_size - 1)) {
6771
*buf++ = '\n';
6872
} else {
69-
*buf++ = ' ' + (offset % 74 + offset / 74) % 95 ;
73+
*buf++ = first_char + (offset % row_size + ((offset / row_size) % row_count)) % chars_scope;
7074
}
7175
offset++;
7276
}
7377
}
7478

7579
static void check_RFC_864_pattern(void *rx_buff, const size_t len, const size_t offset)
7680
{
81+
static bool is_xinetd = false;
7782
void *ref_buff = malloc(len);
7883
TEST_ASSERT_NOT_NULL(ref_buff);
7984

80-
generate_RFC_864_pattern(offset, (uint8_t *)ref_buff, len);
85+
if (offset == 0) {
86+
is_xinetd = ((uint8_t *)rx_buff)[0] == '!';
87+
}
88+
generate_RFC_864_pattern(offset, (uint8_t *)ref_buff, len, is_xinetd);
8189
bool match = memcmp(ref_buff, rx_buff, len) == 0;
8290

8391
free(ref_buff);

TEST_APPS/device/socket_app/cmd_socket.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class SInfo {
152152
{
153153
assert(sock);
154154
}
155-
SInfo(Socket* sock, bool delete_on_exit=true):
155+
SInfo(Socket *sock, bool delete_on_exit = true):
156156
_id(id_count++),
157157
_sock(sock),
158158
_type(SInfo::OTHER),
@@ -408,31 +408,42 @@ static void print_data_as_hex(const uint8_t *buf, int len, int col_width);
408408
* \param offset Start pattern from offset
409409
* \param len Length of pattern to generate.
410410
*/
411-
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len)
411+
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len, bool is_xinetd)
412412
{
413+
const int row_size = 74; // Number of chars in single row
414+
const int row_count = 95; // Number of rows in pattern after which pattern start from beginning
415+
const int chars_scope = is_xinetd ? 93 : 95; // Number of chars from ASCII table used in pattern
416+
const char first_char = is_xinetd ? '!' : ' '; // First char from ASCII table used in pattern
413417
while (len--) {
414-
if (offset % 74 == 72) {
418+
if (offset % row_size == (row_size - 2)) {
415419
*buf++ = '\r';
416-
} else if (offset % 74 == 73) {
420+
} else if (offset % row_size == (row_size - 1)) {
417421
*buf++ = '\n';
418422
} else {
419-
*buf++ = ' ' + (offset % 74 + offset / 74) % 95 ;
423+
*buf++ = first_char + (offset % row_size + ((offset / row_size) % row_count)) % chars_scope;
420424
}
421425
offset++;
422426
}
423427
}
424428

425429
bool SInfo::check_pattern(void *buffer, size_t len)
426430
{
431+
static bool is_xinetd = false;
427432
if (!_check_pattern) {
428433
return true;
429434
}
430435
void *buf = malloc(len);
431436
if (!buf) {
432437
return false;
433438
}
439+
434440
size_t offset = _receivedTotal;
435-
generate_RFC_864_pattern(offset, (uint8_t *)buf, len);
441+
442+
if (offset == 0) {
443+
is_xinetd = ((uint8_t *)buffer)[0] == '!';
444+
}
445+
446+
generate_RFC_864_pattern(offset, (uint8_t *)buf, len, is_xinetd);
436447
bool match = memcmp(buf, buffer, len) == 0;
437448
if (!match) {
438449
cmd_printf("Pattern check failed\r\nWAS:%.*s\r\nREF:%.*s\r\n", len, (char *)buffer, len, (char *)buf);
@@ -1162,11 +1173,11 @@ static int cmd_socket(int argc, char *argv[])
11621173
cmd_printf("Invalid socket id\r\n");
11631174
return CMDLINE_RETCODE_FAIL;
11641175
}
1165-
TCPSocket *new_sock = static_cast<TCPSocket*>(&new_info->socket());
1166-
nsapi_error_t ret = static_cast<TCPServer&>(info->socket()).accept(new_sock, &addr);
1176+
TCPSocket *new_sock = static_cast<TCPSocket *>(&new_info->socket());
1177+
nsapi_error_t ret = static_cast<TCPServer &>(info->socket()).accept(new_sock, &addr);
11671178
if (ret == NSAPI_ERROR_OK) {
11681179
cmd_printf("TCPServer::accept() new socket sid: %d connection from %s port %d\r\n",
1169-
new_info->id(), addr.get_ip_address(), addr.get_port());
1180+
new_info->id(), addr.get_ip_address(), addr.get_port());
11701181
}
11711182
return handle_nsapi_error("TCPServer::accept()", ret);
11721183
}

0 commit comments

Comments
 (0)