ua_network_pubsub_udp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright (c) 2017-2018 Fraunhofer IOSB (Author: Andreas Ebner)
  6. * Copyright 2018 (c) Jose Cabral, fortiss GmbH
  7. */
  8. #include "ua_plugin_network.h"
  9. #include "ua_network_pubsub_udp.h"
  10. #include "ua_util.h"
  11. #include "ua_log_stdout.h"
  12. //UDP multicast network layer specific internal data
  13. typedef struct {
  14. int ai_family; //Protocol family for socket. IPv4/IPv6
  15. struct sockaddr_storage *ai_addr; //https://msdn.microsoft.com/de-de/library/windows/desktop/ms740496(v=vs.85).aspx
  16. UA_UInt32 messageTTL;
  17. UA_Boolean enableLoopback;
  18. UA_Boolean enableReuse;
  19. } UA_PubSubChannelDataUDPMC;
  20. /**
  21. * Open communication socket based on the connectionConfig. Protocol specific parameters are
  22. * provided within the connectionConfig as KeyValuePair.
  23. * Currently supported options: "ttl" , "loopback", "reuse"
  24. *
  25. * @return ref to created channel, NULL on error
  26. */
  27. static UA_PubSubChannel *
  28. UA_PubSubChannelUDPMC_open(const UA_PubSubConnectionConfig *connectionConfig) {
  29. UA_initialize_architecture_network();
  30. UA_NetworkAddressUrlDataType address;
  31. if(UA_Variant_hasScalarType(&connectionConfig->address, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE])){
  32. address = *(UA_NetworkAddressUrlDataType *)connectionConfig->address.data;
  33. } else {
  34. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection creation failed. Invalid Address.");
  35. return NULL;
  36. }
  37. //allocate and init memory for the UDP multicast specific internal data
  38. UA_PubSubChannelDataUDPMC * channelDataUDPMC =
  39. (UA_PubSubChannelDataUDPMC *) UA_calloc(1, (sizeof(UA_PubSubChannelDataUDPMC)));
  40. if(!channelDataUDPMC){
  41. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection creation failed. Out of memory.");
  42. return NULL;
  43. }
  44. //set default values
  45. UA_PubSubChannelDataUDPMC defaultValues = {0, NULL, 255, UA_TRUE, UA_TRUE};
  46. memcpy(channelDataUDPMC, &defaultValues, sizeof(UA_PubSubChannelDataUDPMC));
  47. //iterate over the given KeyValuePair paramters
  48. UA_String ttlParam = UA_STRING("ttl"), loopbackParam = UA_STRING("loopback"), reuseParam = UA_STRING("reuse");
  49. for(size_t i = 0; i < connectionConfig->connectionPropertiesSize; i++){
  50. if(UA_String_equal(&connectionConfig->connectionProperties[i].key.name, &ttlParam)){
  51. if(UA_Variant_hasScalarType(&connectionConfig->connectionProperties[i].value, &UA_TYPES[UA_TYPES_UINT32])){
  52. channelDataUDPMC->messageTTL = *(UA_UInt32 *) connectionConfig->connectionProperties[i].value.data;
  53. }
  54. } else if(UA_String_equal(&connectionConfig->connectionProperties[i].key.name, &loopbackParam)){
  55. if(UA_Variant_hasScalarType(&connectionConfig->connectionProperties[i].value, &UA_TYPES[UA_TYPES_BOOLEAN])){
  56. channelDataUDPMC->enableLoopback = *(UA_Boolean *) connectionConfig->connectionProperties[i].value.data;
  57. }
  58. } else if(UA_String_equal(&connectionConfig->connectionProperties[i].key.name, &reuseParam)){
  59. if(UA_Variant_hasScalarType(&connectionConfig->connectionProperties[i].value, &UA_TYPES[UA_TYPES_BOOLEAN])){
  60. channelDataUDPMC->enableReuse = *(UA_Boolean *) connectionConfig->connectionProperties[i].value.data;
  61. }
  62. } else {
  63. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection creation. Unknown connection parameter.");
  64. }
  65. }
  66. UA_PubSubChannel *newChannel = (UA_PubSubChannel *) UA_calloc(1, sizeof(UA_PubSubChannel));
  67. if(!newChannel){
  68. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection creation failed. Out of memory.");
  69. UA_free(channelDataUDPMC);
  70. return NULL;
  71. }
  72. struct addrinfo hints, *rp, *requestResult = NULL;
  73. memset(&hints, 0, sizeof hints);
  74. hints.ai_family = AF_UNSPEC;
  75. hints.ai_socktype = SOCK_DGRAM;
  76. hints.ai_flags = 0;
  77. hints.ai_protocol = 0;
  78. UA_String hostname, path;
  79. UA_UInt16 networkPort;
  80. if(UA_parseEndpointUrl(&address.url, &hostname, &networkPort, &path) != UA_STATUSCODE_GOOD){
  81. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  82. "PubSub Connection creation failed. Invalid URL.");
  83. UA_free(channelDataUDPMC);
  84. UA_free(newChannel);
  85. return NULL;
  86. }
  87. if(hostname.length > 512) {
  88. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  89. "PubSub Connection creation failed. URL maximum length is 512.");
  90. UA_free(channelDataUDPMC);
  91. UA_free(newChannel);
  92. return NULL;
  93. }
  94. UA_STACKARRAY(char, addressAsChar, sizeof(char) * hostname.length +1);
  95. memcpy(addressAsChar, hostname.data, hostname.length);
  96. addressAsChar[hostname.length] = 0;
  97. char port[6];
  98. sprintf(port, "%u", networkPort);
  99. if(UA_getaddrinfo(addressAsChar, port, &hints, &requestResult) != 0) {
  100. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  101. "PubSub Connection creation failed. Internal error.");
  102. UA_free(channelDataUDPMC);
  103. UA_free(newChannel);
  104. return NULL;
  105. }
  106. //check if the ip address is a multicast address
  107. if(requestResult->ai_family == PF_INET){
  108. struct in_addr imr_interface;
  109. UA_inet_pton(AF_INET, addressAsChar, &imr_interface);
  110. if((UA_ntohl(imr_interface.s_addr) & 0xF0000000) != 0xE0000000){
  111. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  112. "PubSub Connection creation failed. No multicast address.");
  113. UA_freeaddrinfo(requestResult);
  114. UA_free(channelDataUDPMC);
  115. UA_free(newChannel);
  116. return NULL;
  117. }
  118. } else {
  119. //TODO check if ipv6 addrr is multicast address.
  120. }
  121. for(rp = requestResult; rp != NULL; rp = rp->ai_next){
  122. newChannel->sockfd = UA_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  123. if(newChannel->sockfd != UA_INVALID_SOCKET){
  124. break; /*success*/
  125. }
  126. }
  127. if(!rp){
  128. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  129. "PubSub Connection creation failed. Internal error.");
  130. UA_freeaddrinfo(requestResult);
  131. UA_free(channelDataUDPMC);
  132. UA_free(newChannel);
  133. return NULL;
  134. }
  135. channelDataUDPMC->ai_family = rp->ai_family;
  136. channelDataUDPMC->ai_addr = (struct sockaddr_storage *) UA_calloc(1, sizeof(struct sockaddr_storage));
  137. if(!channelDataUDPMC->ai_addr){
  138. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  139. "PubSub Connection creation failed. Out of memory.");
  140. UA_close(newChannel->sockfd);
  141. UA_freeaddrinfo(requestResult);
  142. UA_free(channelDataUDPMC);
  143. UA_free(newChannel);
  144. return NULL;
  145. }
  146. memcpy(channelDataUDPMC->ai_addr, rp->ai_addr, sizeof(*rp->ai_addr));
  147. //link channel and internal channel data
  148. newChannel->handle = channelDataUDPMC;
  149. //Set loop back data to your host
  150. #if UA_IPV6
  151. if(UA_setsockopt(newChannel->sockfd,
  152. requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
  153. requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP,
  154. (const char *)&channelDataUDPMC->enableLoopback, sizeof (channelDataUDPMC->enableLoopback))
  155. #else
  156. if(UA_setsockopt(newChannel->sockfd,
  157. IPPROTO_IP,
  158. IP_MULTICAST_LOOP,
  159. (const char *)&channelDataUDPMC->enableLoopback, sizeof (channelDataUDPMC->enableLoopback))
  160. #endif
  161. < 0) {
  162. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  163. "PubSub Connection creation failed. Loopback setup failed.");
  164. UA_close(newChannel->sockfd);
  165. UA_freeaddrinfo(requestResult);
  166. UA_free(channelDataUDPMC);
  167. UA_free(newChannel);
  168. return NULL;
  169. }
  170. //Set Time to live (TTL). Value of 1 prevent forward beyond the local network.
  171. #if UA_IPV6
  172. if(UA_setsockopt(newChannel->sockfd,
  173. requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
  174. requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL,
  175. (const char *)&channelDataUDPMC->messageTTL, sizeof(channelDataUDPMC->messageTTL))
  176. #else
  177. if(UA_setsockopt(newChannel->sockfd,
  178. IPPROTO_IP,
  179. IP_MULTICAST_TTL,
  180. (const char *)&channelDataUDPMC->messageTTL, sizeof(channelDataUDPMC->messageTTL))
  181. #endif
  182. < 0) {
  183. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  184. "PubSub Connection creation problem. Time to live setup failed.");
  185. }
  186. //Set reuse address -> enables sharing of the same listening address on different sockets.
  187. if(channelDataUDPMC->enableReuse){
  188. int enableReuse = 1;
  189. if(UA_setsockopt(newChannel->sockfd,
  190. SOL_SOCKET, SO_REUSEADDR,
  191. (const char*)&enableReuse, sizeof(enableReuse)) < 0){
  192. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  193. "PubSub Connection creation problem. Reuse address setup failed.");
  194. }
  195. }
  196. //Set the physical interface for outgoing traffic
  197. if(address.networkInterface.length > 0){
  198. UA_STACKARRAY(char, interfaceAsChar, sizeof(char) * address.networkInterface.length + 1);
  199. memcpy(interfaceAsChar, address.networkInterface.data, address.networkInterface.length);
  200. interfaceAsChar[address.networkInterface.length] = 0;
  201. enum{
  202. IPv4,
  203. #if UA_IPV6
  204. IPv6,
  205. #endif
  206. INVALID
  207. } ipVersion;
  208. union {
  209. struct ip_mreq ipv4;
  210. #if UA_IPV6
  211. struct ipv6_mreq ipv6;
  212. #endif
  213. } group;
  214. if(UA_inet_pton(AF_INET, interfaceAsChar, &group.ipv4.imr_interface)){
  215. ipVersion = IPv4;
  216. #if UA_IPV6
  217. } else if (UA_inet_pton(AF_INET6, interfaceAsChar, &group.ipv6.ipv6mr_multiaddr)){
  218. group.ipv6.ipv6mr_interface = UA_if_nametoindex(interfaceAsChar);
  219. ipVersion = IPv6;
  220. #endif
  221. } else {
  222. ipVersion = INVALID;
  223. }
  224. if(ipVersion == INVALID ||
  225. #if UA_IPV6
  226. UA_setsockopt(newChannel->sockfd,
  227. requestResult->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
  228. requestResult->ai_family == PF_INET6 ? IPV6_MULTICAST_IF : IP_MULTICAST_IF,
  229. ipVersion == IPv6 ? (const void *) &group.ipv6.ipv6mr_interface : &group.ipv4.imr_interface,
  230. ipVersion == IPv6 ? sizeof(group.ipv6.ipv6mr_interface) : sizeof(struct in_addr))
  231. #else
  232. UA_setsockopt(newChannel->sockfd,
  233. IPPROTO_IP,
  234. IP_MULTICAST_IF,
  235. &group.ipv4.imr_interface,
  236. sizeof(struct in_addr))
  237. #endif
  238. < 0) {
  239. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  240. "PubSub Connection creation problem. Interface selection failed.");
  241. };
  242. }
  243. UA_freeaddrinfo(requestResult);
  244. newChannel->state = UA_PUBSUB_CHANNEL_PUB;
  245. return newChannel;
  246. }
  247. /**
  248. * Subscribe to a given address.
  249. *
  250. * @return UA_STATUSCODE_GOOD on success
  251. */
  252. static UA_StatusCode
  253. UA_PubSubChannelUDPMC_regist(UA_PubSubChannel *channel, UA_ExtensionObject *transportSettings) {
  254. if(!(channel->state == UA_PUBSUB_CHANNEL_PUB || channel->state == UA_PUBSUB_CHANNEL_RDY)){
  255. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
  256. return UA_STATUSCODE_BADINTERNALERROR;
  257. }
  258. UA_PubSubChannelDataUDPMC * connectionConfig = (UA_PubSubChannelDataUDPMC *) channel->handle;
  259. if(connectionConfig->ai_family == PF_INET){//IPv4 handling
  260. struct sockaddr_in addr;
  261. memcpy(&addr, connectionConfig->ai_addr, sizeof(struct sockaddr_in));
  262. addr.sin_addr.s_addr = INADDR_ANY;
  263. if (UA_bind(channel->sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0){
  264. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
  265. return UA_STATUSCODE_BADINTERNALERROR;
  266. }
  267. struct ip_mreq groupV4;
  268. memcpy(&groupV4.imr_multiaddr, &((const struct sockaddr_in *)connectionConfig->ai_addr)->sin_addr, sizeof(struct ip_mreq));
  269. groupV4.imr_interface.s_addr = UA_htonl(INADDR_ANY);
  270. //multihomed hosts can join several groups on different IF, INADDR_ANY -> kernel decides
  271. if(UA_setsockopt(channel->sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
  272. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
  273. return UA_STATUSCODE_BADINTERNALERROR;
  274. }
  275. #if UA_IPV6
  276. } else if (connectionConfig->ai_family == PF_INET6) {//IPv6 handling
  277. //TODO implement regist for IPv6
  278. #endif
  279. } else {
  280. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection regist failed.");
  281. return UA_STATUSCODE_BADINTERNALERROR;
  282. }
  283. return UA_STATUSCODE_GOOD;
  284. }
  285. /**
  286. * Remove current subscription.
  287. *
  288. * @return UA_STATUSCODE_GOOD on success
  289. */
  290. static UA_StatusCode
  291. UA_PubSubChannelUDPMC_unregist(UA_PubSubChannel *channel, UA_ExtensionObject *transportSettings) {
  292. if(!(channel->state == UA_PUBSUB_CHANNEL_PUB_SUB || channel->state == UA_PUBSUB_CHANNEL_SUB)){
  293. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection unregist failed.");
  294. return UA_STATUSCODE_BADINTERNALERROR;
  295. }
  296. UA_PubSubChannelDataUDPMC * connectionConfig = (UA_PubSubChannelDataUDPMC *) channel->handle;
  297. if(connectionConfig->ai_family == PF_INET){//IPv4 handling
  298. struct ip_mreq groupV4;
  299. memcpy(&groupV4.imr_multiaddr, &((const struct sockaddr_in *)connectionConfig->ai_addr)->sin_addr, sizeof(struct ip_mreq));
  300. groupV4.imr_interface.s_addr = UA_htonl(INADDR_ANY);
  301. if(UA_setsockopt(channel->sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *) &groupV4, sizeof(groupV4)) != 0){
  302. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection unregist failed.");
  303. return UA_STATUSCODE_BADINTERNALERROR;
  304. }
  305. #if UA_IPV6
  306. } else if (connectionConfig->ai_family == PF_INET6) {//IPv6 handling
  307. //TODO implement unregist for IPv6
  308. #endif
  309. } else {
  310. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection unregist failed.");
  311. return UA_STATUSCODE_BADINTERNALERROR;
  312. }
  313. return UA_STATUSCODE_GOOD;
  314. }
  315. /**
  316. * Send messages to the connection defined address
  317. *
  318. * @return UA_STATUSCODE_GOOD if success
  319. */
  320. static UA_StatusCode
  321. UA_PubSubChannelUDPMC_send(UA_PubSubChannel *channel, UA_ExtensionObject *transportSettigns, const UA_ByteString *buf) {
  322. UA_PubSubChannelDataUDPMC *channelConfigUDPMC = (UA_PubSubChannelDataUDPMC *) channel->handle;
  323. if(!(channel->state == UA_PUBSUB_CHANNEL_PUB || channel->state == UA_PUBSUB_CHANNEL_PUB_SUB)){
  324. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection sending failed. Invalid state.");
  325. return UA_STATUSCODE_BADINTERNALERROR;
  326. }
  327. //TODO evalute: chunk messages or check against MTU?
  328. long nWritten = 0;
  329. while (nWritten < (long)buf->length) {
  330. long n = (long)UA_sendto(channel->sockfd, buf->data, buf->length, 0,
  331. (struct sockaddr *) channelConfigUDPMC->ai_addr, sizeof(struct sockaddr_storage));
  332. if(n == -1L) {
  333. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection sending failed.");
  334. return UA_STATUSCODE_BADINTERNALERROR;
  335. }
  336. nWritten += n;
  337. }
  338. return UA_STATUSCODE_GOOD;
  339. }
  340. /**
  341. * Receive messages. The regist function should be called before.
  342. *
  343. * @param timeout in usec | on windows platforms are only multiples of 1000usec possible
  344. * @return
  345. */
  346. static UA_StatusCode
  347. UA_PubSubChannelUDPMC_receive(UA_PubSubChannel *channel, UA_ByteString *message, UA_ExtensionObject *transportSettigns, UA_UInt32 timeout){
  348. if(!(channel->state == UA_PUBSUB_CHANNEL_PUB || channel->state == UA_PUBSUB_CHANNEL_PUB_SUB)) {
  349. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection receive failed. Invalid state.");
  350. return UA_STATUSCODE_BADINTERNALERROR;
  351. }
  352. UA_PubSubChannelDataUDPMC *channelConfigUDPMC = (UA_PubSubChannelDataUDPMC *) channel->handle;
  353. if(timeout > 0) {
  354. fd_set fdset;
  355. FD_ZERO(&fdset);
  356. UA_fd_set(channel->sockfd, &fdset);
  357. struct timeval tmptv = {(long int)(timeout / 1000000),
  358. (long int)(timeout % 1000000)};
  359. int resultsize = UA_select(channel->sockfd+1, &fdset, NULL,
  360. NULL, &tmptv);
  361. if(resultsize == 0) {
  362. message->length = 0;
  363. return UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  364. }
  365. if (resultsize == -1) {
  366. message->length = 0;
  367. return UA_STATUSCODE_BADINTERNALERROR;
  368. }
  369. }
  370. if(channelConfigUDPMC->ai_family == PF_INET){
  371. ssize_t messageLength;
  372. messageLength = UA_recvfrom(channel->sockfd, message->data, message->length, 0, NULL, NULL);
  373. if(messageLength > 0){
  374. message->length = (size_t) messageLength;
  375. } else {
  376. message->length = 0;
  377. }
  378. #if UA_IPV6
  379. } else {
  380. //TODO implement recieve for IPv6
  381. #endif
  382. }
  383. return UA_STATUSCODE_GOOD;
  384. }
  385. /**
  386. * Close channel and free the channel data.
  387. *
  388. * @return UA_STATUSCODE_GOOD if success
  389. */
  390. static UA_StatusCode
  391. UA_PubSubChannelUDPMC_close(UA_PubSubChannel *channel) {
  392. if(UA_close(channel->sockfd) != 0){
  393. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "PubSub Connection delete failed.");
  394. return UA_STATUSCODE_BADINTERNALERROR;
  395. }
  396. UA_deinitialize_architecture_network();
  397. //cleanup the internal NetworkLayer data
  398. UA_PubSubChannelDataUDPMC *networkLayerData = (UA_PubSubChannelDataUDPMC *) channel->handle;
  399. UA_free(networkLayerData->ai_addr);
  400. UA_free(networkLayerData);
  401. UA_free(channel);
  402. return UA_STATUSCODE_GOOD;
  403. }
  404. /**
  405. * Generate a new channel. based on the given configuration.
  406. *
  407. * @param connectionConfig connection configuration
  408. * @return ref to created channel, NULL on error
  409. */
  410. static UA_PubSubChannel *
  411. TransportLayerUDPMC_addChannel(UA_PubSubConnectionConfig *connectionConfig) {
  412. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "PubSub channel requested");
  413. UA_PubSubChannel * pubSubChannel = UA_PubSubChannelUDPMC_open(connectionConfig);
  414. if(pubSubChannel){
  415. pubSubChannel->regist = UA_PubSubChannelUDPMC_regist;
  416. pubSubChannel->unregist = UA_PubSubChannelUDPMC_unregist;
  417. pubSubChannel->send = UA_PubSubChannelUDPMC_send;
  418. pubSubChannel->receive = UA_PubSubChannelUDPMC_receive;
  419. pubSubChannel->close = UA_PubSubChannelUDPMC_close;
  420. pubSubChannel->connectionConfig = connectionConfig;
  421. }
  422. return pubSubChannel;
  423. }
  424. //UDPMC channel factory
  425. UA_PubSubTransportLayer
  426. UA_PubSubTransportLayerUDPMP() {
  427. UA_PubSubTransportLayer pubSubTransportLayer;
  428. pubSubTransportLayer.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  429. pubSubTransportLayer.createPubSubChannel = &TransportLayerUDPMC_addChannel;
  430. return pubSubTransportLayer;
  431. }