Allow "" and NULL on TCP host and service (=> IP=INADDR_ANY, and port=0)
authorMario de Sousa <msousa@fe.up.pt>
Mon, 01 Jun 2020 14:15:03 +0100
changeset 8 f14859c24751
parent 7 9334c8280602
child 9 d6effe86bc2f
Allow "" and NULL on TCP host and service (=> IP=INADDR_ANY, and port=0)
mb_slave.c
mb_tcp.c
--- a/mb_slave.c	Tue Dec 10 16:07:49 2019 +0000
+++ b/mb_slave.c	Mon Jun 01 14:15:03 2020 +0100
@@ -107,22 +107,34 @@
 }
 
 
-/*
- * Functions to convert u16 variables
- * between network and host byte order
- *
- * NOTE: Modbus uses MSByte first, just like
- *       tcp/ip, so we could be tempted to use the htons() and
- *       ntohs() functions to guarantee code portability.
- *
- *       However, on some embedded systems running Linux
- *       these functions only work if the 16 bit words are 
- *       stored on even addresses. This is not always the 
- *       case in our code, so we have to define our own
- *       conversion functions...
- */
-
-/* if using gcc, use it to determine byte order... */
+/* Determine endianess of platform... */
+
+/* WARNING: The following files are being included:
+ *              <stdib.h>  -->  <endian.h>  -->  <bits/endian.h>
+ * 
+ *          endian.h defines the following constants as:
+ *            #define __LITTLE_ENDIAN  and LITTLE_ENDIAN  as 1234
+ *            #define    __BIG_ENDIAN  and    BIG_ENDIAN  as 4321
+ *            #define    __PDP_ENDIAN  and    PDP_ENDIAN  as 3412
+ * 
+ *          bits/endian.h defines the constant BYTE_ORDER as:
+ *              #define __BYTE_ORDER as __LITTLE_ENDIAN
+ * 
+ *          endian.h then sets the following constants
+ *          (if __USE_BSD is set, which seems to be true):
+ *            # define LITTLE_ENDIAN    __LITTLE_ENDIAN
+ *            # define BIG_ENDIAN       __BIG_ENDIAN
+ *            # define PDP_ENDIAN       __PDP_ENDIAN
+ *            # define BYTE_ORDER       __BYTE_ORDER
+ */ 
+
+/* If we still don't know byte order, try to get it from <endian.h> */
+#ifndef __BYTE_ORDER
+#include <endian.h>
+#endif
+
+
+/* If we still don't know byte order => if using gcc, use it to determine byte order... */
 #ifndef __BYTE_ORDER
 #if defined(__GNUC__) 
   /* We have GCC, which should define __LITTLE_ENDIAN__ */ 
@@ -135,26 +147,32 @@
 #endif /* __BYTE_ORDER */
 
 
-/* If we still don't know byte order, try to get it from <sys/param.h> */
 #ifndef __BYTE_ORDER
-#include <sys/param.h>
+#  error   "Unable to determine platform's byte order. Aborting compilation."
+#elif   __BYTE_ORDER == __BIG_ENDIAN
+#  warning "Compiling for BIG endian platform."
+#elif   __BYTE_ORDER == __LITTLE_ENDIAN
+#  warning "Compiling for LITTLE endian platform."
+#else
+#  error   "Aborting compilation due to unsuported byte order (neither BIG not LITTLE endian)."
 #endif
 
 
-#ifndef __BYTE_ORDER
-#  ifdef BYTE_ORDER
-#   if BYTE_ORDER == LITTLE_ENDIAN
-#    define __BYTE_ORDER __LITTLE_ENDIAN
-#   else
-#    if BYTE_ORDER == BIG_ENDIAN
-#      define __BYTE_ORDER __BIG_ENDIAN
-#    endif
-#   endif
-#  endif /* BYTE_ORDER */
-#endif /* __BYTE_ORDER */
-
-
-
+
+/*
+ * Functions to convert u16 variables
+ * between network and host byte order
+ *
+ * NOTE: Modbus uses MSByte first, just like
+ *       tcp/ip, so we could be tempted to use the htons() and
+ *       ntohs() functions to guarantee code portability.
+ *
+ *       However, on some embedded systems running Linux
+ *       these functions only work if the 16 bit words are 
+ *       stored on even addresses. This is not always the 
+ *       case in our code, so we have to define our own
+ *       conversion functions...
+ */
 
 
 #ifdef __BYTE_ORDER
--- a/mb_tcp.c	Tue Dec 10 16:07:49 2019 +0000
+++ b/mb_tcp.c	Mon Jun 01 14:15:03 2020 +0100
@@ -1517,8 +1517,8 @@
 
   /* Check the parameters we were passed... */
   if(sin_initaddr(&tmp_addr,
-                  node_addr.addr.tcp.host,    0,
-                  node_addr.addr.tcp.service, 0,
+                  node_addr.addr.tcp.host,    1, // 1 => allow host NULL, "" or "*" -> INADDR_ANY
+                  node_addr.addr.tcp.service, 1, // 1 => allow serivce NULL or ""   -> port = 0
                   DEF_PROTOCOL)
        < 0) {
 #ifdef ERRMSG