Пример #1
0
/*
 * @brief
 */
void Net_Config(net_src_t source, _Bool up) {
	uint16_t p = 0;

	if (source == NS_SERVER) {
		p = (uint16_t) net_port->integer;
	}

	if (up) { // open the socket
		if (!ip_sockets[source])
			ip_sockets[source] = Net_Socket(net_ip->string, p);
		return;
	}

	// or close it
	if (ip_sockets[source])
		Net_CloseSocket(ip_sockets[source]);

	ip_sockets[source] = 0;
}
Пример #2
0
/**
 * @brief Establishes a TCP connection to the specified host.
 *
 * @param host The host to connect to. See Net_StringToNetaddr.
 */
int32_t Net_Connect(const char *host, struct timeval *timeout) {

	int32_t sock = Net_Socket(NA_STREAM, NULL, 0);

	struct sockaddr_in to;
	Net_StringToSockaddr(host, &to);

	if (connect(sock, (const struct sockaddr *) &to, sizeof(to)) == -1) {

		if (Net_GetError() == EINPROGRESS) {
			fd_set w_set;

			FD_ZERO(&w_set);
			FD_SET((uint32_t) sock, &w_set);

			if (select(sock + 1, NULL, &w_set, NULL, timeout) < 1) {
				Com_Error(ERROR_DROP, "%s\n", Net_GetErrorString());
			}

			int32_t error = -1;
			socklen_t len = sizeof(error);

			if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *) &error, &len) < 0) {
				Com_Error(ERROR_DROP, "%s\n", Net_GetErrorString());
			}

			if (error) {
				Com_Error(ERROR_DROP, "%s\n", strerror(error));
			}
		} else {
			Com_Error(ERROR_DROP, "%s\n", Net_GetErrorString());
		}
	}

	return sock;
}