stream_channel.c 5.91 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	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/>.
*/

#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

jehan's avatar
jehan committed
23
/*************TCP********/
jehan's avatar
jehan committed
24

25
static int stream_channel_process_data(belle_sip_channel_t *obj,unsigned int revents);
jehan's avatar
jehan committed
26

jehan's avatar
jehan committed
27

jehan's avatar
jehan committed
28
static void stream_channel_uninit(belle_sip_stream_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
29 30
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
	if (sock!=(belle_sip_socket_t)-1) stream_channel_close((belle_sip_channel_t*)obj);
jehan's avatar
jehan committed
31 32
}

33
int stream_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
Simon Morlat's avatar
Simon Morlat committed
34
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
jehan's avatar
jehan committed
35 36
	int err;
	err=send(sock,buf,buflen,0);
Simon Morlat's avatar
Simon Morlat committed
37
	if (err==(belle_sip_socket_t)-1){
Ghislain MARY's avatar
Ghislain MARY committed
38
		belle_sip_error("Could not send stream packet on channel [%p]: %s",obj,belle_sip_get_socket_error_string());
jehan's avatar
jehan committed
39 40 41 42 43
		return -errno;
	}
	return err;
}

44
int stream_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen){
Simon Morlat's avatar
Simon Morlat committed
45
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
jehan's avatar
jehan committed
46
	int err;
47
	err=recv(sock,buf,buflen,0);
Simon Morlat's avatar
Simon Morlat committed
48
	if (err==(belle_sip_socket_t)-1){
Ghislain MARY's avatar
Ghislain MARY committed
49
		belle_sip_error("Could not receive stream packet: %s",belle_sip_get_socket_error_string());
jehan's avatar
jehan committed
50 51 52 53 54
		return -errno;
	}
	return err;
}

55
void stream_channel_close(belle_sip_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
56 57
	belle_sip_socket_t sock = belle_sip_source_get_socket((belle_sip_source_t*)obj);
	if (sock!=(belle_sip_socket_t)-1){
58
		close_socket(sock);
Simon Morlat's avatar
Simon Morlat committed
59
		belle_sip_source_uninit((belle_sip_source_t*)obj);
60 61 62
	}
}

63
int stream_channel_connect(belle_sip_channel_t *obj, const struct addrinfo *ai){
jehan's avatar
jehan committed
64 65
	int err;
	int tmp;
Simon Morlat's avatar
Simon Morlat committed
66
	belle_sip_socket_t sock;
jehan's avatar
jehan committed
67
	tmp=1;
68
	
Simon Morlat's avatar
Simon Morlat committed
69
	sock=socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
70
	
Simon Morlat's avatar
Simon Morlat committed
71
	if (sock==(belle_sip_socket_t)-1){
72 73 74 75
		belle_sip_error("Could not create socket: %s",belle_sip_get_socket_error_string());
		return -1;
	}
	
jehan's avatar
jehan committed
76 77 78 79
	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
80 81
	belle_sip_socket_set_nonblocking(sock);
	belle_sip_channel_set_socket(obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
Simon Morlat's avatar
Simon Morlat committed
82
	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_WRITE|BELLE_SIP_EVENT_ERROR);
83
	
84
	err = connect(sock,ai->ai_addr,ai->ai_addrlen);
Ghislain MARY's avatar
Ghislain MARY committed
85
	if (err != 0 && get_socket_error()!=BELLESIP_EINPROGRESS && get_socket_error()!=BELLESIP_EWOULDBLOCK) {
Simon Morlat's avatar
Simon Morlat committed
86 87 88
		belle_sip_error("stream connect failed %s",belle_sip_get_socket_error_string());
		close_socket(sock);
		return -1;
jehan's avatar
jehan committed
89
	}
90
	belle_sip_main_loop_add_source(obj->stack->ml,(belle_sip_source_t*)obj);
jehan's avatar
jehan committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

	return 0;
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stream_channel_t);

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_stream_channel_t)=
{
	{
		{
			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*/
		stream_channel_connect,
		stream_channel_send,
110 111
		stream_channel_recv,
		stream_channel_close,
jehan's avatar
jehan committed
112 113
	}
};
114

Simon Morlat's avatar
Simon Morlat committed
115
int finalize_stream_connection (belle_sip_socket_t sock, struct sockaddr *addr, socklen_t* slen) {
jehan's avatar
jehan committed
116 117
	int err, errnum;
	socklen_t optlen=sizeof(errnum);
Simon Morlat's avatar
Simon Morlat committed
118
	err=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void*)&errnum,&optlen);
