updated parseport to use strtoimax

strtoimax has an extra parameter endptr to store where the parsing was
terminated. endptr is used in parseport to check if the provided string
was valid.
This commit is contained in:
ahgamut 2021-07-10 10:24:34 +05:30
parent f043a400e2
commit fb1bd83e90
4 changed files with 20 additions and 11 deletions

View file

@ -53,7 +53,7 @@ int getaddrinfo(const char *name, const char *service,
if (!name && !service) return EAI_NONAME;
if (!name && (hints->ai_flags & AI_CANONNAME)) return EAI_BADFLAGS;
if (service) {
if (service && (port = parseport(service)) == -1) {
if (hints->ai_socktype == SOCK_STREAM)
strcpy(proto, "tcp");
else if (hints->ai_socktype == SOCK_DGRAM)
@ -62,9 +62,8 @@ int getaddrinfo(const char *name, const char *service,
strcpy(proto, "");
if ((port = LookupServicesByName(service, proto, sizeof(proto), NULL, 0,
NULL)) == -1) {
if ((port = parseport(service)) == -1) return EAI_NONAME;
}
NULL)) == -1)
return EAI_NONAME;
}
if (!(ai = newaddrinfo(port))) return EAI_MEMORY;
if (service) ai->ai_addr4->sin_port = htons(port);

View file

@ -67,7 +67,7 @@ static textwindows noinline char *GetNtProtocolsTxtPath(char *pathbuf,
* @param bufsize is the size of buf
* @param filepath is the location of the protocols file
* (if NULL, uses /etc/protocols)
* @returns 0 on success, -1 on error
* @return 0 on success, -1 on error
*
* @note aliases are not read from the file.
*/
@ -129,7 +129,7 @@ int LookupProtoByNumber(const int protonum, char *buf, size_t bufsize,
* @param bufsize is the size of buf
* @param filepath is the location of protocols file
* (if NULL, uses /etc/protocols)
* @returns -1 on error, or
* @return -1 on error, or
* positive protocol number
*
* @note aliases are read from file for comparison, but not returned.

View file

@ -72,7 +72,7 @@ static textwindows noinline char *GetNtServicesTxtPath(char *pathbuf,
* @param bufsize is the size of buf
* @param filepath is the location of the services file
* (if NULL, uses /etc/services)
* @returns 0 on success, -1 on error
* @return 0 on success, -1 on error
*
* @note aliases are not read from the file.
*/
@ -149,7 +149,7 @@ int LookupServicesByPort(const int servport, char *servproto,
* @param bufsize is the size of buf
* @param filepath is the location of services file
* (if NULL, uses /etc/services)
* @returns -1 on error, or
* @return -1 on error, or
* positive port number
*
* @note aliases are read from file for comparison, but not returned.

View file

@ -20,7 +20,17 @@
#include "libc/sock/sock.h"
#include "libc/sysv/errfuns.h"
int parseport(const char *service) {
int port = atoi(service);
return (0 <= port && port <= 65535) ? port : einval();
/* parses string to port number.
*
* @param service is a NULL-terminated string
* @return valid port number or einval()
*
* @see strtoimax
*/
int parseport(const char* service) {
char* end;
int port = strtoimax(service, &end, 0);
if (!service || end == service || *end != '\0' || port < 0 || port > 65535)
return einval();
return port;
}