Commit 7ecfd4110a6b169bd96629f81dc43f0a352a0cfa
Committed by
Stéphane Raimbault
1 parent
98b7a6a1
Added slave ID check for response messages
Showing
2 changed files
with
9 additions
and
0 deletions
src/modbus.c
| @@ -80,6 +80,8 @@ const char *modbus_strerror(int errnum) { | @@ -80,6 +80,8 @@ const char *modbus_strerror(int errnum) { | ||
| 80 | return "Invalid exception code"; | 80 | return "Invalid exception code"; |
| 81 | case EMBMDATA: | 81 | case EMBMDATA: |
| 82 | return "Too many data"; | 82 | return "Too many data"; |
| 83 | + case EMBBADSLAVE: | ||
| 84 | + return "Response not from requested slave"; | ||
| 83 | default: | 85 | default: |
| 84 | return strerror(errnum); | 86 | return strerror(errnum); |
| 85 | } | 87 | } |
| @@ -492,6 +494,12 @@ static int check_confirmation(modbus_t *ctx, uint8_t *req, | @@ -492,6 +494,12 @@ static int check_confirmation(modbus_t *ctx, uint8_t *req, | ||
| 492 | } | 494 | } |
| 493 | } | 495 | } |
| 494 | 496 | ||
| 497 | + /* Check responding slave is the slave we requested */ | ||
| 498 | + if(req[0] != 0 && rsp[0] != req[0]) { | ||
| 499 | + errno = EMBBADSLAVE; | ||
| 500 | + return -1; | ||
| 501 | + } | ||
| 502 | + | ||
| 495 | rsp_length_computed = compute_response_length_from_request(ctx, req); | 503 | rsp_length_computed = compute_response_length_from_request(ctx, req); |
| 496 | 504 | ||
| 497 | /* Check length */ | 505 | /* Check length */ |
src/modbus.h
| @@ -117,6 +117,7 @@ enum { | @@ -117,6 +117,7 @@ enum { | ||
| 117 | #define EMBBADEXC (EMBXGTAR + 3) | 117 | #define EMBBADEXC (EMBXGTAR + 3) |
| 118 | #define EMBUNKEXC (EMBXGTAR + 4) | 118 | #define EMBUNKEXC (EMBXGTAR + 4) |
| 119 | #define EMBMDATA (EMBXGTAR + 5) | 119 | #define EMBMDATA (EMBXGTAR + 5) |
| 120 | +#define EMBBADSLAVE (EMBXGTAR + 6) | ||
| 120 | 121 | ||
| 121 | extern const unsigned int libmodbus_version_major; | 122 | extern const unsigned int libmodbus_version_major; |
| 122 | extern const unsigned int libmodbus_version_minor; | 123 | extern const unsigned int libmodbus_version_minor; |