stream_channel.c 10.5 KB
Newer Older
jehan's avatar
jehan committed
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
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17 18 19 20
    (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/>.
*/

#include "belle_sip_internal.h"
#include "belle-sip/mainloop.h"
jehan's avatar
jehan committed
21
#include "stream_channel.h"
jehan's avatar
jehan committed
22

Simon Morlat's avatar
Simon Morlat committed
23 24 25 26 27 28 29
static void set_tcp_nodelay(belle_sip_socket_t sock){
	int tmp=1;
	int err=setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
	if (err == -1){
		belle_sip_warning ("Fail to set TCP_NODELAY: %s.", belle_sip_get_socket_error_string());
	}
}
30

jehan's avatar
jehan committed
31
/*************TCP********/
jehan's avatar
jehan committed
32

33
static int stream_channel_process_data(belle_sip_stream_channel_t *obj,unsigned int revents);
jehan's avatar
jehan committed
34

jehan's avatar
jehan committed
35

jehan's avatar
jehan committed
36
static void stream_channel_uninit(belle_sip_stream_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
37
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
38
	if (sock!=(belle_sip_socket_t)-1) stream_channel_close(obj);
jehan's avatar
jehan committed
39 40
}

41
int stream_channel_send(belle_sip_stream_channel_t *obj, const void *buf, size_t buflen){
Simon Morlat's avatar
Simon Morlat committed
42
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
jehan's avatar
jehan committed
43 44
	int err;
	err=send(sock,buf,buflen,0);
Simon Morlat's avatar
Simon Morlat committed
45
	if (err==(belle_sip_socket_t)-1){
46
		int errnum=get_socket_error();
47
		if (!belle_sip_error_code_is_would_block(errnum)){
48
			belle_sip_error("Could not send stream packet on channel [%p]: %s",obj,belle_sip_get_socket_error_string());
49 50
		}else{
			belle_sip_warning("Channel [%p]: stream_channel_send EWOULDBLOCK",obj);
51 52
		}
		return -errnum;
jehan's avatar
jehan committed
53 54 55 56
	}
	return err;
}

57
int stream_channel_recv(belle_sip_stream_channel_t *obj, void *buf, size_t buflen){
Simon Morlat's avatar
Simon Morlat committed
58
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
jehan's avatar
jehan committed
59
	int err;
60
	err=recv(sock,buf,buflen,0);
Simon Morlat's avatar
Simon Morlat committed
61
	if (err==(belle_sip_socket_t)-1){
62
		int errnum=get_socket_error();
63
		if (!belle_sip_error_code_is_would_block(errnum)){
64 65 66
			belle_sip_error("Could not receive stream packet: %s",belle_sip_get_socket_error_string());
		}
		return -errnum;
jehan's avatar
jehan committed
67 68 69 70
	}
	return err;
}

71
void stream_channel_close(belle_sip_stream_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
72 73
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
	if (sock!=(belle_sip_socket_t)-1){
74
#if TARGET_OS_IPHONE
75 76 77 78 79 80 81 82 83 84 85
		if (obj->read_stream != NULL) {
			CFReadStreamClose (obj->read_stream);
			CFRelease (obj->read_stream);
			obj->read_stream=NULL;
		}
		if (obj->write_stream != NULL) {
			CFWriteStreamClose (obj->write_stream);
			CFRelease (obj->write_stream);
			obj->write_stream=NULL;
		}
#endif
86
		close_socket(sock);
87 88 89
	}
}

90
#if TARGET_OS_IPHONE
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static void stream_channel_enable_ios_background_mode(belle_sip_stream_channel_t *obj){
	int sock=belle_sip_source_get_socket((belle_sip_source_t*)obj);
	
	CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &obj->read_stream, &obj->write_stream);
	if (obj->read_stream){
		if (!CFReadStreamSetProperty (obj->read_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)){
			belle_sip_warning("CFReadStreamSetProperty() could not set VoIP service type on read stream.");
		}
	}else belle_sip_warning("CFStreamCreatePairWithSocket() could not create the read stream.");
	if (obj->write_stream){
		if (!CFWriteStreamSetProperty (obj->write_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)){
			belle_sip_warning("CFReadStreamSetProperty() could not set VoIP service type on write stream.");
		}
	}else belle_sip_warning("CFStreamCreatePairWithSocket() could not create the write stream.");
	
	if (!CFReadStreamOpen (obj->read_stream)) {
		belle_sip_warning("CFReadStreamOpen() failed.");
	}
	
	if (!CFWriteStreamOpen (obj->write_stream)) {
		belle_sip_warning("CFWriteStreamOpen() failed.");
112 113 114
	}
}

115 116 117
#endif

