4
0

totemudp.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <sys/uio.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/list.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/logsys.h>
  65. #include "totemudp.h"
  66. #include "util.h"
  67. #include "totemcrypto.h"
  68. #include <nss.h>
  69. #include <pk11pub.h>
  70. #include <pkcs11.h>
  71. #include <prerror.h>
  72. #ifndef MSG_NOSIGNAL
  73. #define MSG_NOSIGNAL 0
  74. #endif
  75. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  76. #define NETIF_STATE_REPORT_UP 1
  77. #define NETIF_STATE_REPORT_DOWN 2
  78. #define BIND_STATE_UNBOUND 0
  79. #define BIND_STATE_REGULAR 1
  80. #define BIND_STATE_LOOPBACK 2
  81. #define MESSAGE_TYPE_MEMB_JOIN 3
  82. struct totemudp_socket {
  83. int mcast_recv;
  84. int mcast_send;
  85. int token;
  86. };
  87. struct totemudp_instance {
  88. struct crypto_instance *crypto_inst;
  89. qb_loop_t *totemudp_poll_handle;
  90. struct totem_interface *totem_interface;
  91. int netif_state_report;
  92. int netif_bind_state;
  93. void *context;
  94. void (*totemudp_deliver_fn) (
  95. void *context,
  96. const void *msg,
  97. unsigned int msg_len);
  98. void (*totemudp_iface_change_fn) (
  99. void *context,
  100. const struct totem_ip_address *iface_address);
  101. void (*totemudp_target_set_completed) (void *context);
  102. /*
  103. * Function and data used to log messages
  104. */
  105. int totemudp_log_level_security;
  106. int totemudp_log_level_error;
  107. int totemudp_log_level_warning;
  108. int totemudp_log_level_notice;
  109. int totemudp_log_level_debug;
  110. int totemudp_subsys_id;
  111. void (*totemudp_log_printf) (
  112. int level,
  113. int subsys,
  114. const char *function,
  115. const char *file,
  116. int line,
  117. const char *format,
  118. ...)__attribute__((format(printf, 6, 7)));
  119. void *udp_context;
  120. char iov_buffer[FRAME_SIZE_MAX];
  121. char iov_buffer_flush[FRAME_SIZE_MAX];
  122. struct iovec totemudp_iov_recv;
  123. struct iovec totemudp_iov_recv_flush;
  124. struct totemudp_socket totemudp_sockets;
  125. struct totem_ip_address mcast_address;
  126. int stats_sent;
  127. int stats_recv;
  128. int stats_delv;
  129. int stats_remcasts;
  130. int stats_orf_token;
  131. struct timeval stats_tv_start;
  132. struct totem_ip_address my_id;
  133. int firstrun;
  134. qb_loop_timer_handle timer_netif_check_timeout;
  135. unsigned int my_memb_entries;
  136. int flushing;
  137. struct totem_config *totem_config;
  138. struct totem_ip_address token_target;
  139. };
  140. struct work_item {
  141. const void *msg;
  142. unsigned int msg_len;
  143. struct totemudp_instance *instance;
  144. };
  145. static int totemudp_build_sockets (
  146. struct totemudp_instance *instance,
  147. struct totem_ip_address *bindnet_address,
  148. struct totem_ip_address *mcastaddress,
  149. struct totemudp_socket *sockets,
  150. struct totem_ip_address *bound_to);
  151. static struct totem_ip_address localhost;
  152. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  153. {
  154. memset (instance, 0, sizeof (struct totemudp_instance));
  155. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  156. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  157. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  158. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  159. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  160. /*
  161. * There is always atleast 1 processor
  162. */
  163. instance->my_memb_entries = 1;
  164. }
  165. #define log_printf(level, format, args...) \
  166. do { \
  167. instance->totemudp_log_printf ( \
  168. level, instance->totemudp_subsys_id, \
  169. __FUNCTION__, __FILE__, __LINE__, \
  170. (const char *)format, ##args); \
  171. } while (0);
  172. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  173. do { \
  174. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  175. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  176. instance->totemudp_log_printf ( \
  177. level, instance->totemudp_subsys_id, \
  178. __FUNCTION__, __FILE__, __LINE__, \
  179. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  180. } while(0)
  181. int totemudp_crypto_set (
  182. void *udp_context,
  183. const char *cipher_type,
  184. const char *hash_type)
  185. {
  186. return (0);
  187. }
  188. static inline void ucast_sendmsg (
  189. struct totemudp_instance *instance,
  190. struct totem_ip_address *system_to,
  191. const void *msg,
  192. unsigned int msg_len)
  193. {
  194. struct msghdr msg_ucast;
  195. int res = 0;
  196. size_t buf_out_len;
  197. unsigned char buf_out[FRAME_SIZE_MAX];
  198. struct sockaddr_storage sockaddr;
  199. struct iovec iovec;
  200. int addrlen;
  201. /*
  202. * Encrypt and digest the message
  203. */
  204. if (crypto_encrypt_and_sign (
  205. instance->crypto_inst,
  206. (const unsigned char *)msg,
  207. msg_len,
  208. buf_out,
  209. &buf_out_len) != 0) {
  210. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  211. return;
  212. }
  213. iovec.iov_base = (void *)buf_out;
  214. iovec.iov_len = buf_out_len;
  215. /*
  216. * Build unicast message
  217. */
  218. memset(&msg_ucast, 0, sizeof(msg_ucast));
  219. totemip_totemip_to_sockaddr_convert(system_to,
  220. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  221. msg_ucast.msg_name = &sockaddr;
  222. msg_ucast.msg_namelen = addrlen;
  223. msg_ucast.msg_iov = (void *)&iovec;
  224. msg_ucast.msg_iovlen = 1;
  225. #ifdef HAVE_MSGHDR_CONTROL
  226. msg_ucast.msg_control = 0;
  227. #endif
  228. #ifdef HAVE_MSGHDR_CONTROLLEN
  229. msg_ucast.msg_controllen = 0;
  230. #endif
  231. #ifdef HAVE_MSGHDR_FLAGS
  232. msg_ucast.msg_flags = 0;
  233. #endif
  234. #ifdef HAVE_MSGHDR_ACCRIGHTS
  235. msg_ucast.msg_accrights = NULL;
  236. #endif
  237. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  238. msg_ucast.msg_accrightslen = 0;
  239. #endif
  240. /*
  241. * Transmit unicast message
  242. * An error here is recovered by totemsrp
  243. */
  244. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  245. MSG_NOSIGNAL);
  246. if (res < 0) {
  247. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  248. "sendmsg(ucast) failed (non-critical)");
  249. }
  250. }
  251. static inline void mcast_sendmsg (
  252. struct totemudp_instance *instance,
  253. const void *msg,
  254. unsigned int msg_len)
  255. {
  256. struct msghdr msg_mcast;
  257. int res = 0;
  258. size_t buf_out_len;
  259. unsigned char buf_out[FRAME_SIZE_MAX];
  260. struct iovec iovec;
  261. struct sockaddr_storage sockaddr;
  262. int addrlen;
  263. /*
  264. * Encrypt and digest the message
  265. */
  266. if (crypto_encrypt_and_sign (
  267. instance->crypto_inst,
  268. (const unsigned char *)msg,
  269. msg_len,
  270. buf_out,
  271. &buf_out_len) != 0) {
  272. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  273. return;
  274. }
  275. iovec.iov_base = (void *)&buf_out;
  276. iovec.iov_len = buf_out_len;
  277. /*
  278. * Build multicast message
  279. */
  280. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  281. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  282. memset(&msg_mcast, 0, sizeof(msg_mcast));
  283. msg_mcast.msg_name = &sockaddr;
  284. msg_mcast.msg_namelen = addrlen;
  285. msg_mcast.msg_iov = (void *)&iovec;
  286. msg_mcast.msg_iovlen = 1;
  287. #ifdef HAVE_MSGHDR_CONTROL
  288. msg_mcast.msg_control = 0;
  289. #endif
  290. #ifdef HAVE_MSGHDR_CONTROLLEN
  291. msg_mcast.msg_controllen = 0;
  292. #endif
  293. #ifdef HAVE_MSGHDR_FLAGS
  294. msg_mcast.msg_flags = 0;
  295. #endif
  296. #ifdef HAVE_MSGHDR_ACCRIGHTS
  297. msg_mcast.msg_accrights = NULL;
  298. #endif
  299. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  300. msg_mcast.msg_accrightslen = 0;
  301. #endif
  302. /*
  303. * Transmit multicast message
  304. * An error here is recovered by totemsrp
  305. */
  306. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  307. MSG_NOSIGNAL);
  308. if (res < 0) {
  309. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  310. "sendmsg(mcast) failed (non-critical)");
  311. }
  312. }
  313. int totemudp_finalize (
  314. void *udp_context)
  315. {
  316. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  317. int res = 0;
  318. if (instance->totemudp_sockets.mcast_recv > 0) {
  319. qb_loop_poll_del (instance->totemudp_poll_handle,
  320. instance->totemudp_sockets.mcast_recv);
  321. close (instance->totemudp_sockets.mcast_recv);
  322. }
  323. if (instance->totemudp_sockets.mcast_send > 0) {
  324. close (instance->totemudp_sockets.mcast_send);
  325. }
  326. if (instance->totemudp_sockets.token > 0) {
  327. qb_loop_poll_del (instance->totemudp_poll_handle,
  328. instance->totemudp_sockets.token);
  329. close (instance->totemudp_sockets.token);
  330. }
  331. return (res);
  332. }
  333. /*
  334. * Only designed to work with a message with one iov
  335. */
  336. static int net_deliver_fn (
  337. int fd,
  338. int revents,
  339. void *data)
  340. {
  341. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  342. struct msghdr msg_recv;
  343. struct iovec *iovec;
  344. struct sockaddr_storage system_from;
  345. int bytes_received;
  346. int res = 0;
  347. char *message_type;
  348. if (instance->flushing == 1) {
  349. iovec = &instance->totemudp_iov_recv_flush;
  350. } else {
  351. iovec = &instance->totemudp_iov_recv;
  352. }
  353. /*
  354. * Receive datagram
  355. */
  356. msg_recv.msg_name = &system_from;
  357. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  358. msg_recv.msg_iov = iovec;
  359. msg_recv.msg_iovlen = 1;
  360. #ifdef HAVE_MSGHDR_CONTROL
  361. msg_recv.msg_control = 0;
  362. #endif
  363. #ifdef HAVE_MSGHDR_CONTROLLEN
  364. msg_recv.msg_controllen = 0;
  365. #endif
  366. #ifdef HAVE_MSGHDR_FLAGS
  367. msg_recv.msg_flags = 0;
  368. #endif
  369. #ifdef HAVE_MSGHDR_ACCRIGHTS
  370. msg_recv.msg_accrights = NULL;
  371. #endif
  372. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  373. msg_recv.msg_accrightslen = 0;
  374. #endif
  375. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  376. if (bytes_received == -1) {
  377. return (0);
  378. } else {
  379. instance->stats_recv += bytes_received;
  380. }
  381. /*
  382. * Authenticate and if authenticated, decrypt datagram
  383. */
  384. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  385. if (res == -1) {
  386. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
  387. log_printf (instance->totemudp_log_level_security,
  388. "Invalid packet data");
  389. iovec->iov_len = FRAME_SIZE_MAX;
  390. return 0;
  391. }
  392. iovec->iov_len = bytes_received;
  393. /*
  394. * Drop all non-mcast messages (more specifically join
  395. * messages should be dropped)
  396. */
  397. message_type = (char *)iovec->iov_base;
  398. if (instance->flushing == 1 && *message_type == MESSAGE_TYPE_MEMB_JOIN) {
  399. iovec->iov_len = FRAME_SIZE_MAX;
  400. return (0);
  401. }
  402. /*
  403. * Handle incoming message
  404. */
  405. instance->totemudp_deliver_fn (
  406. instance->context,
  407. iovec->iov_base,
  408. iovec->iov_len);
  409. iovec->iov_len = FRAME_SIZE_MAX;
  410. return (0);
  411. }
  412. static int netif_determine (
  413. struct totemudp_instance *instance,
  414. struct totem_ip_address *bindnet,
  415. struct totem_ip_address *bound_to,
  416. int *interface_up,
  417. int *interface_num)
  418. {
  419. int res;
  420. res = totemip_iface_check (bindnet, bound_to,
  421. interface_up, interface_num,
  422. instance->totem_config->clear_node_high_bit);
  423. return (res);
  424. }
  425. /*
  426. * If the interface is up, the sockets for totem are built. If the interface is down
  427. * this function is requeued in the timer list to retry building the sockets later.
  428. */
  429. static void timer_function_netif_check_timeout (
  430. void *data)
  431. {
  432. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  433. int interface_up;
  434. int interface_num;
  435. struct totem_ip_address *bind_address;
  436. /*
  437. * Build sockets for every interface
  438. */
  439. netif_determine (instance,
  440. &instance->totem_interface->bindnet,
  441. &instance->totem_interface->boundto,
  442. &interface_up, &interface_num);
  443. /*
  444. * If the network interface isn't back up and we are already
  445. * in loopback mode, add timer to check again and return
  446. */
  447. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  448. interface_up == 0) ||
  449. (instance->my_memb_entries == 1 &&
  450. instance->netif_bind_state == BIND_STATE_REGULAR &&
  451. interface_up == 1)) {
  452. qb_loop_timer_add (instance->totemudp_poll_handle,
  453. QB_LOOP_MED,
  454. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  455. (void *)instance,
  456. timer_function_netif_check_timeout,
  457. &instance->timer_netif_check_timeout);
  458. /*
  459. * Add a timer to check for a downed regular interface
  460. */
  461. return;
  462. }
  463. if (instance->totemudp_sockets.mcast_recv > 0) {
  464. qb_loop_poll_del (instance->totemudp_poll_handle,
  465. instance->totemudp_sockets.mcast_recv);
  466. close (instance->totemudp_sockets.mcast_recv);
  467. }
  468. if (instance->totemudp_sockets.mcast_send > 0) {
  469. close (instance->totemudp_sockets.mcast_send);
  470. }
  471. if (instance->totemudp_sockets.token > 0) {
  472. qb_loop_poll_del (instance->totemudp_poll_handle,
  473. instance->totemudp_sockets.token);
  474. close (instance->totemudp_sockets.token);
  475. }
  476. if (interface_up == 0) {
  477. /*
  478. * Interface is not up
  479. */
  480. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  481. bind_address = &localhost;
  482. /*
  483. * Add a timer to retry building interfaces and request memb_gather_enter
  484. */
  485. qb_loop_timer_add (instance->totemudp_poll_handle,
  486. QB_LOOP_MED,
  487. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  488. (void *)instance,
  489. timer_function_netif_check_timeout,
  490. &instance->timer_netif_check_timeout);
  491. } else {
  492. /*
  493. * Interface is up
  494. */
  495. instance->netif_bind_state = BIND_STATE_REGULAR;
  496. bind_address = &instance->totem_interface->bindnet;
  497. }
  498. /*
  499. * Create and bind the multicast and unicast sockets
  500. */
  501. (void)totemudp_build_sockets (instance,
  502. &instance->mcast_address,
  503. bind_address,
  504. &instance->totemudp_sockets,
  505. &instance->totem_interface->boundto);
  506. qb_loop_poll_add (
  507. instance->totemudp_poll_handle,
  508. QB_LOOP_MED,
  509. instance->totemudp_sockets.mcast_recv,
  510. POLLIN, instance, net_deliver_fn);
  511. qb_loop_poll_add (
  512. instance->totemudp_poll_handle,
  513. QB_LOOP_MED,
  514. instance->totemudp_sockets.token,
  515. POLLIN, instance, net_deliver_fn);
  516. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  517. /*
  518. * This reports changes in the interface to the user and totemsrp
  519. */
  520. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  521. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  522. log_printf (instance->totemudp_log_level_notice,
  523. "The network interface [%s] is now up.",
  524. totemip_print (&instance->totem_interface->boundto));
  525. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  526. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  527. }
  528. /*
  529. * Add a timer to check for interface going down in single membership
  530. */
  531. if (instance->my_memb_entries == 1) {
  532. qb_loop_timer_add (instance->totemudp_poll_handle,
  533. QB_LOOP_MED,
  534. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  535. (void *)instance,
  536. timer_function_netif_check_timeout,
  537. &instance->timer_netif_check_timeout);
  538. }
  539. } else {
  540. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  541. log_printf (instance->totemudp_log_level_notice,
  542. "The network interface is down.");
  543. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  544. }
  545. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  546. }
  547. }
  548. /* Set the socket priority to INTERACTIVE to ensure
  549. that our messages don't get queued behind anything else */
  550. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  551. {
  552. #ifdef SO_PRIORITY
  553. int prio = 6; /* TC_PRIO_INTERACTIVE */
  554. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  555. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  556. }
  557. #endif
  558. }
  559. static int totemudp_build_sockets_ip (
  560. struct totemudp_instance *instance,
  561. struct totem_ip_address *mcast_address,
  562. struct totem_ip_address *bindnet_address,
  563. struct totemudp_socket *sockets,
  564. struct totem_ip_address *bound_to,
  565. int interface_num)
  566. {
  567. struct sockaddr_storage sockaddr;
  568. struct ipv6_mreq mreq6;
  569. struct ip_mreq mreq;
  570. struct sockaddr_storage mcast_ss, boundto_ss;
  571. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  572. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  573. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  574. unsigned int sendbuf_size;
  575. unsigned int recvbuf_size;
  576. unsigned int optlen = sizeof (sendbuf_size);
  577. int addrlen;
  578. int res;
  579. int flag;
  580. uint8_t sflag;
  581. /*
  582. * Create multicast recv socket
  583. */
  584. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  585. if (sockets->mcast_recv == -1) {
  586. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  587. "socket() failed");
  588. return (-1);
  589. }
  590. totemip_nosigpipe (sockets->mcast_recv);
  591. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  592. if (res == -1) {
  593. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  594. "Could not set non-blocking operation on multicast socket");
  595. return (-1);
  596. }
  597. /*
  598. * Force reuse
  599. */
  600. flag = 1;
  601. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  602. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  603. "setsockopt(SO_REUSEADDR) failed");
  604. return (-1);
  605. }
  606. /*
  607. * Bind to multicast socket used for multicast receives
  608. */
  609. totemip_totemip_to_sockaddr_convert(mcast_address,
  610. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  611. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  612. if (res == -1) {
  613. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  614. "Unable to bind the socket to receive multicast packets");
  615. return (-1);
  616. }
  617. /*
  618. * Setup mcast send socket
  619. */
  620. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  621. if (sockets->mcast_send == -1) {
  622. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  623. "socket() failed");
  624. return (-1);
  625. }
  626. totemip_nosigpipe (sockets->mcast_send);
  627. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  628. if (res == -1) {
  629. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  630. "Could not set non-blocking operation on multicast socket");
  631. return (-1);
  632. }
  633. /*
  634. * Force reuse
  635. */
  636. flag = 1;
  637. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  638. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  639. "setsockopt(SO_REUSEADDR) failed");
  640. return (-1);
  641. }
  642. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  643. &sockaddr, &addrlen);
  644. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  645. if (res == -1) {
  646. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  647. "Unable to bind the socket to send multicast packets");
  648. return (-1);
  649. }
  650. /*
  651. * Setup unicast socket
  652. */
  653. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  654. if (sockets->token == -1) {
  655. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  656. "socket() failed");
  657. return (-1);
  658. }
  659. totemip_nosigpipe (sockets->token);
  660. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  661. if (res == -1) {
  662. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  663. "Could not set non-blocking operation on token socket");
  664. return (-1);
  665. }
  666. /*
  667. * Force reuse
  668. */
  669. flag = 1;
  670. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  671. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  672. "setsockopt(SO_REUSEADDR) failed");
  673. return (-1);
  674. }
  675. /*
  676. * Bind to unicast socket used for token send/receives
  677. * This has the side effect of binding to the correct interface
  678. */
  679. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  680. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  681. if (res == -1) {
  682. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  683. "Unable to bind UDP unicast socket");
  684. return (-1);
  685. }
  686. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  687. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  688. /*
  689. * Set buffer sizes to avoid overruns
  690. */
  691. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  692. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  693. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  694. if (res == 0) {
  695. log_printf (instance->totemudp_log_level_debug,
  696. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  697. }
  698. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  699. if (res == 0) {
  700. log_printf (instance->totemudp_log_level_debug,
  701. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  702. }
  703. /*
  704. * Join group membership on socket
  705. */
  706. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  707. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  708. if (instance->totem_config->broadcast_use == 1) {
  709. unsigned int broadcast = 1;
  710. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  711. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  712. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  713. "setting broadcast option failed");
  714. return (-1);
  715. }
  716. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  717. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  718. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  719. "setting broadcast option failed");
  720. return (-1);
  721. }
  722. } else {
  723. switch (bindnet_address->family) {
  724. case AF_INET:
  725. memset(&mreq, 0, sizeof(mreq));
  726. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  727. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  728. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  729. &mreq, sizeof (mreq));
  730. if (res == -1) {
  731. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  732. "join ipv4 multicast group failed");
  733. return (-1);
  734. }
  735. break;
  736. case AF_INET6:
  737. memset(&mreq6, 0, sizeof(mreq6));
  738. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  739. mreq6.ipv6mr_interface = interface_num;
  740. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  741. &mreq6, sizeof (mreq6));
  742. if (res == -1) {
  743. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  744. "join ipv6 multicast group failed");
  745. return (-1);
  746. }
  747. break;
  748. }
  749. }
  750. /*
  751. * Turn on multicast loopback
  752. */
  753. flag = 1;
  754. switch ( bindnet_address->family ) {
  755. case AF_INET:
  756. sflag = 1;
  757. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  758. &sflag, sizeof (sflag));
  759. break;
  760. case AF_INET6:
  761. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  762. &flag, sizeof (flag));
  763. }
  764. if (res == -1) {
  765. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  766. "Unable to turn on multicast loopback");
  767. return (-1);
  768. }
  769. /*
  770. * Set multicast packets TTL
  771. */
  772. flag = instance->totem_interface->ttl;
  773. if (bindnet_address->family == AF_INET6) {
  774. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  775. &flag, sizeof (flag));
  776. if (res == -1) {
  777. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  778. "set mcast v6 TTL failed");
  779. return (-1);
  780. }
  781. } else {
  782. sflag = flag;
  783. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  784. &sflag, sizeof(sflag));
  785. if (res == -1) {
  786. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  787. "set mcast v4 TTL failed");
  788. return (-1);
  789. }
  790. }
  791. /*
  792. * Bind to a specific interface for multicast send and receive
  793. */
  794. switch ( bindnet_address->family ) {
  795. case AF_INET:
  796. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  797. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  798. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  799. "cannot select interface for multicast packets (send)");
  800. return (-1);
  801. }
  802. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  803. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  804. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  805. "cannot select interface for multicast packets (recv)");
  806. return (-1);
  807. }
  808. break;
  809. case AF_INET6:
  810. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  811. &interface_num, sizeof (interface_num)) < 0) {
  812. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  813. "cannot select interface for multicast packets (send v6)");
  814. return (-1);
  815. }
  816. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  817. &interface_num, sizeof (interface_num)) < 0) {
  818. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  819. "cannot select interface for multicast packets (recv v6)");
  820. return (-1);
  821. }
  822. break;
  823. }
  824. return 0;
  825. }
  826. static int totemudp_build_sockets (
  827. struct totemudp_instance *instance,
  828. struct totem_ip_address *mcast_address,
  829. struct totem_ip_address *bindnet_address,
  830. struct totemudp_socket *sockets,
  831. struct totem_ip_address *bound_to)
  832. {
  833. int interface_num;
  834. int interface_up;
  835. int res;
  836. /*
  837. * Determine the ip address bound to and the interface name
  838. */
  839. res = netif_determine (instance,
  840. bindnet_address,
  841. bound_to,
  842. &interface_up,
  843. &interface_num);
  844. if (res == -1) {
  845. return (-1);
  846. }
  847. totemip_copy(&instance->my_id, bound_to);
  848. res = totemudp_build_sockets_ip (instance, mcast_address,
  849. bindnet_address, sockets, bound_to, interface_num);
  850. /* We only send out of the token socket */
  851. totemudp_traffic_control_set(instance, sockets->token);
  852. return res;
  853. }
  854. /*
  855. * Totem Network interface - also does encryption/decryption
  856. * depends on poll abstraction, POSIX, IPV4
  857. */
  858. /*
  859. * Create an instance
  860. */
  861. int totemudp_initialize (
  862. qb_loop_t *poll_handle,
  863. void **udp_context,
  864. struct totem_config *totem_config,
  865. int interface_no,
  866. void *context,
  867. void (*deliver_fn) (
  868. void *context,
  869. const void *msg,
  870. unsigned int msg_len),
  871. void (*iface_change_fn) (
  872. void *context,
  873. const struct totem_ip_address *iface_address),
  874. void (*target_set_completed) (
  875. void *context))
  876. {
  877. struct totemudp_instance *instance;
  878. instance = malloc (sizeof (struct totemudp_instance));
  879. if (instance == NULL) {
  880. return (-1);
  881. }
  882. totemudp_instance_initialize (instance);
  883. instance->totem_config = totem_config;
  884. /*
  885. * Configure logging
  886. */
  887. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  888. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  889. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  890. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  891. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  892. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  893. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  894. /*
  895. * Initialize random number generator for later use to generate salt
  896. */
  897. instance->crypto_inst = crypto_init (totem_config->private_key,
  898. totem_config->private_key_len,
  899. totem_config->crypto_cipher_type,
  900. totem_config->crypto_hash_type,
  901. instance->totemudp_log_printf,
  902. instance->totemudp_log_level_security,
  903. instance->totemudp_log_level_notice,
  904. instance->totemudp_log_level_error,
  905. instance->totemudp_subsys_id);
  906. if (instance->crypto_inst == NULL) {
  907. return (-1);
  908. }
  909. /*
  910. * Initialize local variables for totemudp
  911. */
  912. instance->totem_interface = &totem_config->interfaces[interface_no];
  913. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  914. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  915. instance->totemudp_poll_handle = poll_handle;
  916. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  917. instance->context = context;
  918. instance->totemudp_deliver_fn = deliver_fn;
  919. instance->totemudp_iface_change_fn = iface_change_fn;
  920. instance->totemudp_target_set_completed = target_set_completed;
  921. totemip_localhost (instance->mcast_address.family, &localhost);
  922. localhost.nodeid = instance->totem_config->node_id;
  923. /*
  924. * RRP layer isn't ready to receive message because it hasn't
  925. * initialized yet. Add short timer to check the interfaces.
  926. */
  927. qb_loop_timer_add (instance->totemudp_poll_handle,
  928. QB_LOOP_MED,
  929. 100*QB_TIME_NS_IN_MSEC,
  930. (void *)instance,
  931. timer_function_netif_check_timeout,
  932. &instance->timer_netif_check_timeout);
  933. *udp_context = instance;
  934. return (0);
  935. }
  936. void *totemudp_buffer_alloc (void)
  937. {
  938. return malloc (FRAME_SIZE_MAX);
  939. }
  940. void totemudp_buffer_release (void *ptr)
  941. {
  942. return free (ptr);
  943. }
  944. int totemudp_processor_count_set (
  945. void *udp_context,
  946. int processor_count)
  947. {
  948. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  949. int res = 0;
  950. instance->my_memb_entries = processor_count;
  951. qb_loop_timer_del (instance->totemudp_poll_handle,
  952. instance->timer_netif_check_timeout);
  953. if (processor_count == 1) {
  954. qb_loop_timer_add (instance->totemudp_poll_handle,
  955. QB_LOOP_MED,
  956. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  957. (void *)instance,
  958. timer_function_netif_check_timeout,
  959. &instance->timer_netif_check_timeout);
  960. }
  961. return (res);
  962. }
  963. int totemudp_recv_flush (void *udp_context)
  964. {
  965. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  966. struct pollfd ufd;
  967. int nfds;
  968. int res = 0;
  969. instance->flushing = 1;
  970. do {
  971. ufd.fd = instance->totemudp_sockets.mcast_recv;
  972. ufd.events = POLLIN;
  973. nfds = poll (&ufd, 1, 0);
  974. if (nfds == 1 && ufd.revents & POLLIN) {
  975. net_deliver_fn (instance->totemudp_sockets.mcast_recv,
  976. ufd.revents, instance);
  977. }
  978. } while (nfds == 1);
  979. instance->flushing = 0;
  980. return (res);
  981. }
  982. int totemudp_send_flush (void *udp_context)
  983. {
  984. return 0;
  985. }
  986. int totemudp_token_send (
  987. void *udp_context,
  988. const void *msg,
  989. unsigned int msg_len)
  990. {
  991. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  992. int res = 0;
  993. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  994. return (res);
  995. }
  996. int totemudp_mcast_flush_send (
  997. void *udp_context,
  998. const void *msg,
  999. unsigned int msg_len)
  1000. {
  1001. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1002. int res = 0;
  1003. mcast_sendmsg (instance, msg, msg_len);
  1004. return (res);
  1005. }
  1006. int totemudp_mcast_noflush_send (
  1007. void *udp_context,
  1008. const void *msg,
  1009. unsigned int msg_len)
  1010. {
  1011. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1012. int res = 0;
  1013. mcast_sendmsg (instance, msg, msg_len);
  1014. return (res);
  1015. }
  1016. extern int totemudp_iface_check (void *udp_context)
  1017. {
  1018. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1019. int res = 0;
  1020. timer_function_netif_check_timeout (instance);
  1021. return (res);
  1022. }
  1023. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1024. {
  1025. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1026. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1027. totem_config->crypto_hash_type) +
  1028. UDPIP_HEADER_SIZE;
  1029. }
  1030. const char *totemudp_iface_print (void *udp_context) {
  1031. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1032. const char *ret_char;
  1033. ret_char = totemip_print (&instance->my_id);
  1034. return (ret_char);
  1035. }
  1036. int totemudp_iface_get (
  1037. void *udp_context,
  1038. struct totem_ip_address *addr)
  1039. {
  1040. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1041. int res = 0;
  1042. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1043. return (res);
  1044. }
  1045. int totemudp_token_target_set (
  1046. void *udp_context,
  1047. const struct totem_ip_address *token_target)
  1048. {
  1049. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1050. int res = 0;
  1051. memcpy (&instance->token_target, token_target,
  1052. sizeof (struct totem_ip_address));
  1053. instance->totemudp_target_set_completed (instance->context);
  1054. return (res);
  1055. }
  1056. extern int totemudp_recv_mcast_empty (
  1057. void *udp_context)
  1058. {
  1059. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1060. unsigned int res;
  1061. struct sockaddr_storage system_from;
  1062. struct msghdr msg_recv;
  1063. struct pollfd ufd;
  1064. int nfds;
  1065. int msg_processed = 0;
  1066. /*
  1067. * Receive datagram
  1068. */
  1069. msg_recv.msg_name = &system_from;
  1070. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1071. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1072. msg_recv.msg_iovlen = 1;
  1073. #ifdef HAVE_MSGHDR_CONTROL
  1074. msg_recv.msg_control = 0;
  1075. #endif
  1076. #ifdef HAVE_MSGHDR_CONTROLLEN
  1077. msg_recv.msg_controllen = 0;
  1078. #endif
  1079. #ifdef HAVE_MSGHDR_FLAGS
  1080. msg_recv.msg_flags = 0;
  1081. #endif
  1082. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1083. msg_recv.msg_accrights = NULL;
  1084. #endif
  1085. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1086. msg_recv.msg_accrightslen = 0;
  1087. #endif
  1088. do {
  1089. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1090. ufd.events = POLLIN;
  1091. nfds = poll (&ufd, 1, 0);
  1092. if (nfds == 1 && ufd.revents & POLLIN) {
  1093. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1094. if (res != -1) {
  1095. msg_processed = 1;
  1096. } else {
  1097. msg_processed = -1;
  1098. }
  1099. }
  1100. } while (nfds == 1);
  1101. return (msg_processed);
  1102. }