utils.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include <ctype.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <rabbitmq-c/amqp.h>
  9. #include <rabbitmq-c/framing.h>
  10. #include <stdint.h>
  11. #include "utils.h"
  12. void die(const char *fmt, ...) {
  13. va_list ap;
  14. va_start(ap, fmt);
  15. vfprintf(stderr, fmt, ap);
  16. va_end(ap);
  17. fprintf(stderr, "\n");
  18. exit(1);
  19. }
  20. void die_on_error(int x, char const *context) {
  21. if (x < 0) {
  22. fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x));
  23. exit(1);
  24. }
  25. }
  26. void die_on_amqp_error(amqp_rpc_reply_t x, char const *context) {
  27. switch (x.reply_type) {
  28. case AMQP_RESPONSE_NORMAL:
  29. return;
  30. case AMQP_RESPONSE_NONE:
  31. fprintf(stderr, "%s: missing RPC reply type!\n", context);
  32. break;
  33. case AMQP_RESPONSE_LIBRARY_EXCEPTION:
  34. fprintf(stderr, "%s: %s\n", context, amqp_error_string2(x.library_error));
  35. break;
  36. case AMQP_RESPONSE_SERVER_EXCEPTION:
  37. switch (x.reply.id) {
  38. case AMQP_CONNECTION_CLOSE_METHOD: {
  39. amqp_connection_close_t *m =
  40. (amqp_connection_close_t *)x.reply.decoded;
  41. fprintf(stderr, "%s: server connection error %uh, message: %.*s\n",
  42. context, m->reply_code, (int)m->reply_text.len,
  43. (char *)m->reply_text.bytes);
  44. break;
  45. }
  46. case AMQP_CHANNEL_CLOSE_METHOD: {
  47. amqp_channel_close_t *m = (amqp_channel_close_t *)x.reply.decoded;
  48. fprintf(stderr, "%s: server channel error %uh, message: %.*s\n",
  49. context, m->reply_code, (int)m->reply_text.len,
  50. (char *)m->reply_text.bytes);
  51. break;
  52. }
  53. default:
  54. fprintf(stderr, "%s: unknown server error, method id 0x%08X\n",
  55. context, x.reply.id);
  56. break;
  57. }
  58. break;
  59. }
  60. exit(1);
  61. }
  62. static void dump_row(long count, int numinrow, int *chs) {
  63. int i;
  64. printf("%08lX:", count - numinrow);
  65. if (numinrow > 0) {
  66. for (i = 0; i < numinrow; i++) {
  67. if (i == 8) {
  68. printf(" :");
  69. }
  70. printf(" %02X", chs[i]);
  71. }
  72. for (i = numinrow; i < 16; i++) {
  73. if (i == 8) {
  74. printf(" :");
  75. }
  76. printf(" ");
  77. }
  78. printf(" ");
  79. for (i = 0; i < numinrow; i++) {
  80. if (isprint(chs[i])) {
  81. printf("%c", chs[i]);
  82. } else {
  83. printf(".");
  84. }
  85. }
  86. }
  87. printf("\n");
  88. }
  89. static int rows_eq(int *a, int *b) {
  90. int i;
  91. for (i = 0; i < 16; i++)
  92. if (a[i] != b[i]) {
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. void amqp_dump(void const *buffer, size_t len) {
  98. unsigned char *buf = (unsigned char *)buffer;
  99. long count = 0;
  100. int numinrow = 0;
  101. int chs[16];
  102. int oldchs[16] = {0};
  103. int showed_dots = 0;
  104. size_t i;
  105. for (i = 0; i < len; i++) {
  106. int ch = buf[i];
  107. if (numinrow == 16) {
  108. int j;
  109. if (rows_eq(oldchs, chs)) {
  110. if (!showed_dots) {
  111. showed_dots = 1;
  112. printf(
  113. " .. .. .. .. .. .. .. .. : .. .. .. .. .. .. .. ..\n");
  114. }
  115. } else {
  116. showed_dots = 0;
  117. dump_row(count, numinrow, chs);
  118. }
  119. for (j = 0; j < 16; j++) {
  120. oldchs[j] = chs[j];
  121. }
  122. numinrow = 0;
  123. }
  124. count++;
  125. chs[numinrow++] = ch;
  126. }
  127. dump_row(count, numinrow, chs);
  128. if (numinrow != 0) {
  129. printf("%08lX:\n", count);
  130. }
  131. }