-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle incorrect Modbus/TCP data length header field #31
- Loading branch information
Showing
5 changed files
with
144 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,18 +4,18 @@ | |
* modbus.c - MODBUS protocol related procedures | ||
* | ||
* Copyright (c) 2002-2003, 2013, Victor Antonovich ([email protected]) | ||
* | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* | ||
* - Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
|
@@ -67,21 +67,21 @@ modbus_crc_write(unsigned char *frame, unsigned int len) | |
void | ||
modbus_ex_write(unsigned char *packet, unsigned char code) | ||
{ | ||
MB_HDR(packet, MB_FCODE) |= 0x80; | ||
MB_HDR(packet, MB_DATA) = code; | ||
MB_FRAME(packet, MB_FCODE) |= 0x80; | ||
MB_FRAME(packet, MB_DATA) = code; | ||
WORD_WR_BE(packet + MB_LENGTH_H, MB_EX_LEN); | ||
} | ||
|
||
/* | ||
* Check MODBUS packet header consistency | ||
* Parameters: HEADER - address of the header | ||
* Parameters: PACKET - address of the request packet, | ||
* Return: RC_OK if (mostly) all is right, RC_ERR otherwise | ||
*/ | ||
int | ||
modbus_check_header(unsigned char *packet) | ||
{ | ||
return (MB_HDR(packet, MB_PROTO_ID_H) == 0 && | ||
MB_HDR(packet, MB_PROTO_ID_L) == 0 && | ||
MB_HDR(packet, MB_LENGTH_H) == 0 && | ||
MB_HDR(packet, MB_LENGTH_L) > 0) ? RC_OK : RC_ERR; | ||
return (MB_FRAME(packet, MB_PROTO_ID_H) == 0 && | ||
MB_FRAME(packet, MB_PROTO_ID_L) == 0 && | ||
MB_FRAME(packet, MB_LENGTH_H) == 0 && | ||
MB_FRAME(packet, MB_LENGTH_L) > 0) ? RC_OK : RC_ERR; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,18 +4,18 @@ | |
* modbus.h - MODBUS protocol related procedures | ||
* | ||
* Copyright (c) 2002-2003, 2013, Victor Antonovich ([email protected]) | ||
* | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* | ||
* - Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
|
@@ -38,9 +38,9 @@ | |
#include "crc16.h" | ||
|
||
/* | ||
* Macros for invoking data from MODBUS packet header | ||
* Macro for accessing data in MODBUS frame | ||
*/ | ||
#define MB_HDR(p, d) ( *(p + d) ) | ||
#define MB_FRAME(p, d) ( *(p + d) ) | ||
|
||
/* | ||
* MODBUS frame lengths | ||
|
@@ -50,7 +50,7 @@ | |
#define MB_MAX_LEN 256 | ||
|
||
/* | ||
* Macroses for word operations | ||
* Macros for word operations | ||
*/ | ||
#define HIGH(w) ( (unsigned char)(((w) >> 8) & 0xff) ) | ||
#define LOW(w) ( (unsigned char)((w) & 0xff) ) | ||
|
@@ -79,7 +79,12 @@ | |
#define MB_UNIT_ID 6 /* unit identifier */ | ||
#define MB_FCODE 7 /* function code */ | ||
#define MB_DATA 8 /* MODBUS data */ | ||
|
||
#define MB_DATA_ADDR_H MB_DATA /* MODBUS data: address high byte */ | ||
#define MB_DATA_ADDR_L 9 /* MODBUS data: address low byte */ | ||
#define MB_DATA_NVAL_H 10 /* MODBUS data: number of values high byte */ | ||
#define MB_DATA_NVAL_L 11 /* MODBUS data: number of values low byte */ | ||
#define MB_DATA_NBYTES 12 /* MODBUS data: number of bytes to follow (fc 15,16) */ | ||
|
||
/* | ||
* Exception codes | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters