From e99a4dcc8ced76f7d01e27f743fcaa119701bdfa Mon Sep 17 00:00:00 2001 From: Gautham <41098605+ahgamut@users.noreply.github.com> Date: Sun, 11 Jul 2021 01:06:35 +0530 Subject: [PATCH] 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. --- libc/dns/ent.h | 25 ++++ libc/dns/getaddrinfo.c | 20 +++- libc/dns/getnameinfo.c | 9 +- libc/dns/getprotobyname.c | 46 ++++++++ libc/dns/getprotobynumber.c | 47 ++++++++ libc/dns/getservbyname.c | 23 ++-- libc/dns/getservbyport.c | 23 ++-- libc/dns/netent.c | 46 ++++++++ libc/dns/protoent.c | 38 ++++++ libc/dns/prototxt.c | 195 +++++++++++++++++++++++++++++++ libc/dns/prototxt.h | 18 +++ libc/dns/servicestxt.c | 83 +++++++------ libc/dns/servicestxt.h | 8 +- libc/sock/parseport.c | 16 ++- test/libc/dns/prototxt_test.c | 83 +++++++++++++ test/libc/dns/servicestxt_test.c | 157 ++++++++++++++++--------- 16 files changed, 713 insertions(+), 124 deletions(-) create mode 100644 libc/dns/getprotobyname.c create mode 100644 libc/dns/getprotobynumber.c create mode 100644 libc/dns/netent.c create mode 100644 libc/dns/protoent.c create mode 100644 libc/dns/prototxt.c create mode 100644 libc/dns/prototxt.h create mode 100644 test/libc/dns/prototxt_test.c diff --git a/libc/dns/ent.h b/libc/dns/ent.h index b37ab46fe..930ce0f31 100644 --- a/libc/dns/ent.h +++ b/libc/dns/ent.h @@ -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 */ @@ -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); diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c index 8da6160b2..0d6865b22 100644 --- a/libc/dns/getaddrinfo.c +++ b/libc/dns/getaddrinfo.c @@ -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" /** @@ -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) { @@ -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) { diff --git a/libc/dns/getnameinfo.c b/libc/dns/getnameinfo.c index 3cbbc860c..10a62f5ae 100644 --- a/libc/dns/getnameinfo.c +++ b/libc/dns/getnameinfo.c @@ -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" @@ -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; @@ -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); } diff --git a/libc/dns/getprotobyname.c b/libc/dns/getprotobyname.c new file mode 100644 index 000000000..5a7099b2f --- /dev/null +++ b/libc/dns/getprotobyname.c @@ -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; +} diff --git a/libc/dns/getprotobynumber.c b/libc/dns/getprotobynumber.c new file mode 100644 index 000000000..800d3efab --- /dev/null +++ b/libc/dns/getprotobynumber.c @@ -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; +} diff --git a/libc/dns/getservbyname.c b/libc/dns/getservbyname.c index dbda636c4..4c20cc43a 100644 --- a/libc/dns/getservbyname.c +++ b/libc/dns/getservbyname.c @@ -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; } diff --git a/libc/dns/getservbyport.c b/libc/dns/getservbyport.c index b269e6cf5..891766b7c 100644 --- a/libc/dns/getservbyport.c +++ b/libc/dns/getservbyport.c @@ -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; } diff --git a/libc/dns/netent.c b/libc/dns/netent.c new file mode 100644 index 000000000..abca500f0 --- /dev/null +++ b/libc/dns/netent.c @@ -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) { +} diff --git a/libc/dns/protoent.c b/libc/dns/protoent.c new file mode 100644 index 000000000..bcc35c254 --- /dev/null +++ b/libc/dns/protoent.c @@ -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) { +} diff --git a/libc/dns/prototxt.c b/libc/dns/prototxt.c new file mode 100644 index 000000000..e974ab442 --- /dev/null +++ b/libc/dns/prototxt.c @@ -0,0 +1,195 @@ +/*-*- 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/prototxt.h" + +#include "libc/bits/safemacros.internal.h" +#include "libc/dce.h" +#include "libc/errno.h" +#include "libc/fmt/conv.h" +#include "libc/fmt/fmt.h" +#include "libc/macros.internal.h" +#include "libc/mem/mem.h" +#include "libc/nt/systeminfo.h" +#include "libc/runtime/runtime.h" +#include "libc/str/str.h" + +static textwindows noinline char *GetNtProtocolsTxtPath(char *pathbuf, + uint32_t size) { + /* protocol, not plural */ + const char *const kWinHostsPath = "\\drivers\\etc\\protocol"; + uint32_t len = GetSystemDirectoryA(&pathbuf[0], size); + if (len && len + strlen(kWinHostsPath) + 1 < size) { + if (pathbuf[len] == '\\') pathbuf[len--] = '\0'; + memcpy(&pathbuf[len], kWinHostsPath, strlen(kWinHostsPath) + 1); + return &pathbuf[0]; + } else { + return NULL; + } +} + +/** + * Opens and searches /etc/protocols to find name for a given number. + * + * format of /etc/protocols is like this: + * + * # comment + * # NAME PROTOCOL ALIASES + * ip 0 IP + * icmp 1 ICMP + * + * @param protonum is the protocol number + * @param buf is a buffer to store the official name of the protocol + * @param bufsize is the size of buf + * @param filepath is the location of the protocols file + * (if NULL, uses /etc/protocols) + * @return 0 on success, -1 on error + * + * @note aliases are not read from the file. + */ +int LookupProtoByNumber(const int protonum, char *buf, size_t bufsize, + const char *filepath) { + FILE *f; + char *line; + char pathbuf[PATH_MAX]; + const char *path; + size_t linesize; + int found; + char *name, *number, *comment, *tok; + + if (!(path = filepath)) { + path = "/etc/protocols"; + if (IsWindows()) { + path = + firstnonnull(GetNtProtocolsTxtPath(pathbuf, ARRAYLEN(pathbuf)), path); + } + } + + if (bufsize == 0 || !(f = fopen(path, "r"))) { + return -1; + } + line = NULL; + linesize = 0; + found = 0; + + while (found == 0 && (getline(&line, &linesize, f)) != -1) { + if ((comment = strchr(line, '#'))) *comment = '\0'; + name = strtok_r(line, " \t\r\n\v", &tok); + number = strtok_r(NULL, " \t\r\n\v", &tok); + if (name && number && protonum == atoi(number)) { + if (!memccpy(buf, name, '\0', bufsize)) { + strcpy(buf, ""); + break; + } + found = 1; + } + } + free(line); + + if (ferror(f)) { + errno = ferror(f); + return -1; + } + fclose(f); + + if (!found) return -1; + + return 0; +} + +/** + * Opens and searches /etc/protocols to find number for a given name. + * + * @param protoname is a NULL-terminated string + * @param buf is a buffer to store the official name of the protocol + * @param bufsize is the size of buf + * @param filepath is the location of protocols file + * (if NULL, uses /etc/protocols) + * @return -1 on error, or + * positive protocol number + * + * @note aliases are read from file for comparison, but not returned. + * @see LookupProtoByNumber + */ +int LookupProtoByName(const char *protoname, char *buf, size_t bufsize, + const char *filepath) { + FILE *f; + char *line; + char pathbuf[PATH_MAX]; + const char *path; + size_t linesize; + int found, result; + char *name, *number, *alias, *comment, *tok; + + if (!(path = filepath)) { + path = "/etc/protocols"; + if (IsWindows()) { + path = + firstnonnull(GetNtProtocolsTxtPath(pathbuf, ARRAYLEN(pathbuf)), path); + } + } + + if (bufsize == 0 || !(f = fopen(path, "r"))) { + return -1; + } + line = NULL; + linesize = 0; + found = 0; + result = -1; + + while (found == 0 && (getline(&line, &linesize, f)) != -1) { + if ((comment = strchr(line, '#'))) *comment = '\0'; + name = strtok_r(line, " \t\r\n\v", &tok); + number = strtok_r(NULL, "/ \t\r\n\v", &tok); + if (name && number) { + alias = name; + while (alias && strcasecmp(alias, protoname) != 0) + alias = strtok_r(NULL, " \t\r\n\v", &tok); + + if (alias) /* alias matched with protoname */ + { + if (!memccpy(buf, name, '\0', bufsize)) { + strcpy(buf, ""); + break; + } + result = atoi(number); + found = 1; + } + } + } + free(line); + + if (ferror(f)) { + errno = ferror(f); + return -1; + } + fclose(f); + + if (!found) return -1; + + return result; +} diff --git a/libc/dns/prototxt.h b/libc/dns/prototxt.h new file mode 100644 index 000000000..0b6d6f9ba --- /dev/null +++ b/libc/dns/prototxt.h @@ -0,0 +1,18 @@ +#ifndef COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ +#define COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ +#include "libc/sock/sock.h" +#include "libc/stdio/stdio.h" + +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +int LookupProtoByNumber(const int, char *, size_t, const char *) + paramsnonnull((2)); +int LookupProtoByName(const char *, char *, size_t, const char *) + paramsnonnull((1, 2)); + +/* TODO: implement like struct HostsTxt? */ + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ */ diff --git a/libc/dns/servicestxt.c b/libc/dns/servicestxt.c index f7d688fcd..24742066a 100644 --- a/libc/dns/servicestxt.c +++ b/libc/dns/servicestxt.c @@ -56,25 +56,29 @@ static textwindows noinline char *GetNtServicesTxtPath(char *pathbuf, * * format of /etc/services is like this: * - * # comment - * # NAME PORT/PROTOCOL ALIASES + * # comment + * # NAME PORT/PROTOCOL ALIASES + * ftp 21/tcp + * fsp 21/udp fspd + * ssh 22/tcp * - * ftp 21/tcp - * fsp 21/udp fspd - * ssh 22/tcp - * - * @param servport is the port number (in network byte order) - * @param servproto is a pointer to a string (*servproto can be NULL) + * @param servport is the port number + * @param servproto is a NULL-terminated string (eg "tcp", "udp") + * (if servproto is an empty string, + * if is filled with the first matching + * protocol) + * @param servprotolen the size of servproto * @param buf is a buffer to store the official name of the service * @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. */ -int LookupServicesByPort(const int servport, char **servproto, char *buf, - size_t bufsize, const char *filepath) { +int LookupServicesByPort(const int servport, char *servproto, + size_t servprotolen, char *buf, size_t bufsize, + const char *filepath) { FILE *f; char *line; char pathbuf[PATH_MAX]; @@ -91,25 +95,29 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf, } } - if (bufsize == 0 || !(f = fopen(path, "r"))) { + if (servprotolen == 0 || bufsize == 0 || !(f = fopen(path, "r"))) { return -1; } line = NULL; linesize = 0; found = 0; + strcpy(buf, ""); while (found == 0 && (getline(&line, &linesize, f)) != -1) { if ((comment = strchr(line, '#'))) *comment = '\0'; name = strtok_r(line, " \t\r\n\v", &tok); port = strtok_r(NULL, "/ \t\r\n\v", &tok); proto = strtok_r(NULL, " \t\r\n\v", &tok); - if (name && port && proto && servport == htons(atoi(port))) { - if (!servproto[0]) { - servproto[0] = strdup(proto); - strncpy(buf, name, bufsize); - found = 1; - } else if (strcasecmp(proto, servproto[0]) == 0) { - strncpy(buf, name, bufsize); + if (name && port && proto && servport == atoi(port)) { + if (!servproto[0] || strncasecmp(proto, servproto, servprotolen) == 0) { + if (!servproto[0] && !memccpy(servproto, proto, '\0', servprotolen)) { + strcpy(servproto, ""); + break; + } + if (!memccpy(buf, name, '\0', bufsize)) { + strcpy(buf, ""); + break; + } found = 1; } } @@ -131,19 +139,25 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf, * Opens and searches /etc/services to find port for a given name. * * @param servname is a NULL-terminated string - * @param servproto is a pointer to a string (*servproto can be NULL) + * @param servproto is a NULL-terminated string (eg "tcp", "udp") + * (if servproto is an empty string, + * if is filled with the first matching + * protocol) + * @param servprotolen the size of servproto * @param buf is a buffer to store the official name of the service + * (if NULL, the official name is not stored) * @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 - * positive port number (in network byte order) + * @return -1 on error, or + * positive port number * * @note aliases are read from file for comparison, but not returned. * @see LookupServicesByPort */ -int LookupServicesByName(const char *servname, char **servproto, char *buf, - size_t bufsize, const char *filepath) { +int LookupServicesByName(const char *servname, char *servproto, + size_t servprotolen, char *buf, size_t bufsize, + const char *filepath) { FILE *f; char *line; char pathbuf[PATH_MAX]; @@ -160,13 +174,14 @@ int LookupServicesByName(const char *servname, char **servproto, char *buf, } } - if (bufsize == 0 || !(f = fopen(path, "r"))) { + if (servprotolen == 0 || !(f = fopen(path, "r"))) { return -1; } line = NULL; linesize = 0; found = 0; result = -1; + if (buf && bufsize != 0) strcpy(buf, ""); while (found == 0 && (getline(&line, &linesize, f)) != -1) { if ((comment = strchr(line, '#'))) *comment = '\0'; @@ -180,14 +195,16 @@ int LookupServicesByName(const char *servname, char **servproto, char *buf, if (alias) /* alias matched with servname */ { - if (!servproto[0]) { - servproto[0] = strdup(proto); - result = htons(atoi(port)); - strncpy(buf, name, bufsize); - found = 1; - } else if (strcasecmp(proto, servproto[0]) == 0) { - result = htons(atoi(port)); - strncpy(buf, name, bufsize); + if (!servproto[0] || strncasecmp(proto, servproto, servprotolen) == 0) { + if (!servproto[0] && !memccpy(servproto, proto, '\0', servprotolen)) { + strcpy(servproto, ""); + break; + } + if (buf && bufsize != 0 && !memccpy(buf, name, '\0', bufsize)) { + strcpy(buf, ""); + break; + } + result = atoi(port); found = 1; } } diff --git a/libc/dns/servicestxt.h b/libc/dns/servicestxt.h index 3a718fbb0..28e69eb37 100644 --- a/libc/dns/servicestxt.h +++ b/libc/dns/servicestxt.h @@ -6,10 +6,10 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -int LookupServicesByPort(const int, char **, char *, size_t, const char *) - paramsnonnull((2, 3)); -int LookupServicesByName(const char *, char **, char *, size_t, const char *) - paramsnonnull((1, 2, 3)); +int LookupServicesByPort(const int, char *, size_t, char *, size_t, + const char *) paramsnonnull((2, 4)); +int LookupServicesByName(const char *, char *, size_t, char *, size_t, + const char *) paramsnonnull((1, 2)); /* TODO: implement like struct HostsTxt? */ diff --git a/libc/sock/parseport.c b/libc/sock/parseport.c index 0a215b747..4b9d1b65f 100644 --- a/libc/sock/parseport.c +++ b/libc/sock/parseport.c @@ -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; } diff --git a/test/libc/dns/prototxt_test.c b/test/libc/dns/prototxt_test.c new file mode 100644 index 000000000..d3c457926 --- /dev/null +++ b/test/libc/dns/prototxt_test.c @@ -0,0 +1,83 @@ +/*-*- 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/prototxt.h" + +#include "libc/calls/calls.h" +#include "libc/dns/dns.h" +#include "libc/dns/ent.h" +#include "libc/testlib/testlib.h" + +char testlib_enable_tmp_setup_teardown; + +void SetUp() { + int fd; + const char* sample = "\ +# skip comment string\n\ +rspf 73 RSPF CPHB \n\ +ggp 3 GGP "; + + ASSERT_NE(-1, (fd = creat("protocols", 0755))); + ASSERT_NE(-1, write(fd, sample, strlen(sample))); + ASSERT_NE(-1, close(fd)); +} + +TEST(LookupProtoByNumber, GetNameWhenNumberCorrect) { + char name[16]; /* sample has only names of length 3-4 */ + strcpy(name, ""); + + ASSERT_EQ(-1, /*non-existent number */ + LookupProtoByNumber(24, name, sizeof(name), "protocols")); + + ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */ + LookupProtoByNumber(73, name, 1, "protocols")); + ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */ + + ASSERT_EQ(0, /* works with valid number */ + LookupProtoByNumber(73, name, sizeof(name), "protocols")); + ASSERT_STREQ(name, "rspf"); /* official name written */ +} + +TEST(LookupProtoByName, GetNumberWhenNameOrAlias) { + char name[16]; /* sample has only names of length 3-4 */ + strcpy(name, ""); + + ASSERT_EQ(-1, /* non-existent name or alias */ + LookupProtoByName("tcp", name, sizeof(name), "protocols")); + + ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */ + LookupProtoByName("ggp", name, 1, "protocols")); + ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */ + + ASSERT_EQ(3, /* works with valid name */ + LookupProtoByName("ggp", name, sizeof(name), "protocols")); + ASSERT_STREQ(name, "ggp"); + + ASSERT_EQ(73, /* works with valid alias */ + LookupProtoByName("CPHB", name, sizeof(name), "protocols")); + ASSERT_STREQ(name, "rspf"); /* official name written */ +} diff --git a/test/libc/dns/servicestxt_test.c b/test/libc/dns/servicestxt_test.c index 2f1875de1..0be5d810e 100644 --- a/test/libc/dns/servicestxt_test.c +++ b/test/libc/dns/servicestxt_test.c @@ -27,9 +27,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/dns/servicestxt.h" +#include "libc/calls/calls.h" #include "libc/dns/dns.h" #include "libc/dns/ent.h" -#include "libc/calls/calls.h" #include "libc/testlib/testlib.h" char testlib_enable_tmp_setup_teardown; @@ -48,88 +48,133 @@ ssh 22/tcp # SSH Remote Login Protocol"; } TEST(LookupServicesByPort, GetNameWhenPortCorrect) { - char name[16]; /* sample has only names of length 3 */ + char name[8]; /* service names are of length 3 */ + char eitherproto[8]; /* protocol names are of length 3 */ char proto1[] = "tcp"; char proto2[] = "udp"; - char* localproto[1]; + char* localproto; + strcpy(eitherproto, ""); + strcpy(name, ""); - localproto[0] = NULL; - ASSERT_EQ( - -1, /* non existent port */ - LookupServicesByPort(965, localproto, name, sizeof(name), "services")); - ASSERT_EQ(NULL, localproto[0]); + localproto = eitherproto; + ASSERT_EQ(-1, /* non existent port */ + LookupServicesByPort(965, localproto, sizeof(eitherproto), name, + sizeof(name), "services")); + ASSERT_EQ('\0', localproto[0]); - ASSERT_EQ( - -1, /* port not in network byte order */ - LookupServicesByPort(22, localproto, name, sizeof(name), "services")); - ASSERT_EQ(NULL, localproto[0]); + localproto = eitherproto; + ASSERT_EQ(-1, /* port in network byte order */ + LookupServicesByPort(htons(22), localproto, sizeof(eitherproto), + name, sizeof(name), "services")); + ASSERT_EQ('\0', localproto[0]); - localproto[0] = proto2; + localproto = proto2; ASSERT_EQ(-1, /* port ok but wrong protocol */ - LookupServicesByPort(htons(22), localproto, name, sizeof(name), + LookupServicesByPort(22, localproto, sizeof(proto2), name, + sizeof(name), "services")); + ASSERT_STREQ(proto2, "udp"); + + localproto = proto1; + ASSERT_EQ( + -1, /* protocol is non-NULL/length must be nonzero */ + LookupServicesByPort(22, localproto, 0, name, sizeof(name), "services")); + ASSERT_STREQ(proto1, "tcp"); + + localproto = proto1; + ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */ + LookupServicesByPort(22, localproto, sizeof(proto1), name, 1, "services")); - ASSERT_EQ(localproto[0], proto2); + ASSERT_STREQ(proto1, "tcp"); + ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */ - localproto[0] = proto1; - ASSERT_EQ(0, LookupServicesByPort(htons(22), localproto, name, sizeof(name), - "services")); + localproto = eitherproto; + ASSERT_EQ( + -1, /* sizeof(proto) insufficient, memccpy failure */ + LookupServicesByPort(22, localproto, 1, name, sizeof(name), "services")); + ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */ + + localproto = proto1; + ASSERT_EQ(0, LookupServicesByPort(22, localproto, sizeof(proto1), name, + sizeof(name), "services")); ASSERT_STREQ(name, "ssh"); - ASSERT_EQ(localproto[0], proto1); + ASSERT_STREQ(proto1, "tcp"); - localproto[0] = proto2; - ASSERT_EQ(0, LookupServicesByPort(htons(19), localproto, name, + localproto = proto2; + ASSERT_EQ(0, LookupServicesByPort(19, localproto, sizeof(proto2), name, sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); - ASSERT_EQ(localproto[0], proto2); + ASSERT_STREQ(proto2, "udp"); - localproto[0] = NULL; + localproto = eitherproto; ASSERT_EQ(0, /* pick first matching protocol */ - LookupServicesByPort(htons(19), localproto, name, sizeof(name), - "services")); + LookupServicesByPort(19, localproto, sizeof(eitherproto), name, + sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); - ASSERT_NE(NULL, localproto[0]); /* got alloc'd during the call */ - ASSERT_STREQ(localproto[0], "tcp"); - free(localproto[0]); + ASSERT_NE('\0', localproto[0]); /* buffer filled during the call */ + ASSERT_STREQ(eitherproto, "tcp"); } TEST(LookupServicesByName, GetPortWhenNameOrAlias) { - char name[16]; /* sample has only names of length 3 */ + char name[8]; /* service names are of length 3 */ + char eitherproto[8]; /* protocol names are of length 3 */ char proto1[] = "tcp"; char proto2[] = "udp"; - char* localproto[1]; + char* localproto; + strcpy(eitherproto, ""); + strcpy(name, ""); - localproto[0] = NULL; - ASSERT_EQ( - -1, /* non-existent name */ - LookupServicesByName("http", localproto, name, sizeof(name), "services")); - ASSERT_EQ(NULL, localproto[0]); + localproto = eitherproto; + ASSERT_EQ(-1, /* non-existent name */ + LookupServicesByName("http", localproto, sizeof(eitherproto), name, + sizeof(name), "services")); + ASSERT_EQ('\0', localproto[0]); - localproto[0] = proto2; - ASSERT_EQ( - -1, /* name exists but wrong protocol */ - LookupServicesByName("ssh", localproto, name, sizeof(name), "services")); - ASSERT_EQ(localproto[0], proto2); + localproto = proto2; + ASSERT_EQ(-1, /* name exists but wrong protocol */ + LookupServicesByName("ssh", localproto, sizeof(proto2), name, + sizeof(name), "services")); + ASSERT_STREQ(proto2, "udp"); - localproto[0] = proto1; - ASSERT_EQ( - htons(22), /* in network byte order */ - LookupServicesByName("ssh", localproto, name, sizeof(name), "services")); + localproto = proto2; + ASSERT_EQ(-1, /* protocol is non-NULL/length must be nonzero */ + LookupServicesByName("ssh", localproto, 0, name, sizeof(name), + "services")); + ASSERT_STREQ(proto2, "udp"); + + localproto = proto1; + ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */ + LookupServicesByName("ssh", localproto, sizeof(proto1), name, 1, + "services")); + ASSERT_STREQ(proto1, "tcp"); + ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */ + + localproto = eitherproto; + ASSERT_EQ(-1, /* sizeof(proto) insufficient, memccpy failure */ + LookupServicesByName("ssh", localproto, 1, name, sizeof(name), + "services")); + ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */ + + localproto = proto1; + ASSERT_EQ(22, LookupServicesByName("ssh", localproto, sizeof(proto1), name, + sizeof(name), "services")); ASSERT_STREQ(name, "ssh"); /* official name written to buffer */ - ASSERT_EQ(localproto[0], proto1); + ASSERT_STREQ(proto1, "tcp"); - localproto[0] = proto2; - ASSERT_EQ(htons(19), /* works if alias provided */ - LookupServicesByName("ttytst", localproto, name, sizeof(name), - "services")); + localproto = proto2; + ASSERT_EQ(19, /* works if alias provided */ + LookupServicesByName("ttytst", localproto, sizeof(proto2), name, + sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); /* official name written to buffer */ - ASSERT_EQ(localproto[0], proto2); - localproto[0] = NULL; - ASSERT_EQ(htons(19), /* pick first matching protocol */ - LookupServicesByName("source", localproto, name, sizeof(name), + localproto = proto2; + ASSERT_EQ(19, /* can get port returned without official name */ + LookupServicesByName("ttytst", localproto, sizeof(proto2), NULL, 0, "services")); + + localproto = eitherproto; + ASSERT_EQ(19, /* pick first matching protocol */ + LookupServicesByName("source", localproto, sizeof(eitherproto), + name, sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); - ASSERT_NE(NULL, localproto[0]); /* got alloc'd during the call */ - ASSERT_STREQ(localproto[0], "tcp"); - free(localproto[0]); + ASSERT_STREQ(localproto, "tcp"); }