OLD | NEW |
(Empty) | |
| 1 #define _GNU_SOURCE |
| 2 #include <sys/socket.h> |
| 3 #include <netinet/in.h> |
| 4 #include <netdb.h> |
| 5 #include <inttypes.h> |
| 6 #include <errno.h> |
| 7 #include <string.h> |
| 8 #include "lookup.h" |
| 9 |
| 10 #define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *)) |
| 11 |
| 12 int getservbyname_r(const char *name, const char *prots, |
| 13 struct servent *se, char *buf, size_t buflen, struct servent **res) |
| 14 { |
| 15 struct service servs[MAXSERVS]; |
| 16 int cnt, proto, align; |
| 17 |
| 18 /* Align buffer */ |
| 19 align = -(uintptr_t)buf & ALIGN-1; |
| 20 if (buflen < 2*sizeof(char *)+align) |
| 21 return ERANGE; |
| 22 buf += align; |
| 23 |
| 24 if (!prots) proto = 0; |
| 25 else if (!strcmp(prots, "tcp")) proto = IPPROTO_TCP; |
| 26 else if (!strcmp(prots, "udp")) proto = IPPROTO_UDP; |
| 27 else return EINVAL; |
| 28 |
| 29 cnt = __lookup_serv(servs, name, proto, 0, 0); |
| 30 if (cnt<0) switch (cnt) { |
| 31 case EAI_MEMORY: |
| 32 case EAI_SYSTEM: |
| 33 return ENOMEM; |
| 34 default: |
| 35 return ENOENT; |
| 36 } |
| 37 |
| 38 se->s_name = (char *)name; |
| 39 se->s_aliases = (void *)buf; |
| 40 se->s_aliases[0] = se->s_name; |
| 41 se->s_aliases[1] = 0; |
| 42 se->s_port = htons(servs[0].port); |
| 43 se->s_proto = servs[0].proto == IPPROTO_TCP ? "tcp" : "udp"; |
| 44 |
| 45 *res = se; |
| 46 return 0; |
| 47 } |
OLD | NEW |