provider.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	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"
jehan's avatar
jehan committed
20
#include "listeningpoint_internal.h"
21 22 23 24


static void belle_sip_provider_uninit(belle_sip_provider_t *p){
	belle_sip_list_free(p->listeners);
Simon Morlat's avatar
Simon Morlat committed
25
	belle_sip_list_free_with_data(p->lps,belle_sip_object_unref);
26 27
}

28 29 30
static void channel_state_changed(belle_sip_channel_listener_t *obj, belle_sip_channel_t *chan, belle_sip_channel_state_t state){
}

Simon Morlat's avatar
Simon Morlat committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static void belle_sip_provider_dispatch_message(belle_sip_provider_t *prov, belle_sip_message_t *msg){
	/*should find existing transaction*/

	if (belle_sip_message_is_request(msg)){
		belle_sip_request_event_t event;
		event.source=prov;
		event.server_transaction=NULL;
		event.request=(belle_sip_request_t*)msg;
		event.dialog=NULL;
		BELLE_SIP_PROVIDER_INVOKE_LISTENERS(prov,process_request_event,&event);
	}else{
		belle_sip_response_event_t event;
		event.source=prov;
		event.client_transaction=NULL;
		event.dialog=NULL;
		event.response=(belle_sip_response_t*)msg;
		BELLE_SIP_PROVIDER_INVOKE_LISTENERS(prov,process_response_event,&event);
	}
}

jehan's avatar
jehan committed
51

Simon Morlat's avatar
Simon Morlat committed
52

53 54 55
static void fix_outgoing_via(belle_sip_provider_t *p, belle_sip_channel_t *chan, belle_sip_message_t *msg){
	belle_sip_header_via_t *via=BELLE_SIP_HEADER_VIA(belle_sip_message_get_header(msg,"via"));
	belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(via),"rport",NULL);
Simon Morlat's avatar
Simon Morlat committed
56
	char token[7]="fixme";
57 58 59 60 61 62 63 64 65 66
	if (belle_sip_header_via_get_host(via)==NULL){
		const char *local_ip;
		int local_port;
		local_ip=belle_sip_channel_get_local_address(chan,&local_port);
		belle_sip_header_via_set_host(via,local_ip);
		belle_sip_header_via_set_port(via,local_port);
		belle_sip_header_via_set_protocol(via,"SIP/2.0");
		belle_sip_header_via_set_transport(via,belle_sip_channel_get_transport_name(chan));
	}
	if (belle_sip_header_via_get_branch(via)==NULL){
Simon Morlat's avatar
Simon Morlat committed
67 68
		/*FIXME: should not be set random here: but rather a hash of message invariants*/
		char *branchid=belle_sip_strdup_printf(BELLE_SIP_BRANCH_MAGIC_COOKIE ".%s",token);
69 70 71 72
		belle_sip_header_via_set_branch(via,branchid);
		belle_sip_free(branchid);
	}
}
jehan's avatar
jehan committed
73
/*
Simon Morlat's avatar
Simon Morlat committed
74 75 76 77 78 79 80
static void belle_sip_provider_read_message(belle_sip_provider_t *prov, belle_sip_channel_t *chan){
	char buffer[belle_sip_network_buffer_size];
	int err;
	err=belle_sip_channel_recv(chan,buffer,sizeof(buffer));
	if (err>0){
		belle_sip_message_t *msg;
		buffer[err]='\0';
81
		belle_sip_message("provider %p read message from %s:%i\n%s",prov,chan->peer_name,chan->peer_port,buffer);
Simon Morlat's avatar
Simon Morlat committed
82 83
		msg=belle_sip_message_parse(buffer);
		if (msg){
84
			if (belle_sip_message_is_request(msg)) fix_incoming_via(BELLE_SIP_REQUEST(msg),chan->peer);
Simon Morlat's avatar
Simon Morlat committed
85 86 87 88 89 90
			belle_sip_provider_dispatch_message(prov,msg);
		}else{
			belle_sip_error("Could not parse this message.");
		}
	}
}
jehan's avatar
jehan committed
91
*/
Simon Morlat's avatar
Simon Morlat committed
92 93
static int channel_on_event(belle_sip_channel_listener_t *obj, belle_sip_channel_t *chan, unsigned int revents){
	if (revents & BELLE_SIP_EVENT_READ){
jehan's avatar
jehan committed
94
		belle_sip_provider_dispatch_message(BELLE_SIP_PROVIDER(obj),belle_sip_channel_pick_message(chan));
Simon Morlat's avatar
Simon Morlat committed
95 96 97 98
	}
	return 0;
}

