1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netdb.h>
4 #include <net/if.h>
5 #include <arpa/inet.h>
6 #include <limits.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include "lookup.h"
11 
__lookup_ipliteral(struct address buf[static 1], const char *name, int family)12 int __lookup_ipliteral(struct address buf[static 1], const char *name, int family)
13 {
14 	struct in_addr a4;
15 	struct in6_addr a6;
16 	if (__inet_aton(name, &a4) > 0) {
17 		 /* wrong family */
18 		if (family == AF_INET6) {
19 #ifndef __LITEOS__
20 			MUSL_LOGE("%{public}s: %{public}d: wrong family AF_INET6: %{public}d", __func__, __LINE__, EAI_NONAME);
21 #endif
22 			return EAI_NONAME;
23 		}
24 		memcpy(&buf[0].addr, &a4, sizeof a4);
25 		buf[0].family = AF_INET;
26 		buf[0].scopeid = 0;
27 		return 1;
28 	}
29 
30 	char tmp[64];
31 	char *p = strchr(name, '%'), *z;
32 	unsigned long long scopeid = 0;
33 	if (p && p-name < 64) {
34 		memcpy(tmp, name, p-name);
35 		tmp[p-name] = 0;
36 		name = tmp;
37 	}
38 
39 	if (inet_pton(AF_INET6, name, &a6) <= 0)
40 		return 0;
41 	if (family == AF_INET) /* wrong family */ {
42 #ifndef __LITEOS__
43 		MUSL_LOGE("%{public}s: %{public}d: wrong family AF_INET: %{public}d", __func__, __LINE__, EAI_NONAME);
44 #endif
45 		return EAI_NONAME;
46 	}
47 
48 	memcpy(&buf[0].addr, &a6, sizeof a6);
49 	buf[0].family = AF_INET6;
50 	if (p) {
51 		if (isdigit(*++p)) scopeid = strtoull(p, &z, 10);
52 		else z = p-1;
53 		if (*z) {
54 			if (!IN6_IS_ADDR_LINKLOCAL(&a6) &&
55 			    !IN6_IS_ADDR_MC_LINKLOCAL(&a6)) {
56 #ifndef __LITEOS__
57 				MUSL_LOGE("%{public}s: %{public}d: illegal ipv6 address: %{public}d", __func__, __LINE__, EAI_NONAME);
58 #endif
59 				return EAI_NONAME;
60 			}
61 			scopeid = if_nametoindex(p);
62 			if (!scopeid) {
63 #ifndef __LITEOS__
64 				MUSL_LOGE("%{public}s: %{public}d: scopeid is zero: %{public}d", __func__, __LINE__, EAI_NONAME);
65 #endif
66 				return EAI_NONAME;
67 			}
68 		}
69 		if (scopeid > UINT_MAX) {
70 #ifndef __LITEOS__
71 			MUSL_LOGE("%{public}s: %{public}d: scopeid is over than UINT_MAX: %{public}d", __func__, __LINE__, EAI_NONAME);
72 #endif
73 			return EAI_NONAME;
74 		}
75 	}
76 	buf[0].scopeid = scopeid;
77 	return 1;
78 }
79