int gitno_connect(git_transport *t, const char *host, const char *port) { struct addrinfo *info = NULL, *p; struct addrinfo hints; int ret; GIT_SOCKET s = INVALID_SOCKET; memset(&hints, 0x0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_UNSPEC; if ((ret = p_getaddrinfo(host, port, &hints, &info)) < 0) { giterr_set(GITERR_NET, "Failed to resolve address for %s: %s", host, p_gai_strerror(ret)); return -1; } for (p = info; p != NULL; p = p->ai_next) { s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (s == INVALID_SOCKET) { net_set_error("error creating socket"); break; } if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0) break; /* If we can't connect, try the next one */ gitno_close(s); s = INVALID_SOCKET; } /* Oops, we couldn't connect to any address */ if (s == INVALID_SOCKET && p == NULL) { giterr_set(GITERR_OS, "Failed to connect to %s", host); return -1; } t->socket = s; p_freeaddrinfo(info); if (t->use_ssl && ssl_setup(t, host) < 0) return -1; return 0; }
SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_family) { SockAddr *ret = snew(SockAddr); unsigned long a; char realhost[8192]; int hint_family; /* Default to IPv4. */ hint_family = (address_family == ADDRTYPE_IPV4 ? AF_INET : #ifndef NO_IPV6 address_family == ADDRTYPE_IPV6 ? AF_INET6 : #endif AF_UNSPEC); /* Clear the structure and default to IPv4. */ memset(ret, 0, sizeof(SockAddr)); #ifndef NO_IPV6 ret->ais = NULL; #endif ret->namedpipe = false; ret->addresses = NULL; ret->resolved = false; ret->refcount = 1; *realhost = '\0'; if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) { struct hostent *h = NULL; int err = 0; #ifndef NO_IPV6 /* * Use getaddrinfo when it's available */ if (p_getaddrinfo) { struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = hint_family; hints.ai_flags = AI_CANONNAME; { /* strip [] on IPv6 address literals */ char *trimmed_host = host_strduptrim(host); err = p_getaddrinfo(trimmed_host, NULL, &hints, &ret->ais); sfree(trimmed_host); } if (err == 0) ret->resolved = true; } else #endif { /* * Otherwise use the IPv4-only gethostbyname... * (NOTE: we don't use gethostbyname as a fallback!) */ if ( (h = p_gethostbyname(host)) ) ret->resolved = true; else err = p_WSAGetLastError(); } if (!ret->resolved) { ret->error = (err == WSAENETDOWN ? "Network is down" : err == WSAHOST_NOT_FOUND ? "Host does not exist" : err == WSATRY_AGAIN ? "Host not found" : #ifndef NO_IPV6 p_getaddrinfo&&p_gai_strerror ? p_gai_strerror(err) : #endif "gethostbyname: unknown error"); } else { ret->error = NULL; #ifndef NO_IPV6 /* If we got an address info use that... */ if (ret->ais) { /* Are we in IPv4 fallback mode? */ /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ if (ret->ais->ai_family == AF_INET) memcpy(&a, (char *) &((SOCKADDR_IN *) ret->ais-> ai_addr)->sin_addr, sizeof(a)); if (ret->ais->ai_canonname) strncpy(realhost, ret->ais->ai_canonname, lenof(realhost)); else strncpy(realhost, host, lenof(realhost)); } /* We used the IPv4-only gethostbyname()... */ else #endif { int n; for (n = 0; h->h_addr_list[n]; n++); ret->addresses = snewn(n, unsigned long); ret->naddresses = n; for (n = 0; n < ret->naddresses; n++) { memcpy(&a, h->h_addr_list[n], sizeof(a)); ret->addresses[n] = p_ntohl(a); } memcpy(&a, h->h_addr, sizeof(a)); /* This way we are always sure the h->h_name is valid :) */ strncpy(realhost, h->h_name, sizeof(realhost)); } } } else {
BOOL CAsyncSocketExLayer::ConnectNext(LPCTSTR lpszHostAddress, UINT nHostPort) { DebugAssert(GetLayerState()==unconnected); DebugAssert(m_pOwnerSocket); BOOL res = FALSE; if (m_pNextLayer) res = m_pNextLayer->Connect(lpszHostAddress, nHostPort); else if (m_nFamily == AF_INET) { USES_CONVERSION; DebugAssert(lpszHostAddress != NULL); SOCKADDR_IN sockAddr; memset(&sockAddr,0,sizeof(sockAddr)); LPSTR lpszAscii = T2A((LPTSTR)lpszHostAddress); sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = inet_addr(lpszAscii); if (sockAddr.sin_addr.s_addr == INADDR_NONE) { LPHOSTENT lphost; lphost = gethostbyname(lpszAscii); if (lphost != NULL) sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; else { WSASetLastError(WSAEINVAL); res = FALSE; } } sockAddr.sin_port = htons((u_short)nHostPort); res = (SOCKET_ERROR != connect(m_pOwnerSocket->GetSocketHandle(), (SOCKADDR*)&sockAddr, sizeof(sockAddr)) ); } else if (m_nFamily == AF_INET6 || m_nFamily == AF_UNSPEC) { USES_CONVERSION; DebugAssert(lpszHostAddress != NULL); addrinfo hints, *res0, *res1; SOCKET hSocket; int error; char port[10]; if (p_freeaddrinfo) p_freeaddrinfo(m_addrInfo); m_nextAddr = 0; m_addrInfo = 0; memset(&hints, 0, sizeof(addrinfo)); hints.ai_family = m_nFamily; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = 0; _snprintf(port, 9, "%lu", nHostPort); error = p_getaddrinfo ? p_getaddrinfo(T2CA(lpszHostAddress), port, &hints, &res0) : 1; if (error) return FALSE; for (res1 = res0; res1; res1 = res1->ai_next) { if (m_nFamily == AF_UNSPEC) hSocket = socket(res1->ai_family, res1->ai_socktype, res1->ai_protocol); else hSocket = m_pOwnerSocket->GetSocketHandle(); if (INVALID_SOCKET == hSocket) { res = FALSE; continue; } if (m_nFamily == AF_UNSPEC) { m_pOwnerSocket->m_SocketData.hSocket = hSocket; m_pOwnerSocket->AttachHandle(hSocket); if (!m_pOwnerSocket->AsyncSelect(m_lEvent)) { m_pOwnerSocket->Close(); res = FALSE; continue ; } if (m_pOwnerSocket->m_pFirstLayer) { if (WSAAsyncSelect(m_pOwnerSocket->m_SocketData.hSocket, m_pOwnerSocket->GetHelperWindowHandle(), m_pOwnerSocket->m_SocketData.nSocketIndex+WM_SOCKETEX_NOTIFY, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) ) { m_pOwnerSocket->Close(); res = FALSE; continue; } } if (m_pOwnerSocket->m_pendingCallbacks.size()) PostMessage(m_pOwnerSocket->GetHelperWindowHandle(), WM_USER + 2, (WPARAM)m_pOwnerSocket->m_SocketData.nSocketIndex, 0); } if (m_nFamily == AF_UNSPEC) { m_pOwnerSocket->m_SocketData.nFamily = m_nFamily = res1->ai_family; if (!m_pOwnerSocket->Bind(m_nSocketPort, m_lpszSocketAddress)) { m_pOwnerSocket->m_SocketData.nFamily = m_nFamily = AF_UNSPEC; Close(); continue; } } if (!( res = ( SOCKET_ERROR != connect(m_pOwnerSocket->GetSocketHandle(), res1->ai_addr, res1->ai_addrlen) ) ) && WSAGetLastError() != WSAEWOULDBLOCK) { if (hints.ai_family == AF_UNSPEC) { m_nFamily = AF_UNSPEC; Close(); } continue ; } m_nFamily = res1->ai_family; m_pOwnerSocket->m_SocketData.nFamily = res1->ai_family; res = TRUE; break; } if (res1) res1 = res0->ai_next; if (res1) { m_addrInfo = res0; m_nextAddr = res1; } else { if (p_freeaddrinfo) p_freeaddrinfo(res0); } if (INVALID_SOCKET == m_pOwnerSocket->GetSocketHandle()) res = FALSE ; } if (res || WSAGetLastError() == WSAEWOULDBLOCK) { SetLayerState(connecting); } return res; }
int gitno_connect(gitno_socket *s_out, const char *host, const char *port, int flags) { struct addrinfo *info = NULL, *p; struct addrinfo hints; GIT_SOCKET s = INVALID_SOCKET; int ret; #ifdef GIT_WIN32 /* on win32, the WSA context needs to be initialized * before any socket calls can be performed */ WSADATA wsd; if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { giterr_set(GITERR_OS, "Winsock init failed"); return -1; } if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2) { WSACleanup(); giterr_set(GITERR_OS, "Winsock init failed"); return -1; } #endif /* Zero the socket structure provided */ memset(s_out, 0x0, sizeof(gitno_socket)); memset(&hints, 0x0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_UNSPEC; if ((ret = p_getaddrinfo(host, port, &hints, &info)) < 0) { giterr_set(GITERR_NET, "Failed to resolve address for %s: %s", host, p_gai_strerror(ret)); return -1; } for (p = info; p != NULL; p = p->ai_next) { s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (s == INVALID_SOCKET) { net_set_error("error creating socket"); break; } if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0) break; /* If we can't connect, try the next one */ gitno__close(s); s = INVALID_SOCKET; } /* Oops, we couldn't connect to any address */ if (s == INVALID_SOCKET && p == NULL) { giterr_set(GITERR_OS, "Failed to connect to %s", host); p_freeaddrinfo(info); return -1; } s_out->socket = s; p_freeaddrinfo(info); #ifdef GIT_SSL if ((flags & GITNO_CONNECT_SSL) && ssl_setup(s_out, host, flags) < 0) return -1; #else /* SSL is not supported */ if (flags & GITNO_CONNECT_SSL) { giterr_set(GITERR_OS, "SSL is not supported by this copy of libgit2."); return -1; } #endif return 0; }