Compare commits

...

3 Commits

Author SHA1 Message Date
angt
ddae22a3d9 Use a new random secret key without keyfile 2015-12-15 09:07:44 +01:00
angt
893de45272 Accept lower-case in fromhex() 2015-12-15 08:28:10 +01:00
angt
78ba4c9a59 Do not call ip_get_version() again and again 2015-12-14 18:37:50 +01:00
3 changed files with 31 additions and 20 deletions

View File

@@ -68,6 +68,9 @@ static inline int fromhex (const char c)
if (c>='A' && c<='F') if (c>='A' && c<='F')
return c-'A'+10; return c-'A'+10;
if (c>='a' && c<='f')
return c-'a'+10;
return -1; return -1;
} }

View File

@@ -12,9 +12,9 @@ static inline int ip_get_version (const uint8_t *data, size_t size)
} }
_pure_ _pure_
static inline ssize_t ip_get_size (const uint8_t *data, size_t size) static inline ssize_t ip_get_size (const int ip_version, const uint8_t *data, size_t size)
{ {
switch (ip_get_version(data, size)) { switch (ip_version) {
case 4: case 4:
return ((data[2]<<8)|data[3]); return ((data[2]<<8)|data[3]);
case 6: case 6:
@@ -27,9 +27,9 @@ static inline ssize_t ip_get_size (const uint8_t *data, size_t size)
} }
_pure_ _pure_
static inline ssize_t ip_get_proto (const uint8_t *data, size_t size) static inline ssize_t ip_get_proto (const int ip_version, const uint8_t *data, size_t size)
{ {
switch (ip_get_version(data, size)) { switch (ip_version) {
case 4: case 4:
return data[9]; return data[9];
case 6: case 6:
@@ -42,9 +42,9 @@ static inline ssize_t ip_get_proto (const uint8_t *data, size_t size)
} }
_pure_ _pure_
static inline ssize_t ip_get_hdr_size (const uint8_t *data, size_t size) static inline ssize_t ip_get_hdr_size (const int ip_version, const uint8_t *data, size_t size)
{ {
switch (ip_get_version(data, size)) { switch (ip_version) {
case 4: case 4:
return (data[0]&0xF)<<2; return (data[0]&0xF)<<2;
case 6: case 6:

View File

@@ -491,11 +491,10 @@ static int gt_decrypt (struct crypto_ctx *ctx, buffer_t *dst, buffer_t *src)
return 0; return 0;
} }
static void gt_print_hdr (uint8_t *data, size_t ip_size, const char *sockname) static void gt_print_hdr (const int ip_version, uint8_t *data, size_t ip_size, const char *sockname)
{ {
const int ip_version = ip_get_version(data, GT_MTU_MAX); const ssize_t ip_proto = ip_get_proto(ip_version, data, ip_size);
const ssize_t ip_proto = ip_get_proto(data, GT_MTU_MAX); const ssize_t ip_hdr_size = ip_get_hdr_size(ip_version, data, ip_size);
const ssize_t ip_hdr_size = ip_get_hdr_size(data, GT_MTU_MAX);
char ip_src[33]; char ip_src[33];
char ip_dst[33]; char ip_dst[33];
@@ -555,10 +554,16 @@ static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
{ {
const size_t size = sizeof(ctx->skey); const size_t size = sizeof(ctx->skey);
byte_set(ctx->skey, 1, size); if (!keyfile) {
char buf[2*size+1];
randombytes_buf(ctx->skey, size);
gt_tohex(buf, sizeof(buf), ctx->skey, size);
gt_print("new secret key: %s\n", buf);
if (!keyfile)
return 0; return 0;
}
int fd; int fd;
@@ -757,11 +762,6 @@ int main (int argc, char **argv)
return 1; return 1;
} }
struct crypto_ctx ctx;
if (gt_setup_secretkey(&ctx, keyfile))
return 1;
struct addrinfo *ai = ai_create(host, port, listener); struct addrinfo *ai = ai_create(host, port, listener);
if (!ai) if (!ai)
@@ -800,6 +800,11 @@ int main (int argc, char **argv)
return 1; return 1;
} }
struct crypto_ctx ctx;
if (gt_setup_secretkey(&ctx, keyfile))
return 1;
if (option_is_set(opts, "daemon")) { if (option_is_set(opts, "daemon")) {
switch (fork()) { switch (fork()) {
case -1: case -1:
@@ -943,7 +948,8 @@ int main (int argc, char **argv)
break; break;
} }
const ssize_t ip_size = ip_get_size(data, GT_MTU_MAX); const int ip_version = ip_get_version(data, GT_MTU_MAX);
const ssize_t ip_size = ip_get_size(ip_version, data, GT_MTU_MAX);
if _0_(ip_size<=0) if _0_(ip_size<=0)
continue; continue;
@@ -956,7 +962,7 @@ int main (int argc, char **argv)
} }
if _0_(debug) if _0_(debug)
gt_print_hdr(data, ip_size, sockname); gt_print_hdr(ip_version, data, ip_size, sockname);
blks[blk_write++].size = r; blks[blk_write++].size = r;
blk_count++; blk_count++;
@@ -1031,7 +1037,9 @@ int main (int argc, char **argv)
} }
size_t size = buffer_read_size(&tun.write); size_t size = buffer_read_size(&tun.write);
ssize_t ip_size = ip_get_size(tun.write.read, size);
const int ip_version = ip_get_version(tun.write.read, size);
ssize_t ip_size = ip_get_size(ip_version, tun.write.read, size);
if _0_(!ip_size) { if _0_(!ip_size) {
gt_log("%s: bad packet!\n", sockname); gt_log("%s: bad packet!\n", sockname);