ua_config_default.c 11 KB

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