listeningpoint.c 8.23 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 28 29 30 31
/*
	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"

#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#else

#endif

/*
 Channels: udp
*/

Simon Morlat's avatar
Simon Morlat committed
32

33 34

typedef struct belle_sip_channel_vptr{
Simon Morlat's avatar
Simon Morlat committed
35
	belle_sip_object_vptr_t base;
36 37
	int (*channel_send)(belle_sip_channel_t *obj, const void *buf, size_t buflen);
	int (*channel_recv)(belle_sip_channel_t *obj, void *buf, size_t buflen);
38
	belle_sip_source_t *(*create_source)(belle_sip_channel_t *obj, unsigned int events, unsigned int timeout, belle_sip_source_func_t callback, void *data);
39 40
}belle_sip_channel_vptr_t;

Simon Morlat's avatar
Simon Morlat committed
41 42 43
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_channel_vptr_t,belle_sip_channel_t,belle_sip_object_t,NULL,NULL)
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END

44 45
static void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_listening_point_t *lp){
	obj->lp=lp;
46 47 48 49 50 51 52 53 54 55 56
	obj->peer.ai_addr=(struct sockaddr*)&obj->peer_addr;
}

int belle_sip_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_vptr_t)->channel_send(obj,buf,buflen);
}

int belle_sip_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_vptr_t)->channel_recv(obj,buf,buflen);
}

57 58 59 60 61
belle_sip_source_t *belle_sip_channel_create_source(belle_sip_channel_t *obj, unsigned int events, int timeout, belle_sip_source_func_t callback, void *data){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_vptr_t)->create_source(obj,events,timeout,callback,data);
}


62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj){
	return &obj->peer;
}

struct belle_sip_udp_channel{
	belle_sip_channel_t base;
	int sock;
};

typedef struct belle_sip_udp_channel belle_sip_udp_channel_t;

static void udp_channel_uninit(belle_sip_udp_channel_t *obj){
	if (obj->sock!=-1)
		close(obj->sock);
}

static int udp_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
	belle_sip_udp_channel_t *chan=(belle_sip_udp_channel_t *)obj;
	int err;
	err=sendto(chan->sock,buf,buflen,0,obj->peer.ai_addr,obj->peer.ai_addrlen);
	if (err==-1){
Simon Morlat's avatar
Simon Morlat committed
83
		belle_sip_fatal("Could not send UDP packet: %s",strerror(errno));
84
		return -errno;
85 86 87 88 89 90 91 92 93 94 95
	}
	return err;
}

static int udp_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen){
	belle_sip_udp_channel_t *chan=(belle_sip_udp_channel_t *)obj;
	int err;
	obj->peer.ai_addrlen=sizeof(obj->peer_addr);
	err=recvfrom(chan->sock,buf,buflen,MSG_DONTWAIT,obj->peer.ai_addr,&obj->peer.ai_addrlen);
	if (err==-1 && errno!=EWOULDBLOCK){
		belle_sip_error("Could not receive UDP packet: %s",strerror(errno));
96
		return -errno;
97 98 99 100
	}
	return err;
}

101 102 103
static belle_sip_source_t *udp_channel_create_source(belle_sip_channel_t *obj, unsigned int events, unsigned int timeout, belle_sip_source_func_t callback, void *data){
	belle_sip_udp_channel_t *chan=(belle_sip_udp_channel_t *)obj;
	return belle_sip_fd_source_new(callback,data,chan->sock,events,timeout);
104 105
}

Simon Morlat's avatar
Simon Morlat committed
106
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_channel_vptr_t,belle_sip_udp_channel_t,belle_sip_channel_t,udp_channel_uninit,NULL)
107
	udp_channel_send,
108 109
	udp_channel_recv,
	udp_channel_create_source
Simon Morlat's avatar
Simon Morlat committed
110
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

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];
	
	sock=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
	if (sock==-1){
		belle_sip_error("Cannot create UDP socket: %s",strerror(errno));
		return -1;
	}
	snprintf(portnum,sizeof(portnum),"%i",port);
	err=getaddrinfo(addr,portnum,&hints,&res);
	if (err!=0){
		belle_sip_error("getaddrinfo() failed for %s port %i: %s",addr,port,strerror(errno));
		close(sock);
		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);
		return -1;
	}
	return sock;
}

