Jose Cabral 6 anni fa
parent
commit
24aeec5209

+ 24 - 1
arch/common/lwip62541.h

@@ -16,6 +16,8 @@
 
 #include <lwip/tcpip.h>
 #include <lwip/netdb.h>
+#include <lwip/init.h>
+#include <lwip/sockets.h>
 #define sockaddr_storage sockaddr
 
 #define OPTVAL_TYPE int
@@ -23,7 +25,7 @@
 #define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
 #define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
 
-#define UA_IPV6 0
+#define UA_IPV6 LWIP_IPV6
 #define UA_SOCKET int
 #define UA_INVALID_SOCKET -1
 #define UA_ERRNO errno
@@ -35,6 +37,10 @@
 
 #define UA_send lwip_send
 #define UA_recv lwip_recv
+#define UA_sendto lwip_sendto
+#define UA_recvfrom lwip_recvfrom
+#define UA_htonl lwip_htonl
+#define UA_ntohl lwip_ntohl
 #define UA_close lwip_close
 #define UA_select lwip_select
 #define UA_shutdown lwip_shutdown
@@ -49,6 +55,23 @@
 #define UA_gethostname gethostname_lwip
 #define UA_getaddrinfo lwip_getaddrinfo
 
+#if UA_IPV6
+# define UA_inet_pton(af, src, dst) \
+    (((af) == AF_INET6) ? ip6addr_aton((src),(ip6_addr_t*)(dst)) \
+     : (((af) == AF_INET) ? ip4addr_aton((src),(ip4_addr_t*)(dst)) : 0))
+#else
+# define UA_inet_pton(af, src, dst) \
+     (((af) == AF_INET) ? ip4addr_aton((src),(ip4_addr_t*)(dst)) : 0)
+#endif
+
+#if UA_IPV6
+# define UA_if_nametoindex lwip_if_nametoindex
+
+# if LWIP_VERSION_IS_RELEASE //lwip_if_nametoindex is not yet released
+unsigned int lwip_if_nametoindex(const char *ifname);
+# endif
+#endif
+
 int gethostname_lwip(char* name, size_t len);
 
 #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP

+ 8 - 0
arch/eCos/ua_architecture.h

@@ -43,6 +43,10 @@
 #define UA_getnameinfo getnameinfo
 #define UA_send send
 #define UA_recv recv
+#define UA_sendto sendto
+#define UA_recvfrom recvfrom
+#define UA_htonl htonl
+#define UA_ntohl ntohl
 #define UA_close close
 #define UA_select select
 #define UA_shutdown shutdown
@@ -56,6 +60,10 @@
 #define UA_setsockopt setsockopt
 #define UA_freeaddrinfo freeaddrinfo
 #define UA_gethostname gethostname_ecos
+#define UA_inet_pton(af,src,dst) inet_pton(af, src, (char*) dst)
+#if UA_IPV6
+# define UA_if_nametoindex if_nametoindex
+#endif
 
 int gethostname_ecos(char* name, size_t len);
 

+ 8 - 0
arch/freertosLWIP/ua_architecture_functions.c

@@ -36,4 +36,12 @@ void UA_deinitialize_architecture_network(void){
   return;
 }
 
+#if UA_IPV6
+# if LWIP_VERSION_IS_RELEASE //lwip_if_nametoindex is not yet released
+unsigned int lwip_if_nametoindex(const char *ifname){
+  return 1; //TODO: assume for now that it only has one interface
+}
+# endif //LWIP_VERSION_IS_RELEASE
+#endif //UA_IPV6
+
 #endif /* UA_ARCHITECTURE_FREERTOSLWIP */

+ 11 - 1
arch/posix/ua_architecture.h

@@ -29,6 +29,8 @@
 #include <netdb.h>
 #include <sys/ioctl.h>
 #include <sys/select.h>
+#include <sys/types.h>
+#include <net/if.h>
 #ifndef UA_sleep_ms
 #define UA_sleep_ms(X) usleep(X * 1000)
 #else /* UA_sleep_ms */
@@ -55,7 +57,6 @@ void UA_sleep_ms(size_t ms);
 # include <netinet/tcp.h>
 #endif
 
