summaryrefslogtreecommitdiff
path: root/src/network/getservbyname_r.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/network/getservbyname_r.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
initial check-in, version 0.5.0v0.5.0
Diffstat (limited to 'src/network/getservbyname_r.c')
-rw-r--r--src/network/getservbyname_r.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/network/getservbyname_r.c b/src/network/getservbyname_r.c
new file mode 100644
index 00000000..5c025150
--- /dev/null
+++ b/src/network/getservbyname_r.c
@@ -0,0 +1,41 @@
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <string.h>
+
+int getservbyname_r(const char *name, const char *prots,
+ struct servent *se, char *buf, size_t buflen, struct servent **res)
+{
+ struct addrinfo *ai, hint = { .ai_family = AF_INET };
+ int i;
+
+ /* Align buffer */
+ i = (uintptr_t)buf & sizeof(char *)-1;
+ if (!i) i = sizeof(char *);
+ if (buflen < 3*sizeof(char *)-i) {
+ errno = ERANGE;
+ return -1;
+ }
+ buf += sizeof(char *)-i;
+ buflen -= sizeof(char *)-i;
+
+ if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP;
+ else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP;
+ else return -1;
+
+ if (getaddrinfo(0, name, &hint, &ai) < 0) return -1;
+
+ se->s_name = (char *)name;
+ se->s_aliases = (void *)buf;
+ se->s_aliases[0] = se->s_name;
+ se->s_aliases[1] = 0;
+ se->s_port = ((struct sockaddr_in *)ai->ai_addr)->sin_port;
+ se->s_proto = (char *)prots;
+
+ freeaddrinfo(ai);
+ *res = se;
+ return 0;
+}