99
static void channel_on_sending(belle_sip_channel_listener_t *obj, belle_sip_channel_t *chan, belle_sip_message_t *msg){
jehan's avatar
jehan committed
100
	belle_sip_header_contact_t* contact = (belle_sip_header_contact_t*)belle_sip_message_get_header(msg,"Contact");
jehan's avatar
jehan committed
101
	belle_sip_header_content_length_t* content_lenght = (belle_sip_header_content_length_t*)belle_sip_message_get_header(msg,"Content-Length");
jehan's avatar
jehan committed
102 103
	belle_sip_uri_t* contact_uri;
	/*probably better to be in channel*/
104
	fix_outgoing_via((belle_sip_provider_t*)obj,chan,msg);
Simon Morlat's avatar
Simon Morlat committed
105 106

	/* fix the contact if empty*/
jehan's avatar
jehan committed
107 108 109 110 111 112 113 114 115 116 117 118 119
	if (!(contact_uri =belle_sip_header_address_get_uri((belle_sip_header_address_t*)contact))) {
		contact_uri = belle_sip_uri_new();
		belle_sip_header_address_set_uri((belle_sip_header_address_t*)contact,contact_uri);
	}
	if (!belle_sip_uri_get_host(contact_uri)) {
		belle_sip_uri_set_host(contact_uri,chan->local_ip);
	}
	if (belle_sip_uri_get_transport_param(contact_uri) == NULL && strcasecmp("udp",belle_sip_channel_get_transport_name(chan))!=0) {
		belle_sip_uri_set_transport_param(contact_uri,belle_sip_channel_get_transport_name_lower_case(chan));
	}
	if (belle_sip_uri_get_port(contact_uri) == 0 && chan->local_port!=5060) {
		belle_sip_uri_set_port(contact_uri,chan->local_port);
	}
jehan's avatar
jehan committed
120 121 122 123
	if (!content_lenght && strcasecmp("udp",belle_sip_channel_get_transport_name(chan))!=0) {
		content_lenght = belle_sip_header_content_length_create(0);
		belle_sip_message_add_header(msg,(belle_sip_header_t*)content_lenght);
	}
124 125
}

126
BELLE_SIP_IMPLEMENT_INTERFACE_BEGIN(belle_sip_provider_t,belle_sip_channel_listener_t)
Simon Morlat's avatar
Simon Morlat committed
127
	channel_state_changed,
128 129
	channel_on_event,
	channel_on_sending
130 131 132 133
BELLE_SIP_IMPLEMENT_INTERFACE_END

BELLE_SIP_DECLARE_IMPLEMENTED_INTERFACES_1(belle_sip_provider_t,belle_sip_channel_listener_t);
	
Simon Morlat's avatar
Simon Morlat committed
134
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_provider_t,belle_sip_object_t,belle_sip_provider_uninit,NULL,NULL,FALSE);
Simon Morlat's avatar
Simon Morlat committed
135

136
belle_sip_provider_t *belle_sip_provider_new(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
Simon Morlat's avatar
Simon Morlat committed
137
	belle_sip_provider_t *p=belle_sip_object_new(belle_sip_provider_t);
138
	p->stack=s;
139 140 141 142 143
	belle_sip_provider_add_listening_point(p,lp);
	return p;
}

int belle_sip_provider_add_listening_point(belle_sip_provider_t *p, belle_sip_listening_point_t *lp){
Simon Morlat's avatar
Simon Morlat committed
144
	p->lps=belle_sip_list_append(p->lps,belle_sip_object_ref(lp));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	return 0;
}

belle_sip_listening_point_t *belle_sip_provider_get_listening_point(belle_sip_provider_t *p, const char *transport){
	belle_sip_list_t *l;
	for(l=p->lps;l!=NULL;l=l->next){
		belle_sip_listening_point_t *lp=(belle_sip_listening_point_t*)l->data;
		if (strcasecmp(belle_sip_listening_point_get_transport(lp),transport)==0)
			return lp;
	}
	return NULL;
}

const belle_sip_list_t *belle_sip_provider_get_listening_points(belle_sip_provider_t *p){
	return p->lps;
}

162 163
void belle_sip_provider_add_sip_listener(belle_sip_provider_t *p, belle_sip_listener_t *l){
	p->listeners=belle_sip_list_append(p->listeners,l);
164 165
}

166 167
void belle_sip_provider_remove_sip_listener(belle_sip_provider_t *p, belle_sip_listener_t *l){
	p->listeners=belle_sip_list_remove(p->listeners,l);
168 169
}

170 171 172 173 174 175 176 177
belle_sip_header_call_id_t * belle_sip_provider_create_call_id(belle_sip_provider_t *prov){
	belle_sip_header_call_id_t *cid=belle_sip_header_call_id_new();
	char tmp[32];
	snprintf(tmp,sizeof(tmp),"%u",belle_sip_random());
	belle_sip_header_call_id_set_call_id(cid,tmp);
	return cid;
}

