totemudp.c 33 KB

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