1 #ifndef EL__NETWORK_CONNECTION_H
2 #define EL__NETWORK_CONNECTION_H
3 
4 #include "cache/cache.h"
5 #include "encoding/encoding.h"
6 #include "main/timer.h" /* timer_id_T */
7 #include "network/state.h"
8 #include "util/lists.h"
9 
10 struct download;
11 struct socket;
12 struct uri;
13 
14 
15 struct connection {
16 	LIST_HEAD(struct connection);
17 
18 	struct list_head downloads;
19 	struct progress *progress;
20 
21 	/* If no proxy is used uri and proxied_uri are the same. */
22 	struct uri *uri;
23 	struct uri *proxied_uri;
24 	struct uri *referrer;
25 
26 	/* Cache information. */
27 	enum cache_mode cache_mode;
28 	struct cache_entry *cached;
29 
30 	off_t from;		/* Position for new data in the cache entry. */
31 	off_t received;		/* The number of received bytes. */
32 	off_t est_length;	/* Estimated number of bytes to transfer. */
33 
34 	enum stream_encoding content_encoding;
35 	struct stream_encoded *stream;
36 
37 	/* Called if non NULL when shutting down a connection. */
38 	void (*done)(struct connection *);
39 
40 	unsigned int id;
41 
42 	enum connection_state state;
43 	enum connection_state prev_error;
44 
45 	/* The communication socket with the other side. */
46 	struct socket *socket;
47 	/* The data socket. It is used, when @socket is used for the control,
48 	 * and the actual data is transmitted through a different channel. */
49 	/* The only users now is FTP and SMB. */
50 	struct socket *data_socket;
51 
52 	int tries;
53 	timer_id_T timer;
54 	int cgi_pipes[2];
55 	int stream_pipes[2];
56 
57 	unsigned int running:1;
58 	unsigned int unrestartable:1;
59 	unsigned int detached:1;
60 
61 	/* Each document is downloaded with some priority. When downloading a
62 	 * document, the existing connections are checked to see if a
63 	 * connection to the host already exists before creating a new one.  If
64 	 * it finds out that something had that idea earlier and connection for
65 	 * download of the very same URL is active already, it just attaches
66 	 * the struct download it got to the connection, _and_ updates its @pri
67 	 * array by the priority it has thus, sum of values in all fields of
68 	 * @pri is also kinda refcount of the connection. */
69 	enum connection_priority pri[PRIORITIES];
70 
71 	/* Private protocol specific info. If non-NULL it is free()d when
72 	 * stopping the connection. */
73 	void *info;
74 };
75 
76 int register_check_queue(void);
77 
78 int get_connections_count(void);
79 int get_keepalive_connections_count(void);
80 int get_connections_connecting_count(void);
81 int get_connections_transfering_count(void);
82 
83 void set_connection_state(struct connection *, enum connection_state);
84 
85 int has_keepalive_connection(struct connection *);
86 void add_keepalive_connection(struct connection *conn, long timeout_in_seconds,
87 			      void (*done)(struct connection *));
88 
89 void abort_connection(struct connection *, enum connection_state);
90 void retry_connection(struct connection *, enum connection_state);
91 
92 void change_connection(struct download *old, struct download *new,
93 		       enum connection_priority newpri, int interrupt);
94 void detach_connection(struct download *, off_t);
95 void abort_all_connections(void);
96 void abort_background_connections(void);
97 
98 void set_connection_timeout(struct connection *);
99 
100 void shutdown_connection_stream(struct connection *conn);
101 
102 /* Initiates a connection to get the given @uri. */
103 /* Note that stat's data _MUST_ be struct file_download * if start > 0! Yes,
104  * that should be probably something else than data, but... ;-) */
105 /* Returns 0 on success and -1 on failure. */
106 int load_uri(struct uri *uri, struct uri *referrer, struct download *download,
107 	     enum connection_priority pri, enum cache_mode cache_mode, off_t start);
108 
109 int is_entry_used(struct cache_entry *cached);
110 
111 #endif
112