Skip to content

Commit

Permalink
Add protoent and netent (#209)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahgamut authored Jul 10, 2021
1 parent c002e4b commit e99a4dc
Show file tree
Hide file tree
Showing 16 changed files with 714 additions and 125 deletions.
25 changes: 25 additions & 0 deletions libc/dns/ent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ COSMOPOLITAN_C_START_

extern int h_errno;

struct netent {
char *n_name; /* official network name */
char **n_aliases; /* alias list */
int n_addrtype; /* net address type */
uint32_t n_net; /* network number */
};

struct protoent {
char *p_name; /* official protocol name */
char **p_aliases; /* alias list */
int p_proto; /* protocol number */
};

struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
Expand All @@ -23,6 +36,18 @@ struct servent {
char *s_proto; /* protocol to use */
};

struct netent *getnetent(void);
struct netent *getnetbyname(const char *);
struct netent *getnetbyaddr(uint32_t, int);
void setnetent(int);
void endnetent(void);

struct protoent *getprotoent(void);
struct protoent *getprotobyname(const char *);
struct protoent *getprotobynumber(int);
void setprotoent(int);
void endprotoent(void);

struct hostent *gethostent(void);
struct hostent *gethostbyname(const char *);
struct hostent *gethostbyaddr(const void *, socklen_t, int);
Expand Down
20 changes: 19 additions & 1 deletion libc/dns/getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/dns/resolvconf.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/conv.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/consts/sock.h"
#include "libc/sysv/errfuns.h"

/**
Expand All @@ -43,11 +46,25 @@ int getaddrinfo(const char *name, const char *service,
const struct addrinfo *hints, struct addrinfo **res) {
int rc, port;
const char *canon;
size_t protolen;
struct addrinfo *ai;
char proto[32];
port = 0;

if (!name && !service) return EAI_NONAME;
if (service && (port = parseport(service)) == -1) return EAI_NONAME;
if (!name && (hints->ai_flags & AI_CANONNAME)) return EAI_BADFLAGS;
if (service && (port = parseport(service)) == -1) {
if (hints->ai_socktype == SOCK_STREAM)
strcpy(proto, "tcp");
else if (hints->ai_socktype == SOCK_DGRAM)
strcpy(proto, "udp");
else /* ai_socktype == 0 */
strcpy(proto, "");

if ((port = LookupServicesByName(service, proto, sizeof(proto), NULL, 0,
NULL)) == -1)
return EAI_NONAME;
}
if (!(ai = newaddrinfo(port))) return EAI_MEMORY;
if (service) ai->ai_addr4->sin_port = htons(port);
if (hints) {
Expand All @@ -59,6 +76,7 @@ int getaddrinfo(const char *name, const char *service,
(hints && (hints->ai_flags & AI_PASSIVE) == AI_PASSIVE)
? INADDR_ANY
: INADDR_LOOPBACK;
*res = ai;
return 0;
}
if (inet_pton(AF_INET, name, &ai->ai_addr4->sin_addr.s_addr) == 1) {
Expand Down
9 changes: 6 additions & 3 deletions libc/dns/getnameinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/dns/resolvconf.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *name,
socklen_t namelen, char *service, socklen_t servicelen,
int flags) {
char *p, rdomain[1 + sizeof "255.255.255.255.in-addr.arpa"];
char info[512];
char info[NI_MAXHOST + 1];
int rc, port;
uint8_t *ip;
unsigned int valid_flags;
Expand Down Expand Up @@ -102,8 +103,10 @@ int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *name,
port = ntohs(((struct sockaddr_in *)addr)->sin_port);
info[0] = '\0';
if (service != NULL && servicelen != 0) {
itoa(port, info, 10);
/* TODO: reverse lookup on /etc/services to get name of service */
if ((flags & NI_NUMERICSERV) ||
LookupServicesByPort(port, ((flags & NI_DGRAM) ? "udp" : "tcp"), 4,
info, sizeof(info), NULL) == -1)
itoa(port, info, 10);
if (strlen(info) + 1 > servicelen) return EAI_OVERFLOW;
strcpy(service, info);
}
Expand Down
46 changes: 46 additions & 0 deletions libc/dns/getprotobyname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ This is free and unencumbered software released into the public domain. │
│ │
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
│ distribute this software, either in source code form or as a compiled │
│ binary, for any purpose, commercial or non-commercial, and by any │
│ means. │
│ │
│ In jurisdictions that recognize copyright laws, the author or authors │
│ of this software dedicate any and all copyright interest in the │
│ software to the public domain. We make this dedication for the benefit │
│ of the public at large and to the detriment of our heirs and │
│ successors. We intend this dedication to be an overt act of │
│ relinquishment in perpetuity of all present and future rights to this │
│ software under copyright law. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
│ OTHER DEALINGS IN THE SOFTWARE. │
│ │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dns/ent.h"
#include "libc/dns/prototxt.h"

struct protoent *getprotobyname(const char *name) {
static struct protoent *ptr0, pe0;
static char p_name[DNS_NAME_MAX + 1];

if (!ptr0) {
pe0.p_name = p_name;
if (!(pe0.p_aliases = calloc(1, sizeof(char *)))) return NULL;
pe0.p_proto = -1;
ptr0 = &pe0;
}

ptr0->p_proto = LookupProtoByName(name, ptr0->p_name, DNS_NAME_MAX, NULL);
if (ptr0->p_proto == -1) return NULL;

return ptr0;
}
47 changes: 47 additions & 0 deletions libc/dns/getprotobynumber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ This is free and unencumbered software released into the public domain. │
│ │
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
│ distribute this software, either in source code form or as a compiled │
│ binary, for any purpose, commercial or non-commercial, and by any │
│ means. │
│ │
│ In jurisdictions that recognize copyright laws, the author or authors │
│ of this software dedicate any and all copyright interest in the │
│ software to the public domain. We make this dedication for the benefit │
│ of the public at large and to the detriment of our heirs and │
│ successors. We intend this dedication to be an overt act of │
│ relinquishment in perpetuity of all present and future rights to this │
│ software under copyright law. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
│ OTHER DEALINGS IN THE SOFTWARE. │
│ │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dns/ent.h"
#include "libc/dns/prototxt.h"

struct protoent *getprotobynumber(int proto) {
static struct protoent *ptr1, pe1;
static char p_name[DNS_NAME_MAX + 1];

if (!ptr1) {
pe1.p_name = p_name;
if (!(pe1.p_aliases = calloc(1, sizeof(char *)))) return NULL;
pe1.p_proto = -1;
ptr1 = &pe1;
}

if (LookupProtoByNumber(proto, ptr1->p_name, DNS_NAME_MAX, NULL) == -1)
return NULL;

ptr1->p_proto = proto;
return ptr1;
}
23 changes: 11 additions & 12 deletions libc/dns/getservbyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,32 @@
#include "libc/dns/ent.h"
#include "libc/dns/servicestxt.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

struct servent *getservbyname(const char *name, const char *proto) {
static struct servent *ptr0, se0;
static char s_name[DNS_NAME_MAX + 1];
char *localproto = proto;
static char localproto[DNS_NAME_MAX + 1];
int p;

if (!ptr0) {
se0.s_name = s_name;
if (!(se0.s_aliases = calloc(1, sizeof(char *)))) return NULL;
se0.s_port = 0;
se0.s_proto = NULL;
se0.s_proto = localproto;
ptr0 = &se0;
}

p = LookupServicesByName(name, &localproto, ptr0->s_name, DNS_NAME_MAX, NULL);
if (p == -1) {
// localproto got alloc'd during the lookup?
if (!proto && localproto != proto) free(localproto);
return NULL;
}
if (proto) {
if (!memccpy(localproto, proto, '\0', DNS_NAME_MAX)) return NULL;
} else
strcpy(localproto, "");

ptr0->s_port = p;
if (ptr0->s_proto) free(ptr0->s_proto);
ptr0->s_proto = strdup(localproto);
p = LookupServicesByName(name, ptr0->s_proto, DNS_NAME_MAX, ptr0->s_name,
DNS_NAME_MAX, NULL);
if (p == -1) return NULL;

if (!proto && localproto != proto) free(localproto);
ptr0->s_port = htons(p);

return ptr0;
}
23 changes: 11 additions & 12 deletions libc/dns/getservbyport.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,31 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dns/ent.h"
#include "libc/dns/servicestxt.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

struct servent *getservbyport(int port, const char *proto) {
static struct servent *ptr1, se1;
static char s_name[DNS_NAME_MAX + 1];
char *localproto = proto;
static char localproto[DNS_NAME_MAX + 1];

if (!ptr1) {
se1.s_name = s_name;
if (!(se1.s_aliases = calloc(1, sizeof(char *)))) return NULL;
se1.s_port = 0;
se1.s_proto = NULL;
se1.s_proto = localproto;
ptr1 = &se1;
}

if (LookupServicesByPort(port, &localproto, ptr1->s_name, DNS_NAME_MAX,
NULL) == -1) {
// localproto got alloc'd during the lookup?
if (!proto && localproto != proto) free(localproto);
if (proto) {
if (!memccpy(localproto, proto, '\0', DNS_NAME_MAX)) return NULL;
} else
strcpy(localproto, "");

if (LookupServicesByPort(ntohs(port), ptr1->s_proto, DNS_NAME_MAX,
ptr1->s_name, DNS_NAME_MAX, NULL) == -1)
return NULL;
}

ptr1->s_port = port;
if (ptr1->s_proto) free(ptr1->s_proto);
ptr1->s_proto = strdup(localproto);

if (!proto && localproto != proto) free(localproto);

return ptr1;
}
46 changes: 46 additions & 0 deletions libc/dns/netent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ This is free and unencumbered software released into the public domain. │
│ │
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
│ distribute this software, either in source code form or as a compiled │
│ binary, for any purpose, commercial or non-commercial, and by any │
│ means. │
│ │
│ In jurisdictions that recognize copyright laws, the author or authors │
│ of this software dedicate any and all copyright interest in the │
│ software to the public domain. We make this dedication for the benefit │
│ of the public at large and to the detriment of our heirs and │
│ successors. We intend this dedication to be an overt act of │
│ relinquishment in perpetuity of all present and future rights to this │
│ software under copyright law. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
│ OTHER DEALINGS IN THE SOFTWARE. │
│ │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dns/ent.h"

struct netent *getnetent(void) {
return NULL;
}

struct netent *getnetbyname(const char *name) {
return NULL;
}

struct netent *getnetbyaddr(uint32_t net, int type) {
return NULL;
}

void setnetent(int stayopen) {
}

void endnetent(void) {
}
38 changes: 38 additions & 0 deletions libc/dns/protoent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ This is free and unencumbered software released into the public domain. │
│ │
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
│ distribute this software, either in source code form or as a compiled │
│ binary, for any purpose, commercial or non-commercial, and by any │
│ means. │
│ │
│ In jurisdictions that recognize copyright laws, the author or authors │
│ of this software dedicate any and all copyright interest in the │
│ software to the public domain. We make this dedication for the benefit │
│ of the public at large and to the detriment of our heirs and │
│ successors. We intend this dedication to be an overt act of │
│ relinquishment in perpetuity of all present and future rights to this │
│ software under copyright law. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
│ OTHER DEALINGS IN THE SOFTWARE. │
│ │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/dns/ent.h"

struct protoent *getprotoent(void) {
return NULL;
}

void setprotoent(int stayopen) {
}

void endprotoent(void) {
}
Loading

0 comments on commit e99a4dc

Please sign in to comment.