Example #1
0
/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_connected(SEND_DCC_REC *dcc)
{
        GIOChannel *handle;
	IPADDR addr;
	int port;

	/* accept connection */
	handle = net_accept(dcc->handle, &addr, &port);
	if (handle == NULL)
		return;

	/* TODO: some kind of paranoia check would be nice. it would check
	   that the host of the nick who we sent the request matches the
	   address who connected us. */

	net_disconnect(dcc->handle);
	g_source_remove(dcc->tagconn);
        dcc->tagconn = -1;

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	memcpy(&dcc->addr, &addr, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;

	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_send_read_size, dcc);
	dcc->tagwrite = g_input_add(handle, G_INPUT_WRITE,
				    (GInputFunction) dcc_send_data, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Example #2
0
/* Send data, if all of it couldn't be sent immediately, it will be resent
   automatically after a while. Returns -1 if some unrecoverable error
   occured. */
int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size)
{
	int ret;

	g_return_val_if_fail(rec != NULL, -1);
	g_return_val_if_fail(data != NULL, -1);
	if (size <= 0) return 0;

	if (rec->buffer == NULL) {
                /* nothing in buffer - transmit immediately */
		ret = net_transmit(rec->handle, data, size);
		if (ret < 0) return -1;
		size -= ret;
		data = ((const char *) data) + ret;
	}

	if (size <= 0)
		return 0;

	/* everything couldn't be sent. */
	if (rec->send_tag == -1) {
		rec->send_tag =
			g_input_add(rec->handle, G_INPUT_WRITE,
				    (GInputFunction) sig_sendbuffer, rec);
	}

	return buffer_add(rec, data, size) ? 0 : -1;
}
Example #3
0
int perl_input_add(int source, int condition, SV *func, SV *data, int once)
{
        PERL_SCRIPT_REC *script;
	PERL_SOURCE_REC *rec;
	GIOChannel *channel;
        const char *pkg;

        pkg = perl_get_package();
	script = perl_script_find_package(pkg);
        g_return_val_if_fail(script != NULL, -1);

	rec = g_new0(PERL_SOURCE_REC, 1);
	perl_source_ref(rec);

	rec->once = once;
        rec->script =script;
	rec->func = perl_func_sv_inc(func, pkg);
	rec->data = SvREFCNT_inc(data);

	channel = g_io_channel_unix_new(source);
	rec->tag = g_input_add(channel, condition,
			       (GInputFunction) perl_source_event, rec);
	g_io_channel_unref(channel);

	perl_sources = g_slist_append(perl_sources, rec);
	return rec->tag;
}
Example #4
0
static int botnet_listen(BOTNET_REC *botnet)
{
	IPADDR addr;
	int port;

	g_return_val_if_fail(botnet != NULL, FALSE);

	if (botnet->port <= 0)
		return FALSE;

	port = botnet->port;
	if (botnet->addr == NULL)
		botnet->listen_handle = net_listen(NULL, &port);
	else {
		net_host2ip(botnet->addr, &addr);
		botnet->listen_handle = net_listen(&addr, &port);
	}

	if (botnet->listen_handle == -1) {
		g_warning("Couldn't start listening botnet\n");
		return FALSE;
	}

	botnet->listen_tag = g_input_add(botnet->listen_handle, G_INPUT_READ,
					 (GInputFunction) sig_botnet_listen, botnet);

	return TRUE;
}
Example #5
0
/* Connect to server, call func when finished */
int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
                         NET_CALLBACK func, void *data)
{
    SIMPLE_THREAD_REC *rec;
    int fd[2];

    g_return_val_if_fail(server != NULL, FALSE);
    g_return_val_if_fail(func != NULL, FALSE);

    if (pipe(fd) != 0) {
        g_warning("net_connect_nonblock(): pipe() failed.");
        return FALSE;
    }

    rec = g_new0(SIMPLE_THREAD_REC, 1);
    rec->port = port;
    if (my_ip != NULL) {
        rec->my_ip = g_malloc(sizeof(IPADDR));
        memcpy(rec->my_ip, my_ip, sizeof(IPADDR));
    }
    rec->func = func;
    rec->data = data;
    rec->pipes[0] = g_io_channel_new(fd[0]);
    rec->pipes[1] = g_io_channel_new(fd[1]);

    /* start nonblocking host name lookup */
    net_gethostbyname_nonblock(server, rec->pipes[1], 0);
    rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
                           (GInputFunction) simple_readpipe, rec);

    return TRUE;
}
Example #6
0
/* input function: DCC CHAT - someone tried to connect to our socket */
static void dcc_chat_listen(DCC_REC *dcc)
{
	IPADDR ip;
	int handle, port;

	g_return_if_fail(dcc != NULL);

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == -1)
		return;

	/* TODO: add paranoia check - see dcc-files.c */

	g_source_remove(dcc->tagread);
	close(dcc->handle);

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	memcpy(&dcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;
	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_input, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Example #7
0
static void server_real_connect(SERVER_REC *server, IPADDR *ip,
				const char *unix_socket)
{
	GIOChannel *handle;
	IPADDR *own_ip = NULL;
	const char *errmsg;
	char *errmsg2;
	char ipaddr[MAX_IP_LEN];
        int port, protonum;

	g_return_if_fail(ip != NULL || unix_socket != NULL);

	signal_emit("server connecting", 2, server, ip);

	if (server->connrec->no_connect)
		return;

	if (ip != NULL) {
		own_ip = IPADDR_IS_V6(ip) ? server->connrec->own_ip6 : server->connrec->own_ip4;
		port = server->connrec->proxy != NULL ?
			server->connrec->proxy_port : server->connrec->port;
		protonum = server->connrec->use_sctp ? 132 : 0;
		handle = server->connrec->use_ssl ?
			net_connect_ip_ssl(ip, port, own_ip, server, protonum) : net_connect_ip(ip, port, own_ip, protonum);
	} else {
		handle = net_connect_unix(unix_socket);
	}

	if (handle == NULL) {
		/* failed */
		errmsg = g_strerror(errno);
		errmsg2 = NULL;
		if (errno == EADDRNOTAVAIL) {
			if (own_ip != NULL) {
				/* show the IP which is causing the error */
				net_ip2host(own_ip, ipaddr);
				errmsg2 = g_strconcat(errmsg, ": ", ipaddr, NULL);
			}
			server->no_reconnect = TRUE;
		}
		if (server->connrec->use_ssl && errno == ENOSYS)
			server->no_reconnect = TRUE;

		server->connection_lost = TRUE;
		server_connect_failed(server, errmsg2 ? errmsg2 : errmsg);
		g_free(errmsg2);
	} else {
		server->handle = net_sendbuffer_create(handle, 0);
#ifdef HAVE_OPENSSL
		if (server->connrec->use_ssl)
			server_connect_callback_init_ssl(server, handle);
		else
#endif
		server->connect_tag =
			g_input_add(handle, G_INPUT_WRITE | G_INPUT_READ,
				    (GInputFunction)
				    server_connect_callback_init,
				    server);
	}
}
Example #8
0
static void dcc_chat_passive(CHAT_DCC_REC *dcc)
{
	IPADDR own_ip;
	int port;
	GIOChannel *handle;
	char host[MAX_IP_LEN];

	g_return_if_fail(IS_DCC_CHAT(dcc));

	if (dcc->addrstr[0] == '\0' ||
	    dcc->starttime != 0 || dcc->handle != NULL) {
		/* already sent a chat request / already chatting */
		return;
	}

	handle = dcc_listen(net_sendbuffer_handle(dcc->server->handle),
			    &own_ip, &port);
	if (handle == NULL)
		cmd_return_error(CMDERR_ERRNO);

	dcc->handle = handle;
	dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_listen, dcc);

	/* Let's send the reply to the other client! */
	dcc_ip2str(&own_ip, host);
	irc_send_cmdv(dcc->server, "PRIVMSG %s :\001DCC CHAT CHAT %s %d %d\001",
		      dcc->nick, host, port, dcc->pasv_id);

}
Example #9
0
static void server_connect_callback_init_ssl(SERVER_REC *server, GIOChannel *handle)
{
	int error;

	g_return_if_fail(IS_SERVER(server));

	error = irssi_ssl_handshake(handle);
	if (error == -1) {
		server->connection_lost = TRUE;
		server_connect_failed(server, NULL);
		return;
	}
	if (error & 1) {
		if (server->connect_tag != -1)
			g_source_remove(server->connect_tag);
		server->connect_tag = g_input_add(handle, error == 1 ? G_INPUT_READ : G_INPUT_WRITE,
						  (GInputFunction)
						  server_connect_callback_init_ssl,
						  server);
		return;
	}

	lookup_servers = g_slist_remove(lookup_servers, server);
	if (server->connect_tag != -1) {
		g_source_remove(server->connect_tag);
		server->connect_tag = -1;
	}

	server_connect_finished(server);
}
Example #10
0
/* input function: DCC CHAT - someone tried to connect to our socket */
static void dcc_chat_listen(CHAT_DCC_REC *dcc)
{
	IPADDR ip;
        GIOChannel *handle;
	int port;

	g_return_if_fail(IS_DCC_CHAT(dcc));

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == NULL)
		return;

	/* TODO: add paranoia check - see dcc-files.c */

	net_disconnect(dcc->handle);
	g_source_remove(dcc->tagconn);
	dcc->tagconn = -1;

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	dcc->sendbuf = net_sendbuffer_create(handle, 0);
	memcpy(&dcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;
	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_input, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Example #11
0
static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
{
    RESOLVED_IP_REC iprec;
    GIOChannel *handle;
    IPADDR *ip;

    g_return_if_fail(rec != NULL);

    g_source_remove(rec->tag);

    net_gethostbyname_return(pipe, &iprec);
    g_free_not_null(iprec.errorstr);

    g_io_channel_shutdown(rec->pipes[0], TRUE, NULL);
    g_io_channel_unref(rec->pipes[0]);
    g_io_channel_shutdown(rec->pipes[1], TRUE, NULL);
    g_io_channel_unref(rec->pipes[1]);

    ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
    handle = iprec.error == -1 ? NULL :
             net_connect_ip(ip, rec->port, rec->my_ip);

    g_free_not_null(rec->my_ip);

    if (handle == NULL) {
        /* failed */
        rec->func(NULL, rec->data);
        g_free(rec);
        return;
    }

    rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
                           (GInputFunction) simple_init, rec);
}
Example #12
0
static void sig_botnet_connected(int handle, BOT_UPLINK_REC *uplink)
{
	BOTNET_REC *botnet;
	BOT_REC *bot;

	g_return_if_fail(uplink != NULL);

	botnet = uplink->botnet;

	if (handle == -1) {
		/* error, try another bot */
		botnet_connect(botnet);
		return;
	}

	/* connected to bot */
	bot = g_new0(BOT_REC, 1);
        bot->botnet = botnet;
	bot->link = uplink;
	bot->uplink = TRUE;

	bot->handle = handle;
	bot->read_tag = g_input_add(handle, G_INPUT_READ, (GInputFunction) sig_bot_read, bot);

	botnet->uplink = bot;
	g_node_append_data(botnet->bots, bot);

	/* send nick/pass */
	bot_send_cmdv(bot, "PASS %s", uplink->password);
	bot_send_cmdv(bot, "NICK %s", botnet->nick);
}
Example #13
0
File: dcc-get.c Project: Liaf/irssi
void dcc_get_connect(GET_DCC_REC *dcc)
{
	if (dcc->get_type == DCC_GET_DEFAULT) {
		dcc->get_type = settings_get_bool("dcc_autorename") ?
			DCC_GET_RENAME : DCC_GET_OVERWRITE;
	}

	if (dcc->from_dccserver) {
		sig_dccget_connected(dcc);
		return;
	}

	dcc->handle = dcc_connect_ip(&dcc->addr, dcc->port);

	if (dcc->handle != NULL) {
		dcc->tagconn =
			g_input_add(dcc->handle,
				    G_INPUT_WRITE | G_INPUT_READ,
				    (GInputFunction) sig_dccget_connected,
				    dcc);
	} else {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(DCC(dcc));
	}
}
Example #14
0
/* input function: DCC SERVER - someone tried to connect to our socket */
static void dcc_server_listen(SERVER_DCC_REC *dcc)
{
	SERVER_DCC_REC *newdcc;
	IPADDR ip;
	GIOChannel *handle;
	int port;

	g_return_if_fail(IS_DCC_SERVER(dcc));

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == NULL)
		return;

	/* Create a new DCC SERVER to handle this connection */
	newdcc = dcc_server_clone(dcc);

	newdcc->starttime = time(NULL);
	newdcc->handle = handle;
	newdcc->sendbuf = net_sendbuffer_create(handle, 0);
	memcpy(&newdcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&newdcc->addr, newdcc->addrstr);
	newdcc->port = port;
	newdcc->tagread = g_input_add(handle, G_INPUT_READ,
				      (GInputFunction) dcc_server_input, newdcc);

	signal_emit("dcc connected", 1, newdcc);
}
Example #15
0
static void connect_downlink(BOTNET_REC *botnet, int handle,
			     IPADDR *ip, const char *host)
{
	BOT_DOWNLINK_REC *downlink;
	BOT_REC *bot;

	g_return_if_fail(botnet != NULL);

	/* identify the bot who's trying to connect.. */
	downlink = bot_downlink_find(botnet, ip, host);
	if (downlink == NULL || downlink->password == NULL) {
		/* unknown bot, close connection /
		   bot didn't have password, don't let it connect to us */
		net_disconnect(handle);
		return;
	}

	bot = g_new0(BOT_REC, 1);
	bot->botnet = botnet;
	bot->link = downlink;
	g_node_append_data(botnet->bots, bot);

	/* connected.. */
	bot->handle = handle;
	bot->read_tag = g_input_add(handle, G_INPUT_READ, (GInputFunction) sig_bot_read, bot);
}
Example #16
0
/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_init(DCC_REC *dcc)
{
	int handle, port;
	IPADDR addr;

	g_return_if_fail(dcc != NULL);

	/* accept connection */
	handle = net_accept(dcc->handle, &addr, &port);
	if (handle == -1)
		return;

	/* TODO: some kind of paranoia check would be nice. it would check
	   that the host of the nick who we sent the request matches the
	   address who connected us. */

	g_source_remove(dcc->tagread);
	close(dcc->handle);

	dcc->starttime = time(NULL);
	dcc->fastsend = settings_get_bool("dcc_fast_send");
	dcc->handle = handle;
	memcpy(&dcc->addr, &addr, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;

	dcc->databufsize = settings_get_int("dcc_block_size");
        if (dcc->databufsize <= 0) dcc->databufsize = 2048;
	dcc->databuf = g_malloc(dcc->databufsize);

	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_send_read_size, dcc);
	dcc->tagwrite = !dcc->fastsend ? -1 :
		g_input_add(handle, G_INPUT_WRITE, (GInputFunction) dcc_send_data, dcc);

	signal_emit("dcc connected", 1, dcc);

	if (!dcc->fastsend) {
		/* send first block */
		dcc->gotalldata = TRUE;
		dcc_send_data(dcc);
	}
}
Example #17
0
static void dcc_get_connect(DCC_REC *dcc)
{
	dcc->handle = net_connect_ip(&dcc->addr, dcc->port,
				     source_host_ok ? source_host_ip : NULL);
	if (dcc->handle != -1) {
		dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION,
					   (GInputFunction) sig_dccget_connected, dcc);
	} else {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
	}
}
Example #18
0
/* starts connecting to server */
int server_start_connect(SERVER_REC *server)
{
	const char *connect_address;
        int fd[2];

	g_return_val_if_fail(server != NULL, FALSE);
	if (!server->connrec->unix_socket && server->connrec->port <= 0)
		return FALSE;

	server->rawlog = rawlog_create();

	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],
						   settings_get_bool("resolve_reverse_lookup"));
		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;
}
Example #19
0
/* callback: net_connect() finished for DCC GET */
static void sig_dccget_connected(DCC_REC *dcc)
{
	struct stat statbuf;
	char *fname;

	g_return_if_fail(dcc != NULL);

	if (net_geterror(dcc->handle) != 0) {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
		return;
	}

	g_source_remove(dcc->tagconn);

	g_free_not_null(dcc->file);
	dcc->file = dcc_get_download_path(dcc->arg);

	/* if some plugin wants to change the file name/path here.. */
	signal_emit("dcc get receive", 1, dcc);

	if (stat(dcc->file, &statbuf) == 0 && dcc->get_type == DCC_GET_RENAME) {
		/* file exists, rename.. */
		fname = get_rename_file(dcc->file);
		g_free(dcc->file);
		dcc->file = fname;
	}

	if (dcc->get_type != DCC_GET_RESUME) {
		dcc->fhandle = open(dcc->file, O_WRONLY | O_TRUNC | O_CREAT, dcc_file_create_mode);
		if (dcc->fhandle == -1) {
			signal_emit("dcc error file create", 2, dcc, dcc->file);
			dcc_destroy(dcc);
			return;
		}
	}

	dcc->databufsize = settings_get_int("dcc_block_size");
        if (dcc->databufsize <= 0) dcc->databufsize = 2048;
	dcc->databuf = g_malloc(dcc->databufsize);

	dcc->starttime = time(NULL);
	dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ,
				   (GInputFunction) sig_dccget_receive, dcc);
	signal_emit("dcc connected", 1, dcc);
}
Example #20
0
/* Try to let the other side close the connection, if it still isn't
   disconnected after certain amount of time, close it ourself */
