totemudp.c 34 KB

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