#include <config.h>
#include "utilities.h"
#include <assert.h>
void
pkt_output (
struct pkt *dpkg,
int pkt_length,
FILE *output
)
{
register int a;
u_char *pkt;
pkt = (u_char *)dpkg;
fprintf(output, HLINE);
for (a = 0; a < pkt_length; a++) {
if (a > 0 && a % 8 == 0)
fprintf(output, "\n");
fprintf(output, "%d: %x \t", a, pkt[a]);
}
fprintf(output, "\n");
fprintf(output, HLINE);
}
void
l_fp_output(
l_fp * ts,
FILE * output
)
{
fprintf(output, "%s\n", prettydate(ts));
}
void
l_fp_output_bin (
l_fp *ts,
FILE *output
)
{
register int a, b;
fprintf(output, HLINE);
for(a=0; a<8; a++) {
short tmp = ((unsigned char *) ts)[a];
tmp++;
fprintf(output, "%i: ", a);
for(b=7; b>=0; b--) {
int texp = (int) pow(2, b);
if(tmp - texp > 0) {
fprintf(output, "1");
tmp -= texp;
}
else {
fprintf(output, "0");
}
}
fprintf(output, " ");
}
fprintf(output, "\n");
fprintf(output, HLINE);
}
void
l_fp_output_dec (
l_fp *ts,
FILE *output
)
{
register int a;
fprintf(output, HLINE);
for(a=0; a<8; a++)
fprintf(output, "%i: %i \t", a, ((unsigned char *) ts)[a]);
fprintf(output, "\n");
fprintf(output, HLINE);
}
char *
addrinfo_to_str (
const struct addrinfo *addr
)
{
sockaddr_u s;
ZERO(s);
memcpy(&s, addr->ai_addr, min(sizeof(s), addr->ai_addrlen));
return ss_to_str(&s);
}
char *
ss_to_str(
sockaddr_u *saddr
)
{
return estrdup(stoa(saddr));
}
char *
tv_to_str(
const struct timeval *tv
)
{
const size_t bufsize = 48;
char *buf;
time_t gmt_time, local_time;
struct tm *p_tm_local, *tmp_tm;
int hh, mm, lto;
gmt_time = tv->tv_sec;
tmp_tm = gmtime(&gmt_time);
tmp_tm->tm_isdst = -1;
local_time = mktime(tmp_tm);
p_tm_local = localtime(&gmt_time);
lto = difftime(local_time, gmt_time);
lto /= 60;
hh = lto / 60;
mm = abs(lto % 60);
buf = emalloc(bufsize);
snprintf(buf, bufsize,
"%d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d (%+03d%02d)",
p_tm_local->tm_year + 1900,
p_tm_local->tm_mon + 1,
p_tm_local->tm_mday,
p_tm_local->tm_hour,
p_tm_local->tm_min,
p_tm_local->tm_sec,
(int)tv->tv_usec,
hh,
mm);
return buf;
}
const char *
hostnameaddr(
const char * hostname,
const sockaddr_u * addr
)
{
const char * addrtxt;
char * result;
int cnt;
addrtxt = stoa(addr);
LIB_GETBUF(result);
if (strcmp(hostname, addrtxt))
cnt = snprintf(result, LIB_BUFLENGTH, "%s %s",
hostname, addrtxt);
else
cnt = snprintf(result, LIB_BUFLENGTH, "%s", addrtxt);
if (cnt >= LIB_BUFLENGTH)
snprintf(result, LIB_BUFLENGTH,
"hostnameaddr ERROR have %d (%d needed)",
LIB_BUFLENGTH, cnt + 1);
return result;
}