1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* qstat
* by Steve Jankowski
*
* debug helper functions
* Copyright 2004 Ludwig Nussel
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/param.h>
#else
#include <stdarg.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include <stdarg.h>
#define QSTAT_DEBUG_C
#include "debug.h"
#ifdef ENABLE_DUMP
#ifndef _WIN32
#include <sys/socket.h>
#else
#include <winsock.h>
#endif
#endif
#ifdef DEBUG
void
_debug(const char *file, int line, const char *function, int level, const char *fmt, ...)
{
va_list ap;
char buf[9];
time_t now = time(NULL);
strftime(buf, 9, "%H:%M:%S", localtime(&now));
fprintf(stderr, "debug(%d) %s %s:%d %s() - ", level, buf, file, line, function);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if ('\n' != fmt[strlen(fmt) - 1]) {
fputs("\n", stderr);
}
}
#endif
static int debug_level = 0;
void
set_debug_level(int level)
{
debug_level = level;
}
int
get_debug_level(void)
{
return (debug_level);
}
void
malformed_packet(const struct qserver *server, const char *fmt, ...)
{
va_list ap;
if (!show_errors) {
return;
}
fputs("malformed packet", stderr);
if (server) {
fprintf(stderr, " from %d.%d.%d.%d:%hu",
server->ipaddr & 0xff,
(server->ipaddr >> 8) & 0xff,
(server->ipaddr >> 16) & 0xff,
(server->ipaddr >> 24) & 0xff,
server->port);
}
fputs(": ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (*fmt && (fmt[strlen(fmt) - 1] != '\n')) {
fputc('\n', stderr);
}
}
#ifdef ENABLE_DUMP
static unsigned count = 0;
static void
_dump_packet(const char *tag, const char *buf, int buflen)
{
char *fn = NULL;
int fd, len;
len = 3 + 1 + strlen(tag) + 4 + 1;
fn = malloc(len);
if(!fn) { perror("malloc"); return; }
snprintf(fn, len, "%03u_%s.pkt", count++, tag);
fprintf(stderr, "dumping to %s\n", fn);
fd = open(fn, O_WRONLY | O_CREAT | O_EXCL, 0644);
free(fn);
if (fd == -1) {
perror("open");
return;
}
if (write(fd, buf, buflen) == -1) {
perror("write");
}
close(fd);
}
int do_dump;
ssize_t
send_dump(int s, const void *buf, size_t len, int flags)
{
if (do_dump) {
_dump_packet("send", buf, len);
}
return (send(s, buf, len, flags));
}
#endif
void
dump_packet(const char *buf, int buflen)
{
#ifdef ENABLE_DUMP
_dump_packet("recv", buf, buflen);
#endif
}
void
print_packet(struct qserver *server, const char *buf, int buflen)
{
output_packet(server, buf, buflen, 0);
}
void
output_packet(struct qserver *server, const char *buf, int buflen, int to)
{
static char *hex = "0123456789abcdef";
unsigned char *p = (unsigned char *)buf;
int i, h, a, b, astart, offset = 0;
char line[256];
if (server != NULL) {
fprintf(stderr, "%s %s len %d\n", (to) ? "TO" : "FROM", server->arg, buflen);
}
for (i = buflen; i; offset += 16) {
memset(line, ' ', 256);
h = 0;
h += sprintf(line, "%5d:", offset);
a = astart = h + 16 * 2 + 16 / 4 + 2;
for (b = 16; b && i; b--, i--, p++) {
if ((b & 3) == 0) {
line[h++] = ' ';
}
line[h++] = hex[*p >> 4];
line[h++] = hex[*p & 0xf];
if (isprint(*p)) {
line[a++] = *p;
} else {
line[a++] = '.';
}
if ((a - astart) == 8) {
line[a++] = ' ';
}
}
line[a] = '\0';
fputs(line, stderr);
fputs("\n", stderr);
}
fputs("\n", stderr);
}
|