2 #include <sys/socket.h>
3 #include <netinet/in.h>
9 int getservbyname_r(const char *name, const char *prots,
10 struct servent *se, char *buf, size_t buflen, struct servent **res)
12 struct addrinfo *ai, hint = { .ai_family = AF_INET };
16 getservbyname_r(name, "tcp", se, buf, buflen, res)
17 && getservbyname_r(name, "udp", se, buf, buflen, res) );
20 i = (uintptr_t)buf & sizeof(char *)-1;
21 if (!i) i = sizeof(char *);
22 if (buflen < 3*sizeof(char *)-i) {
26 buf += sizeof(char *)-i;
27 buflen -= sizeof(char *)-i;
29 if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP;
30 else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP;
33 if (getaddrinfo(0, name, &hint, &ai) < 0) return -1;
35 se->s_name = (char *)name;
36 se->s_aliases = (void *)buf;
37 se->s_aliases[0] = se->s_name;
39 se->s_port = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
40 se->s_proto = (char *)prots;