Skip to content

Commit

Permalink
ames: factor out stun implementation and basic de/serialization funct…
Browse files Browse the repository at this point in the history
…ions (#662)

This PR factors out a couple small modules from ames, in anticipation of
merging in the directed messaging driver. Built on top of (and
targeting) #639.
  • Loading branch information
pkova authored Jul 2, 2024
2 parents 64eb9f3 + 0e8ca70 commit 6aaa489
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 423 deletions.
14 changes: 14 additions & 0 deletions pkg/c3/defs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#include "defs.h"

c3_s
c3_sift_short(c3_y buf_y[2]);
c3_w
c3_sift_word(c3_y buf_y[4]);
c3_d
c3_sift_chub(c3_y byt_y[8]);

void
c3_etch_short(c3_y buf_y[2], c3_s sot_s);
void
c3_etch_word(c3_y buf_y[4], c3_w wod_w);
void
c3_etch_chub(c3_y byt_y[8], c3_d num_d);

c3_w c3_align_w(c3_w x, c3_w al, align_dir hilo);
c3_d c3_align_d(c3_d x, c3_d al, align_dir hilo);
void *c3_align_p(void const * p, size_t al, align_dir hilo);
54 changes: 54 additions & 0 deletions pkg/c3/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,60 @@
| (((w) >> 8) & 0xff) << 16 \
| ( (w) & 0xff) << 24 )

inline c3_s
c3_sift_short(c3_y buf_y[2])
{
return (buf_y[1] << 8 | buf_y[0]);
}

inline c3_w
c3_sift_word(c3_y buf_y[4])
{
return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]);
}

inline c3_d
c3_sift_chub(c3_y byt_y[8])
{
return (c3_d)byt_y[0]
| (c3_d)byt_y[1] << 8
| (c3_d)byt_y[2] << 16
| (c3_d)byt_y[3] << 24
| (c3_d)byt_y[4] << 32
| (c3_d)byt_y[5] << 40
| (c3_d)byt_y[6] << 48
| (c3_d)byt_y[7] << 56;
}

inline void
c3_etch_short(c3_y buf_y[2], c3_s sot_s)
{
buf_y[0] = sot_s & 0xff;
buf_y[1] = (sot_s >> 8) & 0xff;
}

inline void
c3_etch_word(c3_y buf_y[4], c3_w wod_w)
{
buf_y[0] = wod_w & 0xff;
buf_y[1] = (wod_w >> 8) & 0xff;
buf_y[2] = (wod_w >> 16) & 0xff;
buf_y[3] = (wod_w >> 24) & 0xff;
}

inline void
c3_etch_chub(c3_y byt_y[8], c3_d num_d)
{
byt_y[0] = num_d & 0xff;
byt_y[1] = (num_d >> 8) & 0xff;
byt_y[2] = (num_d >> 16) & 0xff;
byt_y[3] = (num_d >> 24) & 0xff;
byt_y[4] = (num_d >> 32) & 0xff;
byt_y[5] = (num_d >> 40) & 0xff;
byt_y[6] = (num_d >> 48) & 0xff;
byt_y[7] = (num_d >> 56) & 0xff;
}

/* Asserting allocators.
*/
# define c3_free(s) free(s)
Expand Down
2 changes: 2 additions & 0 deletions pkg/vere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ vere_library(
"*.h",
"db/*.c",
"io/*.c",
"io/*/*.h",
"io/*/*.c",
],
exclude = [
"main.c",
Expand Down
4 changes: 2 additions & 2 deletions pkg/vere/ames_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ _test_stun_addr_roundtrip(u3_lane* inn_u)
c3_y req_y[20] = {0};
c3_i ret_i = 0;

_stun_make_response(req_y, inn_u, rep_y);
u3_stun_make_response(req_y, inn_u, rep_y);

u3_lane lan_u;

if ( c3n == _stun_find_xor_mapped_address(rep_y, sizeof(rep_y), &lan_u) ) {
if ( c3n == u3_stun_find_xor_mapped_address(rep_y, sizeof(rep_y), &lan_u) ) {
fprintf(stderr, "stun: failed to find addr in response\r\n");
ret_i = 1;
}
Expand Down
Loading

0 comments on commit 6aaa489

Please sign in to comment.