Prechádzať zdrojové kódy

addung discoveryUrl to the NetworkLayer interface, changed everything not to use static ips and ports, but hostname of the system

Stasik0 10 rokov pred
rodič
commit
113f945fbf

+ 1 - 1
examples/logger_stdout.c

@@ -12,7 +12,7 @@ static void print_time(void) {
 	UA_DateTime now = UA_DateTime_now();
 	UA_ByteString str;
 	UA_DateTime_toString(now, &str);
-	printf("%.*s", str.length, str.data);
+	printf("%.27s", str.data); //a bit hacky way not to display nanoseconds
 	UA_ByteString_deleteMembers(&str);
 }
 

+ 9 - 6
examples/networklayer_tcp.c

@@ -69,6 +69,7 @@ typedef struct NetworkLayerTCP {
     UA_UInt16 conLinksSize;
     ConnectionLink *conLinks;
     UA_UInt32 port;
+    UA_String discoveryUrl;
     /* We remove the connection links only in the main thread. Attach
        to-be-deleted links with atomic operations */
     struct deleteLink {
@@ -78,6 +79,7 @@ typedef struct NetworkLayerTCP {
 		UA_Int32 sockfd;
 #endif
         struct deleteLink *next;
+
     } *deleteLinkList;
 } NetworkLayerTCP;
 
@@ -299,12 +301,7 @@ static UA_StatusCode NetworkLayerTCP_start(NetworkLayerTCP *layer, UA_Logger *lo
 	setNonBlocking(layer->serversockfd);
 	listen(layer->serversockfd, MAXBACKLOG);
     char msg[256];
-    char hostname[256];
-    gethostname(hostname, 255);
-    sprintf(msg, "Listening for TCP connections on opc.tcp://%s:%d",
-    		hostname,
-    		//inet_ntoa(serv_addr.sin_addr),
-            ntohs(serv_addr.sin_port));
+    sprintf(msg, "Listening on %.*s\n", layer->discoveryUrl.length, layer->discoveryUrl.data);
     UA_LOG_INFO((*logger), UA_LOGGERCATEGORY_SERVER, msg);
     return UA_STATUSCODE_GOOD;
 }
@@ -387,6 +384,7 @@ static UA_Int32 NetworkLayerTCP_stop(NetworkLayerTCP * layer, UA_WorkItem **work
 }
 
 static void NetworkLayerTCP_delete(NetworkLayerTCP *layer) {
+	UA_String_deleteMembers(&layer->discoveryUrl);
 	for(UA_Int32 i=0;i<layer->conLinksSize;++i){
 		free(layer->conLinks[i].connection);
 	}
@@ -401,6 +399,9 @@ UA_ServerNetworkLayer ServerNetworkLayerTCP_new(UA_ConnectionConfig conf, UA_UIn
 	tcplayer->conLinks = NULL;
     tcplayer->port = port;
     tcplayer->deleteLinkList = (void*)0;
+    char hostname[256];
+    gethostname(hostname, 255);
+    UA_String_copyprintf("opc.tcp://%s:%d", &tcplayer->discoveryUrl, hostname, port);
 
     UA_ServerNetworkLayer nl;
     nl.nlHandle = tcplayer;
@@ -408,5 +409,7 @@ UA_ServerNetworkLayer ServerNetworkLayerTCP_new(UA_ConnectionConfig conf, UA_UIn
     nl.getWork = (UA_Int32 (*)(void*, UA_WorkItem**, UA_UInt16)) NetworkLayerTCP_getWork;
     nl.stop = (UA_Int32 (*)(void*, UA_WorkItem**)) NetworkLayerTCP_stop;
     nl.free = (void (*)(void*))NetworkLayerTCP_delete;
+    nl.discoveryUrl = &tcplayer->discoveryUrl;
+
     return nl;
 }

+ 4 - 3
examples/networklayer_udp.c

@@ -117,7 +117,7 @@ void writeCallbackUDP(UDPConnection *handle, UA_ByteStringArray gather_buf) {
 	}
 
 
-	struct sockaddr_in *sin = UA_NULL;
+	struct sockaddr_in *sin = NULL;
 	if (handle->from.sa_family == AF_INET) {
         
 #if defined(__GNUC__) || defined(__clang__)
@@ -198,7 +198,7 @@ static UA_StatusCode ServerNetworkLayerUDP_start(ServerNetworkLayerUDP *layer, U
 
 static UA_Int32 ServerNetworkLayerUDP_getWork(ServerNetworkLayerUDP *layer, UA_WorkItem **workItems,
                                         UA_UInt16 timeout) {
-    UA_WorkItem *items = UA_NULL;
+    UA_WorkItem *items = NULL;
     setFDSet(layer);
     struct timeval tmptv = {0, timeout};
     UA_Int32 resultsize = select(layer->serversockfd+1, &layer->fdset, NULL, NULL, &tmptv);
@@ -244,7 +244,7 @@ static UA_Int32 ServerNetworkLayerUDP_getWork(ServerNetworkLayerUDP *layer, UA_W
             c->fromlen = sendsize;
             c->connection.state = UA_CONNECTION_OPENING;
             c->connection.localConf = layer->conf;
-            c->connection.channel = UA_NULL;
+            c->connection.channel = NULL;
             c->connection.close = (void (*)(void*))closeConnectionUDP;
             c->connection.write = (void (*)(void*, UA_ByteStringArray))writeCallbackUDP;
 
@@ -288,5 +288,6 @@ UA_ServerNetworkLayer ServerNetworkLayerUDP_new(UA_ConnectionConfig conf, UA_UIn
     nl.getWork = (UA_Int32 (*)(void*, UA_WorkItem**, UA_UInt16)) ServerNetworkLayerUDP_getWork;
     nl.stop = (UA_Int32 (*)(void*, UA_WorkItem**)) ServerNetworkLayerUDP_stop;
     nl.free = (void (*)(void*))ServerNetworkLayerUDP_delete;
+    nl.discoveryUrl = NULL;
     return nl;
 }

+ 7 - 1
include/ua_server.h

@@ -192,8 +192,14 @@ typedef struct {
      */
     UA_Int32 (*stop)(void *nlhandle, UA_WorkItem **workItems);
 
-    /** Deletes the network layer. Call only after a successfull shutdown. */
+    /** Deletes the network layer. Call only after a successful shutdown. */
     void (*free)(void *nlhandle);
+
+    /**
+     * String containing the discovery URL that will be add to the server's list
+     * contains the protocol the host and the port of the layer
+     */
+    UA_String* discoveryUrl;
 } UA_ServerNetworkLayer;
 
 /**

+ 12 - 7
src/server/ua_server.c

@@ -44,7 +44,17 @@ void UA_Server_addNetworkLayer(UA_Server *server, UA_ServerNetworkLayer networkL
     server->nls = newlayers;
     server->nls[server->nlsSize] = networkLayer;
     server->nlsSize++;
-    UA_LOG_INFO(server->logger, UA_LOGGERCATEGORY_SERVER, "Networklayer added");
+
+    if(networkLayer.discoveryUrl){
+		UA_String* newUrls = UA_realloc(server->description.discoveryUrls, sizeof(UA_String)*(server->description.discoveryUrlsSize+1)); //TODO: rework this pattern into *_array_insert
+		if(!newUrls) {
+			UA_LOG_ERROR(server->logger, UA_LOGGERCATEGORY_SERVER, "Adding discoveryUrl");
+			return;
+		}
+		server->description.discoveryUrls = newUrls;
+		UA_String_copy(networkLayer.discoveryUrl, &server->description.discoveryUrls[0]);
+		server->description.discoveryUrlsSize++;
+    }
 }
 
 void UA_Server_setServerCertificate(UA_Server *server, UA_ByteString certificate) {
@@ -212,12 +222,7 @@ UA_Server * UA_Server_new(void) {
     UA_ApplicationDescription_init(&server->description);
     UA_String_copycstring(PRODUCT_URI, &server->description.productUri);
     UA_String_copycstring(APPLICATION_URI, &server->description.applicationUri);
-    server->description.discoveryUrlsSize = 1;
-    server->description.discoveryUrls = UA_String_new();
-    if(!server->description.discoveryUrls)
-    	return UA_NULL;
-    UA_String_copycstring("opc.tcp://0.0.0.0:16664", server->description.discoveryUrls); //TODO: 16664 is hardcoded :(
-
+    server->description.discoveryUrlsSize = 0;
 
     UA_LocalizedText_copycstring("Unconfigured open62541 application", &server->description.applicationName);
     server->description.applicationType = UA_APPLICATIONTYPE_SERVER;

+ 2 - 1
src/ua_types.c

@@ -104,7 +104,8 @@ void UA_String_init(UA_String *p) {
 
 UA_TYPE_DELETE_DEFAULT(UA_String)
 void UA_String_deleteMembers(UA_String *p) {
-    UA_free(p->data);
+	if(p->data)
+		UA_free(p->data);
 }
 
 UA_StatusCode UA_String_copy(UA_String const *src, UA_String *dst) {

+ 4 - 0
tools/certs/create_self-signed.py

@@ -1,6 +1,7 @@
 import sys
 import os
 import shutil
+import socket
 
 if len(sys.argv) < 2:
     sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
@@ -10,6 +11,9 @@ if not os.path.exists(sys.argv[1]):
     
 os.chdir(os.path.dirname(os.path.abspath(__file__)))
 
+
+os.environ['HOSTNAME'] = socket.gethostname()
+
 os.system("""openssl genrsa -out ca.key 2048""")
 os.system("""openssl req \
 	-x509 \

+ 3 - 0
tools/certs/localhost.cnf

@@ -12,6 +12,8 @@ RANDFILE		= $ENV::HOME/.rnd
 #oid_file		= $ENV::HOME/.oid
 oid_section		= new_oids
 
+hostname = ${ENV::HOSTNAME}
+
 # To use this configuration file with the "-extfile" option of the
 # "openssl x509" utility, name here the section containing the
 # X.509v3 extensions to use:
@@ -224,6 +226,7 @@ subjectAltName = @alt_names
 
 [ alt_names ]
 DNS.1 = localhost
+DNS.2 = ${hostname}
 IP.1 = 127.0.0.1
 IP.2 = 0.0.0.0
 URI.1 = urn:unconfigured:open62541:open62541Server