From d7dd578fc4e6da6e340c0bb524988272ca315d6e Mon Sep 17 00:00:00 2001 From: ngn Date: Wed, 7 Aug 2024 05:35:13 +0300 Subject: [PATCH] fix: TCP server calls --- include/error.h | 1 + locale/tr/LC_MESSAGES/libmp.po | 7 ++++++- src/ctx/serve.c | 5 +++-- src/error.c | 1 + src/mptp/mptp.c | 4 ++-- src/mptp/server.c | 8 +++++++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/error.h b/include/error.h index 75b2879..7a27aef 100644 --- a/include/error.h +++ b/include/error.h @@ -149,6 +149,7 @@ typedef enum lm_error { LM_ERR_ExtractRootChdirFail = 147, LM_ERR_ExtractOldChdirFail = 148, LM_ERR_MPTPAcceptFail = 149, + LM_ERR_MPTPListenFail = 150, } lm_error_t; typedef struct lm_error_desc { diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index d2c7aa0..59026b9 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-07 04:49+0300\n" +"POT-Creation-Date: 2024-08-07 05:32+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -635,3 +635,8 @@ msgstr "" #, c-format msgid "failed to accept the MPTP connection: %s" msgstr "" + +#: src/error.c:169 +#, c-format +msgid "failed to listen the MPTP socket: %s" +msgstr "" diff --git a/src/ctx/serve.c b/src/ctx/serve.c index d49a49d..dfa2b03 100644 --- a/src/ctx/serve.c +++ b/src/ctx/serve.c @@ -179,7 +179,7 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb if ((sock = lm_mptp_server_listen(host, port)) < 0) return false; - while ((c = lm_mptp_server_accept(sock, &saddr)) < 0) { + while ((c = lm_mptp_server_accept(sock, &saddr)) != -1) { struct lm_ctx_serve_thread_arg *arg = malloc(sizeof(struct lm_ctx_serve_thread_arg)); memcpy(&arg->addr, &saddr, sizeof(struct sockaddr)); @@ -193,5 +193,6 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads, lm_ctx_serve_callb } lm_thpool_stop(&tp); - return true; + // error set by mptp_server_accept + return false; } diff --git a/src/error.c b/src/error.c index 5965a10..1b86248 100644 --- a/src/error.c +++ b/src/error.c @@ -166,6 +166,7 @@ void lm_error_set(lm_error_t code, ...) { {.code = LM_ERR_ExtractRootChdirFail, .desc = _("failed to change directory to root during extraction") }, {.code = LM_ERR_ExtractOldChdirFail, .desc = _("failed to change directory back from root during extraction")}, {.code = LM_ERR_MPTPAcceptFail, .desc = _("failed to accept the MPTP connection: %s") }, + {.code = LM_ERR_MPTPListenFail, .desc = _("failed to listen the MPTP socket: %s") }, }; char *fmt = NULL; diff --git a/src/mptp/mptp.c b/src/mptp/mptp.c index 11e9499..ab44ed6 100644 --- a/src/mptp/mptp.c +++ b/src/mptp/mptp.c @@ -71,7 +71,7 @@ int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) { bzero(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_DGRAM; + hints.ai_socktype = AF_INET; if ((status = getaddrinfo(addr, NULL, &hints, &res)) < 0) { lm_error_set(LM_ERR_MPTPHostFail); @@ -108,7 +108,7 @@ int lm_mptp_socket(char *addr, uint16_t port, struct sockaddr *saddr) { return -1; } - if ((sock = socket(family, AF_INET, SOCK_STREAM)) < 0) { + if ((sock = socket(family, SOCK_STREAM, 0)) < 0) { lm_error_set(LM_ERR_MPTPSocketFail); return -1; } diff --git a/src/mptp/server.c b/src/mptp/server.c index 3d6d8de..0000af6 100644 --- a/src/mptp/server.c +++ b/src/mptp/server.c @@ -22,6 +22,12 @@ int lm_mptp_server_listen(char *addr, uint16_t port) { return -1; } + if(listen(sock, SOMAXCONN) < 0){ + lm_mptp_close(sock); + lm_error_set(LM_ERR_MPTPListenFail, strerror(errno)); + return -1; + } + return sock; } @@ -29,7 +35,7 @@ int lm_mptp_server_accept(int sock, struct sockaddr *addr){ socklen_t sl = sizeof(struct sockaddr); int s = -1; - if((s = accept(sock, addr, &sl) < 0)){ + if((s = accept(sock, addr, &sl)) < 0){ lm_error_set(LM_ERR_MPTPAcceptFail, strerror(errno)); s = -1; }