/* * This work is licensed under a Creative Commons CCZero 1.0 Universal License. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */ #include "networklayer_tcp.h" #include // malloc, free #include // memset #include #ifdef _WIN32 # include # include # include # define CLOSESOCKET(S) closesocket(S) #else # include # include # include #ifndef __CYGWIN__ # include #endif # include # include //gethostbyname for the client # include // read, write, close # include #ifdef __QNX__ #include #endif # define CLOSESOCKET(S) close(S) #endif #ifdef UA_MULTITHREADING # include #endif #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif /****************************/ /* Generic Socket Functions */ /****************************/ static void socket_close(UA_Connection *connection) { connection->state = UA_CONNECTION_CLOSED; shutdown(connection->sockfd,2); CLOSESOCKET(connection->sockfd); } static UA_StatusCode socket_write(UA_Connection *connection, UA_ByteString *buf) { size_t nWritten = 0; while(buf->length > 0 && nWritten < (size_t)buf->length) { UA_Int32 n = 0; do { #ifdef _WIN32 n = send((SOCKET)connection->sockfd, (const char*)buf->data, (size_t)buf->length, 0); const int last_error = WSAGetLastError(); if(n < 0 && last_error != WSAEINTR && last_error != WSAEWOULDBLOCK) { connection->close(connection); socket_close(connection); #ifdef UA_MULTITHREADING UA_ByteString_deleteMembers(buf); #endif return UA_STATUSCODE_BADCONNECTIONCLOSED; } #else n = send(connection->sockfd, (const char*)buf->data, (size_t)buf->length, MSG_NOSIGNAL); if(n == -1L && errno != EINTR && errno != EAGAIN) { connection->close(connection); socket_close(connection); #ifdef UA_MULTITHREADING UA_ByteString_deleteMembers(buf); #endif return UA_STATUSCODE_BADCONNECTIONCLOSED; } #endif } while (n == -1L); nWritten += n; } #ifdef UA_MULTITHREADING UA_ByteString_deleteMembers(buf); #endif return UA_STATUSCODE_GOOD; } static UA_StatusCode socket_recv(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout) { response->data = malloc(connection->localConf.recvBufferSize); if(!response->data) { response->length = -1; return UA_STATUSCODE_GOOD; /* not enough memory retry */ } struct timeval tmptv = {0, timeout * 1000}; if(0 != setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tmptv, sizeof(struct timeval))) { UA_ByteString_deleteMembers(response); socket_close(connection); return UA_STATUSCODE_BADINTERNALERROR; } int ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0); if(ret == 0) { free(response->data); UA_ByteString_init(response); socket_close(connection); return UA_STATUSCODE_BADCONNECTIONCLOSED; /* ret == 0 -> server has closed the connection */ } else if(ret < 0) { free(response->data); UA_ByteString_init(response); #ifdef _WIN32 const int last_error = WSAGetLastError(); if(last_error == WSAEINTR || last_error == WSAEWOULDBLOCK) #else if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) #endif return UA_STATUSCODE_GOOD; /* retry */ else { socket_close(connection); return UA_STATUSCODE_BADCONNECTIONCLOSED; } } response->length = ret; return UA_STATUSCODE_GOOD; } static UA_StatusCode socket_set_nonblocking(UA_Int32 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; } static void FreeConnectionCallback(UA_Server *server, void *ptr) { UA_Connection_deleteMembers((UA_Connection*)ptr); free(ptr); } /***************************/ /* 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 "GetWork" 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 "GetWork", 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 * "GetWork". The server needs to do some internal cleanups (close attached securechannels, etc.). * So even when a closed connection is detected in "GetWork", 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. * * - GetWork: 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 typedef struct { UA_ServerNetworkLayer layer; /* config */ UA_UInt32 port; UA_ConnectionConfig conf; /* todo: rename to localconf. */ #ifndef UA_MULTITHREADING UA_ByteString buffer; // message buffer that is reused #endif /* open sockets and connections */ fd_set fdset; UA_Int32 serversockfd; UA_Int32 highestfd; size_t mappingsSize; struct ConnectionMapping { UA_Connection *connection; UA_Int32 sockfd; } *mappings; } ServerNetworkLayerTCP; static UA_StatusCode ServerNetworkLayerGetSendBuffer(UA_Connection *connection, UA_Int32 length, UA_ByteString *buf) { if((UA_UInt32)length > connection->remoteConf.recvBufferSize) return UA_STATUSCODE_BADCOMMUNICATIONERROR; #ifdef UA_MULTITHREADING return UA_ByteString_newMembers(buf, length); #else ServerNetworkLayerTCP *layer = connection->handle; *buf = layer->buffer; return UA_STATUSCODE_GOOD; #endif } static void ServerNetworkLayerReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) { #ifdef UA_MULTITHREADING UA_ByteString_deleteMembers(buf); #endif } static void ServerNetworkLayerReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) { UA_ByteString_deleteMembers(buf); } /* after every select, we need to reset the sockets we want to listen on */ static void setFDSet(ServerNetworkLayerTCP *layer) { FD_ZERO(&layer->fdset); FD_SET((UA_UInt32)layer->serversockfd, &layer->fdset); layer->highestfd = layer->serversockfd; for(size_t i = 0; i < layer->mappingsSize; i++) { FD_SET((UA_UInt32)layer->mappings[i].sockfd, &layer->fdset); if(layer->mappings[i].sockfd > layer->highestfd) layer->highestfd = layer->mappings[i].sockfd; } } /* callback triggered from the server */ static void ServerNetworkLayerTCP_closeConnection(UA_Connection *connection) { #ifdef UA_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 shutdown(connection->sockfd, 2); /* only shut down here. this triggers the select, where the socket is closed in the main thread */ } /* call only from the single networking thread */ static UA_StatusCode ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd) { UA_Connection *c = malloc(sizeof(UA_Connection)); if(!c) return UA_STATUSCODE_BADINTERNALERROR; UA_Connection_init(c); c->sockfd = newsockfd; c->handle = layer; c->localConf = layer->conf; c->send = socket_write; c->close = ServerNetworkLayerTCP_closeConnection; c->getSendBuffer = ServerNetworkLayerGetSendBuffer; c->releaseSendBuffer = ServerNetworkLayerReleaseSendBuffer; c->releaseRecvBuffer = ServerNetworkLayerReleaseRecvBuffer; c->state = UA_CONNECTION_OPENING; struct ConnectionMapping *nm; nm = realloc(layer->mappings, sizeof(struct ConnectionMapping)*(layer->mappingsSize+1)); if(!nm) { free(c); return UA_STATUSCODE_BADINTERNALERROR; } layer->mappings = nm; layer->mappings[layer->mappingsSize] = (struct ConnectionMapping){c, newsockfd}; layer->mappingsSize++; return UA_STATUSCODE_GOOD; } static UA_StatusCode ServerNetworkLayerTCP_start(ServerNetworkLayerTCP *layer, UA_Logger logger) { layer->layer.logger = logger; #ifdef _WIN32 if((layer->serversockfd = socket(PF_INET, SOCK_STREAM,0)) == (UA_Int32)INVALID_SOCKET) { UA_LOG_WARNING(layer->layer.logger, UA_LOGCATEGORY_COMMUNICATION, "Error opening socket, code: %d", WSAGetLastError()); return UA_STATUSCODE_BADINTERNALERROR; } #else if((layer->serversockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { UA_LOG_WARNING(layer->layer.logger, UA_LOGCATEGORY_COMMUNICATION, "Error opening socket"); return UA_STATUSCODE_BADINTERNALERROR; } #endif const struct sockaddr_in serv_addr = {.sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY, .sin_port = htons(layer->port), .sin_zero = {0}}; int optval = 1; if(setsockopt(layer->serversockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof(optval)) == -1) { UA_LOG_WARNING(layer->layer.logger, UA_LOGCATEGORY_COMMUNICATION, "Error during setting of socket options"); CLOSESOCKET(layer->serversockfd); return UA_STATUSCODE_BADINTERNALERROR; } if(bind(layer->serversockfd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { UA_LOG_WARNING(layer->layer.logger, UA_LOGCATEGORY_COMMUNICATION, "Error during socket binding"); CLOSESOCKET(layer->serversockfd); return UA_STATUSCODE_BADINTERNALERROR; } socket_set_nonblocking(layer->serversockfd); listen(layer->serversockfd, MAXBACKLOG); UA_LOG_INFO(layer->layer.logger, UA_LOGCATEGORY_COMMUNICATION, "Listening on %.*s", layer->layer.discoveryUrl.length, layer->layer.discoveryUrl.data); return UA_STATUSCODE_GOOD; } static size_t ServerNetworkLayerTCP_getJobs(ServerNetworkLayerTCP *layer, UA_Job **jobs, UA_UInt16 timeout) { setFDSet(layer); struct timeval tmptv = {0, timeout}; UA_Int32 resultsize; repeat_select: resultsize = select(layer->highestfd+1, &layer->fdset, NULL, NULL, &tmptv); if(resultsize < 0) { if(errno == EINTR) goto repeat_select; *jobs = NULL; return 0; } /* accept new connections (can only be a single one) */ if(FD_ISSET(layer->serversockfd, &layer->fdset)) { resultsize--; struct sockaddr_in cli_addr; socklen_t cli_len = sizeof(cli_addr); int newsockfd = accept(layer->serversockfd, (struct sockaddr *) &cli_addr, &cli_len); int i = 1; setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i)); if(newsockfd >= 0) { socket_set_nonblocking(newsockfd); ServerNetworkLayerTCP_add(layer, newsockfd); } } /* alloc enough space for a cleanup-connection and free-connection job per resulted socket */ if(resultsize == 0) return 0; UA_Job *js = malloc(sizeof(UA_Job) * resultsize * 2); if(!js) return 0; /* read from established sockets */ size_t j = 0; UA_ByteString buf = UA_BYTESTRING_NULL; for(size_t i = 0; i < layer->mappingsSize && j < (size_t)resultsize; i++) { if(!(FD_ISSET(layer->mappings[i].sockfd, &layer->fdset))) continue; if(socket_recv(layer->mappings[i].connection, &buf, 0) == UA_STATUSCODE_GOOD) { if(!buf.data) continue; js[j] = UA_Connection_completeMessages(layer->mappings[i].connection, buf); } else { UA_Connection *c = layer->mappings[i].connection; /* the socket is already closed */ js[j].type = UA_JOBTYPE_DETACHCONNECTION; js[j].job.closeConnection = layer->mappings[i].connection; layer->mappings[i] = layer->mappings[layer->mappingsSize-1]; layer->mappingsSize--; j++; i--; // iterate over the same index again js[j].type = UA_JOBTYPE_METHODCALL_DELAYED; js[j].job.methodCall.method = FreeConnectionCallback; js[j].job.methodCall.data = c; } j++; } if(j == 0) { free(js); js = NULL; } *jobs = js; return j; } static size_t ServerNetworkLayerTCP_stop(ServerNetworkLayerTCP *layer, UA_Job **jobs) { UA_Job *items = malloc(sizeof(UA_Job) * layer->mappingsSize * 2); if(!items) return 0; for(size_t i = 0; i < layer->mappingsSize; i++) { socket_close(layer->mappings[i].connection); items[i*2].type = UA_JOBTYPE_DETACHCONNECTION; items[i*2].job.closeConnection = layer->mappings[i].connection; items[(i*2)+1].type = UA_JOBTYPE_METHODCALL_DELAYED; items[(i*2)+1].job.methodCall.method = FreeConnectionCallback; items[(i*2)+1].job.methodCall.data = layer->mappings[i].connection; } #ifdef _WIN32 WSACleanup(); #endif *jobs = items; return layer->mappingsSize*2; } /* run only when the server is stopped */ static void ServerNetworkLayerTCP_deleteMembers(ServerNetworkLayerTCP *layer) { #ifndef UA_MULTITHREADING UA_ByteString_deleteMembers(&layer->buffer); #endif for(size_t i = 0; i < layer->mappingsSize; i++) free(layer->mappings[i].connection); free(layer->mappings); } UA_ServerNetworkLayer * ServerNetworkLayerTCP_new(UA_ConnectionConfig conf, UA_UInt32 port) { #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData); #endif ServerNetworkLayerTCP *layer = malloc(sizeof(ServerNetworkLayerTCP)); if(!layer) return NULL; memset(layer, 0, sizeof(ServerNetworkLayerTCP)); layer->conf = conf; layer->mappingsSize = 0; layer->mappings = NULL; layer->port = port; char hostname[256]; gethostname(hostname, 255); UA_String_copyprintf("opc.tcp://%s:%d", &layer->layer.discoveryUrl, hostname, port); #ifndef UA_MULTITHREADING layer->buffer = (UA_ByteString){.length = conf.maxMessageSize, .data = malloc(conf.maxMessageSize)}; #endif layer->layer.start = (UA_StatusCode(*)(UA_ServerNetworkLayer*,UA_Logger))ServerNetworkLayerTCP_start; layer->layer.getJobs = (size_t(*)(UA_ServerNetworkLayer*,UA_Job**,UA_UInt16))ServerNetworkLayerTCP_getJobs; layer->layer.stop = (size_t(*)(UA_ServerNetworkLayer*, UA_Job**))ServerNetworkLayerTCP_stop; layer->layer.deleteMembers = (void(*)(UA_ServerNetworkLayer*))ServerNetworkLayerTCP_deleteMembers; return &layer->layer; } /***************************/ /* Client NetworkLayer TCP */ /***************************/ static UA_StatusCode ClientNetworkLayerGetBuffer(UA_Connection *connection, UA_Int32 length, UA_ByteString *buf) { if((UA_UInt32)length > connection->remoteConf.recvBufferSize) return UA_STATUSCODE_BADCOMMUNICATIONERROR; #ifndef UA_MULTITHREADING if(connection->state == UA_CONNECTION_CLOSED) return UA_STATUSCODE_BADCONNECTIONCLOSED; *buf = *(UA_ByteString*)connection->handle; return UA_STATUSCODE_GOOD; #else return UA_ByteString_newMembers(buf, connection->remoteConf.recvBufferSize); #endif } static void ClientNetworkLayerReleaseBuffer(UA_Connection *connection, UA_ByteString *buf) { #ifdef UA_MULTITHREADING UA_ByteString_deleteMembers(buf); #endif } static void ClientNetworkLayerClose(UA_Connection *connection) { #ifndef UA_MULTITHREADING if(connection->handle) UA_ByteString_delete(connection->handle); connection->handle = NULL; #endif if(connection->state == UA_CONNECTION_CLOSED) return; connection->state = UA_CONNECTION_CLOSED; socket_close(connection); } /* we have no networklayer. instead, attach the reusable buffer to the handle */ UA_Connection ClientNetworkLayerTCP_connect(UA_ConnectionConfig localConf, char *endpointUrl, UA_Logger logger) { UA_Connection connection; UA_Connection_init(&connection); connection.localConf = localConf; #ifndef UA_MULTITHREADING connection.handle = UA_ByteString_new(); UA_ByteString_newMembers(connection.handle, localConf.maxMessageSize); #endif size_t urlLength = strlen(endpointUrl); if(urlLength < 11 || urlLength >= 512) { UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Server url size invalid"); return connection; } if(strncmp(endpointUrl, "opc.tcp://", 10) != 0) { UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Server url does not begin with opc.tcp://"); return connection; } UA_UInt16 portpos = 9; UA_UInt16 port; for(port = 0; portpos < urlLength-1; portpos++) { if(endpointUrl[portpos] == ':') { port = atoi(&endpointUrl[portpos+1]); break; } } if(port == 0) { UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Port invalid"); return connection; } char hostname[512]; for(int i=10; i < portpos; i++) hostname[i-10] = endpointUrl[i]; hostname[portpos-10] = 0; #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData); if((connection.sockfd = socket(PF_INET, SOCK_STREAM,0)) == (UA_Int32)INVALID_SOCKET) { #else if((connection.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { #endif UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Could not create socket"); return connection; } struct hostent *server = gethostbyname(hostname); if(!server) { UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "DNS lookup of %s failed", hostname); return connection; } struct sockaddr_in server_addr; memset(&server_addr, 0, sizeof(server_addr)); memcpy((char *)&server_addr.sin_addr.s_addr, (char *)server->h_addr_list[0], server->h_length); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); connection.state = UA_CONNECTION_OPENING; if(connect(connection.sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { ClientNetworkLayerClose(&connection); UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Connection failed"); return connection; } #ifdef SO_NOSIGPIPE int val = 1; if(setsockopt(connection.sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)) < 0) { UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Couldn't set SO_NOSIGPIPE"); return connection; } #endif //socket_set_nonblocking(connection.sockfd); connection.send = socket_write; connection.recv = socket_recv; connection.close = ClientNetworkLayerClose; connection.getSendBuffer = ClientNetworkLayerGetBuffer; connection.releaseSendBuffer = ClientNetworkLayerReleaseBuffer; connection.releaseRecvBuffer = ClientNetworkLayerReleaseBuffer; return connection; }