123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #include <open62541/client.h>
- #include <open62541/client_config_default.h>
- #include <open62541/plugin/log_stdout.h>
- #include <open62541/server.h>
- #include <open62541/server_config_default.h>
- #include <signal.h>
- #include <stdlib.h>
- const UA_ByteString UA_SECURITY_POLICY_BASIC128_URI =
- {56, (UA_Byte *)"http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15"};
- UA_Boolean running = true;
- static void stopHandler(int sign) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
- running = false;
- }
- char *discovery_url = NULL;
- static void
- serverOnNetworkCallback(const UA_ServerOnNetwork *serverOnNetwork, UA_Boolean isServerAnnounce,
- UA_Boolean isTxtReceived, void *data) {
- if(discovery_url != NULL || !isServerAnnounce) {
- UA_LOG_DEBUG(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "serverOnNetworkCallback called, but discovery URL "
- "already initialized or is not announcing. Ignoring.");
- return;
- }
- if(!isTxtReceived)
- return;
-
-
-
-
-
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Another server announced itself on %.*s",
- (int)serverOnNetwork->discoveryUrl.length, serverOnNetwork->discoveryUrl.data);
- if(discovery_url != NULL)
- UA_free(discovery_url);
- discovery_url = (char*)UA_malloc(serverOnNetwork->discoveryUrl.length + 1);
- memcpy(discovery_url, serverOnNetwork->discoveryUrl.data, serverOnNetwork->discoveryUrl.length);
- discovery_url[serverOnNetwork->discoveryUrl.length] = 0;
- }
- static
- UA_EndpointDescription *getRegisterEndpointFromServer(const char *discoveryServerUrl) {
- UA_Client *client = UA_Client_new();
- UA_ClientConfig_setDefault(UA_Client_getConfig(client));
- UA_EndpointDescription *endpointArray = NULL;
- size_t endpointArraySize = 0;
- UA_StatusCode retval = UA_Client_getEndpoints(client, discoveryServerUrl,
- &endpointArraySize, &endpointArray);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_Array_delete(endpointArray, endpointArraySize,
- &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "GetEndpoints failed with %s", UA_StatusCode_name(retval));
- UA_Client_delete(client);
- return NULL;
- }
- UA_LOG_DEBUG(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Server has %lu endpoints", (unsigned long)endpointArraySize);
- UA_EndpointDescription *foundEndpoint = NULL;
- for(size_t i = 0; i < endpointArraySize; i++) {
- UA_LOG_DEBUG(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "\tURL = %.*s, SecurityMode = %s",
- (int) endpointArray[i].endpointUrl.length,
- endpointArray[i].endpointUrl.data,
- endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_NONE ? "None" :
- endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_SIGN ? "Sign" :
- endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ? "SignAndEncrypt" :
- "Invalid"
- );
-
- if((UA_String_equal(&endpointArray[i].securityPolicyUri, &UA_SECURITY_POLICY_NONE_URI) ||
- UA_String_equal(&endpointArray[i].securityPolicyUri, &UA_SECURITY_POLICY_BASIC128_URI)) && (
- foundEndpoint == NULL || foundEndpoint->securityMode < endpointArray[i].securityMode))
- foundEndpoint = &endpointArray[i];
- }
- UA_EndpointDescription *returnEndpoint = NULL;
- if(foundEndpoint != NULL) {
- returnEndpoint = UA_EndpointDescription_new();
- UA_EndpointDescription_copy(foundEndpoint, returnEndpoint);
- }
- UA_Array_delete(endpointArray, endpointArraySize,
- &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
- UA_Client_delete(client);
- return returnEndpoint;
- }
- #ifdef UA_ENABLE_ENCRYPTION
- static UA_ByteString loadFile(const char *const path) {
- UA_ByteString fileContents = UA_BYTESTRING_NULL;
- if(path == NULL)
- return fileContents;
-
- FILE *fp = fopen(path, "rb");
- if(!fp) {
- errno = 0;
- return fileContents;
- }
-
- fseek(fp, 0, SEEK_END);
- fileContents.length = (size_t) ftell(fp);
- fileContents.data = (UA_Byte *) UA_malloc(fileContents.length * sizeof(UA_Byte));
- if(fileContents.data) {
- fseek(fp, 0, SEEK_SET);
- size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
- if(read != fileContents.length)
- UA_ByteString_clear(&fileContents);
- } else {
- fileContents.length = 0;
- }
- fclose(fp);
- return fileContents;
- }
- #endif
- static
- UA_Client *getRegisterClient(UA_EndpointDescription *endpointRegister, int argc, char **argv) {
- if(endpointRegister->securityMode == UA_MESSAGESECURITYMODE_NONE) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Using LDS endpoint with security None");
- UA_Client *client = UA_Client_new();
- UA_ClientConfig_setDefault(UA_Client_getConfig(client));
- return client;
- }
- #ifdef UA_ENABLE_ENCRYPTION
- if(endpointRegister->securityMode == UA_MESSAGESECURITYMODE_SIGN) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "LDS endpoint which only supports Sign is currently not supported");
- return NULL;
- }
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Using LDS endpoint with security SignAndEncrypt");
- UA_ByteString certificate = UA_BYTESTRING_NULL;
- UA_ByteString privateKey = UA_BYTESTRING_NULL;
- UA_ByteString *trustList = NULL;
- size_t trustListSize = 0;
- UA_ByteString *revocationList = NULL;
- size_t revocationListSize = 0;
- if(argc < 3) {
- UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "The Certificate and key is missing."
- "The required arguments are "
- "<client-certificate.der> <client-private-key.der> "
- "[<trustlist1.crl>, ...]");
- return NULL;
- }
- certificate = loadFile(argv[1]);
- privateKey = loadFile(argv[2]);
-
- if(argc > 3) {
- trustListSize = (size_t) argc - 3;
- UA_StatusCode retval = UA_ByteString_allocBuffer(trustList, trustListSize);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_ByteString_clear(&certificate);
- UA_ByteString_clear(&privateKey);
- return NULL;
- }
- for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
- trustList[trustListCount] = loadFile(argv[trustListCount + 3]);
- }
- }
-
- UA_Client *clientRegister = UA_Client_new();
- UA_ClientConfig *cc = UA_Client_getConfig(clientRegister);
- UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
- trustList, trustListSize,
- revocationList, revocationListSize);
- cc->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
- UA_ByteString_clear(&certificate);
- UA_ByteString_clear(&privateKey);
- for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++)
- UA_ByteString_clear(&trustList[deleteCount]);
- return clientRegister;
- #else
- return NULL;
- #endif
- }
- int main(int argc, char **argv) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_Server *server = UA_Server_new();
- UA_ServerConfig *config = UA_Server_getConfig(server);
-
- UA_ServerConfig_setMinimal(config, 0, NULL);
-
-
-
-
- config->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
- UA_String_clear(&config->applicationDescription.applicationUri);
- config->applicationDescription.applicationUri =
- UA_String_fromChars("urn:open62541.example.server_multicast");
-
- config->discovery.mdnsEnable = true;
- config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
-
- config->discovery.mdnsInterfaceIP = UA_String_fromChars("42.42.42.42");
-
-
-
-
-
-
- config->discovery.mdns.serverCapabilitiesSize = 2;
- UA_String *caps = (UA_String *) UA_Array_new(2, &UA_TYPES[UA_TYPES_STRING]);
- caps[0] = UA_String_fromChars("LDS");
- caps[1] = UA_String_fromChars("NA");
- config->discovery.mdns.serverCapabilities = caps;
-
- UA_StatusCode retval = UA_Server_run_startup(server);
-
-
- UA_Server_setServerOnNetworkCallback(server, serverOnNetworkCallback, NULL);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Could not start the server. StatusCode %s",
- UA_StatusCode_name(retval));
- UA_Server_delete(server);
- UA_free(discovery_url);
- return EXIT_FAILURE;
- }
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Server started. Waiting for announce of LDS Server.");
- while (running && discovery_url == NULL)
- UA_Server_run_iterate(server, true);
- if(!running) {
- UA_Server_delete(server);
- UA_free(discovery_url);
- return EXIT_FAILURE;
- }
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "LDS-ME server found on %s", discovery_url);
-
- UA_EndpointDescription *endpointRegister = getRegisterEndpointFromServer(discovery_url);
- UA_free(discovery_url);
- if(endpointRegister == NULL || endpointRegister->securityMode == UA_MESSAGESECURITYMODE_INVALID) {
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Could not find any suitable endpoints on discovery server");
- UA_Server_delete(server);
- return EXIT_FAILURE;
- }
- UA_Client *clientRegister = getRegisterClient(endpointRegister, argc, argv);
- if(!clientRegister) {
- UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Could not create the client for remote registering");
- UA_Server_delete(server);
- return EXIT_FAILURE;
- }
-
- char *endpointUrl = (char*)UA_malloc(endpointRegister->endpointUrl.length + 1);
- memcpy(endpointUrl, endpointRegister->endpointUrl.data, endpointRegister->endpointUrl.length);
- endpointUrl[endpointRegister->endpointUrl.length] = 0;
- UA_EndpointDescription_delete(endpointRegister);
- retval = UA_Server_addPeriodicServerRegisterCallback(server, clientRegister, endpointUrl,
- 10 * 60 * 1000, 500, NULL);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Could not create periodic job for server register. StatusCode %s",
- UA_StatusCode_name(retval));
- UA_free(endpointUrl);
- UA_Client_disconnect(clientRegister);
- UA_Client_delete(clientRegister);
- UA_Server_delete(server);
- return EXIT_FAILURE;
- }
- while (running)
- UA_Server_run_iterate(server, true);
- UA_Server_run_shutdown(server);
-
- retval = UA_Server_unregister_discovery(server, clientRegister);
- if(retval != UA_STATUSCODE_GOOD)
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
- "Could not unregister server from discovery server. "
- "StatusCode %s", UA_StatusCode_name(retval));
- UA_free(endpointUrl);
- UA_Client_disconnect(clientRegister);
- UA_Client_delete(clientRegister);
- UA_Server_delete(server);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|