Skip to content

Commit

Permalink
Merge pull request #8645 from mtomczykmobica/ONME-3733
Browse files Browse the repository at this point in the history
Detect xinetd service pattern and generate correct pattern (ONME-3733)
  • Loading branch information
0xc0170 authored Nov 8, 2018
2 parents 14eadfa + 5eabfab commit 937d68f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
18 changes: 13 additions & 5 deletions TESTS/netsocket/tcp/tcpsocket_recv_100k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,34 @@ static nsapi_error_t _tcpsocket_connect_to_chargen_srv(TCPSocket &sock)
* \param offset Start pattern from offset
* \param len Length of pattern to generate.
*/
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len)
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len, bool is_xinetd)
{
const int row_size = 74; // Number of chars in single row
const int row_count = 95; // Number of rows in pattern after which pattern start from beginning
const int chars_scope = is_xinetd ? 93 : 95; // Number of chars from ASCII table used in pattern
const char first_char = is_xinetd ? '!' : ' '; // First char from ASCII table used in pattern
while (len--) {
if (offset % 74 == 72) {
if (offset % row_size == (row_size - 2)) {
*buf++ = '\r';
} else if (offset % 74 == 73) {
} else if (offset % row_size == (row_size - 1)) {
*buf++ = '\n';
} else {
*buf++ = ' ' + (offset % 74 + offset / 74) % 95 ;
*buf++ = first_char + (offset % row_size + ((offset / row_size) % row_count)) % chars_scope;
}
offset++;
}
}

static void check_RFC_864_pattern(void *rx_buff, const size_t len, const size_t offset)
{
static bool is_xinetd = false;
void *ref_buff = malloc(len);
TEST_ASSERT_NOT_NULL(ref_buff);

generate_RFC_864_pattern(offset, (uint8_t *)ref_buff, len);
if (offset == 0) {
is_xinetd = ((uint8_t *)rx_buff)[0] == '!';
}
generate_RFC_864_pattern(offset, (uint8_t *)ref_buff, len, is_xinetd);
bool match = memcmp(ref_buff, rx_buff, len) == 0;

free(ref_buff);
Expand Down
29 changes: 20 additions & 9 deletions TEST_APPS/device/socket_app/cmd_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class SInfo {
{
assert(sock);
}
SInfo(Socket* sock, bool delete_on_exit=true):
SInfo(Socket *sock, bool delete_on_exit = true):
_id(id_count++),
_sock(sock),
_type(SInfo::OTHER),
Expand Down Expand Up @@ -408,31 +408,42 @@ static void print_data_as_hex(const uint8_t *buf, int len, int col_width);
* \param offset Start pattern from offset
* \param len Length of pattern to generate.
*/
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len)
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len, bool is_xinetd)
{
const int row_size = 74; // Number of chars in single row
const int row_count = 95; // Number of rows in pattern after which pattern start from beginning
const int chars_scope = is_xinetd ? 93 : 95; // Number of chars from ASCII table used in pattern
const char first_char = is_xinetd ? '!' : ' '; // First char from ASCII table used in pattern
while (len--) {
if (offset % 74 == 72) {
if (offset % row_size == (row_size - 2)) {
*buf++ = '\r';
} else if (offset % 74 == 73) {
} else if (offset % row_size == (row_size - 1)) {
*buf++ = '\n';
} else {
*buf++ = ' ' + (offset % 74 + offset / 74) % 95 ;
*buf++ = first_char + (offset % row_size + ((offset / row_size) % row_count)) % chars_scope;
}
offset++;
}
}

bool SInfo::check_pattern(void *buffer, size_t len)
{
static bool is_xinetd = false;
if (!_check_pattern) {
return true;
}
void *buf = malloc(len);
if (!buf) {
return false;
}

size_t offset = _receivedTotal;
generate_RFC_864_pattern(offset, (uint8_t *)buf, len);

if (offset == 0) {
is_xinetd = ((uint8_t *)buffer)[0] == '!';
}

generate_RFC_864_pattern(offset, (uint8_t *)buf, len, is_xinetd);
bool match = memcmp(buf, buffer, len) == 0;
if (!match) {
cmd_printf("Pattern check failed\r\nWAS:%.*s\r\nREF:%.*s\r\n", len, (char *)buffer, len, (char *)buf);
Expand Down Expand Up @@ -1162,11 +1173,11 @@ static int cmd_socket(int argc, char *argv[])
cmd_printf("Invalid socket id\r\n");
return CMDLINE_RETCODE_FAIL;
}
TCPSocket *new_sock = static_cast<TCPSocket*>(&new_info->socket());
nsapi_error_t ret = static_cast<TCPServer&>(info->socket()).accept(new_sock, &addr);
TCPSocket *new_sock = static_cast<TCPSocket *>(&new_info->socket());
nsapi_error_t ret = static_cast<TCPServer &>(info->socket()).accept(new_sock, &addr);
if (ret == NSAPI_ERROR_OK) {
cmd_printf("TCPServer::accept() new socket sid: %d connection from %s port %d\r\n",
new_info->id(), addr.get_ip_address(), addr.get_port());
new_info->id(), addr.get_ip_address(), addr.get_port());
}
return handle_nsapi_error("TCPServer::accept()", ret);
}
Expand Down

0 comments on commit 937d68f

Please sign in to comment.