update: better way to handle parse_host function

This commit is contained in:
ngn 2024-07-13 16:48:17 +03:00
parent 4ea2d22519
commit e8485da6e6
3 changed files with 25 additions and 15 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-13 14:22+0300\n" "POT-Creation-Date: 2024-07-13 16:47+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View File

@ -101,8 +101,8 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads){
return false; return false;
} }
char host[strlen(addr)+1];
struct sockaddr saddr; struct sockaddr saddr;
char *host = NULL;
lm_mptp_t packet; lm_mptp_t packet;
uint16_t port; uint16_t port;
int sock; int sock;
@ -118,7 +118,7 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads){
return false; return false;
} }
if ((sock = lm_mptp_server_listen(addr, port)) < 0) if ((sock = lm_mptp_server_listen(host, port)) < 0)
return false; return false;
while (lm_mptp_server_recv(sock, &packet, &saddr)) { while (lm_mptp_server_recv(sock, &packet, &saddr)) {

View File

@ -52,22 +52,32 @@ bool contains(char *str, char s) {
} }
bool parse_host(char *addr, char *host, uint16_t *port) { bool parse_host(char *addr, char *host, uint16_t *port) {
char *save = NULL, *portc = NULL; char portc[strlen(addr) + 1];
int portint = 0; ssize_t hi = 0, pi = -1;
int portint = 0;
if ((host = strtok_r(addr, ":", &save)) == NULL) { for (char *c = addr; *c != 0; c++) {
lm_error_set(LM_ERR_BadHost); if (*c == ':') {
pi = 0;
continue;
}
if (pi < 0)
host[hi++] = *c;
else
portc[pi++] = *c;
}
host[hi] = 0;
portc[pi < 0 ? 0 : pi] = 0;
if ((portint = atoi(portc)) < 0 || portint > UINT16_MAX) {
lm_error_set(LM_ERR_BadPort);
return false; return false;
} }
if ((portc = strtok_r(NULL, ":", &save)) == NULL) { if (hi <= 1) {
*port = 0; lm_error_set(LM_ERR_BadHost);
return true;
}
portint = atoi(portc);
if (portint <= 0 || portint > UINT16_MAX) {
lm_error_set(LM_ERR_BadPort);
return false; return false;
} }