evsverify.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <errno.h>
  41. #include <string.h>
  42. #include <corosync/evs.h>
  43. #include "../exec/crypto.h"
  44. char *delivery_string;
  45. struct msg {
  46. unsigned int msg_size;
  47. unsigned char sha1[20];
  48. unsigned char buffer[0];
  49. };
  50. int deliveries = 0;
  51. void evs_deliver_fn (
  52. unsigned int nodeid,
  53. void *m,
  54. int msg_len)
  55. {
  56. struct msg *msg2 = (struct msg *)m;
  57. unsigned char sha1_compare[20];
  58. hash_state sha1_hash;
  59. unsigned int i;
  60. printf ("API '%s' msg '%s'\n", delivery_string, msg2->buffer);
  61. sha1_init (&sha1_hash);
  62. sha1_process (&sha1_hash, msg2->buffer, msg2->msg_size);
  63. sha1_done (&sha1_hash, sha1_compare);
  64. printf ("SIZE %d HASH: ", msg2->msg_size);
  65. for (i = 0; i < 20; i++) {
  66. printf ("%x", sha1_compare[i]);
  67. }
  68. printf ("\n");
  69. if (memcmp (sha1_compare, msg2->sha1, 20) != 0) {
  70. printf ("incorrect hash\n");
  71. exit (1);
  72. }
  73. deliveries++;
  74. }
  75. void evs_confchg_fn (
  76. unsigned int *member_list, int member_list_entries,
  77. unsigned int *left_list, int left_list_entries,
  78. unsigned int *joined_list, int joined_list_entries)
  79. {
  80. int i;
  81. printf ("CONFIGURATION CHANGE\n");
  82. printf ("--------------------\n");
  83. printf ("New configuration\n");
  84. for (i = 0; i < member_list_entries; i++) {
  85. printf ("%x\n", member_list[i]);
  86. }
  87. printf ("Members Left:\n");
  88. for (i = 0; i < left_list_entries; i++) {
  89. printf ("%x\n", left_list[i]);
  90. }
  91. printf ("Members Joined:\n");
  92. for (i = 0; i < joined_list_entries; i++) {
  93. printf ("%x\n", joined_list[i]);
  94. }
  95. }
  96. evs_callbacks_t callbacks = {
  97. evs_deliver_fn,
  98. evs_confchg_fn
  99. };
  100. struct evs_group groups[3] = {
  101. { "key1" },
  102. { "key2" },
  103. { "key3" }
  104. };
  105. struct msg msg;
  106. char buffer[200000];
  107. int main (void)
  108. {
  109. evs_handle_t handle;
  110. evs_error_t result;
  111. unsigned int i = 0, j;
  112. int fd;
  113. unsigned int member_list[32];
  114. unsigned int local_nodeid;
  115. int member_list_entries = 32;
  116. struct msg msg;
  117. hash_state sha1_hash;
  118. struct iovec iov[2];
  119. result = evs_initialize (&handle, &callbacks);
  120. if (result != EVS_OK) {
  121. printf ("Couldn't initialize EVS service %d\n", result);
  122. exit (0);
  123. }
  124. result = evs_membership_get (handle, &local_nodeid,
  125. member_list, &member_list_entries);
  126. printf ("Current membership from evs_membership_get entries %d\n",
  127. member_list_entries);
  128. for (i = 0; i < member_list_entries; i++) {
  129. printf ("member [%d] is %x\n", i, member_list[i]);
  130. }
  131. printf ("local processor from evs_membership_get %x\n", local_nodeid);
  132. printf ("Init result %d\n", result);
  133. result = evs_join (handle, groups, 3);
  134. printf ("Join result %d\n", result);
  135. result = evs_leave (handle, &groups[0], 1);
  136. printf ("Leave result %d\n", result);
  137. delivery_string = "evs_mcast_joined";
  138. iov[0].iov_base = &msg;
  139. iov[0].iov_len = sizeof (struct msg);
  140. iov[1].iov_base = buffer;
  141. /*
  142. * Demonstrate evs_mcast_joined
  143. */
  144. for (i = 0; i < 1000000000; i++) {
  145. msg.msg_size = 100 + rand() % 100000;
  146. iov[1].iov_len = msg.msg_size;
  147. for (j = 0; j < msg.msg_size; j++) {
  148. buffer[j] = j;
  149. }
  150. sprintf ((char *)buffer,
  151. "evs_mcast_joined: This is message %12d", i);
  152. sha1_init (&sha1_hash);
  153. sha1_process (&sha1_hash, buffer, msg.msg_size);
  154. sha1_done (&sha1_hash, msg.sha1);
  155. try_again_one:
  156. result = evs_mcast_joined (handle, EVS_TYPE_AGREED,
  157. iov, 2);
  158. if (result == EVS_ERR_TRY_AGAIN) {
  159. goto try_again_one;
  160. }
  161. result = evs_dispatch (handle, EVS_DISPATCH_ALL);
  162. }
  163. evs_fd_get (handle, &fd);
  164. evs_finalize (handle);
  165. return (0);
  166. }