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 | 80 | return "Invalid exception code"; |
| 81 | 81 | case EMBMDATA: |
| 82 | 82 | return "Too many data"; |
| 83 | + case EMBBADSLAVE: | |
| 84 | + return "Response not from requested slave"; | |
| 83 | 85 | default: |
| 84 | 86 | return strerror(errnum); |
| 85 | 87 | } |
| ... | ... | @@ -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 | 503 | rsp_length_computed = compute_response_length_from_request(ctx, req); |
| 496 | 504 | |
| 497 | 505 | /* Check length */ | ... | ... |
src/modbus.h
| ... | ... | @@ -117,6 +117,7 @@ enum { |
| 117 | 117 | #define EMBBADEXC (EMBXGTAR + 3) |
| 118 | 118 | #define EMBUNKEXC (EMBXGTAR + 4) |
| 119 | 119 | #define EMBMDATA (EMBXGTAR + 5) |
| 120 | +#define EMBBADSLAVE (EMBXGTAR + 6) | |
| 120 | 121 | |
| 121 | 122 | extern const unsigned int libmodbus_version_major; |
| 122 | 123 | extern const unsigned int libmodbus_version_minor; | ... | ... |