libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

WIN32 compatibility for setsockopt() in modbus-tcp.c

Open desteemy opened this issue 8 months ago • 2 comments

UNIX systems <sys/socket.h> require const void *, while Windows <winsock2.h> const char *. Previous version without type casting did not build on Windows, resulting in:

CC modbus-tcp.lo modbus-tcp.c: In function '_modbus_tcp_set_ipv4_options': modbus-tcp.c:239:50: error: passing argument 4 of 'setsockopt' from incompatible pointer type [-Wincompatible-pointer-types] 239 | rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(int)); | ^~~~~~~ | | | int * In file included from modbus-tcp.c:31: C:/msys64/mingw64/include/winsock2.h:1035:88: note: expected 'const char *' but argument is of type 'int *' 1035 | WINSOCK_API_LINKAGE int WSAAPI setsockopt(SOCKET s,int level,int optname,const char *optval,int optlen); | ~~~~~~~~~~~~^~~~~~ modbus-tcp.c: In function 'modbus_tcp_listen': modbus-tcp.c:566:53: error: passing argument 4 of 'setsockopt' from incompatible pointer type [-Wincompatible-pointer-types] 566 | if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) { | ^~~~~~~ | | | int * C:/msys64/mingw64/include/winsock2.h:1035:88: note: expected 'const char *' but argument is of type 'int *' 1035 | WINSOCK_API_LINKAGE int WSAAPI setsockopt(SOCKET s,int level,int optname,const char *optval,int optlen); | ~~~~~~~~~~~~^~~~~~ modbus-tcp.c: In function 'modbus_tcp_pi_listen': modbus-tcp.c:683:58: error: passing argument 4 of 'setsockopt' from incompatible pointer type [-Wincompatible-pointer-types] 683 | rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); | ^~~~~~~ | | | int * C:/msys64/mingw64/include/winsock2.h:1035:88: note: expected 'const char *' but argument is of type 'int *' 1035 | WINSOCK_API_LINKAGE int WSAAPI setsockopt(SOCKET s,int level,int optname,const char *optval,int optlen); | ~~~~~~~~~~~~^~~~~~ make[2]: *** [Makefile:486: modbus-tcp.lo] Error 1 make[1]: *** [Makefile:505: all-recursive] Error 1 make: *** [Makefile:391: all] Error 2

#721

desteemy avatar Mar 18 '25 09:03 desteemy

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

cla-bot[bot] avatar Mar 18 '25 09:03 cla-bot[bot]

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

I have just sent it. Waiting for verification. My guess is that this is not the best option to verify that the person who sends the form is actually the person who commits the code :)

desteemy avatar Mar 18 '25 09:03 desteemy