1 /*
2 	ctrlproxy: A modular IRC proxy
3 	(c) 2002-2005 Jelmer Vernooij <jelmer@nl.linux.org>
4 
5 	This program is free software; you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation; either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #ifndef __CTRLPROXY_NETWORK_H__
21 #define __CTRLPROXY_NETWORK_H__
22 
23 /**
24  * @file
25  * @brief Network functions
26  */
27 
28 #include <glib.h>
29 
30 #ifndef G_MODULE_EXPORT
31 #define G_MODULE_EXPORT
32 #endif
33 
34 #include "state.h"
35 #include "isupport.h"
36 #include <sys/socket.h>
37 
38 #define DEFAULT_NETWORK_CHARSET NULL
39 
40 struct global;
41 struct irc_network;
42 struct irc_client;
43 struct irc_line;
44 struct linestack_context;
45 
46 enum irc_network_connection_state {
47 		NETWORK_CONNECTION_STATE_NOT_CONNECTED = 0,
48 		NETWORK_CONNECTION_STATE_RECONNECT_PENDING,
49 		NETWORK_CONNECTION_STATE_CONNECTING,
50 		NETWORK_CONNECTION_STATE_CONNECTED,
51 		NETWORK_CONNECTION_STATE_LOGIN_SENT,
52 		NETWORK_CONNECTION_STATE_MOTD_RECVD,
53 };
54 
55 /**
56  * Information about the connection to a network.
57  */
58 struct irc_network_connection {
59  	enum irc_network_connection_state state;
60 
61 	/** Time the last line was sent to this network. */
62 	time_t last_line_sent;
63 	/** Time the last line was received from this network. */
64 	time_t last_line_recvd;
65 
66 	struct irc_transport *transport;
67 	union {
68 		struct {
69 			/** Configuration for TCP/IP server currently connected to. */
70 			struct tcp_server_config *current_server;
71 			/** Name of remote server. */
72 			struct sockaddr *remote_name;
73 			/** Name of local host used for connection. */
74 			struct sockaddr *local_name;
75 			/** Socket name length for remote_name and local_name. */
76 			socklen_t namelen;
77 			/** Last reason for disconnect. */
78 			char *last_disconnect_reason;
79 			/** Source ID for function that regularly pings the network. */
80 			gint ping_id;
81 			/** Source ID for function that finishes connect. */
82 			gint connect_id;
83 		} tcp;
84 
85 		struct {
86 			void *private_data;
87 			struct virtual_network_ops {
88 				char *name;
89 				gboolean not_disconnectable;
90 				gboolean (*init) (struct irc_network *);
91 				gboolean (*to_server) (struct irc_network *, struct irc_client *c, const struct irc_line *);
92 				void (*fini) (struct irc_network *);
93 			} *ops;
94 		} virtual;
95 	} data;
96 };
97 
98 typedef void (*network_log_fn) (enum log_level, const struct irc_network *, const char *);
99 
100 struct irc_network_callbacks {
101 	network_log_fn log;
102 	struct irc_login_details *(*get_login_details) (struct irc_network *);
103 	gboolean (*process_from_server) (struct irc_network *, const struct irc_line *);
104 };
105 
106 /**
107  * An IRC network
108  */
109 struct irc_network {
110 	char *name;
111 
112 	/** Global data. */
113 	struct global *global;
114 
115 	/** Private data */
116 	void *private_data;
117 
118 	/** Number of references that exist to this network. */
119 	int references;
120 
121 	/** Clients connected to this network.*/
122 	GList *clients;
123 	guint reconnect_id;
124 
125 	gpointer ssl_credentials;
126 
127 	int reconnect_interval;
128 
129 	/** External network state, when connected. */
130 	struct irc_network_state *external_state;
131 
132 	/** Internal network state, as used by clients */
133 	struct irc_network_state *internal_state;
134 
135 	/** Network information. */
136 	struct irc_network_info *info;
137 
138 	struct irc_network_connection connection;
139 
140 	/** Linestack context. */
141 	struct linestack_context *linestack;
142 
143 	/** How many linestack errors have occurred so far */
144 	guint linestack_errors;
145 
146 	const struct irc_network_callbacks *callbacks;
147 
148 	struct query_stack *queries;
149 };
150 
151 /* server.c */
152 G_MODULE_EXPORT gboolean network_set_charset(struct irc_network *n, const char *name);
153 G_MODULE_EXPORT gboolean autoconnect_networks(GList *);
154 G_MODULE_EXPORT struct irc_network *irc_network_new(const struct irc_network_callbacks *callbacks, void *private_data);
155 G_MODULE_EXPORT gboolean connect_network(struct irc_network *);
156 G_MODULE_EXPORT void network_select_next_server(struct irc_network *n);
157 G_MODULE_EXPORT gboolean disconnect_network(struct irc_network *s);
158 G_MODULE_EXPORT gboolean network_send_line(struct irc_network *s, struct irc_client *c, const struct irc_line *, gboolean);
159 G_MODULE_EXPORT gboolean network_send_args(struct irc_network *s, ...);
160 G_MODULE_EXPORT void register_virtual_network(struct virtual_network_ops *);
161 G_MODULE_EXPORT struct irc_network *find_network(GList *gl, const char *);
162 G_MODULE_EXPORT gboolean virtual_network_recv_line(struct irc_network *l, struct irc_line *);
163 G_MODULE_EXPORT gboolean virtual_network_recv_args(struct irc_network *l, const char *origin, ...);
164 G_MODULE_EXPORT gboolean virtual_network_recv_response(struct irc_network *n, int num, ...);
165 struct ctrlproxy_config;
166 G_MODULE_EXPORT G_GNUC_MALLOC struct linestack_context *new_linestack(struct irc_network *network, struct ctrlproxy_config *settings);
167 G_MODULE_EXPORT G_GNUC_MALLOC char *network_generate_feature_string(struct irc_network *n);
168 G_MODULE_EXPORT struct irc_network *network_ref(struct irc_network *);
169 G_MODULE_EXPORT void network_unref(struct irc_network *);
170 
171 #endif /* __CTRLPROXY_NETWORK_H__ */
172