listeningpoint.c 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	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
19
#include "listeningpoint_internal.h"
20 21 22



jehan's avatar
jehan committed
23
void belle_sip_listening_point_init(belle_sip_listening_point_t *lp, belle_sip_stack_t *s, const char *address, int port){
24 25 26 27 28 29
	lp->port=port;
	lp->addr=belle_sip_strdup(address);
	lp->stack=s;
}

static void belle_sip_listening_point_uninit(belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
30 31 32 33
	int existing_channels;
	if ((existing_channels=belle_sip_list_size(lp->channels)) > 0) {
		belle_sip_warning("Listening point destroying [%i] channels",existing_channels);
	}
34
	belle_sip_list_free_with_data(lp->channels,(void (*)(void*))belle_sip_object_unref);
35 36 37
	belle_sip_free(lp->addr);
}

38

jehan's avatar
jehan committed
39
void belle_sip_listening_point_add_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
jehan's avatar
jehan committed
40
	lp->channels=belle_sip_list_append(lp->channels,chan);/*channel is already owned*/
41 42 43 44 45
}

belle_sip_channel_t *belle_sip_listening_point_create_channel(belle_sip_listening_point_t *obj, const char *dest, int port){
	belle_sip_channel_t *chan=BELLE_SIP_OBJECT_VPTR(obj,belle_sip_listening_point_t)->create_channel(obj,dest,port);
	if (chan){
Simon Morlat's avatar
wip  
Simon Morlat committed
46
		chan->lp=obj;
47 48 49
		belle_sip_listening_point_add_channel(obj,chan);
	}
	return chan;
50 51
}

Simon Morlat's avatar
wip  
Simon Morlat committed
52 53

void belle_sip_listening_point_remove_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
54
	lp->channels=belle_sip_list_remove(lp->channels,chan);
55
	belle_sip_object_unref(chan);
56
}
Simon Morlat's avatar
wip  
Simon Morlat committed
57

58

59
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_listening_point_t);
60 61
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_listening_point_t)={
	{ 
Simon Morlat's avatar
Simon Morlat committed
62
		BELLE_SIP_VPTR_INIT(belle_sip_listening_point_t, belle_sip_object_t,FALSE),
63 64 65 66 67 68 69
		(belle_sip_object_destroy_t)belle_sip_listening_point_uninit,
		NULL,
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
70

71 72 73 74 75 76 77 78 79
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *lp){
	return lp->addr;
}

int belle_sip_listening_point_get_port(const belle_sip_listening_point_t *lp){
	return lp->port;
}

const char *belle_sip_listening_point_get_transport(const belle_sip_listening_point_t *lp){
80
	return BELLE_SIP_OBJECT_VPTR(lp,belle_sip_listening_point_t)->transport;
81 82
}

83 84 85 86 87 88 89 90

int belle_sip_listening_point_get_well_known_port(const char *transport){
	if (strcasecmp(transport,"UDP")==0 || strcasecmp(transport,"TCP")==0 ) return 5060;
	if (strcasecmp(transport,"DTLS")==0 || strcasecmp(transport,"TLS")==0 ) return 5061;
	belle_sip_error("No well known port for transport %s", transport);
	return -1;
}

jehan's avatar
jehan committed
91
belle_sip_channel_t *_belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp,const char *peer_name, int peer_port, const struct addrinfo *addr){
92
	belle_sip_list_t *elem;
93
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
94 95 96 97 98 99 100 101 102 103 104
	
	for(elem=lp->channels;elem!=NULL;elem=elem->next){
		chan=(belle_sip_channel_t*)elem->data;
		if (belle_sip_channel_matches(chan,peer_name,peer_port,addr)){
			return chan;
		}
	}
	return NULL;
}

belle_sip_channel_t *belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp,const char *peer_name, int peer_port){
105 106 107
	struct addrinfo *res=NULL;
	struct addrinfo hints={0};
	char portstr[20];
Simon Morlat's avatar
Simon Morlat committed
108
	belle_sip_channel_t *chan;
109 110 111 112

	hints.ai_flags=AI_NUMERICHOST|AI_NUMERICSERV;
	snprintf(portstr,sizeof(portstr),"%i",peer_port);
	getaddrinfo(peer_name,portstr,&hints,&res);
Simon Morlat's avatar
Simon Morlat committed
113
	chan=_belle_sip_listening_point_get_channel(lp,peer_name,peer_port,res);
114
	if (res) freeaddrinfo(res);
Simon Morlat's avatar
Simon Morlat committed
115
	return chan;
116 117
}

Simon Morlat's avatar
Simon Morlat committed
118

119 120