ua_network_pubsub_udp.c 19 KB

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