1 /*
2  * uhub - A tiny ADC p2p connection hub
3  * Copyright (C) 2007-2014, Jan Vidar Krey
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, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef HAVE_UHUB_HUB_H
21 #define HAVE_UHUB_HUB_H
22 
23 enum status_message
24 {
25 	status_msg_hub_full                  = -1,  /* hub is full */
26 	status_msg_hub_disabled              = -2,  /* hub is disabled */
27 	status_msg_hub_registered_users_only = -3,  /* hub is for registered users only */
28 	status_msg_inf_error_nick_missing    = -4,  /* no nickname given */
29 	status_msg_inf_error_nick_multiple   = -5,  /* multiple nicknames given */
30 	status_msg_inf_error_nick_invalid    = -6,  /* generic/unknown */
31 	status_msg_inf_error_nick_long       = -7,  /* nickname too long */
32 	status_msg_inf_error_nick_short      = -8,  /* nickname too short */
33 	status_msg_inf_error_nick_spaces     = -9,  /* nickname cannot start with spaces */
34 	status_msg_inf_error_nick_bad_chars  = -10, /* nickname contains chars below ascii 32 */
35 	status_msg_inf_error_nick_not_utf8   = -11, /* nickname is not valid utf8 */
36 	status_msg_inf_error_nick_taken      = -12, /* nickname is in use */
37 	status_msg_inf_error_nick_restricted = -13, /* nickname cannot be used on this hub */
38 	status_msg_inf_error_cid_invalid     = -14, /* CID is not valid (generic error) */
39 	status_msg_inf_error_cid_missing     = -15, /* CID is not specified */
40 	status_msg_inf_error_cid_taken       = -16, /* CID is taken (already logged in?). */
41 	status_msg_inf_error_pid_missing     = -17, /* PID is not specified */
42 	status_msg_inf_error_pid_invalid     = -18, /* PID is invalid */
43 	status_msg_ban_permanently           = -19, /* Banned permanently */
44 	status_msg_ban_temporarily           = -20, /* Banned temporarily */
45 	status_msg_auth_invalid_password     = -21, /* Password is wrong */
46 	status_msg_auth_user_not_found       = -22, /* User not found in password database */
47 	status_msg_error_no_memory           = -23, /* Hub is out of memory */
48 
49 	status_msg_user_share_size_low       = -40, /* User is not sharing enough. */
50 	status_msg_user_share_size_high      = -41, /* User is sharing too much. */
51 	status_msg_user_slots_low            = -42, /* User has too few slots open. */
52 	status_msg_user_slots_high           = -43, /* User has too many slots open. */
53 	status_msg_user_hub_limit_low        = -44, /* User is on too few hubs. */
54 	status_msg_user_hub_limit_high       = -45, /* User is on too many hubs. */
55 
56 	status_msg_proto_no_common_hash      = -50, /* No common hash algorithms */
57 	status_msg_proto_obsolete_adc0       = -51, /* Client is using an obsolete protocol version */
58 
59 
60 };
61 
62 
63 enum hub_state
64 {
65 	hub_status_uninitialized = 0, /**<<<"Hub is uninitialized" */
66 	hub_status_running       = 1, /**<<<"Hub is running (normal operation)" */
67 	hub_status_restart       = 2, /**<<<"Hub is restarting (re-reading configuration, etc)" */
68 	hub_status_shutdown      = 3, /**<<<"Hub is shutting down, but not yet stopped. */
69 	hub_status_stopped       = 4, /**<<<"Hub is stopped (Pretty much the same as initialized) */
70 	hub_status_disabled      = 5, /**<<<"Hub is disabled (Running, but not accepting users) */
71 };
72 
73 /**
74  * Always updated each minute.
75  */
76 struct hub_stats
77 {
78 	size_t net_tx;
79 	size_t net_rx;
80 	size_t net_tx_peak;
81 	size_t net_rx_peak;
82 	size_t net_tx_total;
83 	size_t net_rx_total;
84 	struct timeout_evt* timeout;    /**<< "Timeout handler for statistics" */
85 };
86 
87 struct hub_logout_info
88 {
89 	time_t time;
90 	char cid[MAX_CID_LEN+1];
91 	char nick[MAX_NICK_LEN+1];
92 	struct ip_addr_encap addr;
93 	enum user_quit_reason reason;
94 };
95 
96 struct hub_info
97 {
98 	struct net_connection* server;
99 	struct linked_list* server_alt_ports;
100 	struct hub_stats stats;
101 	struct event_queue* queue;
102 	struct hub_config* config;
103 	struct hub_user_manager* users;
104 	struct acl_handle* acl;
105 	struct adc_message* command_info;    /* The hub's INF command */
106 	struct adc_message* command_support; /* The hub's SUP command */
107 	struct adc_message* command_banner;  /* The default welcome message */
108 	time_t tm_started;
109 	int status;
110 	char* recvbuf; /* Global receive buffer */
111 	char* sendbuf; /* Global send buffer */
112 
113 	struct linked_list* logout_info;     /* Log of people logging out. */
114 
115 	struct command_base* commands;       /* Hub command handler */
116 	struct uhub_plugins* plugins;        /* Plug-ins loaded for this hub instance. */
117 
118 #ifdef SSL_SUPPORT
119 	struct ssl_context_handle* ctx;
120 #endif /*  SSL_SUPPORT */
121 };
122 
123 /**
124  * This is the message pre-routing centre.
125  *
126  * Any message coming in to the hub comes through here first,
127  * and will be routed further if valid.
128  *
129  * @return 0 on success, -1 on error
130  */
131 extern int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* message, size_t length);
132 
133 /**
134  * Handle protocol support/subscription messages received clients.
135  *
136  * @return 0 on success, -1 on error
137  */
138 extern int hub_handle_support(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
139 
140 /**
141  * Handle password messages received from clients.
142  *
143  * @return 0 on success, -1 on error
144  */
145 extern int hub_handle_password(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
146 
147 /**
148  * Handle chat messages received from clients.
149  * @return 0 on success, -1 on error.
150  */
151 extern int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
152 
153 /**
154  * Used internally by hub_handle_info
155  * @return 1 if nickname is OK, or 0 if nickname is not accepted.
156  */
157 extern int  hub_handle_info_check_nick(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
158 
159 /**
160  * Used internally by hub_handle_info
161  * @return 1 if CID/PID is OK, or 0 if not valid.
162  */
163 extern int  hub_handle_info_check_cid(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
164 
165 /**
166  * Send the support line for the hub to a particular user.
167  * Only used during the initial handshake.
168  */
169 extern void hub_send_support(struct hub_info* hub, struct hub_user* u);
170 
171 /**
172  * Send a message assigning a SID for a user.
173  * This is only sent after hub_send_support() during initial handshake.
174  */
175 extern void hub_send_sid(struct hub_info* hub, struct hub_user* u);
176 
177 /**
178  * Send a 'ping' message to user.
179  */
180 extern void hub_send_ping(struct hub_info* hub, struct hub_user* user);
181 
182 /**
183  * Send a message containing hub information to a particular user.
184  * This is sent during user connection, but can safely be sent at any
185  * point later.
186  */
187 extern void hub_send_hubinfo(struct hub_info* hub, struct hub_user* u);
188 
189 /**
190  * Send handshake. This basically calls
191  * hub_send_support() and hub_send_sid()
192  */
193 extern void hub_send_handshake(struct hub_info* hub, struct hub_user* u);
194 
195 /**
196  * Send a password challenge to a user.
197  * This is only used if the user tries to access the hub using a
198  * password protected nick name.
199  */
200 extern void hub_send_password_challenge(struct hub_info* hub, struct hub_user* u);
201 
202 /**
203  * Sends a status_message to a user.
204  */
205 extern void hub_send_status(struct hub_info*, struct hub_user* user, enum status_message msg, enum msg_status_level level);
206 
207 /**
208  * Warn user about flooding.
209  */
210 extern void hub_send_flood_warning(struct hub_info*, struct hub_user* user, const char* message);
211 
212 /**
213  * Allocates memory, initializes the hub based on the configuration,
214  * and returns a hub handle.
215  * This hub handle must be passed to hub_shutdown_service() in order to cleanup before exiting.
216  *
217  * @return a pointer to the hub info.
218  */
219 extern struct hub_info* hub_start_service(struct hub_config* config);
220 
221 /**
222  * This shuts down the hub.
223  */
224 extern void hub_shutdown_service(struct hub_info* hub);
225 
226 /**
227  * This configures the hub.
228  */
229 extern void hub_set_variables(struct hub_info* hub, struct acl_handle* acl);
230 
231 /**
232  * This frees the configuration of the hub.
233  */
234 extern void hub_free_variables(struct hub_info* hub);
235 
236 /**
237  * Returns a string for the given status_message (See enum status_message).
238  */
239 extern const char* hub_get_status_message(struct hub_info* hub, enum status_message msg);
240 extern const char* hub_get_status_message_log(struct hub_info* hub, enum status_message msg);
241 
242 /**
243  * Returns the number of logged in users on the hub.
244  */
245 extern size_t hub_get_user_count(struct hub_info* hub);
246 
247 /**
248  * Returns the maximum number of allowed users on the hub.
249  */
250 extern size_t hub_get_max_user_count(struct hub_info* hub);
251 
252 /**
253  * Returns the accumulated shared size for all logged in
254  * users on the hub.
255  */
256 extern uint64_t hub_get_shared_size(struct hub_info* hub);
257 
258 /**
259  * Returns the accumulated number of files for all logged
260  * in users on the hub.
261  */
262 extern uint64_t hub_get_shared_files(struct hub_info* hub);
263 
264 /**
265  * Returns the minimal share size limit as enforced by
266  * this hub's configuration.
267  */
268 extern uint64_t hub_get_min_share(struct hub_info* hub);
269 
270 /**
271  * Returns the minimal share size limit as enforced by
272  * this hub's configuration.
273  */
274 extern uint64_t hub_get_max_share(struct hub_info* hub);
275 
276 /**
277  * Returns the minimum upload slot limit as enforced by
278  * this hub's configuration.
279  * Users with fewer slots in total will not be allowed
280  * to enter the hub.
281  * @return limit or 0 if no limit.
282  */
283 extern size_t hub_get_min_slots(struct hub_info* hub);
284 
285 /**
286  * Returns the maximum upload slot limit as enforced by
287  * this hub's configuration.
288  * Users with more allowed upload slots will not be
289  * allowed to enter the hub.
290  * @return limit or 0 if no limit.
291  */
292 extern size_t hub_get_max_slots(struct hub_info* hub);
293 
294 /**
295  * Returns the maximum number of hubs a user can
296  * be logged in to simultaneously as a regular user (guest).
297  * Users on more hubs will not be allowed to stay on this hub.
298  * @return limit or 0 if no limit.
299  */
300 extern size_t hub_get_max_hubs_user(struct hub_info* hub);
301 extern size_t hub_get_min_hubs_user(struct hub_info* hub);
302 
303 /**
304  * Returns the maximum number of hubs a user can
305  * be logged in to simultaneously as a registered user (password required).
306  * Users on more hubs will not be allowed to stay on this hub.
307  * @return limit or 0 if no limit.
308  */
309 extern size_t hub_get_max_hubs_reg(struct hub_info* hub);
310 extern size_t hub_get_min_hubs_reg(struct hub_info* hub);
311 
312 /**
313  * Returns the maximum number of hubs a user can
314  * be logged in to simultaneously as an operator.
315  * Users who are operator on more than this amount of hubs
316  * will not be allowed to stay on this hub.
317  * @return limit or 0 if no limit.
318  */
319 extern size_t hub_get_max_hubs_op(struct hub_info* hub);
320 extern size_t hub_get_min_hubs_op(struct hub_info* hub);
321 
322 /**
323  * Returns the maximum number of hubs a user can
324  * be logged in to simultaneously regardless of the type of user.
325  */
326 extern size_t hub_get_max_hubs_total(struct hub_info* hub);
327 
328 /**
329  * Schedule runslice.
330  */
331 extern void hub_schedule_runslice(struct hub_info* hub);
332 
333 /**
334  * Run event loop.
335  */
336 extern void hub_event_loop(struct hub_info* hub);
337 
338 /**
339  * Schedule destroying a user.
340  */
341 extern void hub_schedule_destroy_user(struct hub_info* hub, struct hub_user* user);
342 
343 /**
344  * Disconnect a user from the hub.
345  */
346 extern void hub_disconnect_user(struct hub_info* hub, struct hub_user* user, int reason);
347 
348 /**
349  * Log a user logging out.
350  */
351 extern void hub_logout_log(struct hub_info* hub, struct hub_user* user);
352 
353 
354 #endif /* HAVE_UHUB_HUB_H */
355 
356