Skip to content

Commit

Permalink
rendezvous: maintain protocol compaibility
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Nov 17, 2024
1 parent f9de58a commit cda57bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/event_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,22 @@ static bool svc_timeout_filt(
struct service *restrict svc = element;
ASSERT(key.data == svc->id);
ev_tstamp not_seen = now - svc->last_seen;
if (not_seen > s->session_timeout) {
if (not_seen < s->session_timeout) {
return true;
}
if (LOGLEVEL(INFO)) {
char addr1_str[64], addr2_str[64];
format_sa(
addr1_str, sizeof(addr1_str), &svc->server_addr[0].sa);
format_sa(
addr2_str, sizeof(addr2_str), &svc->server_addr[1].sa);
LOG_BIN_F(
DEBUG, svc->id, svc->idlen, "service %p timeout", svc);
free(svc);
return false;
INFO, svc->id, svc->idlen,
"service timeout: (%s, %s), idlen=%zu", addr1_str,
addr2_str, svc->idlen);
}
return true;
free(svc);
return false;
}

void timeout_cb(struct ev_loop *loop, struct ev_timer *watcher, int revents)
Expand Down
15 changes: 9 additions & 6 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,16 @@ void udp_rendezvous(struct server *restrict s, const uint16_t what)
const size_t idlen = s->conf->service_idlen;
const struct sockaddr *sa_server = &s->pkt.rendezvous_server.sa;
const struct sockaddr *sa_local = &s->pkt.rendezvous_local.sa;
unsigned char b[INET6ADDR_LENGTH + idlen];
size_t n = inetaddr_write(b, sizeof(b), sa_local);
unsigned char b[INET6ADDR_LENGTH + sizeof(uint16_t) + idlen];
unsigned char *p = b;
size_t n = inetaddr_write(p, sizeof(b), sa_local);
ASSERT(n > 0);
if (idlen > 0) {
memcpy(b + n, s->conf->service_id, idlen);
n += idlen;
}
p += n;
ASSERT(idlen <= UINT16_MAX);
write_uint16(p, idlen);
p += sizeof(uint16_t), n += sizeof(uint16_t);
memcpy(p, s->conf->service_id, idlen);
n += idlen;
ss0_send(s, sa_server, what, b, n);
}

Expand Down
32 changes: 25 additions & 7 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,23 +609,32 @@ ss0_on_listen(struct server *restrict s, struct msgframe *restrict msg)
return false;
}
msgbuf += n, msglen -= n;
struct hashkey key = { .data = msgbuf, .len = msglen };
struct hashkey key = { .data = NULL, .len = 0 };
if (msglen >= sizeof(uint16_t)) {
n = read_uint16(msgbuf);
msgbuf += sizeof(uint16_t), msglen -= sizeof(uint16_t);
if (msglen >= n) {
key.data = msgbuf;
key.len = n;
}
}
if (LOGLEVEL(VERBOSE)) {
char addr_str[64];
format_sa(addr_str, sizeof(addr_str), &addr.sa);
LOG_BIN_F(
VERBOSE, key.data, key.len, "ss0_on_listen: [%zu] %s",
key.len, addr_str);
}
bool created = false;
struct service *restrict svc;
if (!table_find(s->pkt.services, key, (void **)&svc)) {
svc = malloc(sizeof(struct service) + msglen);
svc = malloc(sizeof(struct service) + key.len);
if (svc == NULL) {
LOGOOM();
return false;
}
svc->idlen = msglen;
memcpy(svc->id, msgbuf, msglen);
svc->idlen = key.len;
memcpy(svc->id, key.data, key.len);
key = (struct hashkey){ .data = svc->id, .len = svc->idlen };
void *element = svc;
s->pkt.services = table_set(s->pkt.services, key, &element);
Expand All @@ -634,18 +643,19 @@ ss0_on_listen(struct server *restrict s, struct msgframe *restrict msg)
free(svc);
return false;
}
created = true;
}
svc->last_seen = ev_now(s->loop);
svc->server_addr[0] = addr;
svc->server_addr[1] = msg->addr;
if (LOGLEVEL(DEBUG)) {
if (created && LOGLEVEL(INFO)) {
char addr1_str[64], addr2_str[64];
format_sa(
addr1_str, sizeof(addr1_str), &svc->server_addr[0].sa);
format_sa(
addr2_str, sizeof(addr2_str), &svc->server_addr[1].sa);
LOG_BIN_F(
DEBUG, svc->id, svc->idlen,
INFO, svc->id, svc->idlen,
"rendezvous listen: (%s, %s), idlen=%zu", addr1_str,
addr2_str, svc->idlen);
}
Expand All @@ -665,7 +675,15 @@ ss0_on_connect(struct server *restrict s, struct msgframe *restrict msg)
return false;
}
msgbuf += n, msglen -= n;
struct hashkey key = { .data = msgbuf, .len = msglen };
struct hashkey key = { .data = NULL, .len = 0 };
if (msglen >= sizeof(uint16_t)) {
n = read_uint16(msgbuf);
msgbuf += sizeof(uint16_t), msglen -= sizeof(uint16_t);
if (msglen >= n) {
key.data = msgbuf;
key.len = n;
}
}
if (LOGLEVEL(VERBOSE)) {
char addr_str[64];
format_sa(addr_str, sizeof(addr_str), &addr.sa);
Expand Down

0 comments on commit cda57bf

Please sign in to comment.