Skip to content

Commit

Permalink
stun: send heard lane to requester
Browse files Browse the repository at this point in the history
  • Loading branch information
yosoyubik committed Oct 26, 2023
1 parent 5bc1013 commit b7c2b41
Showing 1 changed file with 36 additions and 51 deletions.
87 changes: 36 additions & 51 deletions pkg/vere/io/ames.c
Original file line number Diff line number Diff line change
Expand Up @@ -1260,53 +1260,33 @@ _stun_send_cb(uv_udp_send_t *req_u, c3_i sas_i)
static void _stun_on_request(u3_ames *sam_u, c3_y buf_r[20],
const struct sockaddr* adr_u) {
// XX TODO

c3_y buf_y[36] = {0};
struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u;
c3_y buf_y[32] = {0};

// copy STUN request header
memcpy(buf_y, buf_r, 0);

// 0x0101 SUCCESS RESPONSE
buf_y[0] = 0x01;
buf_y[1] = 0x01;

// XX TODO: add sponsee's IP Addres (32 bits)
// use _ames_etch functions
//
buf_y[22] = 0x01; // 21-22 STUN attribute type 0x0001
buf_y[24] = 0x10; // 23-24 STUN attribute length
buf_y[26] = 0x01; // 25-26 family 0x01:IPv4
buf_y[27] = 0x01; // 27-28 family 0x01:IPv4
// Port
//buf_y[27] = (adr_u.sin_port >> 8); // Most significant byte
//buf_y[28] = adr_u.sin_port & 0xFF; // Least significant byte
_ames_etch_short(buf_y + 16, adr_u.sin_port);

buf_y[29] = adr_u.sin_addr.s_addr >> 24;
buf_y[28] = adr_u.sin_addr.s_addr >> 16;
buf_y[28] = adr_u.sin_addr.s_addr >> 8;
buf_y[28] = adr_u.sin_addr.s_addr & 0xFFFF;

u3l_log("hear request");
struct sockaddr_in add_u;

// memset(&add_u, 0, sizeof(add_u));
// add_u.sin_family = AF_INET;
// add_u.sin_addr.s_addr = htonl(lan_u.pip_w);
// add_u.sin_port = htons(lan_u.por_s);

// u3l_log("(%d %d)", add_u.sin_addr.s_addr, add_u.sin_port);

uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, 20);
memcpy(buf_y, buf_r, 20);

c3_w cur_w = 20; // STUN header is 20 bytes
buf_y[0] = 0x01; buf_y[1] = 0x01; // 0x0101 SUCCESS RESPONSE
buf_y[2] = 0x00; buf_y[3] = 0x0c; // 12 bytes
buf_y[cur_w] = 0x00; buf_y[cur_w + 1] = 0x01; // STUN attribute type 0x0001
buf_y[cur_w + 2] = 0x00; buf_y[cur_w + 3] = 0x08; // STUN attribute length
// extra reserved 0x0 byte
// XX use memcpy instead?
_ames_etch_short(buf_y + cur_w+ 5, 0x01); // family 0x01:IPv4
_ames_etch_short(buf_y + cur_w+ 6, add_u->sin_port); // Port
_ames_etch_word(buf_y + cur_w+ 8, htonl(add_u->sin_addr.s_addr)); // IP Addres

uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, 32);
u3l_log("hear request on %u", add_u->sin_addr.s_addr);
u3l_log("send ip: %u", _ames_sift_word(buf_y + 28));
u3_stun_send* snd_u = c3_calloc(sizeof(*snd_u));
snd_u->sam_u = sam_u;
c3_i sas_i = uv_udp_send(
(uv_udp_send_t*)snd_u, &sam_u->wax_u, &buf_u, 1,
adr_u, _stun_send_cb
);

u3l_log("sas %d", sas_i);

if ( sas_i != 0) {
u3l_log("stun: send response fail_sync: %s", uv_strerror(sas_i));
// TODO take error handling actions
Expand Down Expand Up @@ -2426,23 +2406,27 @@ _ames_recv_cb(uv_udp_t* wax_u,
}
c3_free(buf_u->base);
}
else if ( nrd_i == 20 ) {
// STUN resquest/response
// TODO sanity checks
// TODO check STUN cookie
u3_ames* sam_u = wax_u->data;

if (buf_u->base[0] == 0x0 && buf_u->base[1] == 0x01) {
// STUN resquest/response
// TODO sanity checks
// TODO check STUN cookie
else if (buf_u->base[0] == 0x0 && buf_u->base[1] == 0x01) {
// STUN request
u3_ames* sam_u = wax_u->data;
_stun_on_request(sam_u, (c3_y *)buf_u->base, adr_u);
} else if ((buf_u->base[0] == 0x01 && buf_u->base[1] == 0x01)) {
} else if ((buf_u->base[0] == 0x01 && buf_u->base[1] == 0x01)) {
// STUN response
u3_ames* sam_u = wax_u->data;

c3_w ip_addr = _ames_sift_word(buf_u->base + 28);
c3_s port = _ames_sift_short(buf_u->base + 26);

u3l_log("ip: %u", ntohl(ip_addr));
u3l_log("port: %u", ntohs(port));
// Cache lane
sam_u->sun_u.sef_u.por_s = ntohs(port);
sam_u->sun_u.sef_u.pip_w = ntohs(ip_addr);
_stun_on_response(sam_u);
} else {
// TODO STUN error
}
}
else {
} else {
u3_ames* sam_u = wax_u->data;
struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u;
u3_lane lan_u;
Expand Down Expand Up @@ -2675,6 +2659,7 @@ _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat)

case c3__saxo: {
_ames_ef_saxo(sam_u, u3k(dat));
ret_o = c3y;
} break;
}

Expand Down

0 comments on commit b7c2b41

Please sign in to comment.