Exemple #1
0
void Connection::setLocalAddress(const IPAddress &addr) {
  if (addr.getIP()) {
    string ip(IPAddress(addr.getIP()).getHost());
    evhttp_connection_set_local_address(con, ip.c_str());
  }

  if (addr.getPort()) evhttp_connection_set_local_port(con, addr.getPort());
}
void SocketDefaultImpl::connect(const IPAddress &ip) {
  if (!isOpen()) open();

  LOG_INFO(3, "Connecting to " << ip);

  try {
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));

    sin.sin_family = AF_INET;
    sin.sin_port = htons((short)ip.getPort());
    sin.sin_addr.s_addr = htonl(ip);

    SysError::clear();
    if (::connect((socket_t)socket, (struct sockaddr *)&sin, sizeof(sin)) == -1)
      if (SysError::get() != EINPROGRESS)
        THROWS("Failed to connect to " << ip << ": " << SysError());

    connected = true;
    capture(ip, false);

  } catch (const Exception &e) {
    close();

    throw;
  }
}
Exemple #3
0
Connection::Connection(cb::Event::Base &base, DNSBase &dns, BufferEvent &bev,
                       const IPAddress &peer) :
  con(evhttp_connection_base_bufferevent_new
      (base.getBase(), dns.getDNSBase(), bev.adopt(), peer.getHost().c_str(),
       peer.getPort())), deallocate(true) {
  if (!con) THROWS("Failed to create connection to " << peer);
}
void SocketDefaultImpl::bind(const IPAddress &ip) {
  if (!isOpen()) open();

  struct sockaddr_in addr;

  memset((void *)&addr, 0, sizeof(addr));
  addr.sin_family      = AF_INET;
  addr.sin_port        = htons(ip.getPort());
  addr.sin_addr.s_addr = htonl((unsigned)ip ? (unsigned)ip : INADDR_ANY);

  SysError::clear();
  if (::bind((socket_t)socket, (struct sockaddr *)&addr, sizeof(addr)) ==
      SOCKET_ERROR)
    THROWS("Could not bind socket to " << ip << ": " << SysError());
}