Exemple #1
0
void CAsyncSocketExLayer::CloseNext()
{
  if (m_addrInfo)
  {
    if (p_freeaddrinfo) p_freeaddrinfo(m_addrInfo);
  }
  m_nextAddr = 0;
  m_addrInfo = 0;

  m_nPendingEvents = 0;

  SetLayerState(notsock);
  if (m_pNextLayer)
    m_pNextLayer->Close();
}
Exemple #2
0
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;
}
Exemple #3
0
bool CAsyncSocketExLayer::TryNextProtocol()
{
  m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
  closesocket(m_pOwnerSocket->m_SocketData.hSocket);
  m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;

  BOOL ret = FALSE;
  for (; m_nextAddr; m_nextAddr = m_nextAddr->ai_next)
  {
    m_pOwnerSocket->m_SocketData.hSocket = socket(m_nextAddr->ai_family, m_nextAddr->ai_socktype, m_nextAddr->ai_protocol);

    if (m_pOwnerSocket->m_SocketData.hSocket == INVALID_SOCKET)
      continue;

    m_pOwnerSocket->AttachHandle(m_pOwnerSocket->m_SocketData.hSocket);
    if (!m_pOwnerSocket->AsyncSelect(m_lEvent))
    {
      m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
      closesocket(m_pOwnerSocket->m_SocketData.hSocket);
      m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
      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->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
        closesocket(m_pOwnerSocket->m_SocketData.hSocket);
        m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
        continue;
      }
    }

    m_pOwnerSocket->m_SocketData.nFamily = m_nextAddr->ai_family;
    m_nFamily = m_nextAddr->ai_family;
    if (!m_pOwnerSocket->Bind(m_nSocketPort, m_lpszSocketAddress))
    {
      m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
      closesocket(m_pOwnerSocket->m_SocketData.hSocket);
      m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
      continue;
    }

    if (connect(m_pOwnerSocket->GetSocketHandle(), m_nextAddr->ai_addr, m_nextAddr->ai_addrlen) == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
    {
      m_pOwnerSocket->DetachHandle(m_pOwnerSocket->m_SocketData.hSocket);
      closesocket(m_pOwnerSocket->m_SocketData.hSocket);
      m_pOwnerSocket->m_SocketData.hSocket = INVALID_SOCKET;
      continue;
    }

    SetLayerState(connecting);

    ret = true;
    break;
  }

  if (m_nextAddr)
    m_nextAddr = m_nextAddr->ai_next;

  if (!m_nextAddr)
  {
    if (p_freeaddrinfo) p_freeaddrinfo(m_addrInfo);
    m_nextAddr = 0;
    m_addrInfo = 0;
  }

  if (m_pOwnerSocket->m_SocketData.hSocket == INVALID_SOCKET || !ret)
    return FALSE;
  else
    return TRUE;
}
Exemple #4
0
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;
}
Exemple #5
0
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;
}