140
belle_sip_udp_channel_t *belle_sip_udp_channel_new(belle_sip_listening_point_t *lp, int sock, const struct addrinfo *dest){
Simon Morlat's avatar
Simon Morlat committed
141
	belle_sip_udp_channel_t *obj=belle_sip_object_new(belle_sip_udp_channel_t);
142
	belle_sip_channel_init((belle_sip_channel_t*)obj,lp);
143 144 145
	obj->sock=sock;
	memcpy(obj->base.peer.ai_addr,dest->ai_addr,dest->ai_addrlen);
	obj->base.peer.ai_addrlen=dest->ai_addrlen;
146 147 148 149 150 151 152 153 154 155 156 157 158
	return obj;
}

/*
 Listening points: base, udp
*/

struct belle_sip_listening_point{
	belle_sip_object_t base;
	belle_sip_stack_t *stack;
	char *transport;
	char *addr;
	int port;
159
	int is_reliable;
160 161 162
};

typedef struct belle_sip_listening_point_vptr{
Simon Morlat's avatar
Simon Morlat committed
163
	belle_sip_object_vptr_t base;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	belle_sip_channel_t * (*find_output_channel)(belle_sip_listening_point_t *lp,const struct addrinfo *dest);
} belle_sip_listening_point_vptr_t;

static void belle_sip_listening_point_init(belle_sip_listening_point_t *lp, belle_sip_stack_t *s, const char *transport, const char *address, int port){
	lp->transport=belle_sip_strdup(transport);
	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){
	belle_sip_free(lp->addr);
	belle_sip_free(lp->transport);
}

Simon Morlat's avatar
Simon Morlat committed
179 180 181
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_listening_point_vptr_t,belle_sip_listening_point_t,belle_sip_object_t,belle_sip_listening_point_uninit,NULL)
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END

182 183 184 185 186 187 188 189 190 191 192 193
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){
	return lp->transport;
}

194 195 196 197 198 199 200 201 202 203 204
int belle_sip_listening_point_is_reliable(const belle_sip_listening_point_t *lp){
	return lp->is_reliable;
}

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

205 206 207 208 209 210 211
belle_sip_channel_t *belle_sip_listening_point_find_output_channel (belle_sip_listening_point_t *lp,const struct addrinfo *dest){
	return ((belle_sip_listening_point_vptr_t*)lp->base.vptr)->find_output_channel(lp,dest);
}

struct belle_sip_udp_listening_point{
	belle_sip_listening_point_t base;
	belle_sip_udp_channel_t *channel;
212
	int sock;
213 214 215 216 217
};


static belle_sip_channel_t *udp_listening_point_find_output_channel(belle_sip_listening_point_t* obj,const struct addrinfo *dest){
	belle_sip_udp_listening_point_t *lp=(belle_sip_udp_listening_point_t*)obj;
218
	belle_sip_udp_channel_t * chan=belle_sip_udp_channel_new (obj,lp->sock,dest);
219
	return BELLE_SIP_CHANNEL(chan);
220 221 222 223 224 225 226
}

static void belle_sip_udp_listening_point_uninit(belle_sip_udp_listening_point_t *lp){
	belle_sip_object_unref(lp->channel);
	belle_sip_listening_point_uninit((belle_sip_listening_point_t*)lp);
}

Simon Morlat's avatar
Simon Morlat committed
227 228 229 230 231

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_listening_point_vptr_t,belle_sip_udp_listening_point_t,belle_sip_listening_point_t,
	belle_sip_udp_listening_point_uninit,NULL)
udp_listening_point_find_output_channel
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
232 233 234


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
235
	belle_sip_udp_listening_point_t *lp=belle_sip_object_new(belle_sip_udp_listening_point_t);
236
	belle_sip_listening_point_init((belle_sip_listening_point_t*)lp,s,"UDP",ipaddress,port);
237 238 239 240
	lp->sock=create_udp_socket(ipaddress,port);
	if (lp->sock==-1){
		belle_sip_object_unref(s);
		return NULL;
241
	}
242
	lp->base.is_reliable=FALSE;
243 244 245 246
	return BELLE_SIP_LISTENING_POINT(lp);
}