Esempio n. 1
0
bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports)
{
	for (std::vector<ListenSocket*>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
	{
		if ((**n).bind_sa == sa)
		{
			// Replace tag, we know addr and port match, but other info (type, ssl) may not.
			ServerInstance->Logs.Log("SOCKET", LOG_DEFAULT, "Replacing listener on %s from old tag at %s with new tag from %s",
				sa.str().c_str(), (*n)->bind_tag->getTagLocation().c_str(), tag->getTagLocation().c_str());
			(*n)->bind_tag = tag;
			(*n)->ResetIOHookProvider();

			old_ports.erase(n);
			return true;
		}
	}

	ListenSocket* ll = new ListenSocket(tag, sa);
	if (ll->GetFd() < 0)
	{
		ServerInstance->Logs.Log("SOCKET", LOG_DEFAULT, "Failed to listen on %s from tag at %s: %s",
			sa.str().c_str(), tag->getTagLocation().c_str(), strerror(errno));
		delete ll;
		return false;
	}

	ServerInstance->Logs.Log("SOCKET", LOG_DEFAULT, "Added a listener on %s from tag at %s", sa.str().c_str(), tag->getTagLocation().c_str());
	ports.push_back(ll);
	return true;
}
Esempio n. 2
0
bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const
{
	if (addr.family() != type)
		return false;
	irc::sockets::cidr_mask tmp(addr, length);
	return tmp == *this;
}
Esempio n. 3
0
BufferedSocketError BufferedSocket::BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned long timeout)
{
	if (fd < 0)
		fd = socket(dest.sa.sa_family, SOCK_STREAM, 0);

	if (fd < 0)
		return I_ERR_SOCKET;

	if (bind.sa.sa_family != 0)
	{
		if (SocketEngine::Bind(fd, bind) < 0)
			return I_ERR_BIND;
	}

	SocketEngine::NonBlocking(fd);

	if (SocketEngine::Connect(this, &dest.sa, dest.sa_size()) == -1)
	{
		if (errno != EINPROGRESS)
			return I_ERR_CONNECT;
	}

	this->state = I_CONNECTING;

	if (!SocketEngine::AddFd(this, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE | FD_WRITE_WILL_BLOCK))
		return I_ERR_NOMOREFDS;

	this->Timeout = new SocketTimeout(this->GetFd(), this, timeout);
	ServerInstance->Timers.AddTimer(this->Timeout);

	ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "BufferedSocket::DoConnect success");
	return I_ERR_NONE;
}
Esempio n. 4
0
static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range)
{
	const unsigned char* base;
	unsigned char target_byte;

	memset(cidr.bits, 0, sizeof(cidr.bits));

	cidr.type = sa.family();
	switch (cidr.type)
	{
		case AF_UNIX:
			// XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't
			// really think of another way to handle it.
			cidr.length = 0;
			return;

		case AF_INET:
			cidr.length = range > 32 ? 32 : range;
			target_byte = sizeof(sa.in4.sin_addr);
			base = (unsigned char*)&sa.in4.sin_addr;
			break;

		case AF_INET6:
			cidr.length = range > 128 ? 128 : range;
			target_byte = sizeof(sa.in6.sin6_addr);
			base = (unsigned char*)&sa.in6.sin6_addr;
			break;

		default:
			// If we have reached this point then we have encountered a bug.
			ServerInstance->Logs.Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type);
			cidr.length = 0;
			return;
	}

	unsigned int border = cidr.length / 8;
	unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF;
	for(unsigned int i=0; i < target_byte; i++)
	{
		if (i < border)
			cidr.bits[i] = base[i];
		else if (i == border)
			cidr.bits[i] = base[i] & bitmask;
		else
			return;
	}
}
Esempio n. 5
0
bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const
{
	if (family() != other.family())
		return false;

	switch (family())
	{
		case AF_INET:
			return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr);

		case AF_INET6:
			return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16);

		case AF_UNIX:
			return !strcmp(un.sun_path, other.un.sun_path);
	}

	// If we have reached this point then we have encountered a bug.
	ServerInstance->Logs.Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family());
	return !memcmp(this, &other, sizeof(*this));
}
Esempio n. 6
0
bool irc::sockets::satoap(const irc::sockets::sockaddrs& sa, std::string& addr, int &port)
{
	port = sa.port();
	addr = sa.addr();
	return !addr.empty();
}
Esempio n. 7
0
int SocketEngine::Bind(int fd, const irc::sockets::sockaddrs& addr)
{
	return bind(fd, &addr.sa, addr.sa_size());
}
Esempio n. 8
0
ListenSocket::ListenSocket(ConfigTag* tag, const irc::sockets::sockaddrs& bind_to)
	: bind_tag(tag)
{
	irc::sockets::satoap(bind_to, bind_addr, bind_port);
	bind_desc = bind_to.str();

	fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);

	if (this->fd == -1)
		return;

#ifdef IPV6_V6ONLY
	/* This OS supports IPv6 sockets that can also listen for IPv4
	 * connections. If our address is "*" or empty, enable both v4 and v6 to
	 * allow for simpler configuration on dual-stack hosts. Otherwise, if it
	 * is "::" or an IPv6 address, disable support so that an IPv4 bind will
	 * work on the port (by us or another application).
	 */
	if (bind_to.sa.sa_family == AF_INET6)
	{
		std::string addr = tag->getString("address");
		/* This must be >= sizeof(DWORD) on Windows */
		const int enable = (addr.empty() || addr == "*") ? 0 : 1;
		/* This must be before bind() */
		setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&enable), sizeof(enable));
		// errors ignored intentionally
	}
#endif

	ServerInstance->SE->SetReuse(fd);
	int rv = ServerInstance->SE->Bind(this->fd, bind_to);
	if (rv >= 0)
		rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);

	int timeout = tag->getInt("defer", 0);
	if (timeout && !rv)
	{
#if defined TCP_DEFER_ACCEPT
		setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, sizeof(timeout));
#elif defined SO_ACCEPTFILTER
		struct accept_filter_arg afa;
		memset(&afa, 0, sizeof(afa));
		strcpy(afa.af_name, "dataready");
		setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
#endif
	}

	if (rv < 0)
	{
		int errstore = errno;
		ServerInstance->SE->Shutdown(this, 2);
		ServerInstance->SE->Close(this);
		this->fd = -1;
		errno = errstore;
	}
	else
	{
		ServerInstance->SE->NonBlocking(this->fd);
		ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
	}
}