summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--VERSION2
-rw-r--r--gamt.c7
-rw-r--r--redir.c61
-rw-r--r--redir.h1
4 files changed, 61 insertions, 10 deletions
diff --git a/VERSION b/VERSION
index be58634..bd73f47 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.3
+0.4
diff --git a/gamt.c b/gamt.c
index cde3850..4ffa5b5 100644
--- a/gamt.c
+++ b/gamt.c
@@ -37,6 +37,7 @@ static char amt_host[64];
static char amt_port[16];
static char amt_user[32] = "admin";
static char amt_pass[32];
+static int amt_trace;
static int gamt_getstring(GtkWidget *window, char *title, char *message,
char *dest, int dlen, int hide);
@@ -477,6 +478,7 @@ static int gamt_connect(struct gamt_window *gamt)
snprintf(gamt->redir.pass, sizeof(gamt->redir.pass), "%s", amt_pass);
gamt->redir.verbose = 1;
+ gamt->redir.trace = amt_trace;
gamt->redir.cb_data = gamt;
gamt->redir.cb_recv = recv_gtk;
gamt->redir.cb_state = state_gtk;
@@ -623,12 +625,15 @@ main(int argc, char *argv[])
gtk_init(&argc, &argv);
for (;;) {
- if (-1 == (c = getopt(argc, argv, "hdu:p:f:c:b:")))
+ if (-1 == (c = getopt(argc, argv, "hdtu:p:f:c:b:")))
break;
switch (c) {
case 'd':
debug++;
break;
+ case 't':
+ amt_trace++;
+ break;
case 'u':
snprintf(amt_user, sizeof(amt_user), "%s", optarg);
break;
diff --git a/redir.c b/redir.c
index 7343307..c51349e 100644
--- a/redir.c
+++ b/redir.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <ctype.h>
#include "tcp.h"
#include "redir.h"
@@ -36,6 +37,44 @@ static const char *state_desc[] = {
/* ------------------------------------------------------------------ */
+static void hexdump(const char *prefix, const unsigned char *data, size_t size)
+{
+ char ascii[17];
+ int i;
+
+ for (i = 0; i < size; i++) {
+ if (0 == (i%16)) {
+ fprintf(stderr,"%s%s%04x:",
+ prefix ? prefix : "",
+ prefix ? ": " : "",
+ i);
+ memset(ascii,0,sizeof(ascii));
+ }
+ if (0 == (i%4))
+ fprintf(stderr," ");
+ fprintf(stderr," %02x",data[i]);
+ ascii[i%16] = isprint(data[i]) ? data[i] : '.';
+ if (15 == (i%16))
+ fprintf(stderr," %s\n",ascii);
+ }
+ if (0 != (i%16)) {
+ while (0 != (i%16)) {
+ if (0 == (i%4))
+ fprintf(stderr," ");
+ fprintf(stderr," ");
+ i++;
+ };
+ fprintf(stderr," %s\n",ascii);
+ }
+}
+
+static ssize_t redir_write(struct redir *r, const char *buf, size_t count)
+{
+ if (r->trace)
+ hexdump("out", buf, count);
+ return write(r->sock, buf, count);
+}
+
static void redir_state(struct redir *r, enum redir_state new)
{
enum redir_state old = r->state;
@@ -96,7 +135,7 @@ int redir_start(struct redir *r)
memcpy(request+4, r->type, 4);
redir_state(r, REDIR_INIT);
- return write(r->sock, request, sizeof(request));
+ return redir_write(r, request, sizeof(request));
}
int redir_stop(struct redir *r)
@@ -106,7 +145,7 @@ int redir_stop(struct redir *r)
};
redir_state(r, REDIR_CLOSED);
- write(r->sock, request, sizeof(request));
+ redir_write(r, request, sizeof(request));
close(r->sock);
return 0;
}
@@ -128,7 +167,7 @@ int redir_auth(struct redir *r)
request[10 + ulen] = plen;
memcpy(request + 11 + ulen, r->pass, plen);
redir_state(r, REDIR_AUTH);
- rc = write(r->sock, request, len);
+ rc = redir_write(r, request, len);
free(request);
return rc;
}
@@ -153,7 +192,7 @@ int redir_sol_start(struct redir *r)
};
redir_state(r, REDIR_INIT_SOL);
- return write(r->sock, request, sizeof(request));
+ return redir_write(r, request, sizeof(request));
}
int redir_sol_stop(struct redir *r)
@@ -164,7 +203,7 @@ int redir_sol_stop(struct redir *r)
};
redir_state(r, REDIR_CLOSING);
- return write(r->sock, request, sizeof(request));
+ return redir_write(r, request, sizeof(request));
}
int redir_sol_send(struct redir *r, unsigned char *buf, int blen)
@@ -178,7 +217,7 @@ int redir_sol_send(struct redir *r, unsigned char *buf, int blen)
request[8] = blen & 0xff;
request[9] = blen >> 8;
memcpy(request + 10, buf, blen);
- rc = write(r->sock, request, len);
+ rc = redir_write(r, request, len);
free(request);
return rc;
}
@@ -210,6 +249,8 @@ int redir_sol_recv(struct redir *r)
fprintf(stderr, "EOF from socket\n");
return -1;
default:
+ if (r->trace)
+ hexdump("in+", msg, count);
if (r->cb_recv)
r->cb_recv(r->cb_data, msg, count);
len -= count;
@@ -224,8 +265,12 @@ int redir_data(struct redir *r)
int rc, bshift;
rc = read(r->sock, r->buf + r->blen, sizeof(r->buf) - r->blen);
- if (rc <= 0)
+ if (rc <= 0) {
+ perror("read(sock)");
goto err;
+ }
+ if (r->trace)
+ hexdump("in ", r->buf + r->blen, rc);
r->blen += rc;
for (;;) {
@@ -273,7 +318,7 @@ int redir_data(struct redir *r)
bshift = HEARTBEAT_LENGTH;
if (r->blen < bshift)
goto again;
- if (HEARTBEAT_LENGTH != write(r->sock, r->buf, HEARTBEAT_LENGTH)) {
+ if (HEARTBEAT_LENGTH != redir_write(r, r->buf, HEARTBEAT_LENGTH)) {
perror("write(sock)");
goto err;
}
diff --git a/redir.h b/redir.h
index f32f6ee..139c392 100644
--- a/redir.h
+++ b/redir.h
@@ -24,6 +24,7 @@ struct redir {
/* serial-over-lan */
unsigned char type[4];
int verbose;
+ int trace;
enum redir_state state;
int sock;