ua_network_pubsub_udp.c 19 KB

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