Add gt_tohex() and gt_fromhex()
This commit is contained in:
50
src/common.c
50
src/common.c
@@ -38,3 +38,53 @@ void gt_na (const char *name)
|
|||||||
{
|
{
|
||||||
gt_log("%s is not available on your platform!\n", name);
|
gt_log("%s is not available on your platform!\n", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int gt_tohex (char *dst, size_t dst_size, const uint8_t *src, size_t src_size)
|
||||||
|
{
|
||||||
|
if _0_(dst_size<2*src_size+1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
const char tbl[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
|
for (size_t i=0; i<src_size; i++) {
|
||||||
|
dst[(i<<1)+0] = tbl[0xF&(src[i]>>4)];
|
||||||
|
dst[(i<<1)+1] = tbl[0xF&(src[i])];
|
||||||
|
}
|
||||||
|
|
||||||
|
dst[2*src_size] = 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
_const_
|
||||||
|
static inline int fromhex (const char c)
|
||||||
|
{
|
||||||
|
if (c>='0' && c<='9')
|
||||||
|
return c-'0';
|
||||||
|
|
||||||
|
if (c>='A' && c<='F')
|
||||||
|
return c-'a'+10;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gt_fromhex (uint8_t *dst, size_t dst_size, const char *src, size_t src_size)
|
||||||
|
{
|
||||||
|
if _0_(src_size&1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if _0_(src_size>2*dst_size)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for (size_t i=0; i<src_size; i+=2) {
|
||||||
|
const int a = fromhex(src[i]);
|
||||||
|
const int b = fromhex(src[i+1]);
|
||||||
|
|
||||||
|
if _0_(a==-1 || b==-1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
dst[i>>1] = (a<<4)|b;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,3 +37,6 @@ int gt_print (const char *, ...) _printf_(1,2);
|
|||||||
void gt_log (const char *, ...) _printf_(1,2);
|
void gt_log (const char *, ...) _printf_(1,2);
|
||||||
void gt_fatal (const char *, ...) _printf_(1,2) _noreturn_;
|
void gt_fatal (const char *, ...) _printf_(1,2) _noreturn_;
|
||||||
void gt_na (const char *);
|
void gt_na (const char *);
|
||||||
|
|
||||||
|
int gt_tohex (char *, size_t, const uint8_t *, size_t);
|
||||||
|
int gt_fromhex (uint8_t *, size_t, const char *, size_t);
|
||||||
|
|||||||
31
src/main.c
31
src/main.c
@@ -483,38 +483,23 @@ static int gt_decrypt (struct crypto_ctx *ctx, buffer_t *dst, buffer_t *src)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void gt_dump (uint8_t *dst, size_t dst_size, uint8_t *src, size_t src_size)
|
|
||||||
{
|
|
||||||
if (dst_size<2*src_size+1)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const char tbl[] = "0123456789ABCDEF";
|
|
||||||
|
|
||||||
for (size_t i=0; i<src_size; i++) {
|
|
||||||
dst[(i<<1)+0] = tbl[0xF&(src[i]>>4)];
|
|
||||||
dst[(i<<1)+1] = tbl[0xF&(src[i])];
|
|
||||||
}
|
|
||||||
|
|
||||||
dst[2*src_size] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void gt_print_hdr (uint8_t *data, size_t ip_size, const char *sockname)
|
static void gt_print_hdr (uint8_t *data, size_t ip_size, const char *sockname)
|
||||||
{
|
{
|
||||||
const int ip_version = ip_get_version(data, GT_MTU_MAX);
|
const int ip_version = ip_get_version(data, GT_MTU_MAX);
|
||||||
const ssize_t ip_proto = ip_get_proto(data, GT_MTU_MAX);
|
const ssize_t ip_proto = ip_get_proto(data, GT_MTU_MAX);
|
||||||
const ssize_t ip_hdr_size = ip_get_hdr_size(data, GT_MTU_MAX);
|
const ssize_t ip_hdr_size = ip_get_hdr_size(data, GT_MTU_MAX);
|
||||||
|
|
||||||
uint8_t ip_src[33];
|
char ip_src[33];
|
||||||
uint8_t ip_dst[33];
|
char ip_dst[33];
|
||||||
|
|
||||||
switch (ip_version) {
|
switch (ip_version) {
|
||||||
case 4:
|
case 4:
|
||||||
gt_dump(ip_src, sizeof(ip_src), &data[12], 4);
|
gt_tohex(ip_src, sizeof(ip_src), &data[12], 4);
|
||||||
gt_dump(ip_dst, sizeof(ip_dst), &data[16], 4);
|
gt_tohex(ip_dst, sizeof(ip_dst), &data[16], 4);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
gt_dump(ip_src, sizeof(ip_src), &data[9], 16);
|
gt_tohex(ip_src, sizeof(ip_src), &data[9], 16);
|
||||||
gt_dump(ip_dst, sizeof(ip_dst), &data[25], 16);
|
gt_tohex(ip_dst, sizeof(ip_dst), &data[25], 16);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -930,8 +915,8 @@ int main (int argc, char **argv)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if _0_(ip_size!=r) {
|
if _0_(ip_size!=r) {
|
||||||
uint8_t tmp[2*GT_MTU_MAX+1];
|
char tmp[2*GT_MTU_MAX+1];
|
||||||
gt_dump(tmp, sizeof(tmp), data, GT_MTU_MAX);
|
gt_tohex(tmp, sizeof(tmp), data, r);
|
||||||
gt_log("%s: DUMP %zi %s\n", sockname, r, tmp);
|
gt_log("%s: DUMP %zi %s\n", sockname, r, tmp);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user