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