Эх сурвалжийг харах

Check the received function code on the client side

Stéphane Raimbault 14 жил өмнө
parent
commit
f0b6788817
1 өөрчлөгдсөн 12 нэмэгдсэн , 1 устгасан
  1. 12 1
      src/modbus.c

+ 12 - 1
src/modbus.c

@@ -464,10 +464,21 @@ static int receive_msg_req(modbus_t *ctx, uint8_t *req, uint8_t *rsp)
         /* GOOD RESPONSE */
         int req_nb_value;
         int rsp_nb_value;
+        int function = rsp[offset];
+
+        if (function != req[offset]) {
+            if (ctx->debug) {
+                fprintf(stderr,
+                        "Received function not corresponding to the request (%d != %d)\n",
+                        function, req[offset]);
+            }
+            errno = EMBBADDATA;
+            return -1;
+        }
 
         /* The number of values is returned if it's corresponding
          * to the request */
-        switch (rsp[offset]) {
+        switch (function) {
         case _FC_READ_COILS:
         case _FC_READ_DISCRETE_INPUTS:
             /* Read functions, 8 values in a byte (nb