listeningpoint.c 7.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
	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"

/*
 Listening points: base, udp
*/

struct belle_sip_listening_point{
	belle_sip_object_t base;
	belle_sip_stack_t *stack;
28
	belle_sip_list_t *channels;
29 30 31 32
	char *addr;
	int port;
};

33
static void belle_sip_listening_point_init(belle_sip_listening_point_t *lp, belle_sip_stack_t *s, const char *address, int port){
34 35 36 37 38 39
	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){
40
	belle_sip_list_free_with_data(lp->channels,(void (*)(void*))belle_sip_object_unref);
41 42 43
	belle_sip_free(lp->addr);
}

44

45
static void belle_sip_listening_point_add_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
46 47 48 49 50 51 52 53 54
	lp->channels=belle_sip_list_append(lp->channels,belle_sip_object_ref(chan));
}

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){
		belle_sip_listening_point_add_channel(obj,chan);
	}
	return chan;
55 56
}

57
#if 0
58 59
static void belle_sip_listening_point_remove_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
	lp->channels=belle_sip_list_remove(lp->channels,chan);
60
	belle_sip_object_unref(chan);
61 62 63
}
#endif

64
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_listening_point_t);
65 66
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_listening_point_t)={
	{ 
Simon Morlat's avatar
Simon Morlat committed
67
		BELLE_SIP_VPTR_INIT(belle_sip_listening_point_t, belle_sip_object_t,FALSE),
68 69 70 71 72 73 74
		(belle_sip_object_destroy_t)belle_sip_listening_point_uninit,
		NULL,
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
75

76 77 78 79 80 81 82 83 84
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){
85
	return BELLE_SIP_OBJECT_VPTR(lp,belle_sip_listening_point_t)->transport;
86 87
}

88 89 90 91 92 93 94 95

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

Simon Morlat's avatar
Simon Morlat committed
96
static 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){
97
	belle_sip_list_t *elem;
98
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
99 100 101 102 103 104 105 106 107 108 109
	
	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){
110 111 112
	struct addrinfo *res=NULL;
	struct addrinfo hints={0};
	char portstr[20];
Simon Morlat's avatar
Simon Morlat committed
113
	belle_sip_channel_t *chan;
114 115 116 117

	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
118
	chan=_belle_sip_listening_point_get_channel(lp,peer_name,peer_port,res);
119
	if (res) freeaddrinfo(res);
Simon Morlat's avatar
Simon Morlat committed
120
	return chan;
121 122 123 124
}

struct belle_sip_udp_listening_point{
	belle_sip_listening_point_t base;
125
	int sock;
Simon Morlat's avatar
Simon Morlat committed
126
	belle_sip_source_t *source;
127 128 129 130
};


static void belle_sip_udp_listening_point_uninit(belle_sip_udp_listening_point_t *lp){
131
	if (lp->sock!=-1) close(lp->sock);
Simon Morlat's avatar
Simon Morlat committed
132
	if (lp->source) belle_sip_main_loop_remove_source(lp->base.stack->ml,lp->source);
133 134 135
}

static belle_sip_channel_t *udp_create_channel(belle_sip_listening_point_t *lp, const char *dest_ip, int port){
136 137
	belle_sip_channel_t *chan=belle_sip_channel_new_udp(lp->stack,((belle_sip_udp_listening_point_t*)lp)->sock,
	                                                  lp->addr,lp->port,dest_ip,port);
138
	return chan;
139 140
}

141
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_udp_listening_point_t);
142 143 144
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_udp_listening_point_t)={
	{
		{ 
Simon Morlat's avatar
Simon Morlat committed
145
			BELLE_SIP_VPTR_INIT(belle_sip_udp_listening_point_t, belle_sip_listening_point_t,FALSE),
146 147 148 149 150 151 152 153
			(belle_sip_object_destroy_t)belle_sip_udp_listening_point_uninit,
			NULL,
			NULL
		},
		"UDP",
		udp_create_channel
	}
};
154 155


