@@ -192,7 +192,8 @@ static const char *ai_name(const struct addrinfo *ai)
192
192
*/
193
193
static int git_tcp_connect_sock (char * host , int flags )
194
194
{
195
- int sockfd = -1 , saved_errno = 0 ;
195
+ struct strbuf error_message = STRBUF_INIT ;
196
+ int sockfd = -1 ;
196
197
const char * port = STR (DEFAULT_GIT_PORT );
197
198
struct addrinfo hints , * ai0 , * ai ;
198
199
int gai ;
@@ -219,18 +220,12 @@ static int git_tcp_connect_sock(char *host, int flags)
219
220
for (ai0 = ai ; ai ; ai = ai -> ai_next , cnt ++ ) {
220
221
sockfd = socket (ai -> ai_family ,
221
222
ai -> ai_socktype , ai -> ai_protocol );
222
- if (sockfd < 0 ) {
223
- saved_errno = errno ;
224
- continue ;
225
- }
226
- if (connect (sockfd , ai -> ai_addr , ai -> ai_addrlen ) < 0 ) {
227
- saved_errno = errno ;
228
- fprintf (stderr , "%s[%d: %s]: errno=%s\n" ,
229
- host ,
230
- cnt ,
231
- ai_name (ai ),
232
- strerror (saved_errno ));
233
- close (sockfd );
223
+ if ((sockfd < 0 ) ||
224
+ (connect (sockfd , ai -> ai_addr , ai -> ai_addrlen ) < 0 )) {
225
+ strbuf_addf (& error_message , "%s[%d: %s]: errno=%s\n" ,
226
+ host , cnt , ai_name (ai ), strerror (errno ));
227
+ if (0 <= sockfd )
228
+ close (sockfd );
234
229
sockfd = -1 ;
235
230
continue ;
236
231
}
@@ -242,11 +237,13 @@ static int git_tcp_connect_sock(char *host, int flags)
242
237
freeaddrinfo (ai0 );
243
238
244
239
if (sockfd < 0 )
245
- die ("unable to connect a socket (%s) " , strerror ( saved_errno ) );
240
+ die ("unable to connect to %s:\n%s " , host , error_message . buf );
246
241
247
242
if (flags & CONNECT_VERBOSE )
248
243
fprintf (stderr , "done.\n" );
249
244
245
+ strbuf_release (& error_message );
246
+
250
247
return sockfd ;
251
248
}
252
249
0 commit comments