getservbyname_r.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <stdlib.h>
  9. #include "lookup.h"
  10. #define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *))
  11. int getservbyname_r(const char *name, const char *prots,
  12. struct servent *se, char *buf, size_t buflen, struct servent **res)
  13. {
  14. struct service servs[MAXSERVS];
  15. int cnt, proto, align;
  16. *res = 0;
  17. /* Don't treat numeric port number strings as service records. */
  18. char *end = "";
  19. strtoul(name, &end, 10);
  20. if (!*end) return ENOENT;
  21. /* Align buffer */
  22. align = -(uintptr_t)buf & ALIGN-1;
  23. if (buflen < 2*sizeof(char *)+align)
  24. return ERANGE;
  25. buf += align;
  26. if (!prots) proto = 0;
  27. else if (!strcmp(prots, "tcp")) proto = IPPROTO_TCP;
  28. else if (!strcmp(prots, "udp")) proto = IPPROTO_UDP;
  29. else return EINVAL;
  30. cnt = __lookup_serv(servs, name, proto, 0, 0);
  31. if (cnt<0) switch (cnt) {
  32. case EAI_MEMORY:
  33. case EAI_SYSTEM:
  34. return ENOMEM;
  35. default:
  36. return ENOENT;
  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. *res = se;
  45. return 0;
  46. }