-
 /* Thread-Local Storage
  * --------------------
  * Thread-local storage is not required by the main library functionality. It is
@@ -72,6 +73,7 @@ void UA_sleep_ms(size_t ms);
 # define UA_THREAD_LOCAL
 #endif
 
+
 /* unsigned int for windows and workaround to a glibc bug */
 /* Additionally if GNU_LIBRARY is not defined, it may be using
  * musl libc (e.g. Docker Alpine) */
@@ -103,6 +105,10 @@ void UA_sleep_ms(size_t ms);
 #define UA_getnameinfo getnameinfo
 #define UA_send send
 #define UA_recv recv
+#define UA_sendto sendto
+#define UA_recvfrom recvfrom
+#define UA_htonl htonl
+#define UA_ntohl ntohl
 #define UA_close close
 #define UA_select select
 #define UA_shutdown shutdown
@@ -116,6 +122,10 @@ void UA_sleep_ms(size_t ms);
 #define UA_setsockopt setsockopt
 #define UA_freeaddrinfo freeaddrinfo
 #define UA_gethostname gethostname
+#define UA_inet_pton inet_pton
+#if UA_IPV6
+# define UA_if_nametoindex if_nametoindex
+#endif
 
 #include <stdlib.h>
 #define UA_free free

+ 26 - 0
arch/ua_architecture_functions.h

@@ -77,6 +77,10 @@ int UA_sleep_ms(unsigned int miliSeconds); //suspend the thread for a certain am
 ssize_t UA_send(UA_SOCKET sockfd, const void *buf, size_t len, int flags); //equivalent to posix send implementation
 #endif
 
+#ifndef UA_sendto
+ssize_t sendto(UA_SOCKET sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); //equivalent to posix sendto implementation
+#endif
+
 #ifndef UA_select
 int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); //equivalent to posix select implementation
 #endif
