Commit f0b6788817ff9910bc3fb996591ef48ecc546fea

Authored by Stéphane Raimbault
1 parent 815d11f0

Check the received function code on the client side

Showing 1 changed file with 12 additions and 1 deletions
src/modbus.c
@@ -464,10 +464,21 @@ static int receive_msg_req(modbus_t *ctx, uint8_t *req, uint8_t *rsp) @@ -464,10 +464,21 @@ static int receive_msg_req(modbus_t *ctx, uint8_t *req, uint8_t *rsp)
464 /* GOOD RESPONSE */ 464 /* GOOD RESPONSE */
465 int req_nb_value; 465 int req_nb_value;
466 int rsp_nb_value; 466 int rsp_nb_value;
  467 + int function = rsp[offset];
  468 +
  469 + if (function != req[offset]) {
  470 + if (ctx->debug) {
  471 + fprintf(stderr,
  472 + "Received function not corresponding to the request (%d != %d)\n",
  473 + function, req[offset]);
  474 + }
  475 + errno = EMBBADDATA;
  476 + return -1;
  477 + }
467 478
468 /* The number of values is returned if it's corresponding 479 /* The number of values is returned if it's corresponding
469 * to the request */ 480 * to the request */
470 - switch (rsp[offset]) { 481 + switch (function) {
471 case _FC_READ_COILS: 482 case _FC_READ_COILS:
472 case _FC_READ_DISCRETE_INPUTS: 483 case _FC_READ_DISCRETE_INPUTS:
473 /* Read functions, 8 values in a byte (nb 484 /* Read functions, 8 values in a byte (nb