int stream_channel_connect(belle_sip_stream_channel_t *obj, const struct addrinfo *ai){
jehan's avatar
jehan committed
118 119
	int err;
	int tmp;
Simon Morlat's avatar
Simon Morlat committed
120
	belle_sip_socket_t sock;
jehan's avatar
jehan committed
121
	tmp=1;
122
	
Simon Morlat's avatar
Simon Morlat committed
123
	sock=socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
124
	
Simon Morlat's avatar
Simon Morlat committed
125
	if (sock==(belle_sip_socket_t)-1){
126 127 128 129
		belle_sip_error("Could not create socket: %s",belle_sip_get_socket_error_string());
		return -1;
	}
	
jehan's avatar
jehan committed
130 131 132 133
	err=setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
	if (err!=0){
		belle_sip_error("setsockopt TCP_NODELAY failed: [%s]",belle_sip_get_socket_error_string());
	}
Simon Morlat's avatar
Simon Morlat committed
134
	belle_sip_socket_set_nonblocking(sock);
135
	
136
	err = connect(sock,ai->ai_addr,ai->ai_addrlen);
Ghislain MARY's avatar
Ghislain MARY committed
137
	if (err != 0 && get_socket_error()!=BELLESIP_EINPROGRESS && get_socket_error()!=BELLESIP_EWOULDBLOCK) {
Simon Morlat's avatar
Simon Morlat committed
138 139 140
		belle_sip_error("stream connect failed %s",belle_sip_get_socket_error_string());
		close_socket(sock);
		return -1;
jehan's avatar
jehan committed
141
	}
142
	belle_sip_channel_set_socket((belle_sip_channel_t*)obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
143
	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_WRITE|BELLE_SIP_EVENT_ERROR);
144
	belle_sip_source_set_timeout((belle_sip_source_t*)obj,belle_sip_stack_get_transport_timeout(obj->base.stack));
145
	belle_sip_main_loop_add_source(obj->base.stack->ml,(belle_sip_source_t*)obj);
jehan's avatar
jehan committed
146 147 148 149 150
	return 0;
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stream_channel_t);

151
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_stream_channel_t)
jehan's avatar
jehan committed
152 153 154 155 156 157 158 159 160
	{
		{
			BELLE_SIP_VPTR_INIT(belle_sip_stream_channel_t,belle_sip_channel_t,FALSE),
			(belle_sip_object_destroy_t)stream_channel_uninit,
			NULL,
			NULL
		},
		"TCP",
		1, /*is_reliable*/
161 162 163 164
		(int (*)(belle_sip_channel_t *, const struct addrinfo *))stream_channel_connect,
		(int (*)(belle_sip_channel_t *, const void *, size_t ))stream_channel_send,
		(int (*)(belle_sip_channel_t *, void *, size_t ))stream_channel_recv,
		(void (*)(belle_sip_channel_t *))stream_channel_close,
jehan's avatar
jehan committed
165
	}
166
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
167

168
int finalize_stream_connection(belle_sip_stream_channel_t *obj, unsigned int revents, struct sockaddr *addr, socklen_t* slen) {
jehan's avatar
jehan committed
169 170
	int err, errnum;
	socklen_t optlen=sizeof(errnum);
171 172
	belle_sip_socket_t sock=belle_sip_source_get_socket((belle_sip_source_t*)obj);
	
173 174 175 176
	if (revents==BELLE_SIP_EVENT_TIMEOUT){
		belle_sip_warning("channel [%p]: user-defined transport timeout.",obj);
		return -1;
	}
177
	if (!(revents & BELLE_SIP_EVENT_WRITE) && !(revents & BELLE_SIP_EVENT_READ)){
178 179 180 181
		belle_sip_warning("channel [%p]: getting unexpected event while connecting",obj);
		return -1;
	}
	
Simon Morlat's avatar
Simon Morlat committed
182
	err=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void*)&errnum,&optlen);