jehan's avatar
jehan committed
119
	if (err!=0){
Simon Morlat's avatar
Simon Morlat committed
120
		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
121 122 123 124
		return -1;
	}else{
		if (errnum==0){
			/*obtain bind address for client*/
Simon Morlat's avatar
Simon Morlat committed
125
			err=getsockname(sock,addr,slen);
jehan's avatar
jehan committed
126
			if (err<0){
Simon Morlat's avatar
Simon Morlat committed
127
				belle_sip_error("Failed to retrieve sockname  for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
jehan's avatar
jehan committed
128 129 130 131
				return -1;
			}
			return 0;
		}else{
Simon Morlat's avatar
Simon Morlat committed
132
			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
133 134 135 136 137 138 139
			return -1;
		}
	}
}
static int stream_channel_process_data(belle_sip_channel_t *obj,unsigned int revents){
	struct sockaddr_storage ss;
	socklen_t addrlen=sizeof(ss);
Simon Morlat's avatar
Simon Morlat committed
140
	belle_sip_socket_t fd=belle_sip_source_get_socket((belle_sip_source_t*)obj);
Simon Morlat's avatar
Simon Morlat committed
141 142 143

	belle_sip_message("TCP channel process_data");
	
jehan's avatar
jehan committed
144
	if (obj->state == BELLE_SIP_CHANNEL_CONNECTING && (revents&BELLE_SIP_EVENT_WRITE)) {
jehan's avatar
jehan committed
145

jehan's avatar
jehan committed
146 147 148 149
		if (finalize_stream_connection(fd,(struct sockaddr*)&ss,&addrlen)) {
			belle_sip_error("Cannot connect to [%s://%s:%s]",belle_sip_channel_get_transport_name(obj),obj->peer_name,obj->peer_port);
			channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
			return BELLE_SIP_STOP;
jehan's avatar
jehan committed
150
		}
jehan's avatar
jehan committed
151
		belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
jehan's avatar
jehan committed
152 153 154
		belle_sip_channel_set_ready(obj,(struct sockaddr*)&ss,addrlen);
		return BELLE_SIP_CONTINUE;

jehan's avatar
jehan committed
155
	} else if ( obj->state == BELLE_SIP_CHANNEL_READY) {
jehan's avatar
jehan committed
156
		return belle_sip_channel_process_data(obj,revents);
jehan's avatar
jehan committed
157
	} else {
jehan's avatar
jehan committed
158
		belle_sip_warning("Unexpected event [%i], in state [%s] for channel [%p]",revents,belle_sip_channel_state_to_string(obj->state),obj);
jehan's avatar
jehan committed
159
		return BELLE_SIP_STOP;
jehan's avatar
jehan committed
160
	}
jehan's avatar
jehan committed
161
	return BELLE_SIP_CONTINUE;
jehan's avatar
jehan committed
162
}
jehan's avatar
jehan committed
163 164 165 166 167 168 169 170

belle_sip_channel_t * belle_sip_channel_new_tcp(belle_sip_stack_t *stack,const char *bindip, int localport, const char *dest, int port){
	belle_sip_stream_channel_t *obj=belle_sip_object_new(belle_sip_stream_channel_t);
	belle_sip_channel_init((belle_sip_channel_t*)obj
							,stack
							,bindip,localport,dest,port);
	return (belle_sip_channel_t*)obj;
}