utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #include <ctype.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <amqp.h>
  41. #include <amqp_framing.h>
  42. #include <stdint.h>
  43. #include "utils.h"
  44. void die(const char *fmt, ...) {
  45. va_list ap;
  46. va_start(ap, fmt);
  47. vfprintf(stderr, fmt, ap);
  48. va_end(ap);
  49. fprintf(stderr, "\n");
  50. exit(1);
  51. }
  52. void die_on_error(int x, char const *context) {
  53. if (x < 0) {
  54. fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x));
  55. exit(1);
  56. }
  57. }
  58. void die_on_amqp_error(amqp_rpc_reply_t x, char const *context) {
  59. switch (x.reply_type) {
  60. case AMQP_RESPONSE_NORMAL:
  61. return;
  62. case AMQP_RESPONSE_NONE:
  63. fprintf(stderr, "%s: missing RPC reply type!\n", context);
  64. break;
  65. case AMQP_RESPONSE_LIBRARY_EXCEPTION:
  66. fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x.library_error));
  67. break;
  68. case AMQP_RESPONSE_SERVER_EXCEPTION:
  69. switch (x.reply.id) {
  70. case AMQP_CONNECTION_CLOSE_METHOD: {
  71. amqp_connection_close_t *m =
  72. (amqp_connection_close_t *)x.reply.decoded;
  73. fprintf(stderr, "%s: server connection error %uh, message: %.*s\n",
  74. context, m->reply_code, (int)m->reply_text.len,
  75. (char *)m->reply_text.bytes);
  76. break;
  77. }
  78. case AMQP_CHANNEL_CLOSE_METHOD: {
  79. amqp_channel_close_t *m = (amqp_channel_close_t *)x.reply.decoded;
  80. fprintf(stderr, "%s: server channel error %uh, message: %.*s\n",
  81. context, m->reply_code, (int)m->reply_text.len,
  82. (char *)m->reply_text.bytes);
  83. break;
  84. }
  85. default:
  86. fprintf(stderr, "%s: unknown server error, method id 0x%08X\n",
  87. context, x.reply.id);
  88. break;
  89. }
  90. break;
  91. }
  92. exit(1);
  93. }
  94. static void dump_row(long count, int numinrow, int *chs) {
  95. int i;
  96. printf("%08lX:", count - numinrow);
  97. if (numinrow > 0) {
  98. for (i = 0; i < numinrow; i++) {
  99. if (i == 8) {
  100. printf(" :");
  101. }
  102. printf(" %02X", chs[i]);
  103. }
  104. for (i = numinrow; i < 16; i++) {
  105. if (i == 8) {
  106. printf(" :");
  107. }
  108. printf(" ");
  109. }
  110. printf(" ");
  111. for (i = 0; i < numinrow; i++) {
  112. if (isprint(chs[i])) {
  113. printf("%c", chs[i]);
  114. } else {
  115. printf(".");
  116. }
  117. }
  118. }
  119. printf("\n");
  120. }
  121. static int rows_eq(int *a, int *b) {
  122. int i;
  123. for (i = 0; i < 16; i++)
  124. if (a[i] != b[i]) {
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. void amqp_dump(void const *buffer, size_t len) {
  130. unsigned char *buf = (unsigned char *)buffer;
  131. long count = 0;
  132. int numinrow = 0;
  133. int chs[16];
  134. int oldchs[16] = {0};
  135. int showed_dots = 0;
  136. size_t i;
  137. for (i = 0; i < len; i++) {
  138. int ch = buf[i];
  139. if (numinrow == 16) {
  140. int j;
  141. if (rows_eq(oldchs, chs)) {
  142. if (!showed_dots) {
  143. showed_dots = 1;
  144. printf(
  145. " .. .. .. .. .. .. .. .. : .. .. .. .. .. .. .. ..\n");
  146. }
  147. } else {
  148. showed_dots = 0;
  149. dump_row(count, numinrow, chs);
  150. }
  151. for (j = 0; j < 16; j++) {
  152. oldchs[j] = chs[j];
  153. }
  154. numinrow = 0;
  155. }
  156. count++;
  157. chs[numinrow++] = ch;
  158. }
  159. dump_row(count, numinrow, chs);
  160. if (numinrow != 0) {
  161. printf("%08lX:\n", count);
  162. }
  163. }