@@ -85,6 +89,10 @@ int UA_select(UA_SOCKET nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptf
 ssize_t UA_recv(UA_SOCKET sockfd, void *buf, size_t len, int flags); //equivalent to posix recv implementation
 #endif
 
+#ifndef UA_recvfrom
+ssize_t recvfrom(UA_SOCKET sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
+#endif
+
 #ifndef UA_shutdown
 int UA_shutdown(UA_SOCKET sockfd, int how); //equivalent to posix shutdown implementation
 #endif
@@ -125,6 +133,24 @@ int UA_fd_isset(UA_SOCKET fd, fd_set *set);//equivalent to posix FD_ISSET implem
 int UA_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);//equivalent to posix getaddrinfo implementation
 #endif
 
+#ifndef UA_htonl
+uint32_t UA_htonl(uint32_t hostlong);//equivalent to posix UA_htonl implementation
+#endif
+
+#ifndef UA_ntohl
+uint32_t UA_ntohl(uint32_t netlong);//equivalent to posix ntohl implementation
+#endif
+
+#ifndef UA_inet_pton
+int UA_inet_pton(int af, const char *src, void *dst);//equivalent to ANSI inet_pton implementation
+#endif
+
+#if UA_IPV6
+# ifndef UA_if_nametoindex
+unsigned int UA_if_nametoindex(const char *ifname);//equivalent to posix if_nametoindex implementation
+# endif
+#endif
+
 #ifndef UA_socket_set_blocking
 unsigned int UA_socket_set_blocking(UA_SOCKET sockfd);//set a socket as blocking. Returns 0 if OK, other value otherwise
 #endif

+ 2 - 2
arch/ua_network_tcp.c

@@ -374,14 +374,14 @@ setFDSet(ServerNetworkLayerTCP *layer, fd_set *fdset) {
     for(UA_UInt16 i = 0; i < layer->serverSocketsSize; i++) {
         UA_fd_set(layer->serverSockets[i], fdset);
         if(layer->serverSockets[i] > highestfd)
-            highestfd = layer->serverSockets[i];
+            highestfd = (UA_Int32)layer->serverSockets[i];
     }
 
     ConnectionEntry *e;
     LIST_FOREACH(e, &layer->connections, pointers) {
         UA_fd_set(e->connection.sockfd, fdset);
         if(e->connection.sockfd > highestfd)
-            highestfd = e->connection.sockfd;
+            highestfd = (UA_Int32)e->connection.sockfd;
     }
 
     return highestfd;

+ 8 - 1
arch/vxworks/ua_architecture.h

@@ -41,7 +41,6 @@
 #endif
 #define UA_BINARY_OVERLAYABLE_FLOAT 1
 
-
 /* Thread-Local Storage
  * --------------------
  * Thread-local storage is not required by the main library functionality. It is
@@ -85,6 +84,10 @@
 #define UA_getnameinfo getnameinfo
 #define UA_send send
 #define UA_recv recv
+#define UA_sendto sendto
+#define UA_recvfrom recvfrom
+#define UA_htonl htonl
+#define UA_ntohl ntohl
 #define UA_close close
 #define UA_select select
 #define UA_shutdown shutdown
@@ -98,6 +101,10 @@
 #define UA_setsockopt setsockopt
 #define UA_freeaddrinfo freeaddrinfo
 #define UA_gethostname gethostname
+#define UA_inet_pton inet_pton
+#if UA_IPV6
+# define UA_if_nametoindex if_nametoindex
+#endif
 
 #include <stdlib.h>
 #define UA_free free

+ 39 - 15
arch/win32/ua_architecture.h

@@ -52,6 +52,11 @@
 # define UA_THREAD_LOCAL
 #endif
 
+#ifdef _WIN32_WINNT
+#undef _WIN32_WINNT
+#endif
+#define _WIN32_WINNT 0x0600 //windows vista version, which included InepPton
+
 #include <stdlib.h>
 #if defined(_WIN32) && !defined(__clang__)
 # include <malloc.h>
@@ -60,8 +65,8 @@
 #include <stdio.h>
 #include <errno.h>
 #include <winsock2.h>
-#include <ws2tcpip.h>
 #include <windows.h>
+#include <ws2tcpip.h>
 
 #ifdef _MSC_VER
 # ifndef UNDER_CE
@@ -73,8 +78,6 @@
 # define UA_access access
 #endif
 
-
-
 #ifdef POP_SLIST_ENTRY
 # undef SLIST_ENTRY
 # undef POP_SLIST_ENTRY
@@ -91,19 +94,19 @@ E.g. see unit tests. */
 void UA_sleep_ms(size_t ms);
 #endif
 
-#define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
-#define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
-
-#ifdef UNDER_CE
-# define errno
-#endif
 
 // Windows does not support ansi colors
 // #define UA_ENABLE_LOG_COLORS
 
 #define UA_IPV6 1
-#define UA_SOCKET int
-#define UA_INVALID_SOCKET -1
+
+#if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
+# define UA_SOCKET int
+# define UA_INVALID_SOCKET -1
+#else
+# define UA_SOCKET SOCKET
+# define UA_INVALID_SOCKET INVALID_SOCKET
+#endif
 #define UA_ERRNO WSAGetLastError()
 #define UA_INTERRUPTED WSAEINTR
 #define UA_AGAIN WSAEWOULDBLOCK
@@ -111,22 +114,43 @@ void UA_sleep_ms(size_t ms);
 #define UA_WOULDBLOCK WSAEWOULDBLOCK
 #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
 
+#define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
+#define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
+
+#ifdef UNDER_CE
+# define errno
+#endif
+
 #define UA_getnameinfo getnameinfo
-#define UA_send send
+#define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
 #define UA_recv recv
+#define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
+#define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
+#define UA_htonl htonl
+#define UA_ntohl ntohl
 #define UA_close closesocket
-#define UA_select select
+#define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
 #define UA_shutdown shutdown
 #define UA_socket socket
 #define UA_bind bind
 #define UA_listen listen
 #define UA_accept accept
-#define UA_connect connect
+#define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
 #define UA_getaddrinfo getaddrinfo
 #define UA_getsockopt getsockopt
-#define UA_setsockopt setsockopt
+#define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
 #define UA_freeaddrinfo freeaddrinfo
 #define UA_gethostname gethostname
+#define UA_inet_pton InetPton
+
+#if UA_IPV6
+# include <Iphlpapi.h>
+# define UA_if_nametoindex if_nametoindex
+#endif
+
+#ifdef maxStringLength //defined in mingw32
+# undef maxStringLength
+#endif
 
 #define UA_free free
 #define UA_malloc malloc

+ 1 - 2
arch/win32/ua_architecture_functions.c

@@ -24,8 +24,7 @@ unsigned int UA_socket_set_nonblocking(UA_SOCKET sockfd){
 
 void UA_initialize_architecture_network(void){
   WSADATA wsaData;
-  WORD wVersionRequested = MAKEWORD(2, 2);
-  WSAStartup(wVersionRequested, &wsaData);
+  WSAStartup(MAKEWORD(2, 2), &wsaData);
 }
 
 void UA_deinitialize_architecture_network(void){

+ 1 - 1
include/ua_plugin_pubsub.h

@@ -47,7 +47,7 @@ struct UA_PubSubChannel{
     UA_UInt32 publisherId;                                  // unique identifier
     UA_PubSubChannelState state;
     UA_PubSubConnectionConfig *connectionConfig;            //link to parent connection config
-    UA_Int32 sockfd;
+    UA_SOCKET sockfd;
     void *handle;                                           //implementation specific data
     /*@info for handle: each network implementation should provide an structure
     * UA_PubSubChannelData[ImplementationName] This structure can be used by the

+ 76 - 95
plugins/ua_network_pubsub_udp.c

@@ -3,58 +3,9 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  *
  * Copyright (c) 2017-2018 Fraunhofer IOSB (Author: Andreas Ebner)
+ * Copyright 2018 (c) Jose Cabral, fortiss GmbH
  */
 
-/* Enable POSIX features */
-#if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
-# define _XOPEN_SOURCE 600
-#endif
-#ifndef _DEFAULT_SOURCE
-# define _DEFAULT_SOURCE
-#endif
-
-/* On older systems we need to define _BSD_SOURCE.
- * _DEFAULT_SOURCE is an alias for that. */
-#ifndef _BSD_SOURCE
-# define _BSD_SOURCE
-#endif
-
- /* Disable some security warnings on MSVC */
-#ifdef _MSC_VER
-# define _CRT_SECURE_NO_WARNINGS
-#endif
-
- /* Assume that Windows versions are newer than Windows XP */
-#if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
-# undef WINVER
-# undef _WIN32_WINDOWS
-# undef _WIN32_WINNT
-# define WINVER 0x0501
-# define _WIN32_WINDOWS 0x0501
-# define _WIN32_WINNT 0x0501
-#endif
-
-#ifdef _WIN32
-# include <winsock2.h>
-# include <ws2tcpip.h>
-# include <Iphlpapi.h>
-# define CLOSESOCKET(S) closesocket((SOCKET)S)
-# define ssize_t int
-# define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
-# define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
-#else /* _WIN32 */
-#  define CLOSESOCKET(S) close(S)
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <net/if.h>
-# define UA_fd_set(fd, fds) FD_SET(fd, fds)
-# define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
-# endif /* Not Windows */
-
-#include <stdio.h>
 #include "ua_plugin_network.h"
 #include "ua_network_pubsub_udp.h"
 #include "ua_log_stdout.h"
@@ -77,10 +28,7 @@ typedef struct {
  */
 static UA_PubSubChannel *
 UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
-    #ifdef _WIN32
-        WSADATA wsaData;
-        WSAStartup(MAKEWORD(2, 2), &wsaData);
-    #endif /* Not Windows */
+    UA_initialize_architecture_network();
 
     UA_NetworkAddressUrlDataType address;
     if(UA_Variant_hasScalarType(&connectionConfig->address, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE])){
@@ -154,7 +102,7 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     char port[6];
     sprintf(port, "%u", networkPort);
 
-    if(getaddrinfo(addressAsChar, port, &hints, &requestResult) != 0) {
+    if(UA_getaddrinfo(addressAsChar, port, &hints, &requestResult) != 0) {
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "PubSub Connection creation failed. Internal error.");
         UA_free(channelDataUDPMC);
@@ -165,11 +113,11 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     //check if the ip address is a multicast address
     if(requestResult->ai_family == PF_INET){
         struct in_addr imr_interface;
-        inet_pton(AF_INET, addressAsChar, &imr_interface);
-        if((ntohl(imr_interface.s_addr) & 0xF0000000) != 0xE0000000){
+        UA_inet_pton(AF_INET, addressAsChar, &imr_interface);
+        if((UA_ntohl(imr_interface.s_addr) & 0xF0000000) != 0xE0000000){
             UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                          "PubSub Connection creation failed. No multicast address.");
-            freeaddrinfo(requestResult);
+            UA_freeaddrinfo(requestResult);
             UA_free(channelDataUDPMC);
             UA_free(newChannel);
             return NULL;
@@ -179,15 +127,15 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     }
 
     for(rp = requestResult; rp != NULL; rp = rp->ai_next){
-        newChannel->sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-        if(newChannel->sockfd != -1){
+        newChannel->sockfd = UA_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+        if(newChannel->sockfd != UA_INVALID_SOCKET){
             break; /*success*/
         }
     }
     if(!rp){
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "PubSub Connection creation failed. Internal error.");
-        freeaddrinfo(requestResult);
+        UA_freeaddrinfo(requestResult);
         UA_free(channelDataUDPMC);
         UA_free(newChannel);
         return NULL;
@@ -197,8 +145,8 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     if(!channelDataUDPMC->ai_addr){
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "PubSub Connection creation failed. Out of memory.");
-        CLOSESOCKET(newChannel->sockfd);
-        freeaddrinfo(requestResult);
+        UA_close(newChannel->sockfd);
+        UA_freeaddrinfo(requestResult);
         UA_free(channelDataUDPMC);
         UA_free(newChannel);
         return NULL;
@@ -208,24 +156,41 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     newChannel->handle = channelDataUDPMC;
 
     //Set loop back data to your host
-    if(setsockopt(newChannel->sockfd,
-                     requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6:IPPROTO_IP,
+#if UA_IPV6
+    if(UA_setsockopt(newChannel->sockfd,
+                     requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
                      requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP,
-                     (const char *)&channelDataUDPMC->enableLoopback, sizeof (channelDataUDPMC->enableLoopback)) < 0) {
+                     (const char *)&channelDataUDPMC->enableLoopback, sizeof (channelDataUDPMC->enableLoopback))
+#else
+    if(UA_setsockopt(newChannel->sockfd,
+                     IPPROTO_IP,
+                     IP_MULTICAST_LOOP,
+                     (const char *)&channelDataUDPMC->enableLoopback, sizeof (channelDataUDPMC->enableLoopback))
+#endif
+                      < 0) {
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "PubSub Connection creation failed. Loopback setup failed.");
-        CLOSESOCKET(newChannel->sockfd);
-        freeaddrinfo(requestResult);
+        UA_close(newChannel->sockfd);
+        UA_freeaddrinfo(requestResult);
         UA_free(channelDataUDPMC);
         UA_free(newChannel);
         return NULL;
     }
 
     //Set Time to live (TTL). Value of 1 prevent forward beyond the local network.
-    if(setsockopt(newChannel->sockfd,
-                  requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6:IPPROTO_IP,
-                  requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL,
-                  (const char *)&channelDataUDPMC->messageTTL, sizeof(channelDataUDPMC->messageTTL)) < 0) {
+#if UA_IPV6
+    if(UA_setsockopt(newChannel->sockfd,
+                     requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
+                     requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL,
+                     (const char *)&channelDataUDPMC->messageTTL, sizeof(channelDataUDPMC->messageTTL))
+#else
+    if(UA_setsockopt(newChannel->sockfd,
+                     IPPROTO_IP,
+                     IP_MULTICAST_TTL,
+                     (const char *)&channelDataUDPMC->messageTTL, sizeof(channelDataUDPMC->messageTTL))
+#endif
+
+                      < 0) {
         UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "PubSub Connection creation problem. Time to live setup failed.");
     }
@@ -233,7 +198,7 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
     //Set reuse address -> enables sharing of the same listening address on different sockets.
     if(channelDataUDPMC->enableReuse){
         int enableReuse = 1;
-        if(setsockopt(newChannel->sockfd,
+        if(UA_setsockopt(newChannel->sockfd,
                       SOL_SOCKET, SO_REUSEADDR,
                       (const char*)&enableReuse, sizeof(enableReuse)) < 0){
             UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
@@ -248,32 +213,47 @@ UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
         interfaceAsChar[address.networkInterface.length] = 0;
         enum{
             IPv4,
+#if UA_IPV6
             IPv6,
+#endif
             INVALID
         } ipVersion;
         union {
             struct ip_mreq ipv4;
+#if UA_IPV6
             struct ipv6_mreq ipv6;
+#endif
         } group;
-        if(inet_pton(AF_INET, interfaceAsChar, &group.ipv4.imr_interface)){
+        if(UA_inet_pton(AF_INET, interfaceAsChar, &group.ipv4.imr_interface)){
             ipVersion = IPv4;
-        } else if (inet_pton(AF_INET6, interfaceAsChar, &group.ipv6.ipv6mr_multiaddr)){
-            group.ipv6.ipv6mr_interface = if_nametoindex(interfaceAsChar);
+#if UA_IPV6
+        } else if (UA_inet_pton(AF_INET6, interfaceAsChar, &group.ipv6.ipv6mr_multiaddr)){
+            group.ipv6.ipv6mr_interface = UA_if_nametoindex(interfaceAsChar);
             ipVersion = IPv6;
+#endif
         } else {
             ipVersion = INVALID;
         }
         if(ipVersion == INVALID ||
-                setsockopt(newChannel->sockfd,
+#if UA_IPV6
+                UA_setsockopt(newChannel->sockfd,
                            requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
                            requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_IF : IP_MULTICAST_IF,
-                           ipVersion == IPv6 ? (void *) &group.ipv6.ipv6mr_interface : &group.ipv4.imr_interface,
-                           ipVersion == IPv6 ? sizeof(group.ipv6.ipv6mr_interface) : sizeof(struct in_addr)) < 0){
+                           ipVersion == IPv6 ? (const void *) &group.ipv6.ipv6mr_interface : &group.ipv4.imr_interface,
+                           ipVersion == IPv6 ? sizeof(group.ipv6.ipv6mr_interface) : sizeof(struct in_addr))
+#else
+                UA_setsockopt(newChannel->sockfd,
+                           IPPROTO_IP,
+                           IP_MULTICAST_IF,
+                           &group.ipv4.imr_interface,
+                           sizeof(struct in_addr))
+#endif
+                                                         < 0) {
             UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                            "PubSub Connection creation problem. Interface selection failed.");
         };
     }
-    freeaddrinfo(requestResult);
+    UA_freeaddrinfo(requestResult);
     newChannel->state = UA_PUBSUB_CHANNEL_PUB;
     return newChannel;
 }
@@ -294,22 +274,23 @@ UA_PubSubChannelUDPMC_regist(UA_PubSubChannel *channel, UA_ExtensionObject *tran
         struct sockaddr_in addr;
         memcpy(&addr, connectionConfig->ai_addr, sizeof(struct sockaddr_in));
         addr.sin_addr.s_addr = INADDR_ANY;
-        if (bind(channel->sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0){
+        if (UA_bind(channel->sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0){
             UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
             return UA_STATUSCODE_BADINTERNALERROR;
         }
         struct ip_mreq groupV4;
         memcpy(&groupV4.imr_multiaddr, &((const struct sockaddr_in *)connectionConfig->ai_addr)->sin_addr, sizeof(struct ip_mreq));
-        groupV4.imr_interface.s_addr = htonl(INADDR_ANY);
+        groupV4.imr_interface.s_addr = UA_htonl(INADDR_ANY);
         //multihomed hosts can join several groups on different IF, INADDR_ANY -> kernel decides
 
-        if(setsockopt(channel->sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
+        if(UA_setsockopt(channel->sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
             UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
             return UA_STATUSCODE_BADINTERNALERROR;
         }
-
+#if UA_IPV6
     } else if (connectionConfig->ai_family == PF_INET6) {//IPv6 handling
         //TODO implement regist for IPv6
+#endif
     } else {
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
         return UA_STATUSCODE_BADINTERNALERROR;
@@ -332,14 +313,16 @@ UA_PubSubChannelUDPMC_unregist(UA_PubSubChannel *channel, UA_ExtensionObject *tr
     if(connectionConfig->ai_family == PF_INET){//IPv4 handling
         struct ip_mreq groupV4;
         memcpy(&groupV4.imr_multiaddr, &((const struct sockaddr_in *)connectionConfig->ai_addr)->sin_addr, sizeof(struct ip_mreq));
-        groupV4.imr_interface.s_addr = htonl(INADDR_ANY);
+        groupV4.imr_interface.s_addr = UA_htonl(INADDR_ANY);
 
-        if(setsockopt(channel->sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
+        if(UA_setsockopt(channel->sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
             UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection unregist failed.");
             return UA_STATUSCODE_BADINTERNALERROR;
         }
+#if UA_IPV6
     } else if (connectionConfig->ai_family == PF_INET6) {//IPv6 handling
         //TODO implement unregist for IPv6
+#endif
     } else {
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection unregist failed.");
         return UA_STATUSCODE_BADINTERNALERROR;
@@ -362,7 +345,7 @@ UA_PubSubChannelUDPMC_send(UA_PubSubChannel *channel, UA_ExtensionObject *transp
     //TODO evalute: chunk messages or check against MTU?
     long nWritten = 0;
     while (nWritten < (long)buf->length) {
-        long n = sendto(channel->sockfd, buf->data, buf->length, 0,
+        long n = (long)UA_sendto(channel->sockfd, buf->data, buf->length, 0,
                         (struct sockaddr *) channelConfigUDPMC->ai_addr, sizeof(struct sockaddr_storage));
         if(n == -1L) {
             UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection sending failed.");
@@ -393,7 +376,7 @@ UA_PubSubChannelUDPMC_receive(UA_PubSubChannel *channel, UA_ByteString *message,
         UA_fd_set(channel->sockfd, &fdset);
         struct timeval tmptv = {(long int)(timeout / 1000000),
                                 (long int)(timeout % 1000000)};
-        int resultsize = select(channel->sockfd+1, &fdset, NULL,
+        int resultsize = UA_select(channel->sockfd+1, &fdset, NULL,
                                 NULL, &tmptv);
         if(resultsize == 0) {
             message->length = 0;
@@ -407,14 +390,16 @@ UA_PubSubChannelUDPMC_receive(UA_PubSubChannel *channel, UA_ByteString *message,
 
     if(channelConfigUDPMC->ai_family == PF_INET){
         ssize_t messageLength;
-        messageLength = recvfrom(channel->sockfd, message->data, message->length, 0, NULL, NULL);
+        messageLength = UA_recvfrom(channel->sockfd, message->data, message->length, 0, NULL, NULL);
         if(messageLength > 0){
             message->length = (size_t) messageLength;
         } else {
             message->length = 0;
         }
+#if UA_IPV6
     } else {
         //TODO implement recieve for IPv6
+#endif
     }
     return UA_STATUSCODE_GOOD;
 }
@@ -426,10 +411,8 @@ UA_PubSubChannelUDPMC_receive(UA_PubSubChannel *channel, UA_ByteString *message,
  */
 static UA_StatusCode
 UA_PubSubChannelUDPMC_close(UA_PubSubChannel *channel) {
-#ifdef _WIN32
-    WSACleanup();
-#endif /* Not Windows */
-    if(CLOSESOCKET(channel->sockfd) != 0){
+    UA_deinitialize_architecture_network();
+    if(UA_close(channel->sockfd) != 0){
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection delete failed.");
         return UA_STATUSCODE_BADINTERNALERROR;
     }
@@ -470,5 +453,3 @@ UA_PubSubTransportLayerUDPMP() {
     pubSubTransportLayer.createPubSubChannel = &TransportLayerUDPMC_addChannel;
     return pubSubTransportLayer;
 }
-
-#undef _POSIX_C_SOURCE

+ 1 - 1
src/pubsub/ua_pubsub_manager.c

@@ -197,7 +197,7 @@ UA_Server_addPublishedDataSet(UA_Server *server, const UA_PublishedDataSetConfig
 }
 
 UA_StatusCode
-UA_Server_removePublishedDataSet(UA_Server *server, UA_NodeId pds) {
+UA_Server_removePublishedDataSet(UA_Server *server, const UA_NodeId pds) {
     //search the identified PublishedDataSet and store the PDS index
     UA_PublishedDataSet *publishedDataSet = NULL;
     size_t publishedDataSetIndex;