ua_config_standard.c 11 KB

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