/* This work is licensed under a Creative Commons CCZero 1.0 Universal License. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */ #if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501) /* Assume the target is newer than Windows XP */ # undef WINVER # undef _WIN32_WINDOWS # undef _WIN32_WINNT # define WINVER 0x0501 # define _WIN32_WINDOWS 0x0501 # define _WIN32_WINNT 0x0501 #endif #include "ua_network_tcp.h" #include "ua_log_stdout.h" #include "queue.h" #include // malloc, free #include // snprintf #include // memset #include #if UNDER_CE #define errno WSAGetLastError() #endif #ifdef _WIN32 # ifndef __clang__ # include # endif /* inet_ntoa is deprecated on MSVC but used for compatibility */ # define _WINSOCK_DEPRECATED_NO_WARNINGS # include # include # define CLOSESOCKET(S) closesocket((SOCKET)S) # define ssize_t int # define WIN32_INT (int) #else # define CLOSESOCKET(S) close(S) # define SOCKET int # define WIN32_INT # include # include # include # include # include # include // read, write, close # include # ifdef __QNX__ # include # endif #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) # include # if defined(BSD) # include # endif #endif # ifndef __CYGWIN__ # include # endif #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) */ #if defined(_WIN32) || defined(__OpenBSD__) || \ (defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \ (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \ !defined(__GNU_LIBRARY__)) # 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 # define UA_fd_set(fd, fds) FD_SET(fd, fds) # define UA_fd_isset(fd, fds) FD_ISSET(fd, fds) #endif #ifdef UA_ENABLE_MULTITHREADING # include #endif #ifdef _WIN32 #define errno__ WSAGetLastError() # define INTERRUPTED WSAEINTR # define WOULDBLOCK WSAEWOULDBLOCK # define AGAIN WSAEWOULDBLOCK #else # define errno__ errno # define INTERRUPTED EINTR # define WOULDBLOCK EWOULDBLOCK # define AGAIN EAGAIN #endif /****************************/ /* Generic Socket Functions */ /****************************/ static void socket_close(UA_Connection *connection) { connection->state = UA_CONNECTION_CLOSED; shutdown((SOCKET)connection->sockfd,2); CLOSESOCKET(connection->sockfd); } static UA_StatusCode socket_write(UA_Connection *connection, UA_ByteString *buf) { size_t nWritten = 0; do { ssize_t n = 0; do { /* If the OS throws EMSGSIZE, force a smaller packet size: * size_t bytes_to_send = buf->length - nWritten > 1024 ? 1024 : buf->length - nWritten; */ size_t bytes_to_send = buf->length - nWritten; n = send((SOCKET)connection->sockfd, (const char*)buf->data + nWritten, WIN32_INT bytes_to_send, 0); if(n < 0 && errno__ != INTERRUPTED && errno__ != AGAIN) { connection->close(connection); socket_close(connection); UA_ByteString_deleteMembers(buf); return UA_STATUSCODE_BADCONNECTIONCLOSED; } } while(n < 0); nWritten += (size_t)n; } while(nWritten < buf->length); UA_ByteString_deleteMembers(buf); return UA_STATUSCODE_GOOD; } static UA_StatusCode socket_recv(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout) { response->data = (UA_Byte *)malloc(connection->localConf.recvBufferSize); if(!response->data) { response->length = 0; return UA_STATUSCODE_BADOUTOFMEMORY; /* not enough memory retry */ } if(timeout > 0) { /* currently, only the client uses timeouts */ #ifndef _WIN32 UA_UInt32 timeout_usec = timeout * 1000; # ifdef __APPLE__ struct timeval tmptv = {(long int)(timeout_usec / 1000000), timeout_usec % 1000000}; # else struct timeval tmptv = {(long int)(timeout_usec / 1000000), (long int)(timeout_usec % 1000000)}; # endif int ret = setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tmptv, sizeof(struct timeval)); #else DWORD timeout_dw = timeout; int ret = setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_dw, sizeof(DWORD)); #endif if(0 != ret) { UA_ByteString_deleteMembers(response); socket_close(connection); return UA_STATUSCODE_BADCONNECTIONCLOSED; } } #ifdef __CYGWIN__ /* Workaround for https://cygwin.com/ml/cygwin/2013-07/msg00107.html */ ssize_t ret; if(timeout > 0) { fd_set fdset; FD_ZERO(&fdset); UA_fd_set(connection->sockfd, &fdset); UA_UInt32 timeout_usec = timeout * 1000; struct timeval tmptv = {(long int)(timeout_usec / 1000000), (long int)(timeout_usec % 1000000)}; int retval = select(connection->sockfd+1, &fdset, NULL, NULL, &tmptv); if(retval && UA_fd_isset(connection->sockfd, &fdset)) { ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0); } else { ret = 0; } } else { ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0); } #else ssize_t ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0); #endif /* server has closed the connection */ if(ret == 0) { UA_ByteString_deleteMembers(response); socket_close(connection); return UA_STATUSCODE_BADCONNECTIONCLOSED; } /* error case */ if(ret < 0) { UA_ByteString_deleteMembers(response); if(errno__ == INTERRUPTED || (timeout > 0) ? false : (errno__ == EAGAIN || errno__ == WOULDBLOCK)) return UA_STATUSCODE_GOOD; /* statuscode_good but no data -> retry */ socket_close(connection); return UA_STATUSCODE_BADCONNECTIONCLOSED; } /* default case */ response->length = (size_t)ret; return UA_STATUSCODE_GOOD; } static UA_StatusCode socket_set_nonblocking(SOCKET sockfd) { #ifdef _WIN32 u_long iMode = 1; if(ioctlsocket(sockfd, FIONBIO, &iMode) != NO_ERROR) return UA_STATUSCODE_BADINTERNALERROR; #else int opts = fcntl(sockfd, F_GETFL); if(opts < 0 || fcntl(sockfd, F_SETFL, opts|O_NONBLOCK) < 0) return UA_STATUSCODE_BADINTERNALERROR; #endif return UA_STATUSCODE_GOOD; } /***************************/ /* Server NetworkLayer TCP */ /***************************/ /** * For the multithreaded mode, assume a single thread that periodically "gets * work" from the network layer. In addition, several worker threads are * asynchronously calling into the callbacks of the UA_Connection that holds a * single connection. * * Creating a connection: When "GetJobs" encounters a new connection, it creates * a UA_Connection with the socket information. This is added to the mappings * array that links sockets to UA_Connection structs. * * Reading data: In "GetJobs", we listen on the sockets in the mappings array. * If data arrives (or the connection closes), a WorkItem is created that * carries the work and a pointer to the connection. * * Closing a connection: Closing can happen in two ways. Either it is triggered * by the server in an asynchronous callback. Or the connection is close by the * client and this is detected in "GetJobs". The server needs to do some * internal cleanups (close attached securechannels, etc.). So even when a * closed connection is detected in "GetJobs", we trigger the server to close * the connection (with a WorkItem) and continue from the callback. * * - Server calls close-callback: We close the socket, set the connection-state * to closed and add the connection to a linked list from which it is deleted * later. The connection cannot be freed right away since other threads might * still be using it. * * - GetJobs: We remove the connection from the mappings array. In the * non-multithreaded case, the connection is freed. For multithreading, we * return a workitem that is delayed, i.e. that is called only after all * workitems created before are finished in all threads. This workitems * contains a callback that goes through the linked list of connections to be * freed. */ #define MAXBACKLOG 100 /* UA_Connection is the first element. We can exchange pointers to the first * element and free the entire structure. */ typedef struct ConnectionEntry { UA_Connection connection; LIST_ENTRY(ConnectionEntry) pointers; } ConnectionEntry; typedef struct { UA_ConnectionConfig conf; UA_UInt16 port; /* open sockets and connections */ UA_Int32 serversockfd; LIST_HEAD(, ConnectionEntry) connections; } ServerNetworkLayerTCP; static UA_StatusCode ServerNetworkLayerGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) { if(length > connection->remoteConf.recvBufferSize) return UA_STATUSCODE_BADCOMMUNICATIONERROR; return UA_ByteString_allocBuffer(buf, length); } static void ServerNetworkLayerReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) { UA_ByteString_deleteMembers(buf); } static void ServerNetworkLayerReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) { UA_ByteString_deleteMembers(buf); } static void ServerNetworkLayerTCP_freeConnection(UA_Connection *connection) { UA_Connection_deleteMembers(connection); free(connection); } /* Callback triggered from the server (any thread). Only "shutdown" here. This * triggers the select, where the connection is closed and freed in the server's * mainloop. */ static void ServerNetworkLayerTCP_closeConnection(UA_Connection *connection) { connection->state = UA_CONNECTION_CLOSED; UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Connection %i | Force closing the connection", connection->sockfd); shutdown(connection->sockfd, 2); } static UA_StatusCode ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd) { /* Look up the peer name for logging */ struct sockaddr_in addr; socklen_t addrlen = sizeof(struct sockaddr_in); int res = getpeername(newsockfd, (struct sockaddr*)&addr, &addrlen); if(res == 0) { UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Connection %i | New connection over TCP from %s:%d", newsockfd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); } else { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Connection %i | New connection over TCP, " "getpeername failed with errno %i", newsockfd, errno); } /* Allocate and initialize the connection */ ConnectionEntry *e = (ConnectionEntry*)malloc(sizeof(ConnectionEntry)); if(!e) return UA_STATUSCODE_BADOUTOFMEMORY; UA_Connection *c = &e->connection; memset(c, 0, sizeof(UA_Connection)); c->sockfd = newsockfd; c->handle = layer; c->localConf = layer->conf; c->remoteConf = layer->conf; c->send = socket_write; c->close = ServerNetworkLayerTCP_closeConnection; c->free = ServerNetworkLayerTCP_freeConnection; c->getSendBuffer = ServerNetworkLayerGetSendBuffer; c->releaseSendBuffer = ServerNetworkLayerReleaseSendBuffer; c->releaseRecvBuffer = ServerNetworkLayerReleaseRecvBuffer; c->state = UA_CONNECTION_OPENING; /* Add to the linked list */ LIST_INSERT_HEAD(&layer->connections, e, pointers); return UA_STATUSCODE_GOOD; } static UA_StatusCode ServerNetworkLayerTCP_start(UA_ServerNetworkLayer *nl) { ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle; /* Get the discovery url from the hostname */ UA_String du = UA_STRING_NULL; char hostname[256]; if(gethostname(hostname, 255) == 0) { char discoveryUrl[256]; #ifndef _MSC_VER du.length = (size_t)snprintf(discoveryUrl, 255, "opc.tcp://%s:%d", hostname, layer->port); #else du.length = (size_t)_snprintf_s(discoveryUrl, 255, _TRUNCATE, "opc.tcp://%s:%d", hostname, layer->port); #endif du.data = (UA_Byte*)discoveryUrl; } UA_String_copy(&du, &nl->discoveryUrl); /* Create the server socket */ SOCKET newsock = socket(AF_INET6, SOCK_STREAM, 0); #ifdef _WIN32 if(newsock == INVALID_SOCKET) #else if(newsock < 0) #endif { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Error opening the server socket"); return UA_STATUSCODE_BADINTERNALERROR; } /* Set socket options */ int optval = 1; if(setsockopt(newsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof(optval)) == -1 || socket_set_nonblocking(newsock) != UA_STATUSCODE_GOOD) { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Error during setting of server socket options"); CLOSESOCKET(newsock); return UA_STATUSCODE_BADINTERNALERROR; } /* Bind socket to address */ struct sockaddr_in6 serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin6_family = AF_INET6; serv_addr.sin6_port = htons(layer->port); serv_addr.sin6_addr = in6addr_any; if(bind(newsock, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Error during binding of the server socket"); CLOSESOCKET(newsock); return UA_STATUSCODE_BADINTERNALERROR; } /* Start listening */ if(listen(newsock, MAXBACKLOG) < 0) { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Error listening on server socket"); CLOSESOCKET(newsock); return UA_STATUSCODE_BADINTERNALERROR; } layer->serversockfd = (UA_Int32)newsock; /* cast on win32 */ UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "TCP network layer listening on %.*s", nl->discoveryUrl.length, nl->discoveryUrl.data); return UA_STATUSCODE_GOOD; } /* Remove closed connections before going into "listen". Returns whether a * connection was removed. */ static void removeClosedConnections(ServerNetworkLayerTCP *layer, UA_Server *server) { ConnectionEntry *e, *e_tmp; LIST_FOREACH_SAFE(e, &layer->connections, pointers, e_tmp) { if(e->connection.state != UA_CONNECTION_CLOSED) continue; LIST_REMOVE(e, pointers); UA_Server_removeConnection(server, &e->connection); } } /* After every select, we need to reset the sockets we want to listen on */ static UA_Int32 setFDSet(ServerNetworkLayerTCP *layer, fd_set *fdset) { FD_ZERO(fdset); UA_fd_set(layer->serversockfd, fdset); UA_Int32 highestfd = layer->serversockfd; ConnectionEntry *e; LIST_FOREACH(e, &layer->connections, pointers) { UA_fd_set(e->connection.sockfd, fdset); if(e->connection.sockfd > highestfd) highestfd = e->connection.sockfd; } return highestfd; } static UA_StatusCode ServerNetworkLayerTCP_listen(UA_ServerNetworkLayer *nl, UA_Server *server, UA_UInt16 timeout) { /* Every open socket can generate two jobs */ ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle; /* Remove closed sockets */ removeClosedConnections(layer, server); /* Listen on open sockets (including the server) */ fd_set fdset, errset; UA_Int32 highestfd = setFDSet(layer, &fdset); setFDSet(layer, &errset); struct timeval tmptv = {0, timeout * 1000}; UA_Int32 resultsize = select(highestfd+1, &fdset, NULL, &errset, &tmptv); /* Accept new connection via the server socket (can only be a single one) */ if(UA_fd_isset(layer->serversockfd, &fdset)) { --resultsize; SOCKET newsockfd = accept((SOCKET)layer->serversockfd, NULL, NULL); #ifdef _WIN32 if(newsockfd != INVALID_SOCKET) #else if(newsockfd >= 0) #endif { socket_set_nonblocking(newsockfd); /* Do not merge packets on the socket (disable Nagle's algorithm) */ int i = 1; setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY, (const char *)&i, sizeof(i)); ServerNetworkLayerTCP_add(layer, (UA_Int32)newsockfd); } } /* Read from established sockets */ ConnectionEntry *e, *e_tmp; LIST_FOREACH_SAFE(e, &layer->connections, pointers, e_tmp) { UA_ByteString buf = UA_BYTESTRING_NULL; if(!UA_fd_isset(e->connection.sockfd, &errset) && !UA_fd_isset(e->connection.sockfd, &fdset)) continue; UA_StatusCode retval = socket_recv(&e->connection, &buf, 0); if(retval == UA_STATUSCODE_GOOD) { UA_Server_processBinaryMessage(server, &e->connection, &buf); } else if (retval == UA_STATUSCODE_BADCONNECTIONCLOSED) { LIST_REMOVE(e, pointers); UA_Server_removeConnection(server, &e->connection); } } return UA_STATUSCODE_GOOD; } static void ServerNetworkLayerTCP_stop(UA_ServerNetworkLayer *nl, UA_Server *server) { ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle; UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Shutting down the TCP network layer"); shutdown((SOCKET)layer->serversockfd,2); CLOSESOCKET(layer->serversockfd); ConnectionEntry *e, *e_tmp; LIST_FOREACH_SAFE(e, &layer->connections, pointers, e_tmp) { LIST_REMOVE(e, pointers); UA_Server_removeConnection(server, &e->connection); } #ifdef _WIN32 WSACleanup(); #endif } /* run only when the server is stopped */ static void ServerNetworkLayerTCP_deleteMembers(UA_ServerNetworkLayer *nl) { ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle; UA_String_deleteMembers(&nl->discoveryUrl); UA_free(layer); } UA_ServerNetworkLayer UA_ServerNetworkLayerTCP(UA_ConnectionConfig conf, UA_UInt16 port) { #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData); #endif UA_ServerNetworkLayer nl; memset(&nl, 0, sizeof(UA_ServerNetworkLayer)); ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)calloc(1,sizeof(ServerNetworkLayerTCP)); if(!layer) return nl; layer->conf = conf; layer->port = port; nl.handle = layer; nl.start = ServerNetworkLayerTCP_start; nl.listen = ServerNetworkLayerTCP_listen; nl.stop = ServerNetworkLayerTCP_stop; nl.deleteMembers = ServerNetworkLayerTCP_deleteMembers; return nl; } /***************************/ /* Client NetworkLayer TCP */ /***************************/ static UA_StatusCode ClientNetworkLayerGetBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) { if(length > connection->remoteConf.recvBufferSize) return UA_STATUSCODE_BADCOMMUNICATIONERROR; if(connection->state == UA_CONNECTION_CLOSED) return UA_STATUSCODE_BADCONNECTIONCLOSED; return UA_ByteString_allocBuffer(buf, connection->remoteConf.recvBufferSize); } static void ClientNetworkLayerReleaseBuffer(UA_Connection *connection, UA_ByteString *buf) { UA_ByteString_deleteMembers(buf); } static void ClientNetworkLayerClose(UA_Connection *connection) { #ifdef UA_ENABLE_MULTITHREADING if(uatomic_xchg(&connection->state, UA_CONNECTION_CLOSED) == UA_CONNECTION_CLOSED) return; #else if(connection->state == UA_CONNECTION_CLOSED) return; connection->state = UA_CONNECTION_CLOSED; #endif socket_close(connection); } /* we have no networklayer. instead, attach the reusable buffer to the handle */ UA_Connection UA_ClientConnectionTCP(UA_ConnectionConfig conf, const char *endpointUrl) { #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData); #endif UA_Connection connection; memset(&connection, 0, sizeof(UA_Connection)); connection.state = UA_CONNECTION_OPENING; connection.localConf = conf; connection.remoteConf = conf; connection.send = socket_write; connection.recv = socket_recv; connection.close = ClientNetworkLayerClose; connection.free = NULL; /* Not used in the client */ connection.getSendBuffer = ClientNetworkLayerGetBuffer; connection.releaseSendBuffer = ClientNetworkLayerReleaseBuffer; connection.releaseRecvBuffer = ClientNetworkLayerReleaseBuffer; UA_String endpointUrlString = UA_STRING((char*)(uintptr_t)endpointUrl); UA_String hostnameString = UA_STRING_NULL; UA_String pathString = UA_STRING_NULL; UA_UInt16 port = 0; char hostname[512]; UA_StatusCode parse_retval = UA_parseEndpointUrl(&endpointUrlString, &hostnameString, &port, &pathString); if(parse_retval != UA_STATUSCODE_GOOD || hostnameString.length > 511) { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Server url is invalid: %s", endpointUrl); return connection; } if(port == 0) { port = 4840; UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "No port defined, using standard port %d", port); } memcpy(hostname, hostnameString.data, hostnameString.length); hostname[hostnameString.length] = 0; struct addrinfo hints, *server; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_INET; char portStr[6]; #ifndef _MSC_VER snprintf(portStr, 6, "%d", port); #else _snprintf_s(portStr, 6, _TRUNCATE, "%d", port); #endif int error = getaddrinfo(hostname, portStr, &hints, &server); if(error != 0 || !server) { UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "DNS lookup of %s failed with error %s", hostname, gai_strerror(error)); return connection; } /* Get a socket */ SOCKET clientsockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol); #ifdef _WIN32 if(clientsockfd == INVALID_SOCKET) { #else if(clientsockfd < 0) { #endif UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Could not create client socket"); freeaddrinfo(server); return connection; } /* Connect to the server */ connection.sockfd = (UA_Int32)clientsockfd; /* cast for win32 */ error = connect(clientsockfd, server->ai_addr, WIN32_INT server->ai_addrlen); freeaddrinfo(server); if(error < 0) { ClientNetworkLayerClose(&connection); #ifdef _WIN32 wchar_t *s = NULL; FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&s, 0, NULL); UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Connection to %s failed. Error: %d: %S", endpointUrl, WSAGetLastError(), s); LocalFree(s); #else UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Connection to %s failed. Error: %d: %s", endpointUrl, errno, strerror(errno)); #endif return connection; } #ifdef SO_NOSIGPIPE int val = 1; int sso_result = setsockopt(connection.sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)); if(sso_result < 0) UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK, "Couldn't set SO_NOSIGPIPE"); #endif return connection; }