From 787e005d07914680b6276501e6b8f2ec6c8c1a1f Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 18 Jan 2021 20:32:16 +0800 Subject: [PATCH] Support parallel access device's web Signed-off-by: Jianhui Zhao --- src/web.c | 25 +++++++++++++++---------- src/web.h | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/web.c b/src/web.c index aa156c8..b3a19ce 100644 --- a/src/web.c +++ b/src/web.c @@ -68,8 +68,8 @@ static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) goto done; buffer_put_u8(wb, MSG_TYPE_WEB); - buffer_put_u16be(wb, 2 + ret); - buffer_put_u16be(wb, ctx->port); + buffer_put_u16be(wb, 18 + ret); + buffer_put_data(wb, ctx->addr, 18); buffer_put_data(wb, buf, ret); ev_io_start(rtty->loop, &rtty->iow); @@ -128,23 +128,26 @@ static void on_connected(int sock, void *arg) ctx->sock = sock; } -static struct web_request_ctx *find_exist_ctx(struct list_head *reqs, int port) +static struct web_request_ctx *find_exist_ctx(struct list_head *reqs, uint8_t *addr) { struct web_request_ctx *ctx; - list_for_each_entry(ctx, reqs, head) - if (ctx->port == port) + list_for_each_entry(ctx, reqs, head) { + if (!memcmp(ctx->addr, addr, 18)) return ctx; + } + return NULL; } void web_request(struct rtty *rtty, int len) { struct web_request_ctx *ctx; - int port, sock, req_len; struct sockaddr_in addrin = { .sin_family = AF_INET }; + int sock, req_len; + uint8_t addr[18]; void *data; if (len == 0) { @@ -152,13 +155,14 @@ void web_request(struct rtty *rtty, int len) return; } - port = buffer_pull_u16be(&rtty->rb); - req_len = len - 2; + buffer_pull(&rtty->rb, addr, 18); + + req_len = len - 18; if (req_len == 0) return; - ctx = find_exist_ctx(&rtty->web_reqs, port); + ctx = find_exist_ctx(&rtty->web_reqs, addr); if (ctx) { buffer_pull(&rtty->rb, NULL, 6); req_len -= 6; @@ -178,9 +182,10 @@ void web_request(struct rtty *rtty, int len) ctx = (struct web_request_ctx *)calloc(1, sizeof(struct web_request_ctx)); ctx->rtty = rtty; - ctx->port = port; ctx->active = ev_now(rtty->loop); + memcpy(ctx->addr, addr, 18); + data = buffer_put(&ctx->wb, req_len); buffer_pull(&rtty->rb, data, req_len); diff --git a/src/web.h b/src/web.h index 5b3fab8..c173344 100644 --- a/src/web.h +++ b/src/web.h @@ -37,7 +37,7 @@ struct web_request_ctx { struct buffer wb; ev_tstamp active; int sock; - int port; + uint8_t addr[18]; /* upstream connection address: [port ip] */ }; void web_request(struct rtty *rtty, int len);