ua_config_default.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include "ua_config_default.h"
  4. #include "ua_log_stdout.h"
  5. #include "ua_network_tcp.h"
  6. #include "ua_accesscontrol_default.h"
  7. #include "ua_nodestore_default.h"
  8. #include "ua_types_generated.h"
  9. #include "ua_securitypolicy_none.h"
  10. #include "ua_types.h"
  11. #include "ua_types_generated_handling.h"
  12. #include "ua_client_highlevel.h"
  13. #define ANONYMOUS_POLICY "open62541-anonymous-policy"
  14. #define USERNAME_POLICY "open62541-username-policy"
  15. /* Struct initialization works across ANSI C/C99/C++ if it is done when the
  16. * variable is first declared. Assigning values to existing structs is
  17. * heterogeneous across the three. */
  18. static UA_INLINE UA_UInt32Range
  19. UA_UINT32RANGE(UA_UInt32 min, UA_UInt32 max) {
  20. UA_UInt32Range range = {min, max};
  21. return range;
  22. }
  23. static UA_INLINE UA_DurationRange
  24. UA_DURATIONRANGE(UA_Double min, UA_Double max) {
  25. UA_DurationRange range = {min, max};
  26. return range;
  27. }
  28. /*******************************/
  29. /* Default Connection Settings */
  30. /*******************************/
  31. const UA_ConnectionConfig UA_ConnectionConfig_default = {
  32. 0, /* .protocolVersion */
  33. 65535, /* .sendBufferSize, 64k per chunk */
  34. 65535, /* .recvBufferSize, 64k per chunk */
  35. 0, /* .maxMessageSize, 0 -> unlimited */
  36. 0 /* .maxChunkCount, 0 -> unlimited */
  37. };
  38. /***************************/
  39. /* Default Server Settings */
  40. /***************************/
  41. #define MANUFACTURER_NAME "open62541"
  42. #define PRODUCT_NAME "open62541 OPC UA Server"
  43. #define PRODUCT_URI "http://open62541.org"
  44. #define APPLICATION_NAME "open62541-based OPC UA Application"
  45. #define APPLICATION_URI "urn:unconfigured:application"
  46. #define STRINGIFY(arg) #arg
  47. #define VERSION(MAJOR, MINOR, PATCH, LABEL) \
  48. STRINGIFY(MAJOR) "." STRINGIFY(MINOR) "." STRINGIFY(PATCH) LABEL
  49. static UA_StatusCode
  50. createSecurityPolicyNoneEndpoint(UA_ServerConfig *conf, UA_Endpoint *endpoint,
  51. const UA_ByteString localCertificate) {
  52. UA_EndpointDescription_init(&endpoint->endpointDescription);
  53. UA_SecurityPolicy_None(&endpoint->securityPolicy, localCertificate, conf->logger);
  54. endpoint->endpointDescription.securityMode = UA_MESSAGESECURITYMODE_NONE;
  55. endpoint->endpointDescription.securityPolicyUri =
  56. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  57. endpoint->endpointDescription.transportProfileUri =
  58. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  59. /* enable anonymous and username/password */
  60. size_t policies = 2;
  61. endpoint->endpointDescription.userIdentityTokens = (UA_UserTokenPolicy*)
  62. UA_Array_new(policies, &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  63. if(!endpoint->endpointDescription.userIdentityTokens)
  64. return UA_STATUSCODE_BADOUTOFMEMORY;
  65. endpoint->endpointDescription.userIdentityTokensSize = policies;
  66. endpoint->endpointDescription.userIdentityTokens[0].tokenType =
  67. UA_USERTOKENTYPE_ANONYMOUS;
  68. endpoint->endpointDescription.userIdentityTokens[0].policyId =
  69. UA_STRING_ALLOC(ANONYMOUS_POLICY);
  70. endpoint->endpointDescription.userIdentityTokens[1].tokenType =
  71. UA_USERTOKENTYPE_USERNAME;
  72. endpoint->endpointDescription.userIdentityTokens[1].policyId =
  73. UA_STRING_ALLOC(USERNAME_POLICY);
  74. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  75. UA_ApplicationDescription_copy(&conf->applicationDescription,
  76. &endpoint->endpointDescription.server);
  77. return UA_STATUSCODE_GOOD;
  78. }
  79. UA_ServerConfig *
  80. UA_ServerConfig_new_minimal(UA_UInt16 portNumber,
  81. const UA_ByteString *certificate) {
  82. UA_ServerConfig *conf = (UA_ServerConfig*)UA_malloc(sizeof(UA_ServerConfig));
  83. if(!conf)
  84. return NULL;
  85. /* Zero out.. All members have a valid initial value */
  86. memset(conf, 0, sizeof(UA_ServerConfig));
  87. /* --> Start setting the default static config <-- */
  88. conf->nThreads = 1;
  89. conf->logger = UA_Log_Stdout;
  90. /* Server Description */
  91. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  92. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  93. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  94. conf->buildInfo.softwareVersion =
  95. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  96. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  97. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  98. conf->buildInfo.buildDate = 0;
  99. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  100. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  101. conf->applicationDescription.applicationName =
  102. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  103. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  104. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  105. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  106. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  107. /* conf->applicationDescription.discoveryUrls = NULL; */
  108. #ifdef UA_ENABLE_DISCOVERY
  109. /* conf->mdnsServerName = UA_STRING_NULL; */
  110. /* conf->serverCapabilitiesSize = 0; */
  111. /* conf->serverCapabilities = NULL; */
  112. #endif
  113. /* Custom DataTypes */
  114. /* conf->customDataTypesSize = 0; */
  115. /* conf->customDataTypes = NULL; */
  116. /* Networking */
  117. /* conf->networkLayersSize = 0; */
  118. /* conf->networkLayers = NULL; */
  119. /* Endpoints */
  120. /* conf->endpoints = {0, NULL}; */
  121. /* Global Node Lifecycle */
  122. conf->nodeLifecycle.constructor = NULL;
  123. conf->nodeLifecycle.destructor = NULL;
  124. /* Access Control */
  125. conf->accessControl.enableAnonymousLogin = true;
  126. conf->accessControl.enableUsernamePasswordLogin = true;
  127. conf->accessControl.activateSession = activateSession_default;
  128. conf->accessControl.closeSession = closeSession_default;
  129. conf->accessControl.getUserRightsMask = getUserRightsMask_default;
  130. conf->accessControl.getUserAccessLevel = getUserAccessLevel_default;
  131. conf->accessControl.getUserExecutable = getUserExecutable_default;
  132. conf->accessControl.getUserExecutableOnObject = getUserExecutableOnObject_default;
  133. conf->accessControl.allowAddNode = allowAddNode_default;
  134. conf->accessControl.allowAddReference = allowAddReference_default;
  135. conf->accessControl.allowDeleteNode = allowDeleteNode_default;
  136. conf->accessControl.allowDeleteReference = allowDeleteReference_default;
  137. /* Limits for SecureChannels */
  138. conf->maxSecureChannels = 40;
  139. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  140. /* Limits for Sessions */
  141. conf->maxSessions = 100;
  142. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  143. /* Limits for Subscriptions */
  144. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  145. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  146. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  147. conf->maxNotificationsPerPublish = 1000;
  148. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  149. /* Limits for MonitoredItems */
  150. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  151. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  152. #ifdef UA_ENABLE_DISCOVERY
  153. conf->discoveryCleanupTimeout = 60 * 60;
  154. #endif
  155. /* --> Finish setting the default static config <-- */
  156. UA_StatusCode retval = UA_Nodestore_default_new(&conf->nodestore);
  157. if(retval != UA_STATUSCODE_GOOD) {
  158. UA_ServerConfig_delete(conf);
  159. return NULL;
  160. }
  161. /* Add a network layer */
  162. conf->networkLayers = (UA_ServerNetworkLayer*)
  163. UA_malloc(sizeof(UA_ServerNetworkLayer));
  164. if(!conf->networkLayers) {
  165. UA_ServerConfig_delete(conf);
  166. return NULL;
  167. }
  168. conf->networkLayers[0] =
  169. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber);
  170. conf->networkLayersSize = 1;
  171. /* Allocate the endpoint */
  172. conf->endpointsSize = 1;
  173. conf->endpoints = (UA_Endpoint*)UA_malloc(sizeof(UA_Endpoint));
  174. if(!conf->endpoints) {
  175. UA_ServerConfig_delete(conf);
  176. return NULL;
  177. }
  178. /* Populate the endpoint */
  179. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  180. if(certificate)
  181. localCertificate = *certificate;
  182. retval =
  183. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], localCertificate);
  184. if(retval != UA_STATUSCODE_GOOD) {
  185. UA_ServerConfig_delete(conf);
  186. return NULL;
  187. }
  188. return conf;
  189. }
  190. void
  191. UA_ServerConfig_delete(UA_ServerConfig *config) {
  192. if(!config)
  193. return;
  194. /* Server Description */
  195. UA_BuildInfo_deleteMembers(&config->buildInfo);
  196. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  197. #ifdef UA_ENABLE_DISCOVERY
  198. UA_String_deleteMembers(&config->mdnsServerName);
  199. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  200. &UA_TYPES[UA_TYPES_STRING]);
  201. config->serverCapabilities = NULL;
  202. config->serverCapabilitiesSize = 0;
  203. #endif
  204. /* Nodestore */
  205. if(config->nodestore.deleteNodestore)
  206. config->nodestore.deleteNodestore(config->nodestore.context);
  207. /* Custom DataTypes */
  208. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  209. UA_free(config->customDataTypes[i].members);
  210. UA_free(config->customDataTypes);
  211. config->customDataTypes = NULL;
  212. config->customDataTypesSize = 0;
  213. /* Networking */
  214. for(size_t i = 0; i < config->networkLayersSize; ++i)
  215. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  216. UA_free(config->networkLayers);
  217. config->networkLayers = NULL;
  218. config->networkLayersSize = 0;
  219. for(size_t i = 0; i < config->endpointsSize; ++i) {
  220. UA_SecurityPolicy *policy = &config->endpoints[i].securityPolicy;
  221. policy->deleteMembers(policy);
  222. UA_EndpointDescription_deleteMembers(&config->endpoints[i].endpointDescription);
  223. }
  224. UA_free(config->endpoints);
  225. config->endpoints = NULL;
  226. config->endpointsSize = 0;
  227. UA_free(config);
  228. }
  229. /***************************/
  230. /* Default Client Settings */
  231. /***************************/
  232. const UA_ClientConfig UA_ClientConfig_default = {
  233. 5000, /* .timeout, 5 seconds */
  234. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  235. UA_Log_Stdout, /* .logger */
  236. /* .localConnectionConfig */
  237. {0, /* .protocolVersion */
  238. 65535, /* .sendBufferSize, 64k per chunk */
  239. 65535, /* .recvBufferSize, 64k per chunk */
  240. 0, /* .maxMessageSize, 0 -> unlimited */
  241. 0}, /* .maxChunkCount, 0 -> unlimited */
  242. UA_ClientConnectionTCP, /* .connectionFunc */
  243. 0, /* .customDataTypesSize */
  244. NULL /*.customDataTypes */
  245. };
  246. /****************************************/
  247. /* Default Client Subscription Settings */
  248. /****************************************/
  249. #ifdef UA_ENABLE_SUBSCRIPTIONS
  250. const UA_SubscriptionSettings UA_SubscriptionSettings_default = {
  251. 500.0, /* .requestedPublishingInterval */
  252. 10000, /* .requestedLifetimeCount */
  253. 1, /* .requestedMaxKeepAliveCount */
  254. 10, /* .maxNotificationsPerPublish */
  255. true, /* .publishingEnabled */
  256. 0 /* .priority */
  257. };
  258. #endif