Example #1
0
File: Socket.cpp Project: crnt/x0
bool Socket::openTcp(const IPAddress& host, int port, int flags)
{
#ifndef NDEBUG
	setLoggingPrefix("Socket(tcp:%s:%d)", host.str().c_str(), port);
#endif

	flags |= O_NONBLOCK | O_CLOEXEC;

	int typeMask = 0;

#if defined(SOCK_NONBLOCK)
	if (flags & O_NONBLOCK) {
		flags &= ~O_NONBLOCK;
		typeMask |= SOCK_NONBLOCK;
	}
#endif

#if defined(SOCK_CLOEXEC)
	if (flags & O_CLOEXEC) {
		flags &= ~O_CLOEXEC;
		typeMask |= SOCK_CLOEXEC;
	}
#endif

	fd_ = ::socket(host.family(), SOCK_STREAM | typeMask, IPPROTO_TCP);
	if (fd_ < 0) {
		TRACE("socket creation error: %s",  strerror(errno));
		return false;
	}

	if (flags) {
		if (fcntl(fd_, F_SETFL, fcntl(fd_, F_GETFL) | flags) < 0) {
			// error
		}
	}

	int rv = ::connect(fd_, (struct sockaddr*) host.data(), host.size());
	if (rv == 0) {
		TRACE("connect: instant success (fd:%d)", fd_);
		state_ = Operational;
		return true;
	} else if (/*rv < 0 &&*/ errno == EINPROGRESS) {
		TRACE("connect: backgrounding (fd:%d)", fd_);
		state_ = Connecting;
		setMode(Write);
		return true;
	} else {
		TRACE("could not connect to %s:%d: %s", host.str().c_str(), port, strerror(errno));
		::close(fd_);
		fd_ = -1;
		return false;
	}
}
Example #2
0
// Test subnet mask calculations
TEST_P(IPAddressMaskTest, Masks) {
  auto param = GetParam();

  IPAddress ip(param.address);
  IPAddress masked = ip.mask(param.mask);
  EXPECT_EQ(param.subnet, masked.str())
      << param.address << "/" << folly::to<std::string>(param.mask) << " -> "
      << param.subnet;
}
Example #3
0
void TunIntf::addAddress(const IPAddress& addr, uint8_t mask) {
  auto ret = addrs_.emplace(addr, mask);
  if (!ret.second) {
    throw FbossError("Duplication interface address ", addr, "/",
                     static_cast<int>(mask), " for interface ", name_,
                     " @ index", ifIndex_);
  }
  VLOG(3) << "Added address " << addr.str() << "/"
          << static_cast<int>(mask) << " to interface " << name_
          << " @ index " << ifIndex_;
}
Example #4
0
void TunManager::addProbedAddr(int ifIndex, const IPAddress& addr,
                               uint8_t mask) {
  for (auto& intf : intfs_) {
    if (intf.second->getIfIndex() == ifIndex) {
      intf.second->addAddress(addr, mask);
      return;
    }
  }
  // This function is called for all interface addresses discovered from
  // the host. Since we only create TunIntf object for TUN interfaces,
  // it is normal we cannot find the interface matching the ifIndex provided.
  VLOG(3) << "Cannot find interface @ index " << ifIndex
          << " for probed address " << addr.str() << "/"
          << static_cast<int>(mask);
}
Example #5
0
void toAppend(IPAddress addr, fbstring* result) {
  result->append(addr.str());
}