Simon Morlat's avatar
Simon Morlat committed
178 179 180 181 182
belle_sip_client_transaction_t *belle_sip_provider_create_client_transaction(belle_sip_provider_t *prov, belle_sip_request_t *req){
	if (strcmp(belle_sip_request_get_method(req),"INVITE")==0)
		return (belle_sip_client_transaction_t*)belle_sip_ict_new(prov,req);
	else 
		return (belle_sip_client_transaction_t*)belle_sip_nict_new(prov,req);
183 184
}

Simon Morlat's avatar
Simon Morlat committed
185 186 187 188 189
belle_sip_server_transaction_t *belle_sip_provider_create_server_transaction(belle_sip_provider_t *prov, belle_sip_request_t *req){
	if (strcmp(belle_sip_request_get_method(req),"INVITE")==0)
		return (belle_sip_server_transaction_t*)belle_sip_ist_new(prov,req);
	else 
		return (belle_sip_server_transaction_t*)belle_sip_nist_new(prov,req);
190 191 192 193 194 195
}

belle_sip_stack_t *belle_sip_provider_get_sip_stack(belle_sip_provider_t *p){
	return p->stack;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
belle_sip_channel_t * belle_sip_provider_get_channel(belle_sip_provider_t *p, const char *name, int port, const char *transport){
	belle_sip_list_t *l;
	belle_sip_listening_point_t *candidate=NULL,*lp;
	belle_sip_channel_t *chan;
	for(l=p->lps;l!=NULL;l=l->next){
		lp=(belle_sip_listening_point_t*)l->data;
		if (strcasecmp(belle_sip_listening_point_get_transport(lp),transport)==0){
			chan=belle_sip_listening_point_get_channel(lp,name,port);
			if (chan) return chan;
			candidate=lp;
		}
	}
	if (candidate){
		chan=belle_sip_listening_point_create_channel(candidate,name,port);
		if (chan==NULL) belle_sip_error("Could not create channel to %s:%s:%i",transport,name,port);
Simon Morlat's avatar
Simon Morlat committed
211
		else belle_sip_channel_add_listener(chan,BELLE_SIP_CHANNEL_LISTENER(p));
212 213 214
		return chan;
	}
	belle_sip_error("No listening point matching for transport %s",transport);
215
	return NULL;
Simon Morlat's avatar
wip  
Simon Morlat committed
216 217
}

218
void belle_sip_provider_send_request(belle_sip_provider_t *p, belle_sip_request_t *req){
219 220 221 222
	belle_sip_hop_t hop={0};
	belle_sip_channel_t *chan;
	belle_sip_stack_get_next_hop(p->stack,req,&hop);
	chan=belle_sip_provider_get_channel(p,hop.host, hop.port, hop.transport);
223 224 225
	if (chan) {
		belle_sip_channel_queue_message(chan,BELLE_SIP_MESSAGE(req));
	}
226 227
}

Simon Morlat's avatar
wip  
Simon Morlat committed
228
void belle_sip_provider_send_response(belle_sip_provider_t *p, belle_sip_response_t *resp){
229 230 231 232 233
	belle_sip_hop_t hop;
	belle_sip_channel_t *chan;
	belle_sip_response_get_return_hop(resp,&hop);
	chan=belle_sip_provider_get_channel(p,hop.host, hop.port, hop.transport);
	if (chan) belle_sip_channel_queue_message(chan,BELLE_SIP_MESSAGE(resp));
234 235
}

jehan's avatar
jehan committed
236 237 238
belle_sip_response_t* belle_sip_response_event_get_response(const belle_sip_response_event_t* event) {
	return event->response;
}
239 240 241
/*private provider API*/

void belle_sip_provider_set_transaction_terminated(belle_sip_provider_t *p, belle_sip_transaction_t *t){
Simon Morlat's avatar
Simon Morlat committed
242 243 244 245 246 247 248
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(t,belle_sip_client_transaction_t)){
		belle_sip_provider_remove_client_transaction(p,(belle_sip_client_transaction_t*)t);
	}
}

void belle_sip_provider_add_client_transaction(belle_sip_provider_t *prov, belle_sip_client_transaction_t *t){
	prov->client_transactions=belle_sip_list_prepend(prov->client_transactions,belle_sip_object_ref(t));
Simon Morlat's avatar
wip  
Simon Morlat committed
249
}
250

Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255 256 257 258
belle_sip_client_transaction_t * belle_sip_provider_find_matching_client_transaction(belle_sip_provider_t *prov, 
                                                                                   belle_sip_response_t *resp){
	return NULL;
}

void belle_sip_provider_remove_client_transaction(belle_sip_provider_t *prov, belle_sip_client_transaction_t *t){
	prov->client_transactions=belle_sip_list_remove(prov->client_transactions,t);
}