Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Review] feat(plugin): implement configurable logger for pki_default unit #3567

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
rebase on upstream master
  • Loading branch information
saukijan committed Sep 22, 2020
commit e2f4163239781cb4fa39a60d1822a91bbdee6e6e
2 changes: 2 additions & 0 deletions .azure-pipelines/azure-pipelines-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:

- powershell: ./tools/azure-devops/win/install.ps1
displayName: Install Requirements
errorActionPreference: continue # Errors are handled in the script
- powershell: ./tools/azure-devops/win/build.ps1
displayName: "Build: $(CC_NAME)"
errorActionPreference: continue # If set to Stop, we only get a truncated exception message. Error is handled by checking exit code
Expand Down Expand Up @@ -83,6 +84,7 @@ jobs:

- powershell: ./tools/azure-devops/win/install.ps1
displayName: Install Requirements
errorActionPreference: continue # Errors are handled in the script
- powershell: ./tools/azure-devops/win/build.ps1
displayName: "Build: $(CC_NAME)"
errorActionPreference: continue # If set to Stop, we only get a truncated exception message. Error is handled by checking exit code
Expand Down
182 changes: 115 additions & 67 deletions CMakeLists.txt
100755 → 100644

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions arch/common/ua_lwip.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,22 @@
#define UA_getsockopt lwip_getsockopt
#define UA_setsockopt lwip_setsockopt
#define UA_freeaddrinfo lwip_freeaddrinfo
#ifndef UA_gethostname
#define UA_gethostname gethostname_lwip
#else
extern int UA_gethostname(char* name, size_t len);
#endif
#ifndef UA_getsockname
#define UA_getsockname lwip_getsockname
#else
extern int UA_getsockname((int s, struct sockaddr *name, socklen_t *namelen);
#endif
#ifndef UA_getaddrinfo
#define UA_getaddrinfo lwip_getaddrinfo
#else
extern int UA_getaddrinfo(const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res);
#endif

#if UA_IPV6
# define UA_inet_pton(af, src, dst) \
Expand Down
10 changes: 10 additions & 0 deletions arch/freertosLWIP/ua_architecture.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@
#define UA_LOCK_ASSERT(mutexName, num)
#endif

#define UA_strncasecmp strncasecmp

// freeRTOS does not have getifaddr
#undef UA_HAS_GETIFADDR

#ifndef IN6_IS_ADDR_UNSPECIFIED
# define IN6_IS_ADDR_UNSPECIFIED(a) \
(((const uint32_t *) (a))[0] == 0 \
&& ((const uint32_t *) (a))[1] == 0 \
&& ((const uint32_t *) (a))[2] == 0 \
&& ((const uint32_t *) (a))[3] == 0)
#endif

#include <open62541/architecture_functions.h>

#endif /* PLUGINS_ARCH_FREERTOSLWIP_UA_ARCHITECTURE_H_ */
Expand Down
140 changes: 95 additions & 45 deletions arch/network_tcp.c

Large diffs are not rendered by default.

101 changes: 76 additions & 25 deletions arch/network_ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define UA_INTERNAL

#include <open62541/config.h>
#include <open62541/network_ws.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/util.h>
Expand All @@ -30,7 +31,7 @@ struct ConnectionUserData {

typedef struct ConnectionUserData ConnectionUserData;

//one of these is created for each client connecting to us
// one of these is created for each client connecting to us
struct SessionData {
UA_Connection *connection;
};
Expand All @@ -46,10 +47,12 @@ typedef struct {
struct lws_context *context;
UA_Server *server;
UA_ConnectionConfig config;
UA_ByteString certificate;
UA_ByteString privateKey;
} ServerNetworkLayerWS;

static UA_StatusCode
connection_getsendbuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
connection_ws_getsendbuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
UA_SecureChannel *channel = connection->channel;
if(channel && channel->config.sendBufferSize < length)
return UA_STATUSCODE_BADCOMMUNICATIONERROR;
Expand All @@ -63,14 +66,14 @@ connection_getsendbuffer(UA_Connection *connection, size_t length, UA_ByteString
}

static void
connection_releasesendbuffer(UA_Connection *connection, UA_ByteString *buf) {
connection_ws_releasesendbuffer(UA_Connection *connection, UA_ByteString *buf) {
buf->data -= LWS_PRE;
buf->length += LWS_PRE;
UA_ByteString_deleteMembers(buf);
}

static void
connection_releaserecvbuffer(UA_Connection *connection, UA_ByteString *buf) {
connection_ws_releaserecvbuffer(UA_Connection *connection, UA_ByteString *buf) {
UA_ByteString_deleteMembers(buf);
}

Expand All @@ -83,7 +86,7 @@ connection_send(UA_Connection *connection, UA_ByteString *buf) {

ConnectionUserData *buffer = (ConnectionUserData *)connection->handle;
if(connection->state == UA_CONNECTIONSTATE_CLOSED) {
connection_releasesendbuffer(connection, buf);
connection_ws_releasesendbuffer(connection, buf);
return UA_STATUSCODE_BADCONNECTIONCLOSED;
}

Expand All @@ -97,6 +100,8 @@ connection_send(UA_Connection *connection, UA_ByteString *buf) {
static void
ServerNetworkLayerWS_close(UA_Connection *connection) {
connection->state = UA_CONNECTIONSTATE_CLOSED;
// trigger callback and close;
lws_callback_on_writable(((ConnectionUserData *)connection->handle)->wsi);
}

static void
Expand All @@ -105,7 +110,7 @@ freeConnection(UA_Connection *connection) {
ConnectionUserData *userData = (ConnectionUserData *)connection->handle;
while(!SIMPLEQ_EMPTY(&userData->messages)) {
BufferEntry *entry = SIMPLEQ_FIRST(&userData->messages);
connection_releasesendbuffer(connection, &entry->msg);
connection_ws_releasesendbuffer(connection, &entry->msg);
SIMPLEQ_REMOVE_HEAD(&userData->messages, next);
UA_free(entry);
}
Expand All @@ -118,7 +123,7 @@ static int
callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
size_t len) {
struct SessionData *pss = (struct SessionData *)user;
struct VHostData *vhd =
struct VHostData *vhd =
(struct VHostData *)lws_protocol_vh_priv_get(lws_get_vhost(wsi),
lws_get_protocol(wsi));

Expand All @@ -133,21 +138,21 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo
case LWS_CALLBACK_ESTABLISHED:
if(!wsi)
break;
ServerNetworkLayerWS *layer = (ServerNetworkLayerWS*)lws_context_user(vhd->context);
ServerNetworkLayerWS *layer = (ServerNetworkLayerWS *)lws_context_user(vhd->context);
UA_Connection *c = (UA_Connection *)malloc(sizeof(UA_Connection));
ConnectionUserData *buffer =
(ConnectionUserData *)malloc(sizeof(ConnectionUserData));
SIMPLEQ_INIT(&buffer->messages);
buffer->wsi = wsi;
memset(c, 0, sizeof(UA_Connection));
c->sockfd = 0;
c->sockfd = UA_INVALID_SOCKET;
c->handle = buffer;
c->send = connection_send;
c->close = ServerNetworkLayerWS_close;
c->free = freeConnection;
c->getSendBuffer = connection_getsendbuffer;
c->releaseSendBuffer = connection_releasesendbuffer;
c->releaseRecvBuffer = connection_releaserecvbuffer;
c->getSendBuffer = connection_ws_getsendbuffer;
c->releaseSendBuffer = connection_ws_releasesendbuffer;
c->releaseRecvBuffer = connection_ws_releaserecvbuffer;
// stack sets the connection to established
c->state = UA_CONNECTIONSTATE_OPENING;
c->openingDate = UA_DateTime_nowMonotonic();
Expand All @@ -156,15 +161,15 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo

case LWS_CALLBACK_CLOSED:
// notify server
if(!pss->connection->state != UA_CONNECTIONSTATE_CLOSED) {
if(pss->connection->state != UA_CONNECTIONSTATE_CLOSED) {
pss->connection->state = UA_CONNECTIONSTATE_CLOSED;
}

layer = (ServerNetworkLayerWS*)lws_context_user(vhd->context);
layer = (ServerNetworkLayerWS *)lws_context_user(vhd->context);
if(layer && layer->server) {
UA_Server_removeConnection(layer->server, pss->connection);
}

break;

case LWS_CALLBACK_SERVER_WRITEABLE:
Expand All @@ -173,6 +178,16 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo

ConnectionUserData *b = (ConnectionUserData *)pss->connection->handle;
do {
if(!pss->connection ||
pss->connection->state == UA_CONNECTIONSTATE_CLOSED) {
/*
connetion is closed signal it to lws:
lws documentation says:
When you want to close a connection,
you do it by returning -1 from a callback for that connection.
*/
return -1;
}

BufferEntry *entry = SIMPLEQ_FIRST(&b->messages);
if(!entry)
Expand All @@ -184,7 +199,7 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo
lwsl_err("ERROR %d writing to ws\n", m);
return -1;
}
connection_releasesendbuffer(pss->connection, &entry->msg);
connection_ws_releasesendbuffer(pss->connection, &entry->msg);
SIMPLEQ_REMOVE_HEAD(&b->messages, next);
UA_free(entry);
} while(!lws_send_pipe_choked(wsi));
Expand All @@ -195,7 +210,7 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo
}
break;

case LWS_CALLBACK_RECEIVE: {
case LWS_CALLBACK_RECEIVE: {
if(!vhd->context)
break;
layer = (ServerNetworkLayerWS *)lws_context_user(vhd->context);
Expand All @@ -205,7 +220,7 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo
UA_ByteString message = {len, (UA_Byte *)in};
UA_Server_processBinaryMessage(layer->server, pss->connection, &message);
break;
}
}

default:
break;
Expand All @@ -216,9 +231,12 @@ callback_opcua(struct lws *wsi, enum lws_callback_reasons reason, void *user, vo

static struct lws_protocols protocols[] = {
{"http", lws_callback_http_dummy, 0, 0, 0, NULL, 0},
/* default protocol */
{"opcua", callback_opcua, sizeof(struct SessionData), 0, 0, NULL, 0},
{NULL, NULL, 0, 0, 0, NULL, 0}
};
/* defined protocols: https://reference.opcfoundation.org/v104/Core/docs/Part6/7.5.2/ */
{"opcua+uacp", callback_opcua, sizeof(struct SessionData), 0, 0, NULL, 0},
// {"opcua+json", callback_opcua, sizeof(struct SessionData), 0, 0, NULL, 0}, // <-- enable when json coding is fully supported
{NULL, NULL, 0, 0, 0, NULL, 0}};

// make the opcua protocol callback the default one
const struct lws_protocol_vhost_options pvo_opt = {NULL, NULL, "default", "1"};
Expand All @@ -230,18 +248,24 @@ ServerNetworkLayerWS_start(UA_ServerNetworkLayer *nl, const UA_String *customHos

ServerNetworkLayerWS *layer = (ServerNetworkLayerWS *)nl->handle;

UA_Boolean isSecure = layer->certificate.length && layer->privateKey.length;

UA_String protocol = isSecure ? UA_STRING("wss") : UA_STRING("ws");

/* Get the discovery url from the hostname */
UA_String du = UA_STRING_NULL;
char discoveryUrlBuffer[256];
if(customHostname->length) {
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "ws:https://%.*s:%d/",
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "%.*s:https://%.*s:%d/",
(int)protocol.length, protocol.data,
(int)customHostname->length, customHostname->data,
layer->port);
du.data = (UA_Byte *)discoveryUrlBuffer;
} else {
char hostnameBuffer[256];
if(UA_gethostname(hostnameBuffer, 255) == 0) {
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "ws:https://%s:%d/",
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "%.*s:https://%s:%d/",
(int)protocol.length, protocol.data,
hostnameBuffer, layer->port);
du.data = (UA_Byte *)discoveryUrlBuffer;
} else {
Expand All @@ -250,7 +274,7 @@ ServerNetworkLayerWS_start(UA_ServerNetworkLayer *nl, const UA_String *customHos
}
}
// we need discoveryUrl.data as a null-terminated string for vhost_name
nl->discoveryUrl.data = (UA_Byte *)UA_malloc(du.length+1);
nl->discoveryUrl.data = (UA_Byte *)UA_malloc(du.length + 1);
strncpy((char *)nl->discoveryUrl.data, discoveryUrlBuffer, du.length);
nl->discoveryUrl.data[du.length] = '\0';
nl->discoveryUrl.length = du.length;
Expand All @@ -271,6 +295,18 @@ ServerNetworkLayerWS_start(UA_ServerNetworkLayer *nl, const UA_String *customHos
info.pvo = &pvo;
info.user = layer;

if(isSecure) {
info.server_ssl_cert_mem = layer->certificate.data;
info.server_ssl_cert_mem_len = (unsigned int)layer->certificate.length;
info.server_ssl_private_key_mem = layer->privateKey.data;
info.server_ssl_private_key_mem_len = (unsigned int)layer->privateKey.length;

info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"Websocket network layer listening using WSS");

}

struct lws_context *context = lws_create_context(&info);
if(!context) {
UA_LOG_ERROR(layer->logger, UA_LOGCATEGORY_NETWORK, "lws init failed");
Expand Down Expand Up @@ -303,12 +339,22 @@ ServerNetworkLayerWS_stop(UA_ServerNetworkLayer *nl, UA_Server *server) {

static void
ServerNetworkLayerWS_clear(UA_ServerNetworkLayer *nl) {
ServerNetworkLayerWS *layer = (ServerNetworkLayerWS *)nl->handle;

if(layer->certificate.length) {
UA_String_deleteMembers(&layer->certificate);
}

if(layer->privateKey.length) {
UA_String_deleteMembers(&layer->privateKey);
}

UA_free(nl->handle);
UA_String_deleteMembers(&nl->discoveryUrl);
}

UA_ServerNetworkLayer
UA_ServerNetworkLayerWS(UA_ConnectionConfig config, UA_UInt16 port, UA_Logger *logger) {
UA_ServerNetworkLayerWS(UA_ConnectionConfig config, UA_UInt16 port, UA_Logger *logger, const UA_ByteString* certificate, const UA_ByteString* privateKey) {
UA_ServerNetworkLayer nl;
memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
nl.clear = ServerNetworkLayerWS_clear;
Expand All @@ -325,5 +371,10 @@ UA_ServerNetworkLayerWS(UA_ConnectionConfig config, UA_UInt16 port, UA_Logger *l
layer->logger = logger;
layer->port = port;
layer->config = config;
return nl;

if(certificate && privateKey) {
UA_String_copy(certificate,&layer->certificate);
UA_String_copy(privateKey,&layer->privateKey);
}
return nl;
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.