2 #include <sys/socket.h>
3 #include <netinet/in.h>
9 int getservbyport_r(int port, const char *prots,
10 struct servent *se, char *buf, size_t buflen, struct servent **res)
13 struct sockaddr_in sin = {
14 .sin_family = AF_INET,
19 i = (uintptr_t)buf & sizeof(char *)-1;
20 if (!i) i = sizeof(char *);
21 if (buflen < 3*sizeof(char *)-i) {
25 buf += sizeof(char *)-i;
26 buflen -= sizeof(char *)-i;
28 if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return -1;
31 se->s_proto = (char *)prots;
32 se->s_aliases = (void *)buf;
33 buf += 2*sizeof(char *);
34 buflen -= 2*sizeof(char *);
36 se->s_aliases[0] = se->s_name = buf;
38 if (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen,
39 strcmp(prots, "udp") ? 0 : NI_DGRAM) < 0) return -1;