mirror of
https://github.com/irssi/irssi.git
synced 2026-08-17 07:32:04 +02:00
net_connect*() contains now error parameter, so it can be used to properly
check the errno if connect() fails. Added support for connecting to named UNIX sockets. Some cleanups with session handling / server connecting as well. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@2819 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
parent
7437bbea5f
commit
6f7485b8fa
13 changed files with 180 additions and 92 deletions
|
|
@ -161,16 +161,47 @@ static void server_connect_callback_init(SERVER_REC *server, GIOChannel *handle)
|
|||
server_connect_finished(server);
|
||||
}
|
||||
|
||||
static void server_real_connect(SERVER_REC *server, IPADDR *ip,
|
||||
const char *unix_socket)
|
||||
{
|
||||
GIOChannel *handle;
|
||||
IPADDR *own_ip;
|
||||
int port, error;
|
||||
|
||||
g_return_if_fail(ip != NULL || unix_socket != NULL);
|
||||
|
||||
signal_emit("server connecting", 2, server, ip);
|
||||
|
||||
if (ip != NULL) {
|
||||
own_ip = ip == NULL ? NULL :
|
||||
(IPADDR_IS_V6(ip) ? server->connrec->own_ip6 :
|
||||
server->connrec->own_ip4);
|
||||
port = server->connrec->proxy != NULL ?
|
||||
server->connrec->proxy_port : server->connrec->port;
|
||||
handle = net_connect_ip(ip, port, own_ip, &error);
|
||||
} else {
|
||||
handle = net_connect_unix(unix_socket, &error);
|
||||
}
|
||||
|
||||
if (handle == NULL) {
|
||||
/* failed */
|
||||
server->connection_lost = TRUE;
|
||||
server_connect_failed(server, g_strerror(error));
|
||||
} else {
|
||||
server->handle = net_sendbuffer_create(handle, 0);
|
||||
server->connect_tag =
|
||||
g_input_add(handle, G_INPUT_WRITE | G_INPUT_READ,
|
||||
(GInputFunction)
|
||||
server_connect_callback_init,
|
||||
server);
|
||||
}
|
||||
}
|
||||
|
||||
static void server_connect_callback_readpipe(SERVER_REC *server)
|
||||
{
|
||||
SERVER_CONNECT_REC *conn;
|
||||
RESOLVED_IP_REC iprec;
|
||||
GIOChannel *handle;
|
||||
IPADDR *ip, *own_ip;
|
||||
IPADDR *ip;
|
||||
const char *errormsg;
|
||||
int port;
|
||||
|
||||
g_return_if_fail(IS_SERVER(server));
|
||||
|
||||
g_source_remove(server->connect_tag);
|
||||
server->connect_tag = -1;
|
||||
|
|
@ -204,33 +235,18 @@ static void server_connect_callback_readpipe(SERVER_REC *server)
|
|||
&iprec.ip6 : &iprec.ip4;
|
||||
}
|
||||
|
||||
conn = server->connrec;
|
||||
port = conn->proxy != NULL ? conn->proxy_port : conn->port;
|
||||
own_ip = ip == NULL ? NULL :
|
||||
(IPADDR_IS_V6(ip) ? conn->own_ip6 : conn->own_ip4);
|
||||
|
||||
handle = NULL;
|
||||
if (ip != NULL) {
|
||||
signal_emit("server connecting", 2, server, ip);
|
||||
if (server->handle == NULL)
|
||||
handle = net_connect_ip(ip, port, own_ip);
|
||||
else
|
||||
handle = net_sendbuffer_handle(server->handle);
|
||||
}
|
||||
|
||||
if (handle == NULL) {
|
||||
/* failed */
|
||||
if (ip == NULL && (iprec.error == 0 ||
|
||||
net_hosterror_notfound(iprec.error))) {
|
||||
/* IP wasn't found for the host, don't try to reconnect
|
||||
back to this server */
|
||||
/* host lookup ok */
|
||||
server_real_connect(server, ip, NULL);
|
||||
errormsg = NULL;
|
||||
} else {
|
||||
if (iprec.error == 0 || net_hosterror_notfound(iprec.error)) {
|
||||
/* IP wasn't found for the host, don't try to
|
||||
reconnect back to this server */
|
||||
server->dns_error = TRUE;
|
||||
}
|
||||
|
||||
if (ip != NULL) {
|
||||
/* connect() failed */
|
||||
errormsg = g_strerror(errno);
|
||||
} else if (iprec.error == 0) {
|
||||
if (iprec.error == 0) {
|
||||
/* forced IPv4 or IPv6 address but it wasn't found */
|
||||
errormsg = server->connrec->family == AF_INET ?
|
||||
"IPv4 address not found for host" :
|
||||
|
|
@ -240,18 +256,12 @@ static void server_connect_callback_readpipe(SERVER_REC *server)
|
|||
errormsg = iprec.errorstr != NULL ? iprec.errorstr :
|
||||
"Host lookup failed";
|
||||
}
|
||||
|
||||
server->connection_lost = TRUE;
|
||||
server_connect_failed(server, errormsg);
|
||||
g_free_not_null(iprec.errorstr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (server->handle == NULL)
|
||||
server->handle = net_sendbuffer_create(handle, 0);
|
||||
server->connect_tag =
|
||||
g_input_add(handle, G_INPUT_WRITE | G_INPUT_READ,
|
||||
(GInputFunction) server_connect_callback_init,
|
||||
server);
|
||||
g_free(iprec.errorstr);
|
||||
}
|
||||
|
||||
/* initializes server record but doesn't start connecting */
|
||||
|
|
@ -282,6 +292,7 @@ void server_connect_init(SERVER_REC *server)
|
|||
}
|
||||
|
||||
server->tag = server_create_tag(server->connrec);
|
||||
server->connect_tag = -1;
|
||||
}
|
||||
|
||||
/* starts connecting to server */
|
||||
|
|
@ -291,34 +302,56 @@ int server_start_connect(SERVER_REC *server)
|
|||
int fd[2];
|
||||
|
||||
g_return_val_if_fail(server != NULL, FALSE);
|
||||
if (server->connrec->port <= 0) return FALSE;
|
||||
if (!server->connrec->unix_socket && server->connrec->port <= 0)
|
||||
return FALSE;
|
||||
|
||||
server_connect_init(server);
|
||||
|
||||
if (pipe(fd) != 0) {
|
||||
g_warning("server_connect(): pipe() failed.");
|
||||
g_free(server->tag);
|
||||
g_free(server->nick);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
server->connect_pipe[0] = g_io_channel_unix_new(fd[0]);
|
||||
server->connect_pipe[1] = g_io_channel_unix_new(fd[1]);
|
||||
|
||||
connect_address = server->connrec->proxy != NULL ?
|
||||
server->connrec->proxy : server->connrec->address;
|
||||
server->connect_pid =
|
||||
net_gethostbyname_nonblock(connect_address,
|
||||
server->connect_pipe[1]);
|
||||
server->connect_tag =
|
||||
g_input_add(server->connect_pipe[0], G_INPUT_READ,
|
||||
(GInputFunction) server_connect_callback_readpipe,
|
||||
server);
|
||||
server->rawlog = rawlog_create();
|
||||
|
||||
lookup_servers = g_slist_append(lookup_servers, server);
|
||||
if (server->connrec->session_reconnect) {
|
||||
/* /UPGRADE connection - the session_connect is meant
|
||||
for us only once, move it into server->session_connect */
|
||||
server->connrec->session_reconnect = FALSE;
|
||||
server->session_reconnect = TRUE;
|
||||
}
|
||||
|
||||
signal_emit("server looking", 1, server);
|
||||
if (server->connrec->connect_handle != NULL) {
|
||||
/* already connected */
|
||||
GIOChannel *handle = server->connrec->connect_handle;
|
||||
|
||||
server->connrec->connect_handle = NULL;
|
||||
server->handle = net_sendbuffer_create(handle, 0);
|
||||
server_connect_finished(server);
|
||||
} else if (server->connrec->unix_socket) {
|
||||
/* connect with unix socket */
|
||||
server_real_connect(server, NULL, server->connrec->address);
|
||||
} else {
|
||||
/* resolve host name */
|
||||
if (pipe(fd) != 0) {
|
||||
g_warning("server_connect(): pipe() failed.");
|
||||
g_free(server->tag);
|
||||
g_free(server->nick);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
server->connect_pipe[0] = g_io_channel_unix_new(fd[0]);
|
||||
server->connect_pipe[1] = g_io_channel_unix_new(fd[1]);
|
||||
|
||||
connect_address = server->connrec->proxy != NULL ?
|
||||
server->connrec->proxy : server->connrec->address;
|
||||
server->connect_pid =
|
||||
net_gethostbyname_nonblock(connect_address,
|
||||
server->connect_pipe[1]);
|
||||
server->connect_tag =
|
||||
g_input_add(server->connect_pipe[0], G_INPUT_READ,
|
||||
(GInputFunction)
|
||||
server_connect_callback_readpipe,
|
||||
server);
|
||||
|
||||
lookup_servers = g_slist_append(lookup_servers, server);
|
||||
|
||||
signal_emit("server looking", 1, server);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
@ -486,6 +519,9 @@ void server_connect_unref(SERVER_CONNECT_REC *conn)
|
|||
|
||||
CHAT_PROTOCOL(conn)->destroy_server_connect(conn);
|
||||
|
||||
if (conn->connect_handle != NULL)
|
||||
net_disconnect(conn->connect_handle);
|
||||
|
||||
g_free_not_null(conn->proxy);
|
||||
g_free_not_null(conn->proxy_string);
|
||||
g_free_not_null(conn->proxy_string_after);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue