udp_channel.c 4.3 KB
Newer Older
jehan's avatar
jehan committed
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
/*
	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"
#include "channel.h"

BELLE_SIP_DECLARE_CUSTOM_VPTR_BEGIN(belle_sip_udp_channel_t,belle_sip_channel_t)
BELLE_SIP_DECLARE_CUSTOM_VPTR_END

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){
jehan's avatar
jehan committed
43
		belle_sip_error("Could not send UDP packet: from [%s:%i]  to [%s:%i] because [%s]",obj->local_ip
44 45 46 47
												,obj->local_port
												,obj->peer_name
												,obj->peer_port
												,strerror(errno));
jehan's avatar
jehan committed
48 49 50 51 52 53 54 55 56 57 58
		return -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;
	struct sockaddr_storage addr;
	socklen_t addrlen=sizeof(addr);
	err=recvfrom(chan->sock,buf,buflen,MSG_DONTWAIT,(struct sockaddr*)&addr,&addrlen);
jehan's avatar
jehan committed
59

jehan's avatar
jehan committed
60 61 62 63 64 65 66
	if (err==-1 && errno!=EWOULDBLOCK){
		belle_sip_error("Could not receive UDP packet: %s",strerror(errno));
		return -errno;
	}
	return err;
}

67
int udp_channel_connect(belle_sip_channel_t *obj, const struct addrinfo *ai){
jehan's avatar
jehan committed
68 69 70
	struct sockaddr_storage laddr;
	socklen_t lslen=sizeof(laddr);
	if (obj->local_ip==NULL){
71
		belle_sip_get_src_addr_for(ai->ai_addr,ai->ai_addrlen,(struct sockaddr*)&laddr,&lslen);
jehan's avatar
jehan committed
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
		if (lslen==sizeof(struct sockaddr_in6)){
			struct sockaddr_in6 *sin6=(struct sockaddr_in6*)&laddr;
			sin6->sin6_port=htons(obj->local_port);
		}else{
			struct sockaddr_in *sin=(struct sockaddr_in*)&laddr;
			sin->sin_port=htons(obj->local_port);
		}
	}
	belle_sip_channel_set_ready(obj,(struct sockaddr*)&laddr,lslen);
	return 0;
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_udp_channel_t);

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_udp_channel_t)=
{
	{
		{
			BELLE_SIP_VPTR_INIT(belle_sip_udp_channel_t,belle_sip_channel_t,FALSE),
			(belle_sip_object_destroy_t)udp_channel_uninit,
			NULL,
			NULL
		},
		"UDP",
		0, /*is_reliable*/
		udp_channel_connect,
		udp_channel_send,
		udp_channel_recv
	}
};

belle_sip_channel_t * belle_sip_channel_new_udp(belle_sip_stack_t *stack, int sock, const char *bindip, int localport, const char *dest, int port){
	belle_sip_udp_channel_t *obj=belle_sip_object_new(belle_sip_udp_channel_t);
105
	belle_sip_channel_init((belle_sip_channel_t*)obj,stack,bindip,localport,dest,port);
jehan's avatar
jehan committed
106 107 108 109 110 111
	obj->sock=sock;
	return (belle_sip_channel_t*)obj;
}

belle_sip_channel_t * belle_sip_channel_new_udp_with_addr(belle_sip_stack_t *stack, int sock, const char *bindip, int localport, const struct addrinfo *peer){
	belle_sip_udp_channel_t *obj=belle_sip_object_new(belle_sip_udp_channel_t);
112
	struct addrinfo ai;
jehan's avatar
jehan committed
113 114 115 116 117
	char name[NI_MAXHOST];
	char serv[NI_MAXSERV];
	int err;

	obj->sock=sock;
118 119
	ai=*peer;
	err=getnameinfo(ai.ai_addr,ai.ai_addrlen,name,sizeof(name),serv,sizeof(serv),NI_NUMERICHOST|NI_NUMERICSERV);
jehan's avatar
jehan committed
120 121 122 123 124
	if (err!=0){
		belle_sip_error("belle_sip_channel_new_udp_with_addr(): getnameinfo() failed: %s",gai_strerror(err));
		belle_sip_object_unref(obj);
		return NULL;
	}
125
	belle_sip_channel_init((belle_sip_channel_t*)obj,stack,bindip,localport,name,atoi(serv));
126
	err=getaddrinfo(name,serv,&ai,&obj->base.peer); /*might be optimized someway ?*/
jehan's avatar
jehan committed
127 128 129
	if (err!=0){
		belle_sip_error("getaddrinfo() failed for channel [%p] error [%s]",obj,gai_strerror(err));
	}
jehan's avatar
jehan committed
130 131 132
	return (belle_sip_channel_t*)obj;
}