totemudp.c 35 KB

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