totemudp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. /*
  579. * Create multicast recv socket
  580. */
  581. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  582. if (sockets->mcast_recv == -1) {
  583. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  584. "socket() failed");
  585. return (-1);
  586. }
  587. totemip_nosigpipe (sockets->mcast_recv);
  588. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  589. if (res == -1) {
  590. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  591. "Could not set non-blocking operation on multicast socket");
  592. return (-1);
  593. }
  594. /*
  595. * Force reuse
  596. */
  597. flag = 1;
  598. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  599. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  600. "setsockopt(SO_REUSEADDR) failed");
  601. return (-1);
  602. }
  603. /*
  604. * Bind to multicast socket used for multicast receives
  605. */
  606. totemip_totemip_to_sockaddr_convert(mcast_address,
  607. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  608. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  609. if (res == -1) {
  610. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  611. "Unable to bind the socket to receive multicast packets");
  612. return (-1);
  613. }
  614. /*
  615. * Setup mcast send socket
  616. */
  617. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  618. if (sockets->mcast_send == -1) {
  619. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  620. "socket() failed");
  621. return (-1);
  622. }
  623. totemip_nosigpipe (sockets->mcast_send);
  624. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  625. if (res == -1) {
  626. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  627. "Could not set non-blocking operation on multicast socket");
  628. return (-1);
  629. }
  630. /*
  631. * Force reuse
  632. */
  633. flag = 1;
  634. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  635. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  636. "setsockopt(SO_REUSEADDR) failed");
  637. return (-1);
  638. }
  639. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  640. &sockaddr, &addrlen);
  641. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  642. if (res == -1) {
  643. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  644. "Unable to bind the socket to send multicast packets");
  645. return (-1);
  646. }
  647. /*
  648. * Setup unicast socket
  649. */
  650. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  651. if (sockets->token == -1) {
  652. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  653. "socket() failed");
  654. return (-1);
  655. }
  656. totemip_nosigpipe (sockets->token);
  657. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  658. if (res == -1) {
  659. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  660. "Could not set non-blocking operation on token socket");
  661. return (-1);
  662. }
  663. /*
  664. * Force reuse
  665. */
  666. flag = 1;
  667. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  668. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  669. "setsockopt(SO_REUSEADDR) failed");
  670. return (-1);
  671. }
  672. /*
  673. * Bind to unicast socket used for token send/receives
  674. * This has the side effect of binding to the correct interface
  675. */
  676. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  677. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  678. if (res == -1) {
  679. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  680. "Unable to bind UDP unicast socket");
  681. return (-1);
  682. }
  683. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  684. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  685. /*
  686. * Set buffer sizes to avoid overruns
  687. */
  688. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  689. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  690. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  691. if (res == 0) {
  692. log_printf (instance->totemudp_log_level_debug,
  693. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  694. }
  695. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  696. if (res == 0) {
  697. log_printf (instance->totemudp_log_level_debug,
  698. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  699. }
  700. /*
  701. * Join group membership on socket
  702. */
  703. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  704. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  705. if (instance->totem_config->broadcast_use == 1) {
  706. unsigned int broadcast = 1;
  707. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  708. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  709. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  710. "setting broadcast option failed");
  711. return (-1);
  712. }
  713. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  714. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  715. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  716. "setting broadcast option failed");
  717. return (-1);
  718. }
  719. } else {
  720. switch (bindnet_address->family) {
  721. case AF_INET:
  722. memset(&mreq, 0, sizeof(mreq));
  723. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  724. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  725. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  726. &mreq, sizeof (mreq));
  727. if (res == -1) {
  728. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  729. "join ipv4 multicast group failed");
  730. return (-1);
  731. }
  732. break;
  733. case AF_INET6:
  734. memset(&mreq6, 0, sizeof(mreq6));
  735. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  736. mreq6.ipv6mr_interface = interface_num;
  737. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  738. &mreq6, sizeof (mreq6));
  739. if (res == -1) {
  740. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  741. "join ipv6 multicast group failed");
  742. return (-1);
  743. }
  744. break;
  745. }
  746. }
  747. /*
  748. * Turn on multicast loopback
  749. */
  750. flag = 1;
  751. switch ( bindnet_address->family ) {
  752. case AF_INET:
  753. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  754. &flag, sizeof (flag));
  755. break;
  756. case AF_INET6:
  757. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  758. &flag, sizeof (flag));
  759. }
  760. if (res == -1) {
  761. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  762. "Unable to turn on multicast loopback");
  763. return (-1);
  764. }
  765. /*
  766. * Set multicast packets TTL
  767. */
  768. flag = instance->totem_interface->ttl;
  769. if (bindnet_address->family == AF_INET6) {
  770. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  771. &flag, sizeof (flag));
  772. if (res == -1) {
  773. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  774. "set mcast v6 TTL failed");
  775. return (-1);
  776. }
  777. } else {
  778. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  779. &flag, sizeof(flag));
  780. if (res == -1) {
  781. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  782. "set mcast v4 TTL failed");
  783. return (-1);
  784. }
  785. }
  786. /*
  787. * Bind to a specific interface for multicast send and receive
  788. */
  789. switch ( bindnet_address->family ) {
  790. case AF_INET:
  791. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  792. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  793. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  794. "cannot select interface for multicast packets (send)");
  795. return (-1);
  796. }
  797. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  798. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  799. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  800. "cannot select interface for multicast packets (recv)");
  801. return (-1);
  802. }
  803. break;
  804. case AF_INET6:
  805. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  806. &interface_num, sizeof (interface_num)) < 0) {
  807. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  808. "cannot select interface for multicast packets (send v6)");
  809. return (-1);
  810. }
  811. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  812. &interface_num, sizeof (interface_num)) < 0) {
  813. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  814. "cannot select interface for multicast packets (recv v6)");
  815. return (-1);
  816. }
  817. break;
  818. }
  819. return 0;
  820. }
  821. static int totemudp_build_sockets (
  822. struct totemudp_instance *instance,
  823. struct totem_ip_address *mcast_address,
  824. struct totem_ip_address *bindnet_address,
  825. struct totemudp_socket *sockets,
  826. struct totem_ip_address *bound_to)
  827. {
  828. int interface_num;
  829. int interface_up;
  830. int res;
  831. /*
  832. * Determine the ip address bound to and the interface name
  833. */
  834. res = netif_determine (instance,
  835. bindnet_address,
  836. bound_to,
  837. &interface_up,
  838. &interface_num);
  839. if (res == -1) {
  840. return (-1);
  841. }
  842. totemip_copy(&instance->my_id, bound_to);
  843. res = totemudp_build_sockets_ip (instance, mcast_address,
  844. bindnet_address, sockets, bound_to, interface_num);
  845. /* We only send out of the token socket */
  846. totemudp_traffic_control_set(instance, sockets->token);
  847. return res;
  848. }
  849. /*
  850. * Totem Network interface - also does encryption/decryption
  851. * depends on poll abstraction, POSIX, IPV4
  852. */
  853. /*
  854. * Create an instance
  855. */
  856. int totemudp_initialize (
  857. qb_loop_t *poll_handle,
  858. void **udp_context,
  859. struct totem_config *totem_config,
  860. int interface_no,
  861. void *context,
  862. void (*deliver_fn) (
  863. void *context,
  864. const void *msg,
  865. unsigned int msg_len),
  866. void (*iface_change_fn) (
  867. void *context,
  868. const struct totem_ip_address *iface_address),
  869. void (*target_set_completed) (
  870. void *context))
  871. {
  872. struct totemudp_instance *instance;
  873. instance = malloc (sizeof (struct totemudp_instance));
  874. if (instance == NULL) {
  875. return (-1);
  876. }
  877. totemudp_instance_initialize (instance);
  878. instance->totem_config = totem_config;
  879. /*
  880. * Configure logging
  881. */
  882. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  883. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  884. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  885. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  886. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  887. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  888. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  889. /*
  890. * Initialize random number generator for later use to generate salt
  891. */
  892. instance->crypto_inst = crypto_init (totem_config->private_key,
  893. totem_config->private_key_len,
  894. totem_config->crypto_cipher_type,
  895. totem_config->crypto_hash_type,
  896. instance->totemudp_log_printf,
  897. instance->totemudp_log_level_security,
  898. instance->totemudp_log_level_notice,
  899. instance->totemudp_log_level_error,
  900. instance->totemudp_subsys_id);
  901. if (instance->crypto_inst == NULL) {
  902. return (-1);
  903. }
  904. /*
  905. * Initialize local variables for totemudp
  906. */
  907. instance->totem_interface = &totem_config->interfaces[interface_no];
  908. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  909. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  910. instance->totemudp_poll_handle = poll_handle;
  911. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  912. instance->context = context;
  913. instance->totemudp_deliver_fn = deliver_fn;
  914. instance->totemudp_iface_change_fn = iface_change_fn;
  915. instance->totemudp_target_set_completed = target_set_completed;
  916. totemip_localhost (instance->mcast_address.family, &localhost);
  917. localhost.nodeid = instance->totem_config->node_id;
  918. /*
  919. * RRP layer isn't ready to receive message because it hasn't
  920. * initialized yet. Add short timer to check the interfaces.
  921. */
  922. qb_loop_timer_add (instance->totemudp_poll_handle,
  923. QB_LOOP_MED,
  924. 100*QB_TIME_NS_IN_MSEC,
  925. (void *)instance,
  926. timer_function_netif_check_timeout,
  927. &instance->timer_netif_check_timeout);
  928. *udp_context = instance;
  929. return (0);
  930. }
  931. void *totemudp_buffer_alloc (void)
  932. {
  933. return malloc (FRAME_SIZE_MAX);
  934. }
  935. void totemudp_buffer_release (void *ptr)
  936. {
  937. return free (ptr);
  938. }
  939. int totemudp_processor_count_set (
  940. void *udp_context,
  941. int processor_count)
  942. {
  943. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  944. int res = 0;
  945. instance->my_memb_entries = processor_count;
  946. qb_loop_timer_del (instance->totemudp_poll_handle,
  947. instance->timer_netif_check_timeout);
  948. if (processor_count == 1) {
  949. qb_loop_timer_add (instance->totemudp_poll_handle,
  950. QB_LOOP_MED,
  951. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  952. (void *)instance,
  953. timer_function_netif_check_timeout,
  954. &instance->timer_netif_check_timeout);
  955. }
  956. return (res);
  957. }
  958. int totemudp_recv_flush (void *udp_context)
  959. {
  960. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  961. struct pollfd ufd;
  962. int nfds;
  963. int res = 0;
  964. instance->flushing = 1;
  965. do {
  966. ufd.fd = instance->totemudp_sockets.mcast_recv;
  967. ufd.events = POLLIN;
  968. nfds = poll (&ufd, 1, 0);
  969. if (nfds == 1 && ufd.revents & POLLIN) {
  970. net_deliver_fn (instance->totemudp_sockets.mcast_recv,
  971. ufd.revents, instance);
  972. }
  973. } while (nfds == 1);
  974. instance->flushing = 0;
  975. return (res);
  976. }
  977. int totemudp_send_flush (void *udp_context)
  978. {
  979. return 0;
  980. }
  981. int totemudp_token_send (
  982. void *udp_context,
  983. const void *msg,
  984. unsigned int msg_len)
  985. {
  986. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  987. int res = 0;
  988. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  989. return (res);
  990. }
  991. int totemudp_mcast_flush_send (
  992. void *udp_context,
  993. const void *msg,
  994. unsigned int msg_len)
  995. {
  996. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  997. int res = 0;
  998. mcast_sendmsg (instance, msg, msg_len);
  999. return (res);
  1000. }
  1001. int totemudp_mcast_noflush_send (
  1002. void *udp_context,
  1003. const void *msg,
  1004. unsigned int msg_len)
  1005. {
  1006. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1007. int res = 0;
  1008. mcast_sendmsg (instance, msg, msg_len);
  1009. return (res);
  1010. }
  1011. extern int totemudp_iface_check (void *udp_context)
  1012. {
  1013. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1014. int res = 0;
  1015. timer_function_netif_check_timeout (instance);
  1016. return (res);
  1017. }
  1018. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1019. {
  1020. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1021. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1022. totem_config->crypto_hash_type) +
  1023. UDPIP_HEADER_SIZE;
  1024. }
  1025. const char *totemudp_iface_print (void *udp_context) {
  1026. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1027. const char *ret_char;
  1028. ret_char = totemip_print (&instance->my_id);
  1029. return (ret_char);
  1030. }
  1031. int totemudp_iface_get (
  1032. void *udp_context,
  1033. struct totem_ip_address *addr)
  1034. {
  1035. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1036. int res = 0;
  1037. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1038. return (res);
  1039. }
  1040. int totemudp_token_target_set (
  1041. void *udp_context,
  1042. const struct totem_ip_address *token_target)
  1043. {
  1044. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1045. int res = 0;
  1046. memcpy (&instance->token_target, token_target,
  1047. sizeof (struct totem_ip_address));
  1048. instance->totemudp_target_set_completed (instance->context);
  1049. return (res);
  1050. }
  1051. extern int totemudp_recv_mcast_empty (
  1052. void *udp_context)
  1053. {
  1054. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1055. unsigned int res;
  1056. struct sockaddr_storage system_from;
  1057. struct msghdr msg_recv;
  1058. struct pollfd ufd;
  1059. int nfds;
  1060. int msg_processed = 0;
  1061. /*
  1062. * Receive datagram
  1063. */
  1064. msg_recv.msg_name = &system_from;
  1065. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1066. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1067. msg_recv.msg_iovlen = 1;
  1068. #ifdef HAVE_MSGHDR_CONTROL
  1069. msg_recv.msg_control = 0;
  1070. #endif
  1071. #ifdef HAVE_MSGHDR_CONTROLLEN
  1072. msg_recv.msg_controllen = 0;
  1073. #endif
  1074. #ifdef HAVE_MSGHDR_FLAGS
  1075. msg_recv.msg_flags = 0;
  1076. #endif
  1077. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1078. msg_recv.msg_accrights = NULL;
  1079. #endif
  1080. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1081. msg_recv.msg_accrightslen = 0;
  1082. #endif
  1083. do {
  1084. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1085. ufd.events = POLLIN;
  1086. nfds = poll (&ufd, 1, 0);
  1087. if (nfds == 1 && ufd.revents & POLLIN) {
  1088. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1089. if (res != -1) {
  1090. msg_processed = 1;
  1091. } else {
  1092. msg_processed = -1;
  1093. }
  1094. }
  1095. } while (nfds == 1);
  1096. return (msg_processed);
  1097. }