mirror of
https://github.com/open62541/open62541.git
synced 2025-06-03 04:00:21 +00:00
Fix #1360 socket error messages and mDNS socket on windows
This commit is contained in:
parent
b329666d8d
commit
2c62703613
2
deps/mdnsd
vendored
2
deps/mdnsd
vendored
@ -1 +1 @@
|
||||
Subproject commit 4cea3adf05cdea4d1149514c55c0eb2f1d857032
|
||||
Subproject commit 9c11048aefc2a47b78230491931c8346fb6c6b98
|
@ -98,7 +98,7 @@ int main(int argc, char **argv) {
|
||||
signal(SIGINT, stopHandler); /* catches ctrl-c */
|
||||
signal(SIGTERM, stopHandler);
|
||||
|
||||
UA_ServerConfig *config = UA_ServerConfig_new_default();
|
||||
UA_ServerConfig *config = UA_ServerConfig_new_minimal(16600, NULL);
|
||||
// To enable mDNS discovery, set application type to discovery server.
|
||||
config->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
|
||||
UA_String_deleteMembers(&config->applicationDescription.applicationUri);
|
||||
|
@ -415,6 +415,12 @@ getInterfaces(UA_Server *server) {
|
||||
for(size_t attempts = 0; attempts != 3; ++attempts) {
|
||||
// todo: malloc may fail: return a statuscode
|
||||
adapter_addresses = (IP_ADAPTER_ADDRESSES*)UA_malloc(adapter_addresses_buffer_size);
|
||||
if (!adapter_addresses) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"GetAdaptersAddresses out of memory");
|
||||
adapter_addresses = NULL;
|
||||
break;
|
||||
}
|
||||
DWORD error = GetAdaptersAddresses(AF_UNSPEC,
|
||||
GAA_FLAG_SKIP_ANYCAST |
|
||||
GAA_FLAG_SKIP_DNS_SERVER |
|
||||
@ -423,10 +429,6 @@ getInterfaces(UA_Server *server) {
|
||||
&adapter_addresses_buffer_size);
|
||||
|
||||
if(ERROR_SUCCESS == error) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"GetAdaptersAddresses returned an error. "
|
||||
"Not setting mDNS A records.");
|
||||
adapter_addresses = NULL;
|
||||
break;
|
||||
} else if (ERROR_BUFFER_OVERFLOW == error) {
|
||||
// Try again with the new size
|
||||
@ -449,9 +451,11 @@ getInterfaces(UA_Server *server) {
|
||||
void mdns_set_address_record(UA_Server *server, const char *fullServiceDomain,
|
||||
const char *localDomain) {
|
||||
IP_ADAPTER_ADDRESSES* adapter_addresses = getInterfaces(server);
|
||||
if (!adapter_addresses)
|
||||
return;
|
||||
|
||||
/* Iterate through all of the adapters */
|
||||
IP_ADAPTER_ADDRESSES* adapter = NULL;
|
||||
IP_ADAPTER_ADDRESSES* adapter = adapter_addresses->Next;
|
||||
for(; adapter != NULL; adapter = adapter->Next) {
|
||||
/* Skip loopback adapters */
|
||||
if(IF_TYPE_SOFTWARE_LOOPBACK == adapter->IfType)
|
||||
|
@ -34,8 +34,26 @@
|
||||
#include <errno.h>
|
||||
#ifdef _WIN32
|
||||
# define CLOSESOCKET(S) closesocket((SOCKET)S)
|
||||
# define errno__ WSAGetLastError()
|
||||
#else
|
||||
# define CLOSESOCKET(S) close(S)
|
||||
# define errno__ errno
|
||||
#endif
|
||||
|
||||
#ifdef __WIN32
|
||||
#define UA_LOG_ERROR_SOCKET(LOGGER, CATEGORY, DESCRIPTION) { \
|
||||
char *s = NULL; \
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
|
||||
NULL, WSAGetLastError(), \
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
|
||||
(LPSTR)&s, 0, NULL); \
|
||||
UA_LOG_ERROR(LOGGER, CATEGORY, DESCRIPTION, s); \
|
||||
LocalFree(s); \
|
||||
}
|
||||
#else
|
||||
#define UA_LOG_ERROR_SOCKET(LOGGER, CATEGORY, DESCRIPTION) { \
|
||||
UA_LOG_ERROR(LOGGER, CATEGORY, DESCRIPTION, strerror(errno)); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UA_ENABLE_MULTITHREADING
|
||||
@ -58,14 +76,12 @@ multicastWorkerLoop(UA_Server *server) {
|
||||
mdnsd_step(server->mdnsDaemon, server->mdnsSocket,
|
||||
FD_ISSET(server->mdnsSocket, &fds), true, &next_sleep);
|
||||
if (retVal == 1) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not read from socket. %s",
|
||||
strerror(errno));
|
||||
UA_LOG_ERROR_SOCKET(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not read from socket. %s");
|
||||
break;
|
||||
} else if (retVal == 2) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not write to socket. %s",
|
||||
strerror(errno));
|
||||
UA_LOG_ERROR_SOCKET(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not write to socket. %s");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -293,7 +309,7 @@ discovery_createMulticastSocket(void) {
|
||||
in.sin_port = htons(5353);
|
||||
in.sin_addr.s_addr = 0;
|
||||
|
||||
if ((s = (int)socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
||||
if ((s = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
||||
return 0;
|
||||
|
||||
#ifdef SO_REUSEPORT
|
||||
@ -315,13 +331,19 @@ discovery_createMulticastSocket(void) {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
UA_StatusCode
|
||||
initMulticastDiscoveryServer(UA_Server* server) {
|
||||
server->mdnsDaemon = mdnsd_new(QCLASS_IN, 1000);
|
||||
#ifdef _WIN32
|
||||
WORD wVersionRequested = MAKEWORD(2, 2);
|
||||
WSADATA wsaData;
|
||||
WSAStartup(wVersionRequested, &wsaData);
|
||||
#endif
|
||||
|
||||
if((server->mdnsSocket = discovery_createMulticastSocket()) == 0) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Could not create multicast socket. Error: %s",
|
||||
strerror(errno));
|
||||
UA_LOG_ERROR_SOCKET(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Could not create multicast socket. Error: %s");
|
||||
return UA_STATUSCODE_BADUNEXPECTEDERROR;
|
||||
}
|
||||
mdnsd_register_receive_callback(server->mdnsDaemon,
|
||||
@ -581,14 +603,12 @@ iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
|
||||
unsigned short retval = mdnsd_step(server->mdnsDaemon, server->mdnsSocket,
|
||||
processIn, true, &next_sleep);
|
||||
if(retval == 1) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not read from socket. %s",
|
||||
strerror(errno));
|
||||
UA_LOG_ERROR_SOCKET(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not read from socket. %s");
|
||||
return UA_STATUSCODE_BADNOCOMMUNICATION;
|
||||
} else if(retval == 2) {
|
||||
UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not write to socket. %s",
|
||||
strerror(errno));
|
||||
UA_LOG_ERROR_SOCKET(server->config.logger, UA_LOGCATEGORY_SERVER,
|
||||
"Multicast error: Can not write to socket. %s");
|
||||
return UA_STATUSCODE_BADNOCOMMUNICATION;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user