channel.h 8.38 KB
Newer Older
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BELLE_SIP_CHANNEL_H
#define BELLE_SIP_CHANNEL_H

#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#else

#endif

28 29 30 31 32
typedef void (*belle_sip_background_task_end_callback_t)(void *);

unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data);
void belle_sip_end_background_task(unsigned int id);

33
#define belle_sip_network_buffer_size 65535
34 35 36 37 38 39

typedef enum belle_sip_channel_state{
	BELLE_SIP_CHANNEL_INIT,
	BELLE_SIP_CHANNEL_RES_IN_PROGRESS,
	BELLE_SIP_CHANNEL_RES_DONE,
	BELLE_SIP_CHANNEL_CONNECTING,
40
	BELLE_SIP_CHANNEL_RETRY,
41
	BELLE_SIP_CHANNEL_READY,
jehan's avatar
jehan committed
42 43
	BELLE_SIP_CHANNEL_ERROR,
	BELLE_SIP_CHANNEL_DISCONNECTED
44 45
}belle_sip_channel_state_t;

Simon Morlat's avatar
Simon Morlat committed
46
const char * belle_sip_channel_state_to_string(belle_sip_channel_state_t state);
47

48 49 50 51 52 53 54
/**
* belle_sip_channel_t is an object representing a single communication channel ( socket or file descriptor), 
* unlike the belle_sip_listening_point_t that can owns several channels for TCP or TLS (incoming server child sockets or 
* outgoing client sockets).
**/
typedef struct belle_sip_channel belle_sip_channel_t;

55
BELLE_SIP_DECLARE_INTERFACE_BEGIN(belle_sip_channel_listener_t)
Simon Morlat's avatar
Simon Morlat committed
56 57
void (*on_state_changed)(belle_sip_channel_listener_t *l, belle_sip_channel_t *, belle_sip_channel_state_t state);
int (*on_event)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, unsigned revents);
58
void (*on_sending)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, belle_sip_message_t *msg);
jehan's avatar
jehan committed
59
int (*on_auth_requested)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, const char* distinghised_name);
60 61
BELLE_SIP_DECLARE_INTERFACE_END

Simon Morlat's avatar
Simon Morlat committed
62
#define BELLE_SIP_CHANNEL_LISTENER(obj) BELLE_SIP_INTERFACE_CAST(obj,belle_sip_channel_listener_t)
jehan's avatar
jehan committed
63
#define MAX_CHANNEL_BUFF_SIZE 64000 + 1500 + 1
jehan's avatar
jehan committed
64 65 66 67 68 69 70 71 72

typedef enum input_stream_state {
	WAITING_MESSAGE_START=0
	,MESSAGE_AQUISITION=1
	,BODY_AQUISITION=2
}input_stream_state_t;

typedef struct belle_sip_channel_input_stream{
	input_stream_state_t state;
jehan's avatar
jehan committed
73
	char buff[MAX_CHANNEL_BUFF_SIZE];
jehan's avatar
jehan committed
74 75
	char* read_ptr;
	char* write_ptr;
jehan's avatar
jehan committed
76
	belle_sip_message_t *msg;
77 78 79 80
	unsigned char *body; /*used when receiving a chunked body*/
	int content_length;
	int chuncked_mode;
	int chunk_size;
jehan's avatar
jehan committed
81 82 83
}belle_sip_channel_input_stream_t;

typedef struct belle_sip_stream_channel belle_sip_stream_channel_t;
jehan's avatar
jehan committed
84
typedef struct belle_sip_tls_channel belle_sip_tls_channel_t;
Simon Morlat's avatar
Simon Morlat committed
85

86 87
struct belle_sip_channel{
	belle_sip_source_t base;
88
	belle_sip_stack_t *stack;
Simon Morlat's avatar
Simon Morlat committed
89
	belle_sip_listening_point_t *lp; /*the listening point that owns this channel*/
90
	belle_sip_channel_state_t state;
91
	belle_sip_list_t *listeners;
Simon Morlat's avatar
Simon Morlat committed
92
	int ai_family;
93
	char *peer_cname;
94 95
	char *peer_name;
	int peer_port;
96 97
	char *local_ip;
	int local_port;
98 99
	char *public_ip;
	int public_port;
100
	belle_sip_resolver_context_t* resolver_ctx;
101
	struct addrinfo *peer_list;
102
	struct addrinfo *current_peer;
103
	belle_sip_list_t *outgoing_messages;
jehan's avatar
jehan committed
104
	belle_sip_list_t* incoming_messages;
jehan's avatar
jehan committed
105
	belle_sip_channel_input_stream_t input_stream;
106
	belle_sip_source_t *inactivity_timer;
107
	uint64_t last_recv_time;
Simon Morlat's avatar
Simon Morlat committed
108
	int simulated_recv_return; /* used to simulate network error. 0= no data (disconnected) >0= do nothing -1= network error*/
109
	unsigned bg_task_id;
110 111
	unsigned char force_close; /* when channel is intentionnaly disconnected, in order to prevent looping notifications*/
	unsigned char learnt_ip_port;
112
	unsigned char has_name; /*set when the name of the peer is known, which is not the case of inboud connections*/
Simon Morlat's avatar
Simon Morlat committed
113
	unsigned char about_to_be_closed;
114 115 116 117
};

#define BELLE_SIP_CHANNEL(obj)		BELLE_SIP_CAST(obj,belle_sip_channel_t)

jehan's avatar
jehan committed
118

119
BELLE_SIP_BEGIN_DECLS
Ghislain MARY's avatar
Ghislain MARY committed
120

121
void belle_sip_channel_add_listener(belle_sip_channel_t *chan, belle_sip_channel_listener_t *l);
122

