View | Details | Raw Unified | Return to bug 556299
Collapse All | Expand All

(-)src/SocketCore.h (+6 lines)
Lines 363-368 Link Here
363
  static void bindAddress(const std::string& interface);
363
  static void bindAddress(const std::string& interface);
364
};
364
};
365
365
366
// Wrapper function for getaddrinfo(). The value
367
// flags|DEFAULT_AI_FLAGS is used as ai_flags.
368
int callGetaddrinfo
369
(struct addrinfo** resPtr, const char* host, const char* service, int family,
370
 int sockType, int flags, int protocol);
371
366
} // namespace aria2
372
} // namespace aria2
367
373
368
#endif // _D_SOCKET_CORE_H_
374
#endif // _D_SOCKET_CORE_H_
(-)src/NameResolver.cc (-8 / +3 lines)
Lines 40-45 Link Here
40
#include "message.h"
40
#include "message.h"
41
#include "StringFormat.h"
41
#include "StringFormat.h"
42
#include "util.h"
42
#include "util.h"
43
#include "SocketCore.h"
43
44
44
namespace aria2 {
45
namespace aria2 {
45
46
Lines 48-73 Link Here
48
void NameResolver::resolve(std::deque<std::string>& resolvedAddresses,
49
void NameResolver::resolve(std::deque<std::string>& resolvedAddresses,
49
			   const std::string& hostname)
50
			   const std::string& hostname)
50
{
51
{
51
  struct addrinfo hints;
52
  struct addrinfo* res;
52
  struct addrinfo* res;
53
  memset(&hints, 0, sizeof(hints));
54
  hints.ai_family = _family;
55
  hints.ai_socktype = _socktype;
56
  hints.ai_flags = 0;
57
  hints.ai_protocol = 0;
58
  int s;
53
  int s;
59
  s = getaddrinfo(hostname.c_str(), 0, &hints, &res);
54
  s = callGetaddrinfo(&res, hostname.c_str(), 0, _family, _socktype, 0, 0);
60
  if(s) {
55
  if(s) {
61
    throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
56
    throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
62
				 hostname.c_str(), gai_strerror(s)).str());
57
				 hostname.c_str(), gai_strerror(s)).str());
63
  }
58
  }
59
  auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo);
64
  struct addrinfo* rp;
60
  struct addrinfo* rp;
65
  for(rp = res; rp; rp = rp->ai_next) {
61
  for(rp = res; rp; rp = rp->ai_next) {
66
    std::pair<std::string, uint16_t> addressPort
62
    std::pair<std::string, uint16_t> addressPort
67
      = util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen);
63
      = util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen);
68
    resolvedAddresses.push_back(addressPort.first);
64
    resolvedAddresses.push_back(addressPort.first);
69
  }
65
  }
70
  freeaddrinfo(res);
71
}
66
}
72
67
73
void NameResolver::setSocktype(int socktype)
68
void NameResolver::setSocktype(int socktype)
(-)src/SocketCore.cc (-31 / +25 lines)
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
(-)src/a2netcompat.h (+6 lines)
Lines 97-100 Link Here
97
# define sock_t int
97
# define sock_t int
98
#endif
98
#endif
99
99
100
#ifdef AI_ADDRCONFIG
101
# define DEFAULT_AI_FLAGS AI_ADDRCONFIG
102
#else // !AI_ADDRCONFIG
103
# define DEFAULT_AI_FLAGS 0
104
#endif // !AI_ADDRCONFIG
105
100
#endif // _D_A2NETCOMPAT_H_
106
#endif // _D_A2NETCOMPAT_H_

Return to bug 556299