void net_disconnect_later(int handle)
{
	NET_DISCONNECT_REC *rec;

	rec = g_new(NET_DISCONNECT_REC, 1);
	rec->created = time(NULL);
	rec->handle = handle;
	rec->tag = g_input_add(handle, G_INPUT_READ,
			       (GInputFunction) sig_disconnect, rec);

	if (timeout_tag == -1) {
		timeout_tag = g_timeout_add(10000, (GSourceFunc)
					    sig_timeout_disconnect, NULL);
	}

	disconnects = g_slist_append(disconnects, rec);
}
Example #21
0
static void server_real_connect(SERVER_REC *server, IPADDR *ip,
				const char *unix_socket)
{
	GIOChannel *handle;
        IPADDR *own_ip;
        int port;

	g_return_if_fail(ip != NULL || unix_socket != NULL);

	signal_emit("server connecting", 2, server, ip);

	if (server->connrec->no_connect)
		return;

	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 = server->connrec->use_ssl ?
			net_connect_ip_ssl(ip, port, own_ip, server->connrec->ssl_cert, server->connrec->ssl_pkey,
server->connrec->ssl_cafile, server->connrec->ssl_capath, server->connrec->ssl_verify) :
			net_connect_ip(ip, port, own_ip);
	} else {
		handle = net_connect_unix(unix_socket);
	}

	if (handle == NULL) {
		/* failed */
		if (errno == EADDRNOTAVAIL ||
		    (server->connrec->use_ssl && errno == ENOSYS))
			server->no_reconnect = TRUE;

		server->connection_lost = TRUE;
		server_connect_failed(server, g_strerror(errno));
	} 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);
	}
}
Example #22
0
int perl_input_add(int source, int condition,
		   const char *func, const char *data)
{
	PERL_SOURCE_REC *rec;
        GIOChannel *channel;

	rec = g_new(PERL_SOURCE_REC, 1);
	rec->func = g_strdup_printf("%s::%s", perl_get_package(), func);
	rec->data = g_strdup(data);

        channel = g_io_channel_unix_new(source);
	rec->tag = g_input_add(channel, condition,
			       (GInputFunction) perl_source_event, rec);
	g_io_channel_unref(channel);

	perl_sources = g_slist_append(perl_sources, rec);
	return rec->tag;
}
Example #23
0
/* callback: DCC CHAT - net_connect_nonblock() finished */
static void sig_chat_connected(DCC_REC *dcc)
{
	g_return_if_fail(dcc != NULL);

	g_source_remove(dcc->tagread);
	if (net_geterror(dcc->handle) != 0) {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
		return;
	}

	/* connect ok. */
	dcc->starttime = time(NULL);
	dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_input, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Example #24
0
/* SYNTAX: DCC SERVER [+|-scf] [port] */
static void cmd_dcc_server(const char *data, IRC_SERVER_REC *server)
{
	void *free_arg;
	GIOChannel *handle;
	SERVER_DCC_REC *dcc;
	IPADDR own_ip;
	char *flags, *port;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 2, &flags, &port))
		return;

	dcc = dcc_server_find_port(port);
	if (dcc != NULL) {
		/* Server is already running, update it */
		dcc_server_update_flags(dcc, flags);
		cmd_params_free(free_arg);
		return;
	}

	/* start listening */
	if (!IS_IRC_SERVER(server) || !server->connected) {
		cmd_param_error(CMDERR_NOT_CONNECTED);
	}

	handle = dcc_listen_port(net_sendbuffer_handle(server->handle),
				 &own_ip, atoi(port));

	if (handle == NULL) {
		cmd_param_error(CMDERR_ERRNO);
	}

	dcc = dcc_server_create(server, flags);
	dcc->handle = handle;
	dcc->port = atoi(port);
	dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ,
				   (GInputFunction) dcc_server_listen, dcc);

	signal_emit("dcc server started", 1, dcc);

	cmd_params_free(free_arg);
}
Example #25
0
void dcc_get_send_received(DCC_REC *dcc)
{
	guint32 recd;

	recd = (guint32) htonl(dcc->transfd);
	memcpy(dcc->count_buf, &recd, 4);

	dcc->count_pos = net_transmit(dcc->handle, dcc->count_buf+dcc->count_pos, 4-dcc->count_pos);
	if (dcc->count_pos == 4) dcc->count_pos = 0;

	/* count_pos might be -1 here. if this happens, the
	   count_buf should be re-sent.. also, if it's 1, 2 or 3, the
	   last 1-3 bytes should be sent later. these happen probably
	   never, but I just want to do it right.. :) */
	if (dcc->tagwrite != -1) {
		dcc->tagwrite = g_input_add(dcc->handle, G_INPUT_WRITE,
					    (GInputFunction) sig_dccget_send, dcc);
	}
}
Example #26
0
static void add_listen(const char *ircnet, int port)
{
	LISTEN_REC *rec;
	IPADDR ip4, ip6, *my_ip;

	if (port <= 0 || *ircnet == '\0')
		return;

	/* bind to specific host/ip? */
	my_ip = NULL;
	if (*settings_get_str("irssiproxy_bind") != '\0') {
		if (net_gethostbyname(settings_get_str("irssiproxy_bind"),
				      &ip4, &ip6) != 0) {
			printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
				  "Proxy: can not resolve '%s' - aborting",
				  settings_get_str("irssiproxy_bind"));
			return;
		}

		my_ip = ip6.family == 0 ? &ip4 : ip4.family == 0 ||
			settings_get_bool("resolve_prefer_ipv6") ? &ip6 : &ip4;
	}

	rec = g_new0(LISTEN_REC, 1);
	rec->ircnet = g_strdup(ircnet);
	rec->port = port;

	rec->handle = net_listen(my_ip, &rec->port);

	if (rec->handle == NULL) {
		printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
			  "Proxy: Listen in port %d failed: %s",
			  rec->port, g_strerror(errno));
		g_free(rec->ircnet);
                g_free(rec);
		return;
	}

	rec->tag = g_input_add(rec->handle, G_INPUT_READ,
			       (GInputFunction) sig_listen, rec);

        proxy_listens = g_slist_append(proxy_listens, rec);
}
Example #27
0
static void dcc_chat_connect(DCC_REC *dcc)
{
	g_return_if_fail(dcc != NULL);

	if (dcc->addrstr[0] == '\0' || dcc->starttime != 0) {
		/* already sent a chat request / already chatting */
		return;
	}

	dcc->handle = net_connect_ip(&dcc->addr, dcc->port,
				     source_host_ok ? source_host_ip : NULL);
	if (dcc->handle != -1) {
		dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION,
					   (GInputFunction) sig_chat_connected, dcc);
	} else {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
	}
}
Example #28
0
static void sig_listen(LISTEN_REC *listen)
{
	CLIENT_REC *rec;
	IPADDR ip;
	NET_SENDBUF_REC *sendbuf;
        GIOChannel *handle;
	char host[MAX_IP_LEN];
	int port;

	g_return_if_fail(listen != NULL);

	/* accept connection */
	handle = net_accept(listen->handle, &ip, &port);
	if (handle == NULL)
		return;
	net_ip2host(&ip, host);
	sendbuf = net_sendbuffer_create(handle, 0);
	rec = g_new0(CLIENT_REC, 1);
	rec->listen = listen;
	rec->handle = sendbuf;
        rec->host = g_strdup(host);
	rec->port = port;
	if (g_strcmp0(listen->ircnet, "*") == 0) {
		rec->proxy_address = g_strdup("irc.proxy");
		rec->server = servers == NULL ? NULL : IRC_SERVER(servers->data);
	} else {
		rec->proxy_address = g_strdup_printf("%s.proxy", listen->ircnet);
		rec->server = servers == NULL ? NULL :
			IRC_SERVER(server_find_chatnet(listen->ircnet));
	}
	rec->recv_tag = g_input_add(handle, G_INPUT_READ,
			       (GInputFunction) sig_listen_client, rec);

	proxy_clients = g_slist_prepend(proxy_clients, rec);
	rec->listen->clients = g_slist_prepend(rec->listen->clients, rec);

	signal_emit("proxy client connecting", 1, rec);
	printtext(rec->server, NULL, MSGLEVEL_CLIENTNOTICE,
		  "Proxy: New client %s:%d on port %d (%s)",
		  rec->host, rec->port, listen->port, listen->ircnet);
}
Example #29
0
static void dcc_chat_connect(CHAT_DCC_REC *dcc)
{
	g_return_if_fail(IS_DCC_CHAT(dcc));

	if (dcc->addrstr[0] == '\0' ||
	    dcc->starttime != 0 || dcc->handle != NULL) {
		/* already sent a chat request / already chatting */
		return;
	}

	dcc->handle = dcc_connect_ip(&dcc->addr, dcc->port);
	if (dcc->handle != NULL) {
		dcc->tagconn = g_input_add(dcc->handle,
					   G_INPUT_WRITE | G_INPUT_READ,
					   (GInputFunction) sig_chat_connected, dcc);
	} else {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(DCC(dcc));
	}
}
Example #30
0
static void sig_listen(PLUGIN_DATA *data, gint handle)
{
    CLIENT_REC *rec;
    IPADDR ip;
    gint port;

    g_return_if_fail(data != NULL);
    if (servers == NULL) return;

    /* accept connection */
    handle = net_accept(handle, &ip, &port);
    if (handle == -1)
        return;

    rec = g_new0(CLIENT_REC, 1);
    rec->handle = handle;
    rec->server = servers == NULL ? NULL : servers->data;
    rec->tag = g_input_add(handle, G_INPUT_READ, (GInputFunction) sig_listen_client, rec);

    data->clients = g_slist_append(data->clients, rec);
}