From 76ca1e8d7d907cd98211633c2150d34b89cf4791 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Fri, 9 Jul 2021 12:21:24 +0530 Subject: [PATCH] servicestxt doesn't use network byte order anymore as mentioned by @jart in #204, the LookupServicesBy functions don't need to handle network byte order, that can be done in getservbyname or getservbyport. Changed the tests to also reflect this arrangement. --- libc/dns/getprotobyname.c | 3 +-- libc/dns/getservbyname.c | 2 +- libc/dns/getservbyport.c | 2 +- libc/dns/servicestxt.c | 10 ++++----- test/libc/dns/servicestxt_test.c | 37 ++++++++++++++++---------------- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/libc/dns/getprotobyname.c b/libc/dns/getprotobyname.c index 13cc5e5ac..5a7099b2f 100644 --- a/libc/dns/getprotobyname.c +++ b/libc/dns/getprotobyname.c @@ -31,7 +31,7 @@ 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; @@ -44,4 +44,3 @@ struct protoent *getprotobyname(const char *name) { return ptr0; } - diff --git a/libc/dns/getservbyname.c b/libc/dns/getservbyname.c index dbda636c4..c94711dc6 100644 --- a/libc/dns/getservbyname.c +++ b/libc/dns/getservbyname.c @@ -50,7 +50,7 @@ struct servent *getservbyname(const char *name, const char *proto) { return NULL; } - ptr0->s_port = p; + ptr0->s_port = htons(p); if (ptr0->s_proto) free(ptr0->s_proto); ptr0->s_proto = strdup(localproto); diff --git a/libc/dns/getservbyport.c b/libc/dns/getservbyport.c index b269e6cf5..8fd6359c5 100644 --- a/libc/dns/getservbyport.c +++ b/libc/dns/getservbyport.c @@ -41,7 +41,7 @@ struct servent *getservbyport(int port, const char *proto) { ptr1 = &se1; } - if (LookupServicesByPort(port, &localproto, ptr1->s_name, DNS_NAME_MAX, + if (LookupServicesByPort(ntohs(port), &localproto, ptr1->s_name, DNS_NAME_MAX, NULL) == -1) { // localproto got alloc'd during the lookup? if (!proto && localproto != proto) free(localproto); diff --git a/libc/dns/servicestxt.c b/libc/dns/servicestxt.c index f7d688fcd..238e4225f 100644 --- a/libc/dns/servicestxt.c +++ b/libc/dns/servicestxt.c @@ -63,7 +63,7 @@ static textwindows noinline char *GetNtServicesTxtPath(char *pathbuf, * fsp 21/udp fspd * ssh 22/tcp * - * @param servport is the port number (in network byte order) + * @param servport is the port number * @param servproto is a pointer to a string (*servproto can be NULL) * @param buf is a buffer to store the official name of the service * @param bufsize is the size of buf @@ -103,7 +103,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf, 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 (name && port && proto && servport == atoi(port)) { if (!servproto[0]) { servproto[0] = strdup(proto); strncpy(buf, name, bufsize); @@ -137,7 +137,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *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) + * positive port number * * @note aliases are read from file for comparison, but not returned. * @see LookupServicesByPort @@ -182,11 +182,11 @@ int LookupServicesByName(const char *servname, char **servproto, char *buf, { if (!servproto[0]) { servproto[0] = strdup(proto); - result = htons(atoi(port)); + result = atoi(port); strncpy(buf, name, bufsize); found = 1; } else if (strcasecmp(proto, servproto[0]) == 0) { - result = htons(atoi(port)); + result = atoi(port); strncpy(buf, name, bufsize); found = 1; } diff --git a/test/libc/dns/servicestxt_test.c b/test/libc/dns/servicestxt_test.c index 2f1875de1..6ed97eb26 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; @@ -59,33 +59,33 @@ TEST(LookupServicesByPort, GetNameWhenPortCorrect) { LookupServicesByPort(965, localproto, name, sizeof(name), "services")); ASSERT_EQ(NULL, localproto[0]); - ASSERT_EQ( - -1, /* port not in network byte order */ - LookupServicesByPort(22, localproto, name, sizeof(name), "services")); + ASSERT_EQ(-1, /* port in network byte order */ + LookupServicesByPort(htons(22), localproto, name, sizeof(name), + "services")); ASSERT_EQ(NULL, localproto[0]); localproto[0] = proto2; - ASSERT_EQ(-1, /* port ok but wrong protocol */ - LookupServicesByPort(htons(22), localproto, name, sizeof(name), - "services")); + ASSERT_EQ( + -1, /* port ok but wrong protocol */ + LookupServicesByPort(22, localproto, name, sizeof(name), "services")); ASSERT_EQ(localproto[0], proto2); localproto[0] = proto1; - ASSERT_EQ(0, LookupServicesByPort(htons(22), localproto, name, sizeof(name), - "services")); + ASSERT_EQ( + 0, LookupServicesByPort(22, localproto, name, sizeof(name), "services")); ASSERT_STREQ(name, "ssh"); ASSERT_EQ(localproto[0], proto1); localproto[0] = proto2; - ASSERT_EQ(0, LookupServicesByPort(htons(19), localproto, name, - sizeof(name), "services")); + ASSERT_EQ( + 0, LookupServicesByPort(19, localproto, name, sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); ASSERT_EQ(localproto[0], proto2); localproto[0] = NULL; - ASSERT_EQ(0, /* pick first matching protocol */ - LookupServicesByPort(htons(19), localproto, name, sizeof(name), - "services")); + ASSERT_EQ( + 0, /* pick first matching protocol */ + LookupServicesByPort(19, localproto, name, sizeof(name), "services")); ASSERT_STREQ(name, "chargen"); ASSERT_NE(NULL, localproto[0]); /* got alloc'd during the call */ ASSERT_STREQ(localproto[0], "tcp"); @@ -111,21 +111,20 @@ TEST(LookupServicesByName, GetPortWhenNameOrAlias) { ASSERT_EQ(localproto[0], proto2); localproto[0] = proto1; - ASSERT_EQ( - htons(22), /* in network byte order */ - LookupServicesByName("ssh", localproto, name, sizeof(name), "services")); + ASSERT_EQ(22, LookupServicesByName("ssh", localproto, name, sizeof(name), + "services")); ASSERT_STREQ(name, "ssh"); /* official name written to buffer */ ASSERT_EQ(localproto[0], proto1); localproto[0] = proto2; - ASSERT_EQ(htons(19), /* works if alias provided */ + ASSERT_EQ(19, /* works if alias provided */ LookupServicesByName("ttytst", localproto, 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 */ + ASSERT_EQ(19, /* pick first matching protocol */ LookupServicesByName("source", localproto, name, sizeof(name), "services")); ASSERT_STREQ(name, "chargen");