1 /*
2 	belle-sip - SIP (RFC3261) library.
3     Copyright (C) 2010  Belledonne Communications SARL
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 2 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 #ifndef BELLE_SIP_CHANNEL_H
19 #define BELLE_SIP_CHANNEL_H
20 
21 #ifndef _WIN32
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #else
25 
26 #endif
27 
28 #define belle_sip_network_buffer_size 65535
29 #define belle_sip_send_network_buffer_size 16384
30 
31 typedef enum belle_sip_channel_state{
32 	BELLE_SIP_CHANNEL_INIT,
33 	BELLE_SIP_CHANNEL_RES_IN_PROGRESS,
34 	BELLE_SIP_CHANNEL_RES_DONE,
35 	BELLE_SIP_CHANNEL_CONNECTING,
36 	BELLE_SIP_CHANNEL_RETRY,
37 	BELLE_SIP_CHANNEL_READY,
38 	BELLE_SIP_CHANNEL_ERROR,
39 	BELLE_SIP_CHANNEL_DISCONNECTED
40 }belle_sip_channel_state_t;
41 
42 const char * belle_sip_channel_state_to_string(belle_sip_channel_state_t state);
43 
44 /**
45 * belle_sip_channel_t is an object representing a single communication channel ( socket or file descriptor),
46 * unlike the belle_sip_listening_point_t that can owns several channels for TCP or TLS (incoming server child sockets or
47 * outgoing client sockets).
48 **/
49 typedef struct belle_sip_channel belle_sip_channel_t;
50 
51 BELLE_SIP_DECLARE_INTERFACE_BEGIN(belle_sip_channel_listener_t)
52 void (*on_state_changed)(belle_sip_channel_listener_t *l, belle_sip_channel_t *, belle_sip_channel_state_t state);
53 void (*on_message_headers)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, belle_sip_message_t *msg);
54 void (*on_message)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, belle_sip_message_t *msg);
55 void (*on_sending)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, belle_sip_message_t *msg);
56 int (*on_auth_requested)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, const char* distinghised_name);
57 BELLE_SIP_DECLARE_INTERFACE_END
58 
59 #define BELLE_SIP_CHANNEL_LISTENER(obj) BELLE_SIP_INTERFACE_CAST(obj,belle_sip_channel_listener_t)
60 
61 typedef enum input_stream_state {
62 	WAITING_MESSAGE_START,
63 	MESSAGE_AQUISITION,
64 	BODY_AQUISITION
65 }input_stream_state_t;
66 
67 typedef enum output_stream_state{
68 	OUTPUT_STREAM_IDLE,
69 	OUTPUT_STREAM_SENDING_HEADERS,
70 	OUTPUT_STREAM_SENDING_BODY
71 }output_stream_state_t;
72 
73 typedef struct belle_sip_channel_input_stream{
74 	input_stream_state_t state;
75 	char buff[belle_sip_network_buffer_size];
76 	char* read_ptr;
77 	char* write_ptr;
78 	belle_sip_message_t *msg;
79 	size_t content_length;
80 	int chuncked_mode;
81 	int chunk_size;
82 	int chunk_read_size;
83 }belle_sip_channel_input_stream_t;
84 
85 typedef struct belle_sip_stream_channel belle_sip_stream_channel_t;
86 typedef struct belle_sip_tls_channel belle_sip_tls_channel_t;
87 
88 struct belle_sip_channel{
89 	belle_sip_source_t base;
90 	belle_sip_stack_t *stack;
91 	belle_sip_listening_point_t *lp; /*the listening point that owns this channel*/
92 	belle_sip_channel_state_t state;
93 	belle_sip_list_t *state_listeners;
94 	belle_sip_list_t *full_listeners;
95 	int ai_family;
96 	char *peer_cname;
97 	char *peer_name;
98 	int peer_port;
99 	char *local_ip;
100 	int local_port;
101 	char *public_ip;
102 	int public_port;
103 	belle_sip_resolver_context_t* resolver_ctx;
104 	struct addrinfo *peer_list;
105 	struct addrinfo *current_peer;
106 	belle_sip_list_t *outgoing_messages;
107 	belle_sip_message_t *cur_out_message;
108 	output_stream_state_t out_state;
109 	uint8_t *ewouldblock_buffer;
110 	size_t ewouldblock_size;
111 	size_t ewouldblock_offset;
112 	belle_sip_channel_input_stream_t input_stream;
113 	belle_sip_list_t* incoming_messages;
114 	belle_sip_source_t *inactivity_timer;
115 	belle_sip_source_t *dns_ttl_timer;
116 	uint64_t last_recv_time;
117 	int simulated_recv_return; /* used to simulate network error. 0= no data (disconnected) >0= do nothing -1= network error, 1500 special number to silently discard incoming buffer*/
118 	unsigned long bg_task_id;
119 	unsigned long recv_bg_task_id;
120 	unsigned char force_close; /* when channel is intentionnaly disconnected, in order to prevent looping notifications*/
121 	unsigned char learnt_ip_port;
122 	unsigned char has_name; /*set when the name of the peer is known, which is not the case of inboud connections*/
123 	unsigned char about_to_be_closed;
124 	unsigned char srv_overrides_port; /*set when this channel was connected to destination port provided by SRV resolution*/
125 	unsigned char soft_error; /*set when this channel enters ERROR state because of error detected in upper layer */
126 	int stop_logging_buffer; /*log buffer content only if this is non binary data, and stop it at the first occurence*/
127 	bool_t closed_by_remote; /*If the channel has been remotely closed*/
128 	bool_t dns_ttl_timedout;
129 };
130 
131 #define BELLE_SIP_CHANNEL(obj)		BELLE_SIP_CAST(obj,belle_sip_channel_t)
132 
133 
134 BELLE_SIP_BEGIN_DECLS
135 
136 void belle_sip_channel_add_listener(belle_sip_channel_t *chan, belle_sip_channel_listener_t *l);
137 
138 void belle_sip_channel_remove_listener(belle_sip_channel_t *obj, belle_sip_channel_listener_t *l);
139 
140 int belle_sip_channel_matches(const belle_sip_channel_t *obj, const belle_sip_hop_t *hop, const struct addrinfo *addr);
141 
142 void belle_sip_channel_resolve(belle_sip_channel_t *obj);
143 
144 void belle_sip_channel_connect(belle_sip_channel_t *obj);
145 
146 void belle_sip_channel_prepare(belle_sip_channel_t *obj);
147 
148 void belle_sip_channel_close(belle_sip_channel_t *obj);
149 /**
150  *
151  * returns number of send byte or <0 in case of error
152  */
153 int belle_sip_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen);
154 
155 int belle_sip_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen);
156 /*only used by channels implementation*/
157 void belle_sip_channel_set_ready(belle_sip_channel_t *obj, const struct sockaddr *addr, socklen_t slen);
158 void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_stack_t *stack, const char *bindip,int localport, const char *peer_cname, const char *peername, int peer_port);
159 void belle_sip_channel_init_with_addr(belle_sip_channel_t *obj, belle_sip_stack_t *stack, const char *bindip, int localport, const struct sockaddr *peer_addr, socklen_t addrlen);
160 void belle_sip_channel_set_socket(belle_sip_channel_t *obj, belle_sip_socket_t sock, belle_sip_source_func_t datafunc);
161 /*end of channel implementations*/
162 /**
163  * Get a received message from the receive queue. This caller takes the ownership of the message.
164  */
165 belle_sip_message_t* belle_sip_channel_pick_message(belle_sip_channel_t *obj);
166 
167 int belle_sip_channel_queue_message(belle_sip_channel_t *obj, belle_sip_message_t *msg);
168 
169 int belle_sip_channel_is_reliable(const belle_sip_channel_t *obj);
170 
171 const char * belle_sip_channel_get_transport_name(const belle_sip_channel_t *obj);
172 const char * belle_sip_channel_get_transport_name_lower_case(const belle_sip_channel_t *obj);
173 
174 const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj);
175 
176 const char *belle_sip_channel_get_local_address(belle_sip_channel_t *obj, int *port);
177 const char *belle_sip_channel_get_public_address(belle_sip_channel_t *obj, int *port);
178 
179 /*
180  * Returns a sip-uri suitable for using in record-route.
181  * If the channel is not natted, it will return the listening port of the listening point corresponding to the channel.
182 **/
183 belle_sip_uri_t *belle_sip_channel_create_routable_uri(belle_sip_channel_t *chan);
184 
185 
186 #define belle_sip_channel_get_state(chan) ((chan)->state)
187 
188 void channel_set_state(belle_sip_channel_t *obj, belle_sip_channel_state_t state);
189 
190 /*
191  * Process incoming data and synchronously invoke the listeners if
192  * complete message are received. The invocation of the listeners may
193  * result in the channel being destroyed (ex: calling belle_sip_listening_point_clean_channels() within
194  * a transaction completed notification).
195  * WARNING: As a result, the caller of this function must be take into account that the channel no longer exists
196  * in return from this function.
197  */
198 int belle_sip_channel_process_data(belle_sip_channel_t *obj,unsigned int revents);
199 
200 /*this function is to be used only in belle_sip_listening_point_clean_channels()*/
201 void belle_sip_channel_force_close(belle_sip_channel_t *obj);
202 
203 /*this function is for transactions to report that a channel seems non working because a timeout occured for example.
204  It results in the channel possibly entering error state, so that it gets cleaned. Next transactions will re-open a new one and
205  get a better chance of receiving an answer.
206  Returns TRUE if the channel enters error state, 0 otherwise (channel is given a second chance) */
207 int belle_sip_channel_notify_timeout(belle_sip_channel_t *obj);
208 
209 /*Used by transaction layer to report a server having internal errors, so that we can retry with another IP (in case of DNS SRV)*/
210 BELLESIP_EXPORT void belle_sip_channel_notify_server_error(belle_sip_channel_t *obj);
211 
212 /*
213  * Check if the DNS TTL has expired. If this is the case, set the channel status to RES_IN_PROGRESS.
214  */
215 void belle_sip_channel_check_dns_reusability(belle_sip_channel_t *obj);
216 
217 BELLE_SIP_END_DECLS
218 
219 
220 BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_channel_t,belle_sip_source_t)
221 	const char *transport;
222 	int reliable;
223 	int (*connect)(belle_sip_channel_t *obj, const struct addrinfo *ai);
224 	int (*channel_send)(belle_sip_channel_t *obj, const void *buf, size_t buflen);
225 	int (*channel_recv)(belle_sip_channel_t *obj, void *buf, size_t buflen);
226 	void (*close)(belle_sip_channel_t *obj);
227 BELLE_SIP_DECLARE_CUSTOM_VPTR_END
228 
229 /*
230  * tls client certificate authentication. might be relevant for both tls and dtls channels.Only implemented in tls channel for now
231  */
232 void belle_sip_tls_channel_set_client_certificates_chain(belle_sip_tls_channel_t *obj, belle_sip_certificates_chain_t* cert_chain);
233 void belle_sip_tls_channel_set_client_certificate_key(belle_sip_tls_channel_t *obj, belle_sip_signing_key_t* key);
234 
235 belle_sip_channel_t *belle_sip_channel_find_from_list_with_addrinfo(belle_sip_list_t *l, const belle_sip_hop_t *hop, const struct addrinfo *addr);
236 belle_sip_channel_t *belle_sip_channel_find_from_list(belle_sip_list_t *l, int ai_family, const belle_sip_hop_t *hop);
237 
238 #define BELLE_SIP_TLS_CHANNEL(obj)		BELLE_SIP_CAST(obj,belle_sip_tls_channel_t)
239 
240 struct belle_tls_crypto_config{
241 	belle_sip_object_t base;
242 	char *root_ca; /**< path to the trusted certificate chain used when verifiying peer certificate */
243 	char *root_ca_data; /**< content of the trusted certificate chain used when verifiying peer certificate */
244 	int exception_flags; /**< override some exception raised during certificate verification, can be:
245 						   BELLE_TLS_VERIFY_NONE do not override any exception
246 						   BELLE_TLS_VERIFY_CN_MISMATCH ignore Common Name mismatch exception
247 						   BELLE_TLS_VERIFY_ANY_REASON(ignore any exception */
248 	void *ssl_config; /**< externally provided ssl configuration context, will be casted and given to the underlying crypto library, use only if you really know what you're doing */
249 };
250 
251 #endif
252