123 124
void belle_sip_channel_remove_listener(belle_sip_channel_t *obj, belle_sip_channel_listener_t *l);

125
int belle_sip_channel_matches(const belle_sip_channel_t *obj, const belle_sip_hop_t *hop, const struct addrinfo *addr);
126

jehan's avatar
jehan committed
127
void belle_sip_channel_resolve(belle_sip_channel_t *obj);
128

jehan's avatar
jehan committed
129
void belle_sip_channel_connect(belle_sip_channel_t *obj);
130

Simon Morlat's avatar
Simon Morlat committed
131 132
void belle_sip_channel_prepare(belle_sip_channel_t *obj);

133
void belle_sip_channel_close(belle_sip_channel_t *obj);
134 135 136 137
/**
 *
 * returns number of send byte or <0 in case of error
 */
138 139 140
int belle_sip_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen);

int belle_sip_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen);
jehan's avatar
jehan committed
141 142
/*only used by channels implementation*/
void belle_sip_channel_set_ready(belle_sip_channel_t *obj, const struct sockaddr *addr, socklen_t slen);
143
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);
144
void belle_sip_channel_init_with_addr(belle_sip_channel_t *obj, belle_sip_stack_t *stack, const struct sockaddr *peer_addr, socklen_t addrlen);
Simon Morlat's avatar
Simon Morlat committed
145
void belle_sip_channel_set_socket(belle_sip_channel_t *obj, belle_sip_socket_t sock, belle_sip_source_func_t datafunc);
jehan's avatar
jehan committed
146
/*end of channel implementations*/
jehan's avatar
jehan committed
147
/**
Simon Morlat's avatar
Simon Morlat committed
148
 * Get a received message from the receive queue. This caller takes the ownership of the message.
jehan's avatar
jehan committed
149 150
 */
belle_sip_message_t* belle_sip_channel_pick_message(belle_sip_channel_t *obj);
151 152 153

int belle_sip_channel_queue_message(belle_sip_channel_t *obj, belle_sip_message_t *msg);

154 155
int belle_sip_channel_is_reliable(const belle_sip_channel_t *obj);

156
const char * belle_sip_channel_get_transport_name(const belle_sip_channel_t *obj);
jehan's avatar
jehan committed
157
const char * belle_sip_channel_get_transport_name_lower_case(const belle_sip_channel_t *obj);
158

159 160
const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj);

161 162
const char *belle_sip_channel_get_local_address(belle_sip_channel_t *obj, int *port);

Simon Morlat's avatar
Simon Morlat committed
163 164
#define belle_sip_channel_get_state(chan) ((chan)->state)

jehan's avatar
jehan committed
165 166
void channel_set_state(belle_sip_channel_t *obj, belle_sip_channel_state_t state);

Simon Morlat's avatar
Simon Morlat committed
167
/*just invokes the listeners to process data*/
jehan's avatar
jehan committed
168
int belle_sip_channel_process_data(belle_sip_channel_t *obj,unsigned int revents);
169

Simon Morlat's avatar
Simon Morlat committed
170 171 172
/*this function is to be used only in belle_sip_listening_point_clean_channels()*/
void belle_sip_channel_force_close(belle_sip_channel_t *obj);

Simon Morlat's avatar
Simon Morlat committed
173
/*this function is for transactions to report that a channel seems non working because a timeout occured for example.
174 175 176 177
 It results in the channel possibly entering error state, so that it gets cleaned. Next transactions will re-open a new one and
 get a better chance of receiving an answer.
 Returns TRUE if the channel enters error state, 0 otherwise (channel is given a second chance) */
int belle_sip_channel_notify_timeout(belle_sip_channel_t *obj);
Simon Morlat's avatar
Simon Morlat committed
178

179
BELLE_SIP_END_DECLS
Ghislain MARY's avatar
Ghislain MARY committed
180 181


182
BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_channel_t,belle_sip_source_t)
183 184
	const char *transport;
	int reliable;
185
	int (*connect)(belle_sip_channel_t *obj, const struct addrinfo *ai);
186 187
	int (*channel_send)(belle_sip_channel_t *obj, const void *buf, size_t buflen);
	int (*channel_recv)(belle_sip_channel_t *obj, void *buf, size_t buflen);
188
	void (*close)(belle_sip_channel_t *obj);
189 190
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

Simon Morlat's avatar
Simon Morlat committed
191
/*
jehan's avatar
jehan committed
192
 * tls client certificate authentication. might be relevant for both tls and dtls channels.Only implemented in tls channel for now
Simon Morlat's avatar
Simon Morlat committed
193 194 195 196
 */
void belle_sip_tls_channel_set_client_certificates_chain(belle_sip_tls_channel_t *obj, belle_sip_certificates_chain_t* cert_chain);
void belle_sip_tls_channel_set_client_certificate_key(belle_sip_tls_channel_t *obj, belle_sip_signing_key_t* key);

Simon Morlat's avatar
Simon Morlat committed
197
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);
Simon Morlat's avatar
Simon Morlat committed
198
belle_sip_channel_t *belle_sip_channel_find_from_list(belle_sip_list_t *l, int ai_family, const belle_sip_hop_t *hop);
Simon Morlat's avatar
Simon Morlat committed
199

Simon Morlat's avatar
Simon Morlat committed
200
#define BELLE_SIP_TLS_CHANNEL(obj)		BELLE_SIP_CAST(obj,belle_sip_tls_channel_t)
201

Simon Morlat's avatar
Simon Morlat committed
202 203 204 205 206 207
struct belle_tls_verify_policy{
	belle_sip_object_t base;
	char *root_ca;
	int exception_flags;
};

208
#endif