about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-07-22 19:42:44 -0400
committerRich Felker <dalias@aerifal.cx>2012-07-22 19:42:44 -0400
commitefe72c561925a711d8dbc7894ef27fe823205e21 (patch)
treec94964ec4861e63ee9fa81991de60e20d9ea3d7e
parentb4f632bb279724eee4aa25bc4f575a9fde172551 (diff)
downloadmusl-efe72c561925a711d8dbc7894ef27fe823205e21.tar.gz
musl-efe72c561925a711d8dbc7894ef27fe823205e21.tar.xz
musl-efe72c561925a711d8dbc7894ef27fe823205e21.zip
getaddrinfo /etc/services lookup support
-rw-r--r--src/network/getaddrinfo.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/network/getaddrinfo.c b/src/network/getaddrinfo.c
index 93c75160..d16b6561 100644
--- a/src/network/getaddrinfo.c
+++ b/src/network/getaddrinfo.c
@@ -58,7 +58,6 @@ int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint,
 	union sa sa = {{0}};
 	unsigned char reply[1024];
 	int i, j;
-	//char hostbuf[256];
 	char line[512];
 	FILE *f, _f;
 	unsigned char _buf[1024];
@@ -79,10 +78,24 @@ int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint,
 		port = strtoul(serv, &z, 10);
 		if (!*z && port > 65535) return EAI_SERVICE;
 		if (!port) {
+			size_t servlen = strlen(serv);
+			char protname[4];
+
 			if (flags & AI_NUMERICSERV) return EAI_SERVICE;
 
-			//f = fopen("/etc/services", "rb");
-			return EAI_SERVICE;
+			f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
+			if (!f) return EAI_SERVICE;
+			while (fgets(line, sizeof line, f)) {
+				if (strncmp(line, serv, servlen))
+					continue;
+				if (sscanf(line+servlen, "%lu/%3s", &port, protname) < 2)
+					continue;
+				if (strcmp(protname, proto==IPPROTO_UDP ? "udp" : "tcp"))
+					continue;
+				break;
+			}
+			__fclose_ca(f);
+			if (feof(f)) return EAI_SERVICE;
 		}
 		port = htons(port);
 	}