listeningpoint.c 6.72 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/*
	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
*/

struct belle_sip_channel{
	belle_sip_object_t base;
	struct addrinfo peer;
	struct sockaddr_storage peer_addr;
};

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

static void belle_sip_channel_init(belle_sip_channel_t *obj){
	belle_sip_object_init_type(obj,belle_sip_channel_t);
	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);
}

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

void belle_sip_udp_channel_set_dest(belle_sip_udp_channel_t *chan, const struct addrinfo *ai){
	memcpy(chan->base.peer.ai_addr,ai->ai_addr,ai->ai_addrlen);
	chan->base.peer.ai_addrlen=ai->ai_addrlen;
}

static belle_sip_channel_vptr_t udp_channel_vptr={
	udp_channel_send,
	udp_channel_recv
};

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

belle_sip_udp_channel_t *belle_sip_udp_channel_new(const char *addr, int port){
	belle_sip_udp_channel_t *obj=belle_sip_object_new_with_vptr(belle_sip_udp_channel_t,&udp_channel_vptr,udp_channel_uninit);
	belle_sip_channel_init((belle_sip_channel_t*)obj);
	obj->sock=create_udp_socket(addr,port);
	if (obj->sock==-1){
		belle_sip_object_unref(obj);
		return NULL;
	}
	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;
};

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

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


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;
	belle_sip_udp_channel_set_dest(lp->channel,dest);
	return BELLE_SIP_CHANNEL(lp);
}

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);
	lp->channel=belle_sip_udp_channel_new(ipaddress,port);
	if (lp->channel==NULL){
		belle_sip_object_unref(lp);
		lp=NULL;
	}
	return BELLE_SIP_LISTENING_POINT(lp);
}