| 92 | } |
| 93 | |
| 94 | int SocketConnect::connect(const std::string& hostname, |
| 95 | int port, |
| 96 | std::string& errMsg, |
| 97 | const CancellationRequest& isCancellationRequested) |
| 98 | { |
| 99 | // |
| 100 | // First do DNS resolution |
| 101 | // |
| 102 | auto dnsLookup = std::make_shared<DNSLookup>(hostname, port); |
| 103 | auto res = dnsLookup->resolve(errMsg, isCancellationRequested); |
| 104 | if (res == nullptr) |
| 105 | { |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | int sockfd = -1; |
| 110 | |
| 111 | // iterate through the records to find a working peer |
| 112 | struct addrinfo* address; |
| 113 | for (address = res.get(); address != nullptr; address = address->ai_next) |
| 114 | { |
| 115 | // |
| 116 | // Second try to connect to the remote host |
| 117 | // |
| 118 | sockfd = connectToAddress(address, errMsg, isCancellationRequested); |
| 119 | if (sockfd != -1) |
| 120 | { |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return sockfd; |
| 126 | } |
| 127 | |
| 128 | // FIXME: configure is a terrible name |
| 129 | void SocketConnect::configure(int sockfd) |