Skip to content

Commit e79ac08

Browse files
authored
Merge pull request #57 from openziti/tcp-half-close
terminate ziti connection on client errors
2 parents 542ae66 + 71a680e commit e79ac08

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

.github/workflows/bump.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- name: make build dir
3737
run: cmake -E make_directory ${{runner.workspace}}/build
3838
- name: configure cmake
39-
run: cmake -S ${{ github.workspace }} -B ${{runner.workspace}}/build
39+
run: cmake -DCMAKE_BUILD_TYPE=Debug -S ${{ github.workspace }} -B ${{runner.workspace}}/build
4040
- name: build
4141
run: cmake --build ${{runner.workspace}}/build --target bundle
4242
- uses: actions/upload-artifact@v2

deps/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include(FetchContent)
22

33
FetchContent_Declare(ziti-sdk-c
44
GIT_REPOSITORY https://github.com/openziti/ziti-sdk-c.git
5-
GIT_TAG 0.16.4
5+
GIT_TAG 0.16.6
66
)
77
set(ZITI_BUILD_TESTS off)
88
set(ZITI_BUILD_PROGRAMS off)

lib/tunnel_tcp.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ static err_t on_tcp_client_data(void *io_ctx, struct tcp_pcb *pcb, struct pbuf *
151151
}
152152

153153
static void on_tcp_client_err(void *io_ctx, err_t err) {
154+
struct io_ctx_s *io = io_ctx;
154155
// we initiated close and cleared arg err should be ERR_ABRT
155156
if (io_ctx == NULL) {
156-
ZITI_LOG(TRACE, "client finished err=%d", err);
157+
ZITI_LOG(TRACE, "client pcb(%p) finished err=%d", (*io->tnlr_io_ctx_p)->tcp, err);
157158
}
158159
else {
159-
// TODO handle better? At least close ziti and free context!
160-
ZITI_LOG(ERROR, "unhandled client err=%d", err);
160+
ZITI_LOG(ERROR, "client pcb(%p) err=%d, terminating connection", (*io->tnlr_io_ctx_p)->tcp, err);
161+
(*io->tnlr_io_ctx_p)->tnlr_ctx->opts.ziti_close(io->ziti_io_ctx);
161162
}
162163
}
163164

@@ -173,18 +174,20 @@ ssize_t tunneler_tcp_write(struct tcp_pcb *pcb, const void *data, size_t len) {
173174
}
174175
// avoid ERR_MEM.
175176
size_t sendlen = MIN(len, tcp_sndbuf(pcb));
177+
ZITI_LOG(TRACE, "pcb[%p] sendlen=%zd", pcb, sendlen);
178+
if (sendlen > 0) {
179+
err_t w_err = tcp_write(pcb, data, (u16_t) sendlen,
180+
TCP_WRITE_FLAG_COPY); // TODO hold data until client acks... via on_client_ack maybe? then we wouldn't need to copy here.
181+
if (w_err != ERR_OK) {
182+
ZITI_LOG(ERROR, "failed to tcp_write %d (%ld, %zd)", w_err, sendlen, len);
183+
return -1;
184+
}
176185

177-
err_t w_err = tcp_write(pcb, data, (u16_t)sendlen, TCP_WRITE_FLAG_COPY); // TODO hold data until client acks... via on_client_ack maybe? then we wouldn't need to copy here.
178-
if (w_err != ERR_OK) {
179-
ZITI_LOG(ERROR, "failed to tcp_write %d (%ld, %zd)", w_err, sendlen, len);
180-
return -1;
181-
}
182-
183-
if (tcp_output(pcb) != ERR_OK) {
184-
ZITI_LOG(ERROR, "failed to tcp_output");
185-
return -1;
186+
if (tcp_output(pcb) != ERR_OK) {
187+
ZITI_LOG(ERROR, "failed to tcp_output");
188+
return -1;
189+
}
186190
}
187-
188191
return sendlen;
189192
}
190193

lib/ziti_tunnel_cbs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ ssize_t on_ziti_data(ziti_connection conn, uint8_t *data, ssize_t len) {
3333
ZITI_LOG(TRACE, "got %zd bytes from ziti", len);
3434
if (ziti_io_ctx == NULL || ziti_io_ctx->tnlr_io_ctx == NULL) {
3535
ZITI_LOG(DEBUG, "null io_context - connection may have been closed already");
36-
return len;
36+
ziti_conn_set_data(conn, NULL);
37+
ziti_close(&conn);
38+
if (ziti_io_ctx) free(ziti_io_ctx);
39+
return UV_ECONNABORTED;
3740
}
3841
if (len > 0) {
3942
int accepted = ziti_tunneler_write(&ziti_io_ctx->tnlr_io_ctx, data, len);

0 commit comments

Comments
 (0)