stream_channel.c 5.7 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/>.
*/



jehan's avatar
jehan committed
21 22 23



jehan's avatar
jehan committed
24 25 26 27 28
#include <sys/socket.h>
#include <netinet/tcp.h>

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

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

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

jehan's avatar
jehan committed
35

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

41
int stream_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
jehan's avatar
jehan committed
42 43 44 45
	belle_sip_fd_t sock = belle_sip_source_get_fd((belle_sip_source_t*)obj);
	int err;
	err=send(sock,buf,buflen,0);
	if (err==-1){
46
		belle_sip_error("Could not send stream packet on channel [%p]: %s",obj,strerror(errno));
jehan's avatar
jehan committed
47 48 49 50 51
		return -errno;
	}
	return err;
}

52
int stream_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen){
jehan's avatar
jehan committed
53 54
	belle_sip_fd_t sock = belle_sip_source_get_fd((belle_sip_source_t*)obj);
	int err;
55 56
	err=recv(sock,buf,buflen,0);
	if (err==-1){
jehan's avatar
jehan committed
57 58 59 60 61 62
		belle_sip_error("Could not receive stream packet: %s",strerror(errno));
		return -errno;
	}
	return err;
}

63 64 65 66 67 68 69 70
void stream_channel_close(belle_sip_channel_t *obj){
	belle_sip_fd_t sock = belle_sip_source_get_fd((belle_sip_source_t*)obj);
	if (sock!=-1){
		close_socket(sock);
		obj->base.fd=-1;
	}
}

71
int stream_channel_connect(belle_sip_channel_t *obj, const struct addrinfo *ai){
jehan's avatar
jehan committed
72 73
	int err;
	int tmp;
74
	belle_sip_fd_t sock;
jehan's avatar
jehan committed
75
	tmp=1;
76
	
Simon Morlat's avatar
Simon Morlat committed
77
	sock=socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
78 79 80 81 82 83
	
	if (sock==-1){
		belle_sip_error("Could not create socket: %s",belle_sip_get_socket_error_string());
		return -1;
	}
	
jehan's avatar
jehan committed
84 85 86 87 88
	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());
	}
	fcntl(sock,F_SETFL,fcntl(sock,F_GETFL) | O_NONBLOCK);
89
	belle_sip_channel_set_fd(obj,sock,(belle_sip_source_func_t)stream_channel_process_data);
Simon Morlat's avatar
Simon Morlat committed
90
	belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_WRITE|BELLE_SIP_EVENT_ERROR);
jehan's avatar
jehan committed
91
	belle_sip_main_loop_add_source(obj->stack->ml,(belle_sip_source_t*)obj);
92
	err = connect(sock,ai->ai_addr,ai->ai_addrlen);
jehan's avatar
jehan committed
93
	if (err != 0 && get_socket_error()!=EINPROGRESS) {
Simon Morlat's avatar
Simon Morlat committed
94 95 96
		belle_sip_error("stream connect failed %s",belle_sip_get_socket_error_string());
		close_socket(sock);
		return -1;
jehan's avatar
jehan committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	}

	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,
117 118
		stream_channel_recv,
		stream_channel_close,
jehan's avatar
jehan committed
119 120
	}
};
121

jehan's avatar
jehan committed
122
int finalize_stream_connection (belle_sip_fd_t fd, struct sockaddr *addr, socklen_t* slen) {
jehan's avatar
jehan committed
123 124
	int err, errnum;
	socklen_t optlen=sizeof(errnum);
jehan's avatar
jehan committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
	err=getsockopt(fd,SOL_SOCKET,SO_ERROR,&errnum,&optlen);
	if (err!=0){
		belle_sip_error("Failed to retrieve connection status for fd [%i]: cause [%s]",fd,belle_sip_get_socket_error_string());
		return -1;
	}else{
		if (errnum==0){
			/*obtain bind address for client*/
			err=getsockname(fd,addr,slen);
			if (err<0){
				belle_sip_error("Failed to retrieve sockname  for fd [%i]: cause [%s]",fd,belle_sip_get_socket_error_string());
				return -1;
			}
			return 0;
		}else{
139
			belle_sip_error("Connection failed  for fd [%i]: cause [%s]",fd,belle_sip_get_socket_error_string_from_code(errnum));
jehan's avatar
jehan committed
140 141 142 143 144 145 146
			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);
jehan's avatar
jehan committed
147
	belle_sip_fd_t fd=belle_sip_source_get_fd((belle_sip_source_t*)obj);
Simon Morlat's avatar
Simon Morlat committed
148 149 150

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

jehan's avatar
jehan committed
153 154 155 156 157
		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);
			channel_process_queue(obj);
			return BELLE_SIP_STOP;
jehan's avatar
jehan committed
158
		}
jehan's avatar
jehan committed
159
		belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR);
jehan's avatar
jehan committed
160 161 162
		belle_sip_channel_set_ready(obj,(struct sockaddr*)&ss,addrlen);
		return BELLE_SIP_CONTINUE;

jehan's avatar
jehan committed
163 164 165
	} else if ( obj->state == BELLE_SIP_CHANNEL_READY) {
		belle_sip_channel_process_data(obj,revents);
	} else {
jehan's avatar
jehan committed
166
		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
167
	}
jehan's avatar
jehan committed
168
	return BELLE_SIP_CONTINUE;
jehan's avatar
jehan committed
169
}
jehan's avatar
jehan committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

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;
}













jehan's avatar
jehan committed
191 192