channel.h 5.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
	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
    the Free Software Foundation, either version 3 of the License, or
    (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
static const int belle_sip_network_buffer_size=65535;
29 30 31 32 33 34 35 36 37 38

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,
	BELLE_SIP_CHANNEL_READY,
	BELLE_SIP_CHANNEL_ERROR
}belle_sip_channel_state_t;

39

40 41 42 43 44 45 46
/**
* 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;

47
BELLE_SIP_DECLARE_INTERFACE_BEGIN(belle_sip_channel_listener_t)
Simon Morlat's avatar
Simon Morlat committed
48 49
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);
50
void (*on_sending)(belle_sip_channel_listener_t *l, belle_sip_channel_t *obj, belle_sip_message_t *msg);
51 52
BELLE_SIP_DECLARE_INTERFACE_END

Simon Morlat's avatar
Simon Morlat committed
53
#define BELLE_SIP_CHANNEL_LISTENER(obj) BELLE_SIP_INTERFACE_CAST(obj,belle_sip_channel_listener_t)
jehan's avatar
jehan committed
54
#define MAX_CHANNEL_BUFF_SIZE 64000 + 1500 + 1
jehan's avatar
jehan committed
55 56 57 58 59 60 61 62 63

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
64
	char buff[MAX_CHANNEL_BUFF_SIZE];
jehan's avatar
jehan committed
65 66
	char* read_ptr;
	char* write_ptr;
jehan's avatar
jehan committed
67
	belle_sip_message_t *msg;
jehan's avatar
jehan committed
68 69 70
}belle_sip_channel_input_stream_t;

typedef struct belle_sip_stream_channel belle_sip_stream_channel_t;
Simon Morlat's avatar
Simon Morlat committed
71

72 73
struct belle_sip_channel{
	belle_sip_source_t base;
74
	belle_sip_stack_t *stack;
75
	belle_sip_channel_state_t state;
76
	belle_sip_list_t *listeners;
77 78
	char *peer_name;
	int peer_port;
79 80
	char *local_ip;
	int local_port;
81 82 83
	unsigned long resolver_id;
	struct addrinfo *peer;
	belle_sip_message_t *msg;
jehan's avatar
jehan committed
84
	belle_sip_list_t* incoming_messages;
jehan's avatar
jehan committed
85
	belle_sip_channel_input_stream_t input_stream;
86 87 88 89
};

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

90
belle_sip_channel_t * belle_sip_channel_new_udp(belle_sip_stack_t *stack, int sock, const char *bindip, int localport, const char *peername, int peerport);
91

92
belle_sip_channel_t * belle_sip_channel_new_udp_with_addr(belle_sip_stack_t *stack, int sock, const char *bindip, int localport, const struct addrinfo *ai);
Simon Morlat's avatar
Simon Morlat committed
93

jehan's avatar
jehan committed
94
belle_sip_channel_t * belle_sip_channel_new_tcp(belle_sip_stack_t *stack, const char *bindip, int localport,const char *name, int port);
95 96

void belle_sip_channel_add_listener(belle_sip_channel_t *chan, belle_sip_channel_listener_t *l);
97

98 99
void belle_sip_channel_remove_listener(belle_sip_channel_t *obj, belle_sip_channel_listener_t *l);

Simon Morlat's avatar
Simon Morlat committed
100
int belle_sip_channel_matches(const belle_sip_channel_t *obj, const char *peername, int peerport, const struct addrinfo *addr);
101

jehan's avatar
jehan committed
102
void belle_sip_channel_resolve(belle_sip_channel_t *obj);
103

jehan's avatar
jehan committed
104
void belle_sip_channel_connect(belle_sip_channel_t *obj);
105 106 107 108

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
109 110 111 112
/*only used by channels implementation*/
void belle_sip_channel_set_ready(belle_sip_channel_t *obj, const struct sockaddr *addr, socklen_t slen);
void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_stack_t *stack, int fd, belle_sip_source_func_t process_data, const char *bindip,int localport,const char *peername, int peer_port);
/*end of channel implementations*/
jehan's avatar
jehan committed
113 114 115 116
/**
 * pickup last received message. This method take the ownership of the message.
 */
belle_sip_message_t* belle_sip_channel_pick_message(belle_sip_channel_t *obj);
117 118 119

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

120 121
int belle_sip_channel_is_reliable(const belle_sip_channel_t *obj);

122
const char * belle_sip_channel_get_transport_name(const belle_sip_channel_t *obj);
jehan's avatar
jehan committed
123
const char * belle_sip_channel_get_transport_name_lower_case(const belle_sip_channel_t *obj);
124

125 126
const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj);

127 128
const char *belle_sip_channel_get_local_address(belle_sip_channel_t *obj, int *port);

Simon Morlat's avatar
Simon Morlat committed
129 130
#define belle_sip_channel_get_state(chan) ((chan)->state)

jehan's avatar
jehan committed
131 132 133 134
void channel_set_state(belle_sip_channel_t *obj, belle_sip_channel_state_t state);

void channel_process_queue(belle_sip_channel_t *obj);

Simon Morlat's avatar
Simon Morlat committed
135 136
/*just invokes the listeners to process data*/
void belle_sip_channel_process_data(belle_sip_channel_t *obj,unsigned int revents);
137 138

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_channel_t,belle_sip_source_t)
139 140
	const char *transport;
	int reliable;
141 142 143 144 145
	int (*connect)(belle_sip_channel_t *obj, const struct sockaddr *, socklen_t socklen);
	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);
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

jehan's avatar
jehan committed
146 147 148



149 150

#endif