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 ""
"Project-Id-Version: PACKAGE VERSION\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"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\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;
}
char host[strlen(addr)+1];
struct sockaddr saddr;
char *host = NULL;
lm_mptp_t packet;
uint16_t port;
int sock;
@ -118,7 +118,7 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads){
return false;
}
if ((sock = lm_mptp_server_listen(addr, port)) < 0)
if ((sock = lm_mptp_server_listen(host, port)) < 0)
return false;
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) {
char *save = NULL, *portc = NULL;
int portint = 0;
char portc[strlen(addr) + 1];
ssize_t hi = 0, pi = -1;
int portint = 0;
if ((host = strtok_r(addr, ":", &save)) == NULL) {
lm_error_set(LM_ERR_BadHost);
for (char *c = addr; *c != 0; c++) {
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;
}
if ((portc = strtok_r(NULL, ":", &save)) == NULL) {
*port = 0;
return true;
}
portint = atoi(portc);
if (portint <= 0 || portint > UINT16_MAX) {
lm_error_set(LM_ERR_BadPort);
if (hi <= 1) {
lm_error_set(LM_ERR_BadHost);
return false;
}