Beispiel #1
0
static void
transport_on_listening_socket_event
(
	OWSLSocket socket,
	OWSLEvent event,
	void * user_data
)
{
	if ((event & OWSL_EVENT_ERROR) != 0)
	{
		OWSLSocketType implementation = owsl_type_get (socket)  ;
		struct sockaddr * bind_address = owsl_bound_address_get (socket) ;
		transport_socket_remove (socket, TRANSPORT_DATA) ;
		if (transport_listen_error_callback != NULL)
		{
			transport_listen_error_callback (transport_protocol_get (implementation), bind_address) ;
		}
		return ;
	}

	if ((event & OWSL_EVENT_READ) != 0)
	{
		OWSLSocket new_socket = owsl_accept (socket, NULL, NULL) ;
		if (new_socket > 0)
		{
			transport_socket_add (new_socket, TRANSPORT_DATA) ;
		}
		return ;
	}
	return ;
}
Beispiel #2
0
static struct sockaddr *
transport_address_listen
(
	TransportProtocol protocol,
	const struct sockaddr * bind_address,
	int bind_address_length,
	int strict_bind,   /* boolean to forbid binding on another address */
	int pending_max
)
{
	OWSLSocket socket ;
	OWSLSocketType implementation = transport_implementation_get (protocol, bind_address->sa_family) ;

	if (pthread_mutex_lock (& transport_socket_lists_mutex))
	{
		return NULL ;
	}
	socket = transport_socket_get
	(
		TRANSPORT_LISTENING,
		implementation,
		bind_address,
		strict_bind
	) ;
	if (socket != 0)
	{
		pthread_mutex_unlock (& transport_socket_lists_mutex) ;
		return NULL ;
	}
	socket = transport_listening_socket_new
	(
		implementation,
		bind_address,
		/*bind_address_length*/sizeof(struct sockaddr),
		strict_bind,
		pending_max
	) ;
	if (socket < 0)
	{
		pthread_mutex_unlock (& transport_socket_lists_mutex) ;
		return NULL ;
	}
	if (pthread_mutex_unlock (& transport_socket_lists_mutex))
	{
		return NULL ;
	}

	return owsl_bound_address_get (socket) ;
}
Beispiel #3
0
static OWSLSocket
transport_listening_socket_new
(
	OWSLSocketType implementation,
	const struct sockaddr * bind_address,
	int bind_address_length,
	int strict_bind,
	int pending_max
)
{
	OWSLSocket * socket ;

	socket = transport_socket_add
	(
		owsl_socket_by_type (implementation),
		TRANSPORT_LISTENING
	) ;
	if (socket == NULL)
	{
			return -1 ;
	}

	if (owsl_bind (* socket, bind_address, bind_address_length))
	{
		transport_socket_remove (* socket, TRANSPORT_LISTENING) ;
		return -1 ;
	}
	if (strict_bind && owsl_address_compare (bind_address, owsl_bound_address_get (* socket)))
	{
		transport_socket_remove (* socket, TRANSPORT_LISTENING) ;
		return -1 ;
	}

	if (owsl_socket_type_mode_get (implementation) == OWSL_MODE_STREAM)
	{
		if (owsl_listen (* socket, pending_max))
		{
			transport_socket_remove (* socket, TRANSPORT_LISTENING) ;
			return -1 ;
		}
	}

	return * socket ;
}
Beispiel #4
0
struct sockaddr *
transport_listening_address_get
(
	TransportProtocol protocol,
	OWSLAddressFamily address_family,
	OWListIterator ** iterator
)
{
	OWList * list ;
	struct sockaddr * address = NULL ;
	OWSLSocketType implementation ;

	if (iterator == NULL)
	{
		return NULL ;
	}

	implementation = transport_implementation_get (protocol, address_family) ;
	if (implementation != OWSL_TYPE_UNKNOWN)
	{
		if (* iterator == NULL)
		{
			list = transport_socket_list_get (TRANSPORT_LISTENING, owsl_socket_type_mode_get (implementation)) ;
			* iterator = owlist_iterator_new (list, OWLIST_READ) ;
		}
		if (* iterator != NULL)
		{
			OWSLSocket socket = transport_socket_get_next
			(
				implementation,
				* iterator
			) ;
			if (socket > 0)
			{
				address = owsl_bound_address_get (socket) ;
			}
		}
	}

	return address ;
}
Beispiel #5
0
static char *
ping_request_new
(
	const char * sip_login,
	const char * sip_realm,
	OWSLSocket socket
)
{
	int return_code ;
	char * request ;
	struct sockaddr * bound_address ;
	char bound_ip [OWSL_IP_MAX_SIZE] ;
	unsigned short bound_port ;
	int i ;
	int int_max_decimal_digits ;

	bound_address = owsl_bound_address_get (socket) ;
	if (bound_address == NULL)
	{
		return NULL ;
	}

	return_code = owsl_address_parse (bound_address, NULL, bound_ip, sizeof (bound_ip), & bound_port) ;
	if (return_code != 0)
	{
		return NULL ;
	}

	int_max_decimal_digits = 0 ;
	i = INT_MAX ;
	while (i > 0)
	{
		int_max_decimal_digits ++ ;
		i /= 10 ;
	}

	request = (char *) malloc
	(
		210 + /* static text */
		strlen (sip_login) * 3 +
		strlen (sip_realm) * 4 +
		strlen (bound_ip) * 2 +
		5 /* bound_port */ +
		5 /* rand() */ +
		int_max_decimal_digits * 4 +
		1 /* '\0' */
	) ;

	srand ((unsigned int) time (NULL)) ;
	sprintf
	(
		request,
		"REGISTER sip:%s SIP/2.0\r\n"
		"Via: SIP/2.0/UDP %s:%d;rport;branch=%d\r\n"
		"Route: <sip:%s;lr>\r\n"
		"From: nobody <sip:%s@%s>;tag=%d\r\n"
		"To: <sip:%s@%s>\r\n"
		"Call-ID: %d\r\n"
		"Contact: <sip:%s@%s>\r\n"
		"CSeq: %d REGISTER\r\n"
		"X-Wengo-Ping: network test\r\n"
		"Content-Length: 0\r\n\r\n",
		sip_realm,       /* request uri */
		bound_ip,        /* via ip */
		bound_port,      /* via port */
		(int) rand (),   /* via branch */
		sip_realm,       /* route uri */
		sip_login,       /* from uri user */
		sip_realm,       /* from uri domain */
		(int) rand (),   /* from tag */
		sip_login,       /* to uri user */
		sip_realm,       /* to uri domain */
		(int) rand (),   /* call id */
		sip_login,       /* contact uri user */
		bound_ip,        /* contact uri domain */
		(int) rand ()    /* cseq number */
	) ;

	return request ;
}