diff --git a/components/freemodbus/tcp_master/port/port_tcp_master.c b/components/freemodbus/tcp_master/port/port_tcp_master.c index 0bd76ea..003f709 100644 --- a/components/freemodbus/tcp_master/port/port_tcp_master.c +++ b/components/freemodbus/tcp_master/port/port_tcp_master.c @@ -432,11 +432,14 @@ static BOOL xMBTCPPortMasterCheckHost(const CHAR* pcHostStr, ip_addr_t* pxHostAd struct in_addr addr4 = ((struct sockaddr_in *) (pxAddrList->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4); pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr)); - } else { + } +#if CONFIG_LWIP_IPV6 + else { struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxAddrList->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6); pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr)); } +#endif if (pxHostAddr) { *pxHostAddr = xTargetAddr; } @@ -496,7 +499,9 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo) struct in_addr addr4 = ((struct sockaddr_in *) (pxCurAddr->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&xTargetAddr), &addr4); pcStr = ip4addr_ntoa_r(ip_2_ip4(&xTargetAddr), cStr, sizeof(cStr)); - } else if (pxCurAddr->ai_family == AF_INET6) { + } +#if CONFIG_LWIP_IPV6 + else if (pxCurAddr->ai_family == AF_INET6) { struct in6_addr addr6 = ((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6); pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr)); @@ -504,7 +509,7 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo) ((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_scope_id = esp_netif_get_netif_impl_index(xMbPortConfig.pvNetIface); } - +#endif if (pxInfo->xSockId <= 0) { pxInfo->xSockId = socket(pxCurAddr->ai_family, pxCurAddr->ai_socktype, pxCurAddr->ai_protocol); if (pxInfo->xSockId < 0) { diff --git a/components/freemodbus/tcp_slave/port/port_tcp_slave.c b/components/freemodbus/tcp_slave/port/port_tcp_slave.c index 01ff9b0..d5d83e7 100644 --- a/components/freemodbus/tcp_slave/port/port_tcp_slave.c +++ b/components/freemodbus/tcp_slave/port/port_tcp_slave.c @@ -184,12 +184,11 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr) MB_PORT_CHECK((xListenSockId > 0), -1, "Incorrect listen socket ID."); // Address structure large enough for both IPv4 or IPv6 address - struct sockaddr_in6 xSrcAddr; - + struct sockaddr_storage xSrcAddr; CHAR cAddrStr[128]; int xSockId = -1; CHAR* pcStr = NULL; - socklen_t xSize = sizeof(struct sockaddr_in6); + socklen_t xSize = sizeof(struct sockaddr_storage); // Accept new socket connection if not active xSockId = accept(xListenSockId, (struct sockaddr *)&xSrcAddr, &xSize); @@ -198,11 +197,14 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr) close(xSockId); } else { // Get the sender's ip address as string - if (xSrcAddr.sin6_family == PF_INET) { + if (xSrcAddr.ss_family == PF_INET) { inet_ntoa_r(((struct sockaddr_in *)&xSrcAddr)->sin_addr.s_addr, cAddrStr, sizeof(cAddrStr) - 1); - } else if (xSrcAddr.sin6_family == PF_INET6) { - inet6_ntoa_r(xSrcAddr.sin6_addr, cAddrStr, sizeof(cAddrStr) - 1); } +#if CONFIG_LWIP_IPV6 + else if (xSrcAddr.ss_family == PF_INET6) { + inet6_ntoa_r(((struct sockaddr_in6 *)&xSrcAddr)->sin6_addr, cAddrStr, sizeof(cAddrStr) - 1); + } +#endif ESP_LOGI(MB_TCP_SLAVE_PORT_TAG, "Socket (#%d), accept client connection from address: %s", xSockId, cAddrStr); pcStr = calloc(1, strlen(cAddrStr) + 1); if (pcStr && pcIPAddr) {