Add protoent and netent (#209)

The implementations of the getproto* functions follow from the getserv*
functions: same static name allocation, same type of internal function
that opens a file to search, aliases are not written to the struct, same
type of error handling/returns.

This changes also fixes a getaddrinfo AI_PASSIVE memory error. When
getaddrinfo is passed name = NULL and AI_PASSIVE in hints->ai_flags, it was
setting the s_addr value to INADDR_ANY but *not* returning the addrinfo
pointer via *res = ai. This caused a free(NULL) memory error when the caller
tried to free res, because the caller expects res to be a valid pointer to a
struct addrinfo.

Our non-standard API parseport() has been updated 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:
Gautham 2021-07-11 01:06:35 +05:30 committed by GitHub
parent c002e4ba76
commit e99a4dcc8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 713 additions and 124 deletions

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