2 #include <sys/socket.h>
3 #include <netinet/in.h>
11 #define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *))
13 int getservbyname_r(const char *name
, const char *prots
,
14 struct servent
*se
, char *buf
, size_t buflen
, struct servent
**res
)
16 struct service servs
[MAXSERVS
];
17 int cnt
, proto
, align
;
21 /* Don't treat numeric port number strings as service records. */
23 strtoul(name
, &end
, 10);
24 if (!*end
) return ENOENT
;
27 align
= -(uintptr_t)buf
& ALIGN
-1;
28 if (buflen
< 2*sizeof(char *)+align
)
32 if (!prots
) proto
= 0;
33 else if (!strcmp(prots
, "tcp")) proto
= IPPROTO_TCP
;
34 else if (!strcmp(prots
, "udp")) proto
= IPPROTO_UDP
;
37 cnt
= __lookup_serv(servs
, name
, proto
, 0, 0);
38 if (cnt
<0) switch (cnt
) {
46 se
->s_name
= (char *)name
;
47 se
->s_aliases
= (void *)buf
;
48 se
->s_aliases
[0] = se
->s_name
;
50 se
->s_port
= htons(servs
[0].port
);
51 se
->s_proto
= servs
[0].proto
== IPPROTO_TCP
? "tcp" : "udp";