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 
19 #include "belle_sip_internal.h"
20 #include "belle-sip/mainloop.h"
21 #include "stream_channel.h"
22 
set_tcp_nodelay(belle_sip_socket_t sock)23 static void set_tcp_nodelay(belle_sip_socket_t sock){
24 	int tmp=1;
25 	int err=bctbx_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
26 	if (err == -1){
27 		belle_sip_warning ("Fail to set TCP_NODELAY: %s.", belle_sip_get_socket_error_string());
28 	}
29 }
30 
31 /*************TCP********/
32 
33 static int stream_channel_process_data(belle_sip_stream_channel_t *obj,unsigned int revents);
34 
35 
stream_channel_uninit(belle_sip_stream_channel_t * obj)36 static void stream_channel_uninit(belle_sip_stream_channel_t *obj){
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);
39 }
40 
stream_channel_send(belle_sip_stream_channel_t * obj,const void * buf,size_t buflen)41 int stream_channel_send(belle_sip_stream_channel_t *obj, const void *buf, size_t buflen){
42 	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
43 	int err=bctbx_send(sock,buf,buflen,0);
44 	if (err==(belle_sip_socket_t)-1){
45 		int errnum=get_socket_error();
46 		if (!belle_sip_error_code_is_would_block(errnum)){
47 			belle_sip_error("Could not send stream packet on channel [%p]: %s",obj,belle_sip_get_socket_error_string_from_code(errnum));
48 		}
49 		return -errnum;
50 	}
51 	return err;
52 }
53 
stream_channel_recv(belle_sip_stream_channel_t * obj,void * buf,size_t buflen)54 int stream_channel_recv(belle_sip_stream_channel_t *obj, void *buf, size_t buflen){
55 	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
56 	int err=bctbx_recv(sock,buf,buflen,0);
57 
58 	if (err==(belle_sip_socket_t)-1){
59 		int errnum=get_socket_error();
60 		if (errnum == BCTBX_ENOTCONN) { //Do NOT treat it as an error
61 			belle_sip_message("Socket is not connected because of IOS10 background policy");
62 			obj->base.closed_by_remote = TRUE;
63 			return 0;
64 		}
65 
66 		if (!belle_sip_error_code_is_would_block(errnum)){
67 			belle_sip_error("Could not receive stream packet: %s",belle_sip_get_socket_error_string_from_code(errnum));
68 		}
69 		return -errnum;
70 	}
71 	return err;
72 }
73 
stream_channel_close(belle_sip_stream_channel_t * obj)74 void stream_channel_close(belle_sip_stream_channel_t *obj){
75 	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
76 	if (sock!=(belle_sip_socket_t)-1){
77 #if TARGET_OS_IPHONE
78 		if (obj->read_stream != NULL) {
79 			CFReadStreamClose (obj->read_stream);
80 			CFRelease (obj->read_stream);
81 			obj->read_stream=NULL;
82 		}
83 		if (obj->write_stream != NULL) {
84 			CFWriteStreamClose (obj->write_stream);
85 			CFRelease (obj->write_stream);
86 			obj->write_stream=NULL;
87 		}
88 #endif
89 		belle_sip_close_socket(sock);
90 	}
91 }
92 
93 #if TARGET_OS_IPHONE
stream_channel_enable_ios_background_mode(belle_sip_stream_channel_t * obj)94 static void stream_channel_enable_ios_background_mode(belle_sip_stream_channel_t *obj){
95 	int sock=belle_sip_source_get_socket((belle_sip_source_t*)obj);
96 
97 	CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &obj->read_stream, &obj->write_stream);
98 	if (obj->read_stream){
99 		if (!CFReadStreamSetProperty (obj->read_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)){
100 			belle_sip_warning("CFReadStreamSetProperty() could not set VoIP service type on read stream.");
101 		}
102 	}else belle_sip_warning("CFStreamCreatePairWithSocket() could not create the read stream.");
103 	if (obj->write_stream){
104 		if (!CFWriteStreamSetProperty (obj->write_stream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)){
105 			belle_sip_warning("CFReadStreamSetProperty() could not set VoIP service type on write stream.");
106 		}
107 	}else belle_sip_warning("CFStreamCreatePairWithSocket() could not create the write stream.");
108 
109 	if (!CFReadStreamOpen (obj->read_stream)) {
110 		belle_sip_warning("CFReadStreamOpen() failed.");
111 	}
112 
113 	if (!CFWriteStreamOpen (obj->write_stream)) {
114 		belle_sip_warning("CFWriteStreamOpen() failed.");
115 	}
116 }
117 
118 #endif
119 
stream_channel_connect(belle_sip_stream_channel_t * obj,const struct addrinfo * ai)120 int stream_channel_connect(belle_sip_stream_channel_t *obj, const struct addrinfo *ai){
121 	int err;
122 	int tmp;
123 	belle_sip_socket_t sock;
124 	tmp=1;
125 
126 	obj->base.ai_family=ai->ai_family;
127 	sock=bctbx_socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
128 
129 	if (sock==(belle_sip_socket_t)-1){
130 		belle_sip_error("Could not create socket: %s",belle_sip_get_socket_error_string());
131 		return -1;
132 	}
133 
134 	err=bctbx_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
135 	if (err!=0){
136 		belle_sip_error("bctbx_setsockopt TCP_NODELAY failed: [%s]",belle_sip_get_socket_error_string());
137 	}
138 	belle_sip_socket_set_nonblocking(sock);
139 	if (ai->ai_family==AF_INET6){
140 		belle_sip_socket_enable_dual_stack(sock);
141 	}
142 
143 	err = bctbx_connect(sock,ai->ai_addr,(socklen_t)ai->ai_addrlen);
144 	if (err != 0 && get_socket_error()!=BELLESIP_EINPROGRESS && get_socket_error()!=BELLESIP_EWOULDBLOCK) {
145 		belle_sip_error("stream connect failed %s",belle_sip_get_socket_error_string());
146 		belle_sip_close_socket(sock);
147 		return -1;
148 	}
149 	belle_sip_channel_set_socket((belle_sip_channel_t*)obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
150 	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_WRITE|BELLE_SIP_EVENT_ERROR);
151 	belle_sip_source_set_timeout((belle_sip_source_t*)obj,belle_sip_stack_get_transport_timeout(obj->base.stack));
152 	belle_sip_main_loop_add_source(obj->base.stack->ml,(belle_sip_source_t*)obj);
153 	return 0;
154 }
155 
156 BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stream_channel_t);
157 
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_stream_channel_t)158 BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_stream_channel_t)
159 	{
160 		{
161 			BELLE_SIP_VPTR_INIT(belle_sip_stream_channel_t,belle_sip_channel_t,FALSE),
162 			(belle_sip_object_destroy_t)stream_channel_uninit,
163 			NULL,
164 			NULL,
165 			BELLE_SIP_DEFAULT_BUFSIZE_HINT
166 		},
167 		"TCP",
168 		1, /*is_reliable*/
169 		(int (*)(belle_sip_channel_t *, const struct addrinfo *))stream_channel_connect,
170 		(int (*)(belle_sip_channel_t *, const void *, size_t ))stream_channel_send,
171 		(int (*)(belle_sip_channel_t *, void *, size_t ))stream_channel_recv,
172 		(void (*)(belle_sip_channel_t *))stream_channel_close,
173 	}
174 BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
175 
finalize_stream_connection(belle_sip_stream_channel_t * obj,unsigned int revents,struct sockaddr * addr,socklen_t * slen)176 int finalize_stream_connection(belle_sip_stream_channel_t *obj, unsigned int revents, struct sockaddr *addr, socklen_t* slen) {
177 	int err, errnum;
178 	socklen_t optlen=sizeof(errnum);
179 	belle_sip_socket_t sock=belle_sip_source_get_socket((belle_sip_source_t*)obj);
180 
181 	if (revents==BELLE_SIP_EVENT_TIMEOUT){
182 		belle_sip_warning("channel [%p]: user-defined transport timeout.",obj);
183 		return -1;
184 	}
185 	if (!(revents & BELLE_SIP_EVENT_WRITE) && !(revents & BELLE_SIP_EVENT_READ)){
186 		belle_sip_warning("channel [%p]: getting unexpected event while connecting",obj);
187 		return -1;
188 	}
189 
190 	err=bctbx_getsockopt(sock,SOL_SOCKET,SO_ERROR,(void*)&errnum,&optlen);
191 	if (err!=0){
192 		belle_sip_error("Failed to retrieve connection status for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
193 		return -1;
194 	}else{
195 		if (errnum==0){
196 			/*obtain bind address for client*/
197 			err=bctbx_getsockname(sock,addr,slen);
198 			if (err<0){
199 				belle_sip_error("Failed to retrieve sockname  for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
200 				return -1;
201 			}
202 #if TARGET_OS_IPHONE
203 			stream_channel_enable_ios_background_mode(obj);
204 #endif
205 			if (obj->base.stack->dscp && obj->base.lp){
206 				/*apply dscp only to channel belonging to a SIP listening point*/
207 				belle_sip_socket_set_dscp(sock,obj->base.ai_family,obj->base.stack->dscp);
208 			}
209 			set_tcp_nodelay(sock);
210 			return 0;
211 		}else{
212 			belle_sip_error("Connection failed  for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string_from_code(errnum));
213 			return -1;
214 		}
215 	}
216 }
217 
stream_channel_process_data(belle_sip_stream_channel_t * obj,unsigned int revents)218 static int stream_channel_process_data(belle_sip_stream_channel_t *obj,unsigned int revents){
219 	struct sockaddr_storage ss;
220 	socklen_t addrlen=sizeof(ss);
221 	belle_sip_channel_state_t state=belle_sip_channel_get_state((belle_sip_channel_t*)obj);
222 	belle_sip_channel_t *base=(belle_sip_channel_t*)obj;
223 
224 	/*belle_sip_message("TCP channel process_data");*/
225 
226 	if (state == BELLE_SIP_CHANNEL_CONNECTING ) {
227 		if (finalize_stream_connection(obj,revents,(struct sockaddr*)&ss,&addrlen)) {
228 			belle_sip_error("Cannot connect to [%s://%s:%i]",belle_sip_channel_get_transport_name(base),base->peer_name,base->peer_port);
229 			channel_set_state(base,BELLE_SIP_CHANNEL_ERROR);
230 			return BELLE_SIP_STOP;
231 		}
232 		belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
233 		belle_sip_source_set_timeout((belle_sip_source_t*)obj,-1);
234 		belle_sip_channel_set_ready(base,(struct sockaddr*)&ss,addrlen);
235 		return BELLE_SIP_CONTINUE;
236 	} else if (state == BELLE_SIP_CHANNEL_READY) {
237 		return belle_sip_channel_process_data(base,revents);
238 	} else {
239 		belle_sip_warning("Unexpected event [%i], in state [%s] for channel [%p]",revents,belle_sip_channel_state_to_string(state),obj);
240 		return BELLE_SIP_STOP;
241 	}
242 	return BELLE_SIP_CONTINUE;
243 }
244 
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)245 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){
246 	belle_sip_channel_init((belle_sip_channel_t*)obj, stack
247 					,bindip,localport,peer_cname,dest,port);
248 }
249 
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)250 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){
251 	belle_sip_stream_channel_t *obj=belle_sip_object_new(belle_sip_stream_channel_t);
252 	belle_sip_stream_channel_init_client(obj,stack,bindip,localport,peer_cname,dest,port);
253 	return (belle_sip_channel_t*)obj;
254 }
255 
256 /*child of server socket*/
belle_sip_stream_channel_new_child(belle_sip_stack_t * stack,belle_sip_socket_t sock,struct sockaddr * remote_addr,socklen_t slen)257 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){
258 	struct sockaddr_storage localaddr;
259 	socklen_t local_len=sizeof(localaddr);
260 	belle_sip_stream_channel_t *obj;
261 	int err;
262 	int optval=1;
263 
264 	err = bctbx_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
265 			(char*)&optval, sizeof (optval));
266 	if (err == -1){
267 		belle_sip_warning ("Fail to set SIP/TCP address reusable: %s.", belle_sip_get_socket_error_string());
268 	}
269 
270 	set_tcp_nodelay(sock);
271 
272 	if (bctbx_getsockname(sock,(struct sockaddr*)&localaddr,&local_len)==-1){
273 		belle_sip_error("bctbx_getsockname() failed: %s",belle_sip_get_socket_error_string());
274 		return NULL;
275 	}
276 
277 	obj=belle_sip_object_new(belle_sip_stream_channel_t);
278 	belle_sip_channel_init_with_addr((belle_sip_channel_t*)obj,stack,NULL,0,remote_addr,slen);
279 	belle_sip_socket_set_nonblocking(sock);
280 	belle_sip_channel_set_socket((belle_sip_channel_t*)obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
281 	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
282 	belle_sip_channel_set_ready((belle_sip_channel_t*)obj,(struct sockaddr*)&localaddr,local_len);
283 	belle_sip_main_loop_add_source(stack->ml,(belle_sip_source_t*)obj);
284 	return (belle_sip_channel_t*)obj;
285 }
286 
287 
288