From fe121698d20d1014e14ab625741dfeabfe6f0859 Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Mon, 1 Apr 2024 11:14:59 +0200 Subject: [PATCH 1/2] Make presence of netinet/ip.h optional Some environments, for example lwip, don't have it, so let's make it configurable so we can also build and work on those platforms by simplying using project configuration. --- configure.ac | 1 + src/modbus-tcp.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/configure.ac b/configure.ac index bbb655b0b..e39d37aa6 100644 --- a/configure.ac +++ b/configure.ac @@ -85,6 +85,7 @@ AC_CHECK_HEADERS([ \ linux/serial.h \ netdb.h \ netinet/in.h \ + netinet/ip.h \ netinet/tcp.h \ sys/ioctl.h \ sys/params.h \ diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index 0c571097a..ba9cf05a2 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -41,7 +41,9 @@ #endif # include +#ifdef HAVE_NETINET_IP_H # include +#endif # include # include # include From f2a619dfef5d032b886c7cf7b9f47c4f2c90e86a Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Mon, 1 Apr 2024 11:40:06 +0200 Subject: [PATCH 2/2] Make gai_strerror optional via configuration Check via usual autoconf and when not present print just the error code instead of the readable string. Useful for compact embedded stacks where this is not present. --- configure.ac | 2 +- src/modbus-tcp.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index e39d37aa6..4c3c77788 100644 --- a/configure.ac +++ b/configure.ac @@ -105,7 +105,7 @@ AC_CHECK_DECLS([__CYGWIN__]) AC_SEARCH_LIBS(accept, network socket) # Checks for library functions. -AC_CHECK_FUNCS([accept4 getaddrinfo gettimeofday inet_pton inet_ntop select socket strerror strlcpy]) +AC_CHECK_FUNCS([accept4 gai_strerror getaddrinfo gettimeofday inet_pton inet_ntop select socket strerror strlcpy]) # Required for MinGW with GCC v4.8.1 on Win7 AC_DEFINE(WINVER, 0x0501, _) diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index ba9cf05a2..a1f369084 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -403,7 +403,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service, &ai_hints, &ai_list); if (rc != 0) { if (ctx->debug) { +#ifdef HAVE_GAI_STRERROR fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc)); +#else + fprintf(stderr, "Error returned by getaddrinfo: %d\n", rc); +#endif } freeaddrinfo(ai_list); errno = ECONNREFUSED; @@ -629,7 +633,11 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) rc = getaddrinfo(node, service, &ai_hints, &ai_list); if (rc != 0) { if (ctx->debug) { +#ifdef HAVE_GAI_STRERROR fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc)); +#else + fprintf(stderr, "Error returned by getaddrinfo: %d\n", rc); +#endif } freeaddrinfo(ai_list); errno = ECONNREFUSED;