Lines 44-49
Link Here
|
44 |
# define IFA_F_OPTIMISTIC 0 |
44 |
# define IFA_F_OPTIMISTIC 0 |
45 |
#endif |
45 |
#endif |
46 |
|
46 |
|
|
|
47 |
#define IN6AI_FIXED_SIZE 16 |
48 |
#define IN6AI_CHUNK_SIZE 64 |
49 |
|
47 |
|
50 |
|
48 |
struct cached_data |
51 |
struct cached_data |
49 |
{ |
52 |
{ |
Lines 61-66
static struct cached_data noai6ai_cached =
Link Here
|
61 |
.in6ailen = 0 |
64 |
.in6ailen = 0 |
62 |
}; |
65 |
}; |
63 |
|
66 |
|
|
|
67 |
struct in6ai_chunk |
68 |
{ |
69 |
struct in6addrinfo info[IN6AI_CHUNK_SIZE]; |
70 |
struct in6ai_chunk *next; |
71 |
}; |
72 |
|
73 |
struct in6ai_list |
74 |
{ |
75 |
struct in6addrinfo info[IN6AI_FIXED_SIZE]; |
76 |
unsigned count; |
77 |
struct in6ai_chunk *next; |
78 |
}; |
79 |
|
64 |
libc_freeres_ptr (static struct cached_data *cache); |
80 |
libc_freeres_ptr (static struct cached_data *cache); |
65 |
__libc_lock_define_initialized (static, lock); |
81 |
__libc_lock_define_initialized (static, lock); |
66 |
|
82 |
|
Lines 102-107
cache_valid_p (void)
Link Here
|
102 |
} |
118 |
} |
103 |
|
119 |
|
104 |
|
120 |
|
|
|
121 |
static void |
122 |
in6ailist_init(struct in6ai_list *list) |
123 |
{ |
124 |
list->count = 0; |
125 |
list->next = NULL; |
126 |
} |
127 |
|
128 |
|
129 |
static struct in6addrinfo * |
130 |
in6ailist_add(struct in6ai_list *list) |
131 |
{ |
132 |
if (list->count < IN6AI_FIXED_SIZE) |
133 |
return &list->info[list->count++]; |
134 |
|
135 |
unsigned idx = list->count - IN6AI_FIXED_SIZE; |
136 |
struct in6ai_chunk *chunk = list->next; |
137 |
while (idx > IN6AI_CHUNK_SIZE) |
138 |
{ |
139 |
chunk = chunk->next; |
140 |
idx -= IN6AI_CHUNK_SIZE; |
141 |
} |
142 |
|
143 |
if (idx == IN6AI_CHUNK_SIZE) |
144 |
{ |
145 |
chunk->next = malloc(sizeof(struct in6ai_chunk)); |
146 |
if (!chunk->next) |
147 |
return NULL; |
148 |
chunk = chunk->next; |
149 |
chunk->next = NULL; |
150 |
idx = 0; |
151 |
} |
152 |
list->count++; |
153 |
return &chunk->info[idx]; |
154 |
} |
155 |
|
156 |
|
157 |
static struct cached_data * |
158 |
in6ailist_export(struct in6ai_list *list) |
159 |
{ |
160 |
struct cached_data *result; |
161 |
unsigned count = list->count; |
162 |
result = malloc(sizeof(struct cached_data) |
163 |
+ count * sizeof(struct in6addrinfo)); |
164 |
if (!result) |
165 |
return NULL; |
166 |
|
167 |
struct in6addrinfo *p = result->in6ai; |
168 |
unsigned n; |
169 |
|
170 |
n = (count > IN6AI_FIXED_SIZE) ? IN6AI_FIXED_SIZE : count; |
171 |
memcpy(p, list->info, n * sizeof(struct in6addrinfo)); |
172 |
p += n; |
173 |
count -= n; |
174 |
|
175 |
struct in6ai_chunk *chunk = list->next; |
176 |
while (n > 0) |
177 |
{ |
178 |
n = (count > IN6AI_CHUNK_SIZE) ? IN6AI_CHUNK_SIZE : count; |
179 |
memcpy(p, list->info, n * sizeof(struct in6addrinfo)); |
180 |
p += n; |
181 |
count -= n; |
182 |
chunk = chunk->next; |
183 |
} |
184 |
|
185 |
result->in6ailen = list->count; |
186 |
return result; |
187 |
} |
188 |
|
189 |
|
190 |
static void |
191 |
in6ailist_cleanup(struct in6ai_list *list) |
192 |
{ |
193 |
struct in6ai_chunk *p = list->next; |
194 |
struct in6ai_chunk *next; |
195 |
|
196 |
while (p) |
197 |
{ |
198 |
next = p->next; |
199 |
free(p); |
200 |
p = next; |
201 |
} |
202 |
} |
203 |
|
204 |
|
105 |
static struct cached_data * |
205 |
static struct cached_data * |
106 |
make_request (int fd, pid_t pid) |
206 |
make_request (int fd, pid_t pid) |
107 |
{ |
207 |
{ |
Lines 129-134
make_request (int fd, pid_t pid)
Link Here
|
129 |
memset (&nladdr, '\0', sizeof (nladdr)); |
229 |
memset (&nladdr, '\0', sizeof (nladdr)); |
130 |
nladdr.nl_family = AF_NETLINK; |
230 |
nladdr.nl_family = AF_NETLINK; |
131 |
|
231 |
|
|
|
232 |
struct in6ai_list in6ai_list; |
233 |
in6ailist_init(&in6ai_list); |
234 |
|
132 |
#ifdef PAGE_SIZE |
235 |
#ifdef PAGE_SIZE |
133 |
/* Help the compiler optimize out the malloc call if PAGE_SIZE |
236 |
/* Help the compiler optimize out the malloc call if PAGE_SIZE |
134 |
is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ |
237 |
is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ |
Lines 158-169
make_request (int fd, pid_t pid)
Link Here
|
158 |
goto out_fail; |
261 |
goto out_fail; |
159 |
|
262 |
|
160 |
bool done = false; |
263 |
bool done = false; |
161 |
struct in6ailist |
|
|
162 |
{ |
163 |
struct in6addrinfo info; |
164 |
struct in6ailist *next; |
165 |
} *in6ailist = NULL; |
166 |
size_t in6ailistlen = 0; |
167 |
bool seen_ipv4 = false; |
264 |
bool seen_ipv4 = false; |
168 |
bool seen_ipv6 = false; |
265 |
bool seen_ipv6 = false; |
169 |
|
266 |
|
Lines 238-265
make_request (int fd, pid_t pid)
Link Here
|
238 |
} |
335 |
} |
239 |
} |
336 |
} |
240 |
|
337 |
|
241 |
struct in6ailist *newp = alloca (sizeof (*newp)); |
338 |
struct in6addrinfo *info = in6ailist_add(&in6ai_list); |
242 |
newp->info.flags = (((ifam->ifa_flags |
339 |
if (!info) |
243 |
& (IFA_F_DEPRECATED |
340 |
goto out_fail; |
244 |
| IFA_F_OPTIMISTIC)) |
341 |
info->flags = (((ifam->ifa_flags |
245 |
? in6ai_deprecated : 0) |
342 |
& (IFA_F_DEPRECATED |
246 |
| ((ifam->ifa_flags |
343 |
| IFA_F_OPTIMISTIC)) |
247 |
& IFA_F_HOMEADDRESS) |
344 |
? in6ai_deprecated : 0) |
248 |
? in6ai_homeaddress : 0)); |
345 |
| ((ifam->ifa_flags |
249 |
newp->info.prefixlen = ifam->ifa_prefixlen; |
346 |
& IFA_F_HOMEADDRESS) |
250 |
newp->info.index = ifam->ifa_index; |
347 |
? in6ai_homeaddress : 0)); |
|
|
348 |
info->prefixlen = ifam->ifa_prefixlen; |
349 |
info->index = ifam->ifa_index; |
251 |
if (ifam->ifa_family == AF_INET) |
350 |
if (ifam->ifa_family == AF_INET) |
252 |
{ |
351 |
{ |
253 |
newp->info.addr[0] = 0; |
352 |
info->addr[0] = 0; |
254 |
newp->info.addr[1] = 0; |
353 |
info->addr[1] = 0; |
255 |
newp->info.addr[2] = htonl (0xffff); |
354 |
info->addr[2] = htonl (0xffff); |
256 |
newp->info.addr[3] = *(const in_addr_t *) address; |
355 |
info->addr[3] = *(const in_addr_t *) address; |
257 |
} |
356 |
} |
258 |
else |
357 |
else |
259 |
memcpy (newp->info.addr, address, sizeof (newp->info.addr)); |
358 |
memcpy (info->addr, address, sizeof (info->addr)); |
260 |
newp->next = in6ailist; |
|
|
261 |
in6ailist = newp; |
262 |
++in6ailistlen; |
263 |
} |
359 |
} |
264 |
else if (nlmh->nlmsg_type == NLMSG_DONE) |
360 |
else if (nlmh->nlmsg_type == NLMSG_DONE) |
265 |
/* We found the end, leave the loop. */ |
361 |
/* We found the end, leave the loop. */ |
Lines 269-278
make_request (int fd, pid_t pid)
Link Here
|
269 |
while (! done); |
365 |
while (! done); |
270 |
|
366 |
|
271 |
struct cached_data *result; |
367 |
struct cached_data *result; |
272 |
if (seen_ipv6 && in6ailist != NULL) |
368 |
if (seen_ipv6 && in6ai_list.count > 0) |
273 |
{ |
369 |
{ |
274 |
result = malloc (sizeof (*result) |
370 |
result = in6ailist_export(&in6ai_list); |
275 |
+ in6ailistlen * sizeof (struct in6addrinfo)); |
|
|
276 |
if (result == NULL) |
371 |
if (result == NULL) |
277 |
goto out_fail; |
372 |
goto out_fail; |
278 |
|
373 |
|
Lines 280-293
make_request (int fd, pid_t pid)
Link Here
|
280 |
result->usecnt = 2; |
375 |
result->usecnt = 2; |
281 |
result->seen_ipv4 = seen_ipv4; |
376 |
result->seen_ipv4 = seen_ipv4; |
282 |
result->seen_ipv6 = true; |
377 |
result->seen_ipv6 = true; |
283 |
result->in6ailen = in6ailistlen; |
|
|
284 |
|
285 |
do |
286 |
{ |
287 |
result->in6ai[--in6ailistlen] = in6ailist->info; |
288 |
in6ailist = in6ailist->next; |
289 |
} |
290 |
while (in6ailist != NULL); |
291 |
} |
378 |
} |
292 |
else |
379 |
else |
293 |
{ |
380 |
{ |
Lines 297-307
make_request (int fd, pid_t pid)
Link Here
|
297 |
result = &noai6ai_cached; |
384 |
result = &noai6ai_cached; |
298 |
} |
385 |
} |
299 |
|
386 |
|
|
|
387 |
in6ailist_cleanup(&in6ai_list); |
300 |
if (use_malloc) |
388 |
if (use_malloc) |
301 |
free (buf); |
389 |
free (buf); |
302 |
return result; |
390 |
return result; |
303 |
|
391 |
|
304 |
out_fail: |
392 |
out_fail: |
|
|
393 |
in6ailist_cleanup(&in6ai_list); |
305 |
if (use_malloc) |
394 |
if (use_malloc) |
306 |
free (buf); |
395 |
free (buf); |
307 |
return NULL; |
396 |
return NULL; |
308 |
- |
|
|