• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *))
12 
getservbyname_r(const char * name,const char * prots,struct servent * se,char * buf,size_t buflen,struct servent ** res)13 int getservbyname_r(const char *name, const char *prots,
14 	struct servent *se, char *buf, size_t buflen, struct servent **res)
15 {
16 	struct service servs[MAXSERVS];
17 	int cnt, proto, align;
18 
19 	*res = 0;
20 
21 	/* Don't treat numeric port number strings as service records. */
22 	char *end = "";
23 	strtoul(name, &end, 10);
24 	if (!*end) return ENOENT;
25 
26 	/* Align buffer */
27 	align = -(uintptr_t)buf & ALIGN-1;
28 	if (buflen < 2*sizeof(char *)+align)
29 		return ERANGE;
30 	buf += align;
31 
32 	if (!prots) proto = 0;
33 	else if (!strcmp(prots, "tcp")) proto = IPPROTO_TCP;
34 	else if (!strcmp(prots, "udp")) proto = IPPROTO_UDP;
35 	else return EINVAL;
36 
37 	cnt = __lookup_serv(servs, name, proto, 0, 0);
38 	if (cnt<0) switch (cnt) {
39 	case EAI_MEMORY:
40 	case EAI_SYSTEM:
41 		return ENOMEM;
42 	default:
43 		return ENOENT;
44 	}
45 
46 	se->s_name = (char *)name;
47 	se->s_aliases = (void *)buf;
48 	se->s_aliases[0] = se->s_name;
49 	se->s_aliases[1] = 0;
50 	se->s_port = htons(servs[0].port);
51 	se->s_proto = servs[0].proto == IPPROTO_TCP ? "tcp" : "udp";
52 
53 	*res = se;
54 	return 0;
55 }
56