Skip to content
Permalink
Browse files

add fr_get/put_16/32/64 and use then in from/to network code

TBH, we should probably get rid of htonl()+memcpy in the rest of
the code, as these macros seem better.
  • Loading branch information
alandekok committed Sep 24, 2019
1 parent 1a03e88 commit 8481e2346a0605295302bc2eb1846016866a1add
Showing with 41 additions and 33 deletions.
  1. +32 −0 src/lib/util/net.h
  2. +9 −33 src/lib/util/value.c
@@ -163,6 +163,38 @@ uint16_t fr_udp_checksum(uint8_t const *data, uint16_t len, uint16_t checksum,
int fr_udp_header_check(uint8_t const *data, uint16_t remaining, ip_header_t const *ip);
uint16_t fr_ip_header_checksum(uint8_t const *data, uint8_t ihl);

static inline void fr_put_be16(uint8_t *a, uint16_t val)
{
a[0] = (val >> 8) & 0xff;
a[1] = val & 0xff;
}

static inline void fr_put_be32(uint8_t *a, uint32_t val)
{
fr_put_be16(a, (uint16_t) ((val >> 16) & 0xffff));
fr_put_be16(a + 2, (uint16_t) (val & 0xffff));
}

static inline void fr_put_be64(uint8_t *a, uint64_t val)
{
fr_put_be32(a, (uint32_t) ((val >> 32) & 0xffffffff));
fr_put_be32(a + 4, (uint32_t) (val & 0xffffffff));
}

static inline uint16_t fr_get_be16(uint8_t const *a)
{
return (a[0] << 8) | a[1];
}

static inline uint32_t fr_get_be32(uint8_t const *a)
{
return ((uint32_t) fr_get_be16(a) << 16) | fr_get_be16(a + 2);
}

static inline uint64_t fr_get_be64(uint8_t const *a)
{
return ((uint64_t) fr_get_be32(a) << 32) | fr_get_be32(a + 4);
}
#ifdef __cplusplus
}
#endif
@@ -50,6 +50,7 @@ RCSID("$Id$")
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/talloc.h>
#include <freeradius-devel/util/value.h>
#include <freeradius-devel/util/net.h>

#include <assert.h>
#include <ctype.h>
@@ -1197,8 +1198,7 @@ ssize_t fr_value_box_to_network(size_t *need, uint8_t *dst, size_t dst_len, fr_v
if (date >= ((int64_t) 1) << 16) {
memset(dst, 0xff, 2);
} else {
dst[0] = (date >> 8) & 0xff;
dst[1] = date & 0xff;
fr_put_be16(dst, date);
}
break;

@@ -1207,22 +1207,12 @@ ssize_t fr_value_box_to_network(size_t *need, uint8_t *dst, size_t dst_len, fr_v
if (date >= ((int64_t) 1) << 32) {
memset(dst, 0xff, 4);
} else {
dst[0] = (date >> 24) & 0xff;
dst[1] = (date >> 16) & 0xff;
dst[2] = (date >> 8) & 0xff;
dst[3] = date & 0xff;
fr_put_be32(dst, date);
}
break;

case 8:
dst[0] = (date >> 56) & 0xff;
dst[1] = (date >> 48) & 0xff;
dst[2] = (date >> 40) & 0xff;
dst[3] = (date >> 32) & 0xff;
dst[4] = (date >> 24) & 0xff;
dst[5] = (date >> 16) & 0xff;
dst[6] = (date >> 8) & 0xff;
dst[7] = date & 0xff;
fr_put_be32(dst, date);
break;

default:
@@ -1269,8 +1259,7 @@ ssize_t fr_value_box_to_network(size_t *need, uint8_t *dst, size_t dst_len, fr_v
if (date >= ((int64_t) 1) << 16) {
memset(dst, 0xff, 2);
} else {
dst[0] = (date >> 8) & 0xff;
dst[1] = date & 0xff;
fr_put_be16(dst, date);
}
break;

@@ -1279,22 +1268,12 @@ ssize_t fr_value_box_to_network(size_t *need, uint8_t *dst, size_t dst_len, fr_v
if (date >= ((int64_t) 1) << 32) {
memset(dst, 0xff, 4);
} else {
dst[0] = (date >> 24) & 0xff;
dst[1] = (date >> 16) & 0xff;
dst[2] = (date >> 8) & 0xff;
dst[3] = date & 0xff;
fr_put_be32(dst, date);
}
break;

case 8:
dst[0] = (date >> 56) & 0xff;
dst[1] = (date >> 48) & 0xff;
dst[2] = (date >> 40) & 0xff;
dst[3] = (date >> 32) & 0xff;
dst[4] = (date >> 24) & 0xff;
dst[5] = (date >> 16) & 0xff;
dst[6] = (date >> 8) & 0xff;
dst[7] = date & 0xff;
fr_put_be64(dst, date);
break;

default:
@@ -2174,10 +2153,8 @@ static inline int fr_value_box_cast_to_ethernet(TALLOC_CTX *ctx, fr_value_box_t

case FR_TYPE_UINT64: {
uint8_t array[8];
uint64_t i;

i = htonll(src->vb_uint64);
memcpy(array, &i, 8);
fr_put_be64(array, src->vb_uint64);

/*
* For OUIs in the DB.
@@ -2795,8 +2772,7 @@ int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst,

if ((src->type == FR_TYPE_IFID) &&
(dst_type == FR_TYPE_UINT64)) {
memcpy(&dst->vb_uint64, src->vb_ifid, sizeof(src->vb_ifid));
dst->vb_uint64 = htonll(dst->vb_uint64);
dst->vb_uint64 = fr_get_be64(&src->vb_ifid[0]);

fixed_length:
dst->type = dst_type;

0 comments on commit 8481e23

Please sign in to comment.
You can’t perform that action at this time.