ua_config_standard.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /* Access Control */
  119. conf->accessControl.enableAnonymousLogin = true;
  120. conf->accessControl.enableUsernamePasswordLogin = true;
  121. conf->accessControl.activateSession = activateSession_default;
  122. conf->accessControl.closeSession = closeSession_default;
  123. conf->accessControl.getUserRightsMask = getUserRightsMask_default;
  124. conf->accessControl.getUserAccessLevel = getUserAccessLevel_default;
  125. conf->accessControl.getUserExecutable = getUserExecutable_default;
  126. conf->accessControl.getUserExecutableOnObject = getUserExecutableOnObject_default;
  127. conf->accessControl.allowAddNode = allowAddNode_default;
  128. conf->accessControl.allowAddReference = allowAddReference_default;
  129. conf->accessControl.allowDeleteNode = allowDeleteNode_default;
  130. conf->accessControl.allowDeleteReference = allowDeleteReference_default;
  131. /* Limits for SecureChannels */
  132. conf->maxSecureChannels = 40;
  133. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  134. /* Limits for Sessions */
  135. conf->maxSessions = 100;
  136. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  137. /* Limits for Subscriptions */
  138. conf->publishingIntervalLimits = UA_DOUBLERANGE(100.0, 3600.0 * 1000.0);
  139. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  140. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  141. conf->maxNotificationsPerPublish = 1000;
  142. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  143. /* Limits for MonitoredItems */
  144. conf->samplingIntervalLimits = UA_DOUBLERANGE(50.0, 24.0 * 3600.0 * 1000.0);
  145. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  146. #ifdef UA_ENABLE_DISCOVERY
  147. conf->discoveryCleanupTimeout = 60 * 60;
  148. #endif
  149. /* --> Finish setting the default static config <-- */
  150. /* Add a network layer */
  151. conf->networkLayers = (UA_ServerNetworkLayer*)
  152. UA_malloc(sizeof(UA_ServerNetworkLayer));
  153. if(!conf->networkLayers) {
  154. UA_free(conf);
  155. return NULL;
  156. }
  157. conf->networkLayers[0] =
  158. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber);
  159. conf->networkLayersSize = 1;
  160. /* Allocate the endpoint */
  161. conf->endpoints.endpoints = (UA_Endpoint*)UA_malloc(sizeof(UA_Endpoint));
  162. if(!conf->endpoints.endpoints) {
  163. conf->networkLayers[0].deleteMembers(&conf->networkLayers[0]);
  164. UA_free(conf->networkLayers);
  165. UA_free(conf);
  166. return NULL;
  167. }
  168. conf->endpoints.count = 1;
  169. /* Populate the endpoint */
  170. UA_StatusCode retval =
  171. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints.endpoints[0],
  172. certificate);
  173. if(retval != UA_STATUSCODE_GOOD) {
  174. conf->networkLayers[0].deleteMembers(&conf->networkLayers[0]);
  175. UA_free(conf->networkLayers);
  176. UA_free(conf->endpoints.endpoints);
  177. UA_free(conf);
  178. return NULL;
  179. }
  180. return conf;
  181. }
  182. void
  183. UA_ServerConfig_delete(UA_ServerConfig *config) {
  184. if(!config)
  185. return;
  186. /* Server Description */
  187. UA_BuildInfo_deleteMembers(&config->buildInfo);
  188. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  189. #ifdef UA_ENABLE_DISCOVERY
  190. UA_String_deleteMembers(&config->mdnsServerName);
  191. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  192. &UA_TYPES[UA_TYPES_STRING]);
  193. /* config->serverCapabilities = NULL; */
  194. /* config->serverCapabilitiesSize = 0; */
  195. #endif
  196. /* Custom DataTypes */
  197. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  198. UA_free(config->customDataTypes[i].members);
  199. UA_free(config->customDataTypes);
  200. /* config->customDataTypes = NULL; */
  201. /* config0>customDataTypesSize = 0; */
  202. /* Networking */
  203. for(size_t i = 0; i < config->networkLayersSize; ++i)
  204. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  205. UA_free(config->networkLayers);
  206. /* config->networkLayers = NULL; */
  207. /* config->networkLayersSize = 0; */
  208. for(size_t i = 0; i < config->endpoints.count; ++i)
  209. UA_EndpointDescription_deleteMembers(&config->endpoints.endpoints[i].endpointDescription);
  210. UA_free(config->endpoints.endpoints);
  211. /* config->endpoints.endpoints = NULL; */
  212. /* config->endpoints.count = 0; */
  213. UA_free(config);
  214. }
  215. /***************************/
  216. /* Default Client Settings */
  217. /***************************/
  218. const UA_ClientConfig UA_ClientConfig_default = {
  219. 5000, /* .timeout, 5 seconds */
  220. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  221. UA_Log_Stdout, /* .logger */
  222. /* .localConnectionConfig */
  223. {0, /* .protocolVersion */
  224. 65535, /* .sendBufferSize, 64k per chunk */
  225. 65535, /* .recvBufferSize, 64k per chunk */
  226. 0, /* .maxMessageSize, 0 -> unlimited */
  227. 0}, /* .maxChunkCount, 0 -> unlimited */
  228. UA_ClientConnectionTCP, /* .connectionFunc */
  229. 0, /* .customDataTypesSize */
  230. NULL /*.customDataTypes */
  231. };
  232. /****************************************/
  233. /* Default Client Subscription Settings */
  234. /****************************************/
  235. #ifdef UA_ENABLE_SUBSCRIPTIONS
  236. const UA_SubscriptionSettings UA_SubscriptionSettings_default = {
  237. 500.0, /* .requestedPublishingInterval */
  238. 10000, /* .requestedLifetimeCount */
  239. 1, /* .requestedMaxKeepAliveCount */
  240. 10, /* .maxNotificationsPerPublish */
  241. true, /* .publishingEnabled */
  242. 0 /* .priority */
  243. };
  244. #endif