fix: TCP server calls

This commit is contained in:
ngn 2024-08-07 05:35:13 +03:00
parent edc732ac69
commit d7dd578fc4
6 changed files with 20 additions and 6 deletions

View File

@ -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 {

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}