From ea836c09048453babda517e6a440d7266304ca02 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Thu, 11 Jun 2020 23:21:37 +0200 Subject: Use struct connect for the client connection as well in proxy --- proxy.c | 18 ++++++++++++------ proxy.h | 5 +---- utils.c | 19 +++++++++++++------ utils.h | 5 +++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/proxy.c b/proxy.c index 58f81ca..d06803d 100644 --- a/proxy.c +++ b/proxy.c @@ -74,9 +74,12 @@ proxy_free(struct uring_task *task) format_bytes(cts, sizeof(cts), proxy->client_bytes); format_bytes(stc, sizeof(stc), proxy->server_bytes); - fprintf(stderr, "%s: proxy connection %s -> %s closed (CtS: %s, StC: %s), duration %s\n", - proxy->scfg->name, proxy->clientstr, proxy->serverstr, cts, stc, - duration); + fprintf(stderr, "%s: proxy connection %s -> %s closed " + "(CtS: %s, StC: %s), duration %s\n", + proxy->scfg->name, + proxy->client_conn.remotestr, + proxy->server_conn.remotestr, + cts, stc, duration); } list_del(&proxy->list); @@ -193,7 +196,9 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, int res) proxy->sfd = proxy->servertask.fd; fprintf(stderr, "%s: proxy connection %s -> %s opened\n", - proxy->scfg->name, proxy->clientstr, proxy->server_conn.remotestr); + proxy->scfg->name, + proxy->client_conn.remotestr, + proxy->server_conn.remotestr); proxy->begin = time(NULL); uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in); @@ -214,10 +219,11 @@ proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, in proxy->sfd = -1; proxy->cfd = fd; proxy->scfg = scfg; - proxy->client = *client; uring_task_init(&proxy->task, "proxy", &scfg->task, proxy_free); - sockaddr_to_str(&proxy->client, proxy->clientstr, sizeof(proxy->clientstr)); + connection_set_local(cfg, &proxy->client_conn, fd); + connection_set_remote(cfg, &proxy->client_conn, client); + uring_task_init(&proxy->clienttask, "proxy_client", &proxy->task, proxy_client_free); uring_task_set_buf(&proxy->clienttask, &proxy->clientbuf); uring_task_set_fd(&proxy->clienttask, fd); diff --git a/proxy.h b/proxy.h index 6b6e265..838cbb5 100644 --- a/proxy.h +++ b/proxy.h @@ -2,16 +2,13 @@ #define fooproxyhfoo struct server_proxy { - struct sockaddr_in46 client; - char clientstr[ADDRSTRLEN]; + struct connection client_conn; struct uring_task_buf clientbuf; struct uring_task clienttask; uint64_t client_bytes; int cfd; struct connection server_conn; - struct sockaddr_in46 server; - char serverstr[ADDRSTRLEN]; struct uring_task_buf serverbuf; struct uring_task servertask; uint64_t server_bytes; diff --git a/utils.c b/utils.c index 8749c75..05fe124 100644 --- a/utils.c +++ b/utils.c @@ -187,16 +187,24 @@ socket_set_low_latency(struct cfg *cfg, int sfd) perror("setsockopt"); } -static void -connection_set_local_addr(struct cfg *cfg, struct connection *conn, int fd) +void +connection_set_local(struct cfg *cfg, struct connection *conn, int fd) { - if (fd < 0 || getsockname(fd, (struct sockaddr *)&conn->local.storage, + if (fd < 0 || getsockname(fd, + (struct sockaddr *)&conn->local.storage, &conn->local.addrlen) < 0) sprintf(conn->localstr, ""); else sockaddr_to_str(&conn->local, conn->localstr, sizeof(conn->localstr)); } +void +connection_set_remote(struct cfg *cfg, struct connection *conn, struct sockaddr_in46 *remote) +{ + conn->remote = *remote; + sockaddr_to_str(&conn->remote, &conn->remotestr, sizeof(conn->remotestr)); +} + static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn); static void @@ -212,7 +220,7 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) return; } - connection_set_local_addr(cfg, conn, task->fd); + connection_set_local(cfg, conn, task->fd); fprintf(stderr, "%s: (%s) connection established %s -> %s\n", __func__, task->name, conn->localstr, conn->remotestr); @@ -245,8 +253,7 @@ again: } conn->next_addr++; - conn->remote = *remote; - sockaddr_to_str(&conn->remote, conn->remotestr, sizeof(conn->remotestr)); + connection_set_remote(cfg, conn, remote); fprintf(stderr, "%s: attempting to connect to %s\n", task->name, conn->remotestr); diff --git a/utils.h b/utils.h index 5a6f69e..41c71e5 100644 --- a/utils.h +++ b/utils.h @@ -52,6 +52,11 @@ struct uring_task; void socket_set_low_latency(struct cfg *cfg, int sfd); +void connection_set_local(struct cfg *cfg, struct connection *conn, int fd); + +void connection_set_remote(struct cfg *cfg, struct connection *conn, + struct sockaddr_in46 *remote); + void connect_any(struct cfg *cfg, struct uring_task *task, struct list_head *addrs, struct connection *conn, void (*callback)(struct cfg *, struct connection *, int res)); -- cgit v1.2.3