Improve getservbyname and getservbyport (#207)

- support aliases in /etc/services
- use case insensitive comparisons
- add tests
This commit is contained in:
Gautham 2021-07-06 00:55:26 +05:30 committed by GitHub
parent 58fb2fb3d3
commit c0bec24fa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 202 additions and 58 deletions

View file

@ -30,30 +30,25 @@
struct servent *getservbyport(int port, const char *proto) {
static struct servent *ptr1, se1;
char name[DNS_NAME_MAX];
static char s_name[DNS_NAME_MAX + 1];
char *localproto = proto;
if (!ptr1) {
se1.s_name = NULL;
se1.s_aliases = (char **)malloc(sizeof(char *) * 1);
if (!se1.s_aliases) return NULL;
se1.s_aliases[0] = NULL;
se1.s_name = s_name;
if (!(se1.s_aliases = calloc(1, sizeof(char *)))) return NULL;
se1.s_port = 0;
se1.s_proto = NULL;
ptr1 = &se1;
}
if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) {
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);
return NULL;
}
ptr1->s_port = port;
if (ptr1->s_name) free(ptr1->s_name);
ptr1->s_name = strdup(name);
if (ptr1->s_proto) free(ptr1->s_proto);
ptr1->s_proto = strdup(localproto);