156 157 158 159 160 161 162 163
static int create_udp_socket(const char *addr, int port){
	struct addrinfo hints={0};
	struct addrinfo *res=NULL;
	int err;
	int sock;
	char portnum[10];
	
	snprintf(portnum,sizeof(portnum),"%i",port);
Simon Morlat's avatar
Simon Morlat committed
164 165 166 167
	hints.ai_family=AF_UNSPEC;
	hints.ai_socktype=SOCK_DGRAM;
	hints.ai_protocol=IPPROTO_UDP;
	hints.ai_flags=AI_NUMERICSERV;
168 169
	err=getaddrinfo(addr,portnum,&hints,&res);
	if (err!=0){
Simon Morlat's avatar
Simon Morlat committed
170 171 172 173 174 175 176
		belle_sip_error("getaddrinfo() failed for %s port %i: %s",addr,port,gai_strerror(err));
		return -1;
	}
	sock=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
	if (sock==-1){
		belle_sip_error("Cannot create UDP socket: %s",strerror(errno));
		freeaddrinfo(res);
177 178 179 180 181 182
		return -1;
	}
	err=bind(sock,res->ai_addr,res->ai_addrlen);
	if (err==-1){
		belle_sip_error("udp bind() failed for %s port %i: %s",addr,port,strerror(errno));
		close(sock);
Simon Morlat's avatar
Simon Morlat committed
183
		freeaddrinfo(res);
184 185
		return -1;
	}
Simon Morlat's avatar
Simon Morlat committed
186
	freeaddrinfo(res);
187 188 189
	return sock;
}

Simon Morlat's avatar
Simon Morlat committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/*peek data from the master socket to see where it comes from, and dispatch to matching channel.
 * If the channel does not exist, create it */
static int on_udp_data(belle_sip_udp_listening_point_t *lp, unsigned int events){
	int err;
	unsigned char buf[1];
	struct sockaddr_storage addr;
	socklen_t addrlen=sizeof(addr);

	if (events & BELLE_SIP_EVENT_READ){
		belle_sip_message("udp_listening_point: data to read.");
		err=recvfrom(lp->sock,buf,sizeof(buf),MSG_PEEK,(struct sockaddr*)&addr,&addrlen);
		if (err==-1){
			belle_sip_error("udp_listening_point: recvfrom() failed: %s",strerror(errno));
		}else{
			belle_sip_channel_t *chan;
			struct addrinfo ai={0};
			ai.ai_addr=(struct sockaddr*)&addr;
			ai.ai_addrlen=addrlen;
			chan=_belle_sip_listening_point_get_channel((belle_sip_listening_point_t*)lp,NULL,0,&ai);
			if (chan==NULL){
210
				chan=belle_sip_channel_new_udp_with_addr(lp->base.stack,lp->sock,lp->base.addr,lp->base.port,&ai);
Simon Morlat's avatar
Simon Morlat committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
				if (chan!=NULL){
					belle_sip_message("udp_listening_point: new channel created to %s:%i",chan->peer_name,chan->peer_port);
					belle_sip_listening_point_add_channel((belle_sip_listening_point_t*)lp,chan);
				}
			}
			if (chan){
				/*notify the channel*/
				belle_sip_message("Notifying channel.");
				belle_sip_channel_process_data(chan,events);
			}
		}
	}
	return BELLE_SIP_CONTINUE;
}

226
belle_sip_listening_point_t * belle_sip_udp_listening_point_new(belle_sip_stack_t *s, const char *ipaddress, int port){
Simon Morlat's avatar
Simon Morlat committed
227
	belle_sip_udp_listening_point_t *lp=belle_sip_object_new(belle_sip_udp_listening_point_t);
228 229 230 231 232 233
	belle_sip_listening_point_init((belle_sip_listening_point_t*)lp,s,ipaddress,port);
	lp->sock=create_udp_socket(ipaddress,port);
	if (lp->sock==-1){
		belle_sip_object_unref(lp);
		return NULL;
	}
Simon Morlat's avatar
Simon Morlat committed
234 235
	lp->source=belle_sip_fd_source_new((belle_sip_source_func_t)on_udp_data,lp,lp->sock,BELLE_SIP_EVENT_READ,-1);
	belle_sip_main_loop_add_source(s->ml,lp->source);
236 237 238 239
	return BELLE_SIP_LISTENING_POINT(lp);
}