• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #ifdef __LITEOS_A__
3 #define _BSD_SOURCE
4 #include<netdb.h>
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<string.h>
8 #include<arpa/inet.h>
9 #include<errno.h>
10 #endif
11 #include <netdb.h>
12 #include <unsupported_api.h>
13 
14 #ifdef __LITEOS_A__
15 #define HOST_FILE_PATH "/etc/hosts"
16 #define NET_FILE_PATH  "/etc/networks"
17 #define BUFFER_SIZE    200
18 #define INET_ADDR_LEN  4
19 #define INET6_ADDR_LEN 16
20 #define MAX_ALIAS_NO   10
21 #define MAX_ADDR_NO    10
22 #define MAX_NAME_LEN   20
23 #define MAX_ALIAS_LEN  20
24 
25 struct hostdata {
26 	FILE *hostf;
27 	int stayopen;
28 	struct hostent he;
29 	char buf[BUFFER_SIZE];
30 	char *addr[MAX_ADDR_NO + 1];
31 	char addr_list[MAX_ADDR_NO + 1][INET6_ADDR_LEN + 1];
32 	char *aliases[MAX_ALIAS_NO + 1];
33 };
34 
35 struct netdata {
36 	FILE *netf;
37 	int stayopen;
38 	struct netent ne;
39 	char buf[BUFFER_SIZE];
40 	char *aliases[MAX_ALIAS_NO + 1];
41 };
42 
43 static struct hostdata *hd = NULL;
44 static struct netdata  *nd = NULL;
45 
gethostent_resolv()46 static int gethostent_resolv()
47 {
48 	char *ch, *ptr = NULL;
49 	if (hd->buf[0] == '#')
50 		return -1;
51 
52 	ch = strchr(hd->buf, '#');
53 	if (ch)
54 		*ch = '\0';
55 	ch = strtok_r(hd->buf, " \t\n", &ptr);
56 	if (!ch)
57 		return -1;
58 
59 	if (inet_pton(AF_INET, ch, hd->addr_list[0]) == 1) {
60 		hd->he.h_addrtype = AF_INET;
61 		hd->he.h_length = INET_ADDR_LEN;
62 		hd->addr_list[0][INET_ADDR_LEN] = '\0';
63 	} else if (inet_pton(AF_INET6, ch, hd->addr_list[0]) == 1) {
64 		hd->he.h_addrtype = AF_INET6;
65 		hd->he.h_length = INET6_ADDR_LEN;
66 		hd->addr_list[0][INET6_ADDR_LEN] = '\0';
67 	} else {
68 		return -1;
69 	}
70 
71 	hd->addr[0] = hd->addr_list[0];
72 	hd->addr[1] = NULL;
73 	hd->he.h_addr_list = hd->addr;
74 
75 	ch = strtok_r(NULL, " \t\n", &ptr);
76 	if (!ch)
77 		return -1;
78 	hd->he.h_name = ch;
79 
80 	int i = 0;
81 	while (i < MAX_ALIAS_NO) {
82 		ch = strtok_r(NULL, " \t\n", &ptr);
83 		hd->aliases[i++] = ch;
84 	}
85 
86 	hd->aliases[MAX_ALIAS_NO] = NULL;
87 	hd->he.h_aliases = hd->aliases;
88 	return 0;
89 }
90 #endif
91 
sethostent(int stayopen)92 void sethostent(int stayopen)
93 {
94 #ifdef __LITEOS_A__
95 	if (hd == NULL) {
96 		if ((hd = malloc(sizeof(struct hostdata))) == NULL) {
97 			h_errno = NO_RECOVERY;
98 			return;
99 		}
100 		hd->hostf = NULL;
101 	}
102 	if (hd->hostf == NULL)
103 		hd->hostf = fopen(NET_FILE_PATH, "r");
104 	else
105 		rewind(hd->hostf);
106 	hd->stayopen = stayopen;
107 #endif
108 }
109 
gethostent(void)110 struct hostent *gethostent(void)
111 {
112 #ifdef __LITEOS_A__
113 	if (hd == NULL) {
114 		if ((hd = malloc(sizeof(struct hostdata))) == NULL) {
115 			h_errno = NO_RECOVERY;
116 			return NULL;
117 		}
118 		hd->hostf = NULL;
119 		hd->stayopen = 0;
120 	}
121 	if (!hd->hostf && !(hd->hostf = fopen(HOST_FILE_PATH, "r"))) {
122 		h_errno = NO_RECOVERY;
123 		free(hd);
124 		hd =NULL;
125 		return NULL;
126 	}
127 	do {
128 		if (!fgets(hd->buf, BUFFER_SIZE, hd->hostf)) {
129 			h_errno = HOST_NOT_FOUND;
130 			return NULL;
131 		}
132 	} while (gethostent_resolv());
133 
134 	return &(hd->he);
135 #else
136 	return 0;
137 #endif
138 }
139 
140 #ifdef __LITEOS_A__
getnetent_resolv()141 static int getnetent_resolv()
142 {
143 	if (nd->buf[0] == '#')
144 		return -1;
145 	char *ch, *ptr = NULL;
146 	ch = strchr(nd->buf, '#');
147 	if (ch)
148 		*ch = '\0';
149 	ch = strtok_r(nd->buf, " \t\n", &ptr);
150 	if (!ch)
151 		return -1;
152 	nd->ne.n_name = ch;
153 	nd->ne.n_addrtype = AF_INET;
154 
155 	ch = strtok_r(NULL, " \t\n", &ptr);
156 	if (!ch)
157 		return -1;
158 	nd->ne.n_net = inet_network(ch);
159 
160 	int i = 0;
161 	while ((ch = strtok_r(NULL, " \t\n", &ptr)) != NULL && i < MAX_ALIAS_NO) {
162 		nd->aliases[i++] = ch;
163 	}
164 	nd->aliases[i] = NULL;
165 	nd->ne.n_aliases = nd->aliases;
166 	return 0;
167 }
168 #endif
169 
getnetent(void)170 struct netent *getnetent(void)
171 {
172 #ifdef __LITEOS_A__
173 	if (nd == NULL) {
174 		if ((nd = malloc(sizeof(struct netdata))) == NULL) {
175 			errno = ENOMEM;
176 			return NULL;
177 		}
178 		nd->netf = NULL;
179 		nd->stayopen = 0;
180 	}
181 	if (nd->netf == NULL && (nd->netf = fopen(NET_FILE_PATH, "r")) == NULL) {
182 		errno = ENOENT;
183 		free(nd);
184 		nd = NULL;
185 		return NULL;
186 	}
187 	while (1) {
188 		if (fgets(nd->buf, BUFFER_SIZE, nd->netf) == NULL)
189 			break;
190 		if (getnetent_resolv() == 0)
191 			return &(nd->ne);
192 	}
193 
194 	return NULL;
195 #else
196 	return 0;
197 #endif
198 }
199 
endhostent(void)200 void endhostent(void)
201 {
202 #ifdef __LITEOS_A__
203 	if (hd == NULL)
204 		return;
205 	if (hd->stayopen == 0) {
206 		if (hd->hostf != NULL)
207 			fclose(hd->hostf);
208 		free(hd);
209 		hd = NULL;
210 	}
211 #endif
212 }
213 
214 #ifdef __LITEOS_A__
setnetent(int stayopen)215 void setnetent(int stayopen)
216 {
217 	if (nd == NULL) {
218 		if ((nd = malloc(sizeof(struct netdata))) == NULL) {
219 			errno = ENOMEM;
220 			return;
221 		}
222 		nd->netf = NULL;
223 	}
224 	if (nd->netf == NULL)
225 		nd->netf = fopen(NET_FILE_PATH, "r");
226 	else
227 		rewind(nd->netf);
228 	nd->stayopen = stayopen;
229 }
230 
endnetent(void)231 void endnetent(void)
232 {
233 	if (nd == NULL)
234 		return;
235 	if (!nd->stayopen) {
236 		if (nd->netf != NULL)
237 			fclose(nd->netf);
238 		free(nd);
239 		nd = NULL;
240 	}
241 }
242 #else
243 weak_alias(sethostent, setnetent);
244 weak_alias(endhostent, endnetent);
245 #endif
246