jehan's avatar
jehan committed
183
	if (err!=0){
Simon Morlat's avatar
Simon Morlat committed
184
		belle_sip_error("Failed to retrieve connection status for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
jehan's avatar
jehan committed
185 186 187 188
		return -1;
	}else{
		if (errnum==0){
			/*obtain bind address for client*/
Simon Morlat's avatar
Simon Morlat committed
189
			err=getsockname(sock,addr,slen);
jehan's avatar
jehan committed
190
			if (err<0){
Simon Morlat's avatar
Simon Morlat committed
191
				belle_sip_error("Failed to retrieve sockname  for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
jehan's avatar
jehan committed
192 193
				return -1;
			}
194 195 196
#if TARGET_OS_IPHONE
			stream_channel_enable_ios_background_mode(obj);
#endif
Simon Morlat's avatar
Simon Morlat committed
197 198
			if (obj->base.stack->dscp)
				belle_sip_socket_set_dscp(sock,obj->base.lp->ai_family,obj->base.stack->dscp);
Simon Morlat's avatar
Simon Morlat committed
199
			set_tcp_nodelay(sock);
jehan's avatar
jehan committed
200 201
			return 0;
		}else{
Simon Morlat's avatar
Simon Morlat committed
202
			belle_sip_error("Connection failed  for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string_from_code(errnum));
jehan's avatar
jehan committed
203 204 205 206
			return -1;
		}
	}
}
207

208
static int stream_channel_process_data(belle_sip_stream_channel_t *obj,unsigned int revents){
jehan's avatar
jehan committed
209 210
	struct sockaddr_storage ss;
	socklen_t addrlen=sizeof(ss);
211 212
	belle_sip_channel_state_t state=belle_sip_channel_get_state((belle_sip_channel_t*)obj);
	belle_sip_channel_t *base=(belle_sip_channel_t*)obj;
Simon Morlat's avatar
Simon Morlat committed
213

214
	/*belle_sip_message("TCP channel process_data");*/
Simon Morlat's avatar
Simon Morlat committed
215
	
216 217
	if (state == BELLE_SIP_CHANNEL_CONNECTING ) {
		if (finalize_stream_connection(obj,revents,(struct sockaddr*)&ss,&addrlen)) {
Simon Morlat's avatar
Simon Morlat committed
218
			belle_sip_error("Cannot connect to [%s://%s:%i]",belle_sip_channel_get_transport_name(base),base->peer_name,base->peer_port);
219
			channel_set_state(base,BELLE_SIP_CHANNEL_ERROR);
jehan's avatar
jehan committed
220
			return BELLE_SIP_STOP;
jehan's avatar
jehan committed
221
		}
jehan's avatar
jehan committed
222
		belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
223
		belle_sip_source_set_timeout((belle_sip_source_t*)obj,-1);
224
		belle_sip_channel_set_ready(base,(struct sockaddr*)&ss,addrlen);
jehan's avatar
jehan committed
225
		return BELLE_SIP_CONTINUE;
226 227
	} else if (state == BELLE_SIP_CHANNEL_READY) {
		return belle_sip_channel_process_data(base,revents);
jehan's avatar
jehan committed
228
	} else {
229
		belle_sip_warning("Unexpected event [%i], in state [%s] for channel [%p]",revents,belle_sip_channel_state_to_string(state),obj);
jehan's avatar
jehan committed
230
		return BELLE_SIP_STOP;
jehan's avatar
jehan committed
231
	}
jehan's avatar
jehan committed
232
	return BELLE_SIP_CONTINUE;
jehan's avatar
jehan committed
233
}
jehan's avatar
jehan committed
234

235 236 237 238 239 240
void belle_sip_stream_channel_init_client(belle_sip_stream_channel_t *obj, belle_sip_stack_t *stack, const char *bindip, int localport, const char *peer_cname, const char *dest, int port){
	belle_sip_channel_init((belle_sip_channel_t*)obj, stack
					,bindip,localport,peer_cname,dest,port);
}

belle_sip_channel_t * belle_sip_stream_channel_new_client(belle_sip_stack_t *stack,const char *bindip, int localport, const char *peer_cname, const char *dest, int port){
jehan's avatar
jehan committed
241
	belle_sip_stream_channel_t *obj=belle_sip_object_new(belle_sip_stream_channel_t);
242
	belle_sip_stream_channel_init_client(obj,stack,bindip,localport,peer_cname,dest,port);
jehan's avatar
jehan committed
243 244
	return (belle_sip_channel_t*)obj;
}
245

Simon Morlat's avatar
Simon Morlat committed
246
/*child of server socket*/
247 248 249 250
belle_sip_channel_t * belle_sip_stream_channel_new_child(belle_sip_stack_t *stack, belle_sip_socket_t sock, struct sockaddr *remote_addr, socklen_t slen){
	struct sockaddr_storage localaddr;
	socklen_t local_len=sizeof(localaddr);
	belle_sip_stream_channel_t *obj;
Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255 256 257 258 259 260
	int err;
	int optval=1;
	
	err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
			(char*)&optval, sizeof (optval));
	if (err == -1){
		belle_sip_warning ("Fail to set SIP/TCP address reusable: %s.", belle_sip_get_socket_error_string());
	}
	
	set_tcp_nodelay(sock);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	
	if (getsockname(sock,(struct sockaddr*)&localaddr,&local_len)==-1){
		belle_sip_error("getsockname() failed: %s",belle_sip_get_socket_error_string());
		return NULL;
	}
	
	obj=belle_sip_object_new(belle_sip_stream_channel_t);
	belle_sip_channel_init_with_addr((belle_sip_channel_t*)obj,stack,remote_addr,slen);
	belle_sip_socket_set_nonblocking(sock);
	belle_sip_channel_set_socket((belle_sip_channel_t*)obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
	belle_sip_channel_set_ready((belle_sip_channel_t*)obj,(struct sockaddr*)&localaddr,local_len);
	belle_sip_main_loop_add_source(stack->ml,(belle_sip_source_t*)obj);
	return (belle_sip_channel_t*)obj;
}