Commit 6bc39ad930ce8e3e541fafda24c5b86cf1ea07d3

Authored by Stéphane Raimbault
1 parent ad4623c0

Add check for protocol identifer in TCP mode

Showing 1 changed file with 15 additions and 5 deletions
src/modbus-tcp.c
@@ -191,19 +191,29 @@ static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int ms @@ -191,19 +191,29 @@ static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int ms
191 } 191 }
192 192
193 static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req, 193 static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
194 - const uint8_t *rsp, int rsp_length) 194 + const uint8_t *rsp, int rsp_length)
195 { 195 {
196 - /* Check TID */ 196 + /* Check transaction ID */
197 if (req[0] != rsp[0] || req[1] != rsp[1]) { 197 if (req[0] != rsp[0] || req[1] != rsp[1]) {
198 if (ctx->debug) { 198 if (ctx->debug) {
199 - fprintf(stderr, "Invalid TID received 0x%X (not 0x%X)\n", 199 + fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",
200 (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]); 200 (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
201 } 201 }
202 errno = EMBBADDATA; 202 errno = EMBBADDATA;
203 return -1; 203 return -1;
204 - } else {  
205 - return 0;  
206 } 204 }
  205 +
  206 + /* Check protocol ID */
  207 + if (rsp[2] != 0x0 && rsp[3] != 0x0) {
  208 + if (ctx->debug) {
  209 + fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",
  210 + (rsp[2] << 8) + rsp[3]);
  211 + }
  212 + errno = EMBBADDATA;
  213 + return -1;
  214 + }
  215 +
  216 + return 0;
207 } 217 }
208 218
209 static int _modbus_tcp_set_ipv4_options(int s) 219 static int _modbus_tcp_set_ipv4_options(int s)