Example #1
0
/*
 * Internal callback used to establish an outbound connection.
 */
static void net_connect_job_internal_cb(struct net_connection* con, int event, void* ptr)
{
	int ret;
	struct net_connect_job* job = net_con_get_ptr(con);
	struct net_connect_job* next_job = job->next;
	struct net_connect_handle* handle = job->handle;

	if (event == NET_EVENT_TIMEOUT)
	{
		// FIXME: Try next address, or if no more addresses left declare failure to connect.
		if (job->addr.ss_family == AF_INET6)
		{
			net_connect_job_stop(job);

			if (!next_job)
			{
				LOG_TRACE("No more IPv6 addresses to try!");
			}

		}
		else
		{
			net_connect_job_stop(job);

			if (!next_job)
			{
				LOG_TRACE("No more IPv4 addresses to try!");
			}
		}

		if (net_connect_depleted(handle))
		{
			LOG_TRACE("No more addresses left. Unable to connect!");
			net_connect_callback(handle, net_connect_status_timeout, NULL);
		}
		return;
	}

	if (event == NET_EVENT_WRITE)
	{
		net_connect_job_process(job);
	}
}
Example #2
0
static void event_callback(struct net_connection* con, int events, void *arg)
{
	ADC_TRACE;
	struct ADC_client* client = (struct ADC_client*) net_con_get_ptr(con);

	switch (client->state)
	{
		case ps_conn:
			if (events == NET_EVENT_TIMEOUT)
			{
				client->callback(client, ADC_CLIENT_DISCONNECTED, 0);
				return;
			}
			break;

#ifdef SSL_SUPPORT
		case ps_conn_ssl:
			if (events == NET_EVENT_TIMEOUT)
			{
				client->callback(client, ADC_CLIENT_DISCONNECTED, 0);
				return;
			}

			ADC_client_on_connected_ssl(client);
			break;
#endif

		default:
			if (events & NET_EVENT_READ)
			{
				if (ADC_client_recv(client) == -1)
				{
					ADC_client_on_disconnected(client);
				}
			}

			if (events & NET_EVENT_WRITE)
			{
				ADC_client_send_queue(client);
			}
	}
}
Example #3
0
static void probe_net_event(struct net_connection* con, int events, void *arg)
{
	struct hub_probe* probe = (struct hub_probe*) net_con_get_ptr(con);
	if (events == NET_EVENT_TIMEOUT)
	{
		char buf[512];
		ssize_t len = snprintf(buf, sizeof(buf), "This hub runs on ADC protocol only!\n\nThese are the possible reasons why you see this message:\n\n * you used address without protocol specification, e.g. example.com:1234 instead of adc://example.com:1234\n * your client does not support ADC protocol.\n|$ForceMove %s|", probe->hub->config->nmdc_only_redirect_addr);
		net_con_send(con, buf, (size_t) len);
		probe_destroy(probe);
		return;
	}

	if (events & NET_EVENT_READ)
	{
		int bytes = net_con_peek(con, probe_recvbuf, PROBE_RECV_SIZE);
		if (bytes < 0)
		{
			probe_destroy(probe);
			return;
		}

		if (bytes >= 4)
		{
			if (memcmp(probe_recvbuf, "HSUP", 4) == 0)
			{
				LOG_TRACE("Probed ADC");
#ifdef SSL_SUPPORT
				if (probe->hub->config->tls_enable && probe->hub->config->tls_require)
				{
					LOG_TRACE("Not TLS connection - closing connection.");
					if (*probe->hub->config->tls_require_redirect_addr)
					{
						char buf[512];
						ssize_t len = snprintf(buf, sizeof(buf), "ISUP " ADC_PROTO_SUPPORT "\nISID AAAB\nIINF NIRedirecting...\nIQUI AAAB RD%s\n", probe->hub->config->tls_require_redirect_addr);
						net_con_send(con, buf, (size_t) len);
						LOG_TRACE("Not TLS connection - Redirecting to %s.", probe->hub->config->tls_require_redirect_addr);
					}
					else
					{
						LOG_TRACE("Not TLS connection - closing connection.");
					}
				}
				else
#endif
				if (user_create(probe->hub, probe->connection, &probe->addr))
				{
					probe->connection = 0;
				}
				probe_destroy(probe);
				return;
			}
			else if ((memcmp(probe_recvbuf, "GET ", 4) == 0) ||
				 (memcmp(probe_recvbuf, "POST", 4) == 0) ||
				 (memcmp(probe_recvbuf, "HEAD", 4) == 0))
			{
				/* Looks like HTTP - Not supported, but we log it. */
				LOG_TRACE("Probed HTTP connection. Not supported closing connection (%s)", ip_convert_to_string(&probe->addr));
				const char* buf = "501 Not implemented\r\n\r\n";
				net_con_send(con, buf, strlen(buf));
			}
#ifdef SSL_SUPPORT
			else if (bytes >= 11 &&
				probe_recvbuf[0] == 22 &&
				probe_recvbuf[1] == 3 && /* protocol major version */
				probe_recvbuf[5] == 1 && /* message type */
				probe_recvbuf[9] == probe_recvbuf[1])
			{
				if (probe->hub->config->tls_enable)
				{
					LOG_TRACE("Probed TLS %d.%d connection", (int) probe_recvbuf[1], (int) probe_recvbuf[2]);
					if (user_create(probe->hub, probe->connection, &probe->addr))
					{
						probe->connection = 0;
					}
					net_con_ssl_handshake(con, net_con_ssl_mode_server, probe->hub->ctx);
				}
				else
				{
					LOG_TRACE("Probed TLS %d.%d connection. TLS disabled in hub.", (int) probe_recvbuf[9], (int) probe_recvbuf[10]);
				}
			}
			else
			{
				LOG_TRACE("Probed unsupported protocol: %x%x%x%x.", (int) probe_recvbuf[0], (int) probe_recvbuf[1], (int) probe_recvbuf[2], (int) probe_recvbuf[3]);
			}
#endif
			probe_destroy(probe);
			return;
		}
	}
}