Codebase list apt-cacher-ng / aef28d5
Deduplicate results of getaddrinfo before binding Sometimes it contains duplicates (from our POV, probably different in some details but it doesn't matter), must not cause weird behavior of application. Eduard Bloch 7 years ago
1 changed file(s) with 14 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1616 #include <cstdio>
1717 #include <list>
1818 #include <map>
19 #include <unordered_set>
1920 #include <iostream>
2021 #include <algorithm> // std::min_element, std::max_element
2122
256257
257258 struct addrinfo *res, *p;
258259 if(0!=getaddrinfo(addi, port.c_str(), &hints, &res))
259 {
260 perror("Error resolving address for binding");
261 return;
262 }
263
260 {
261 perror("Error resolving address for binding");
262 return;
263 }
264
265 std::unordered_set<std::string> dedup;
264266 for(p=res; p; p=p->ai_next)
265267 {
268 if(p->ai_family != AF_INET6 && p->ai_family != AF_INET)
269 continue;
270
271 // processed before?
272 if(!dedup.insert(std::string((LPCSTR)p->ai_addr, p->ai_addrlen)).second)
273 continue;
274
266275 int nSockFd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
267276 if (nSockFd<0)
268277 goto error_socket;