totemudp.c 34 KB

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