listeningpoint.c 7.8 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 35 36

typedef struct belle_sip_channel_vptr{
	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);
37
	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);
38 39
}belle_sip_channel_vptr_t;

40
static void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_listening_point_t *lp){
41
	belle_sip_object_init_type(obj,belle_sip_channel_t);
42
	obj->lp=lp;
43 44 45 46 47 48 49 50 51 52 53
	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);
}

54 55 56 57 58
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);
}


59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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){
		belle_sip_error("Could not send UDP packet: %s",strerror(errno));
81
		return -errno;
82 83 84 85 86 87 88 89 90 91 92
	}
	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));
93
		return -errno;
94 95 96 97
	}
	return err;
}

98 99 100
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);
101 102 103 104
}

static belle_sip_channel_vptr_t udp_channel_vptr={
	udp_channel_send,
105 106
	udp_channel_recv,
	udp_channel_create_source
107 108 109 110 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
};

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

137
belle_sip_udp_channel_t *belle_sip_udp_channel_new(belle_sip_listening_point_t *lp, int sock, const struct addrinfo *dest){
138
	belle_sip_udp_channel_t *obj=belle_sip_object_new_with_vptr(belle_sip_udp_channel_t,&udp_channel_vptr,udp_channel_uninit);
139
	belle_sip_channel_init((belle_sip_channel_t*)obj,lp);
140 141 142
	obj->sock=sock;
	memcpy(obj->base.peer.ai_addr,dest->ai_addr,dest->ai_addrlen);
	obj->base.peer.ai_addrlen=dest->ai_addrlen;
143 144 145 146 147 148 149 150 151 152 153 154 155
	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;
156
	int is_reliable;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
};

typedef struct belle_sip_listening_point_vptr{
	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){
	belle_sip_object_init_type(lp,belle_sip_listening_point_t);
	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);
}

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

188 189 190 191 192 193 194 195 196 197 198 199

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

200 201 202 203 204 205 206
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;
207
	int sock;
208 209 210 211 212
};


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;
213
	belle_sip_udp_channel_t * chan=belle_sip_udp_channel_new (obj,lp->sock,dest);
214
	return BELLE_SIP_CHANNEL(chan);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

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

static belle_sip_listening_point_vptr_t udp_listening_point_vptr={
	udp_listening_point_find_output_channel
};


belle_sip_listening_point_t * belle_sip_udp_listening_point_new(belle_sip_stack_t *s, const char *ipaddress, int port){
	belle_sip_udp_listening_point_t *lp=belle_sip_object_new_with_vptr(belle_sip_udp_listening_point_t,&udp_listening_point_vptr,belle_sip_udp_listening_point_uninit);
	belle_sip_listening_point_init((belle_sip_listening_point_t*)lp,s,"UDP",ipaddress,port);
230 231 232 233
	lp->sock=create_udp_socket(ipaddress,port);
	if (lp->sock==-1){
		belle_sip_object_unref(s);
		return NULL;
234
	}
235
	lp->base.is_reliable=FALSE;
236 237 238 239
	return BELLE_SIP_LISTENING_POINT(lp);
}