|
Lines 191-204
Link Here
|
| 191 |
(const char* host, uint16_t port, int family, int sockType, |
191 |
(const char* host, uint16_t port, int family, int sockType, |
| 192 |
int getaddrinfoFlags, std::string& error) |
192 |
int getaddrinfoFlags, std::string& error) |
| 193 |
{ |
193 |
{ |
| 194 |
struct addrinfo hints; |
|
|
| 195 |
struct addrinfo* res; |
194 |
struct addrinfo* res; |
| 196 |
memset(&hints, 0, sizeof(hints)); |
195 |
int s = callGetaddrinfo(&res, host, uitos(port).c_str(), family, sockType, |
| 197 |
hints.ai_family = family; |
196 |
getaddrinfoFlags, 0); |
| 198 |
hints.ai_socktype = sockType; |
|
|
| 199 |
hints.ai_flags = getaddrinfoFlags; |
| 200 |
hints.ai_protocol = 0; |
| 201 |
int s = getaddrinfo(host, uitos(port).c_str(), &hints, &res); |
| 202 |
if(s) { |
197 |
if(s) { |
| 203 |
error = gai_strerror(s); |
198 |
error = gai_strerror(s); |
| 204 |
return -1; |
199 |
return -1; |
|
Lines 307-325
Link Here
|
| 307 |
{ |
302 |
{ |
| 308 |
closeConnection(); |
303 |
closeConnection(); |
| 309 |
|
304 |
|
| 310 |
struct addrinfo hints; |
|
|
| 311 |
struct addrinfo* res; |
305 |
struct addrinfo* res; |
| 312 |
memset(&hints, 0, sizeof(hints)); |
|
|
| 313 |
hints.ai_family = _protocolFamily; |
| 314 |
hints.ai_socktype = _sockType; |
| 315 |
hints.ai_flags = 0; |
| 316 |
hints.ai_protocol = 0; |
| 317 |
int s; |
306 |
int s; |
| 318 |
s = getaddrinfo(host.c_str(), uitos(port).c_str(), &hints, &res); |
307 |
s = callGetaddrinfo(&res, host.c_str(), uitos(port).c_str(), _protocolFamily, |
|
|
308 |
_sockType, 0, 0); |
| 319 |
if(s) { |
309 |
if(s) { |
| 320 |
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME, |
310 |
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME, |
| 321 |
host.c_str(), gai_strerror(s)).str()); |
311 |
host.c_str(), gai_strerror(s)).str()); |
| 322 |
} |
312 |
} |
|
|
313 |
auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo); |
| 323 |
struct addrinfo* rp; |
314 |
struct addrinfo* rp; |
| 324 |
for(rp = res; rp; rp = rp->ai_next) { |
315 |
for(rp = res; rp; rp = rp->ai_next) { |
| 325 |
sock_t fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
316 |
sock_t fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
|
Lines 362-368
Link Here
|
| 362 |
// later. In such case, next ai_addr should be tried. |
353 |
// later. In such case, next ai_addr should be tried. |
| 363 |
break; |
354 |
break; |
| 364 |
} |
355 |
} |
| 365 |
freeaddrinfo(res); |
|
|
| 366 |
if(sockfd == (sock_t) -1) { |
356 |
if(sockfd == (sock_t) -1) { |
| 367 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_CONNECT, host.c_str(), |
357 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_CONNECT, host.c_str(), |
| 368 |
strerror(errno)).str()); |
358 |
strerror(errno)).str()); |
|
Lines 1058-1075
Link Here
|
| 1058 |
_wantRead = false; |
1048 |
_wantRead = false; |
| 1059 |
_wantWrite = false; |
1049 |
_wantWrite = false; |
| 1060 |
|
1050 |
|
| 1061 |
struct addrinfo hints; |
|
|
| 1062 |
struct addrinfo* res; |
1051 |
struct addrinfo* res; |
| 1063 |
memset(&hints, 0, sizeof(hints)); |
|
|
| 1064 |
hints.ai_family = _protocolFamily; |
| 1065 |
hints.ai_socktype = _sockType; |
| 1066 |
hints.ai_flags = 0; |
| 1067 |
hints.ai_protocol = 0; |
| 1068 |
int s; |
1052 |
int s; |
| 1069 |
s = getaddrinfo(host.c_str(), uitos(port).c_str(), &hints, &res); |
1053 |
s = callGetaddrinfo(&res, host.c_str(), uitos(port).c_str(), _protocolFamily, |
|
|
1054 |
_sockType, 0, 0); |
| 1070 |
if(s) { |
1055 |
if(s) { |
| 1071 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, gai_strerror(s)).str()); |
1056 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, gai_strerror(s)).str()); |
| 1072 |
} |
1057 |
} |
|
|
1058 |
auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo); |
| 1073 |
struct addrinfo* rp; |
1059 |
struct addrinfo* rp; |
| 1074 |
ssize_t r = -1; |
1060 |
ssize_t r = -1; |
| 1075 |
for(rp = res; rp; rp = rp->ai_next) { |
1061 |
for(rp = res; rp; rp = rp->ai_next) { |
|
Lines 1083-1089
Link Here
|
| 1083 |
break; |
1069 |
break; |
| 1084 |
} |
1070 |
} |
| 1085 |
} |
1071 |
} |
| 1086 |
freeaddrinfo(res); |
|
|
| 1087 |
if(r == -1) { |
1072 |
if(r == -1) { |
| 1088 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, errorMsg()).str()); |
1073 |
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, errorMsg()).str()); |
| 1089 |
} |
1074 |
} |
|
Lines 1200-1214
Link Here
|
| 1200 |
} |
1185 |
} |
| 1201 |
#endif // HAVE_GETIFADDRS |
1186 |
#endif // HAVE_GETIFADDRS |
| 1202 |
if(bindAddrs.empty()) { |
1187 |
if(bindAddrs.empty()) { |
| 1203 |
struct addrinfo hints; |
1188 |
struct addrinfo* res; |
| 1204 |
struct addrinfo* res = 0; |
|
|
| 1205 |
memset(&hints, 0, sizeof(hints)); |
| 1206 |
hints.ai_family = _protocolFamily; |
| 1207 |
hints.ai_socktype = SOCK_STREAM; |
| 1208 |
hints.ai_flags = 0; |
| 1209 |
hints.ai_protocol = 0; |
| 1210 |
int s; |
1189 |
int s; |
| 1211 |
s = getaddrinfo(interface.c_str(), 0, &hints, &res); |
1190 |
s = callGetaddrinfo(&res, interface.c_str(), 0, _protocolFamily, |
|
|
1191 |
SOCK_STREAM, 0, 0); |
| 1212 |
if(s) { |
1192 |
if(s) { |
| 1213 |
throw DL_ABORT_EX |
1193 |
throw DL_ABORT_EX |
| 1214 |
(StringFormat(MSG_INTERFACE_NOT_FOUND, |
1194 |
(StringFormat(MSG_INTERFACE_NOT_FOUND, |
|
Lines 1255-1258
Link Here
|
| 1255 |
} |
1235 |
} |
| 1256 |
} |
1236 |
} |
| 1257 |
|
1237 |
|
|
|
1238 |
int callGetaddrinfo |
| 1239 |
(struct addrinfo** resPtr, const char* host, const char* service, int family, |
| 1240 |
int sockType, int flags, int protocol) |
| 1241 |
{ |
| 1242 |
struct addrinfo hints; |
| 1243 |
memset(&hints, 0, sizeof(hints)); |
| 1244 |
hints.ai_family = family; |
| 1245 |
hints.ai_socktype = sockType; |
| 1246 |
hints.ai_flags = DEFAULT_AI_FLAGS; |
| 1247 |
hints.ai_flags |= flags; |
| 1248 |
hints.ai_protocol = protocol; |
| 1249 |
return getaddrinfo(host, service, &hints, resPtr); |
| 1250 |
} |
| 1251 |
|
| 1258 |
} // namespace aria2 |
1252 |
} // namespace aria2 |