migrate everything to apr sockets

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1025 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2006-04-04 02:28:11 +00:00
parent 3cf68432c1
commit ab65eb3873
3 changed files with 78 additions and 101 deletions

View File

@ -49,11 +49,11 @@ extern "C" {
typedef void (*switch_rtp_invalid_handler)(switch_rtp *rtp_session,
switch_raw_socket_t sock,
switch_socket_t *sock,
void *data,
unsigned int datalen,
uint32_t fromip,
uint16_t fromport);
switch_sockaddr_t *from_addr);
switch_rtp *switch_rtp_new(char *rx_ip,
int rx_port,
@ -65,7 +65,7 @@ switch_rtp *switch_rtp_new(char *rx_ip,
switch_memory_pool *pool);
void switch_rtp_destroy(switch_rtp **rtp_session);
switch_raw_socket_t switch_rtp_get_rtp_socket(switch_rtp *rtp_session);
switch_socket_t *switch_rtp_get_rtp_socket(switch_rtp *rtp_session);
void switch_rtp_set_invald_handler(switch_rtp *rtp_session, switch_rtp_invalid_handler on_invalid);
int switch_rtp_read(switch_rtp *rtp_session, void *data, uint32_t datalen, int *payload_type);
int switch_rtp_zerocopy_read(switch_rtp *rtp_session, void **data, int *payload_type);

View File

@ -103,6 +103,7 @@ struct private_object {
struct switch_frame read_frame;
struct switch_frame cng_frame;
struct mdl_profile *profile;
switch_sockaddr_t *stun_addr;
unsigned char read_buf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
unsigned char cng_buf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
switch_core_session *session;
@ -120,7 +121,7 @@ struct private_object {
char *remote_user;
unsigned int cand_id;
unsigned int desc_id;
switch_raw_socket_t rtp_sock;
switch_socket_t *rtp_sock;
char last_digit;
unsigned int dc;
time_t last_digit_time;
@ -176,7 +177,7 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
static switch_status channel_kill_channel(switch_core_session *session, int sig);
static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsession, ldl_signal_t signal, char *msg);
static ldl_status handle_response(ldl_handle_t *handle, char *id);
static void stun_callback(struct switch_rtp *switch_rtp, switch_raw_socket_t sock, void *data, unsigned int len, uint32_t ip, uint16_t port);
static void stun_callback(struct switch_rtp *switch_rtp, switch_socket_t *sock, void *data, unsigned int len, switch_sockaddr_t *from_addr);
static switch_status load_config(void);
@ -703,8 +704,9 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
uint8_t buf[256] = {0};
char login[80];
stun_packet_t *packet;
struct sockaddr_in servaddr;
//struct sockaddr_in servaddr;
unsigned int elapsed;
switch_size_t bytes;
if (tech_pvt->last_stun) {
elapsed = (unsigned int)((switch_time_now() - tech_pvt->last_stun) / 1000);
@ -716,13 +718,12 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
}
snprintf(login, sizeof(login), "%s%s", tech_pvt->remote_user, tech_pvt->local_user);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(tech_pvt->remote_ip);
servaddr.sin_port=htons(tech_pvt->remote_port);
packet = stun_packet_build_header(STUN_BINDING_REQUEST, NULL, buf);
stun_packet_attribute_add_username(packet, login, 32);
sendto(tech_pvt->rtp_sock, (char *)packet, stun_packet_length(packet), 0 ,(struct sockaddr *)&servaddr, sizeof(servaddr));
bytes = stun_packet_length(packet);
switch_socket_sendto(tech_pvt->rtp_sock, tech_pvt->stun_addr, 0, (void *)packet, &bytes);
//sendto(tech_pvt->rtp_sock, (char *)packet, stun_packet_length(packet), 0 ,(struct sockaddr *)&servaddr, sizeof(servaddr));
//xstun
//printf("XXXX SEND STUN REQ %s U=%s to %s:%d\n", packet->header.id, login, tech_pvt->remote_ip, tech_pvt->remote_port);
tech_pvt->stuncount = 25;
@ -1304,6 +1305,16 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi
tech_pvt->remote_port = candidates[x].port;
tech_pvt->remote_user = switch_core_session_strdup(session, candidates[x].username);
if (switch_sockaddr_info_get(&tech_pvt->stun_addr,
tech_pvt->remote_ip,
SWITCH_UNSPEC,
tech_pvt->remote_port,
0,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Address Error!\n");
return LDL_STATUS_FALSE;
}
if (tech_pvt->codec_index < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Don't have my codec yet here's one\n");
@ -1391,7 +1402,7 @@ static ldl_status handle_response(ldl_handle_t *handle, char *id)
return LDL_STATUS_SUCCESS;
}
static void stun_callback(struct switch_rtp *switch_rtp, switch_raw_socket_t sock, void *data, unsigned int len, uint32_t ip, uint16_t port)
static void stun_callback(struct switch_rtp *switch_rtp, switch_socket_t *sock, void *data, unsigned int len, switch_sockaddr_t *from_addr)
{
stun_packet_t *packet;
stun_packet_attribute_t *attr;
@ -1444,23 +1455,19 @@ static void stun_callback(struct switch_rtp *switch_rtp, switch_raw_socket_t soc
if (packet->header.type == STUN_BINDING_REQUEST && strstr(username,tech_pvt->remote_user)) {
uint8_t buf[512];
stun_packet_t *rpacket;
struct sockaddr_in servaddr;
char *remote_ip;
switch_size_t bytes;
servaddr.sin_addr.s_addr = ip;
remote_ip = inet_ntoa(servaddr.sin_addr);
memset(buf, 0, sizeof(buf));
rpacket = stun_packet_build_header(STUN_BINDING_RESPONSE, packet->header.id, buf);
stun_packet_attribute_add_username(rpacket, username, 32);
stun_packet_attribute_add_binded_address(rpacket, remote_ip, port);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=ip;
servaddr.sin_port=port;
switch_sockaddr_ip_get(&remote_ip, from_addr);
stun_packet_attribute_add_binded_address(rpacket, remote_ip, from_addr->port);
//xstun
//switch_console_printf(SWITCH_CHANNEL_CONSOLE, "RESPONSE TO BIND %s:%d [%s]\n", remote_ip, port, username);
sendto(sock, (char *)rpacket, stun_packet_length(rpacket), 0 ,(struct sockaddr *)&servaddr, sizeof(servaddr));
//sendto(sock, (char *)rpacket, stun_packet_length(rpacket), 0 ,(struct sockaddr *)&servaddr, sizeof(servaddr));
bytes = stun_packet_length(rpacket);
switch_socket_sendto(tech_pvt->rtp_sock, from_addr, 0, (void*)rpacket, &bytes);
//switch_set_flag(tech_pvt, TFLAG_IO);
}

View File

@ -29,23 +29,6 @@
* switch_rtp.c -- RTP
*
*/
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#elif defined HAVE_WINSOCK2_H
# include <winsock2.h>
# include <ws2tcpip.h>
# define RTPW_USE_WINSOCK2 1
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef RTPW_USE_WINSOCK2
# define DICT_FILE "words.txt"
#else
@ -66,12 +49,6 @@
#include <datatypes.h>
#include <srtp.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define do_close(s) if (s > -1) {close(s); s = -1;}
#define rtp_header_len 12
@ -86,13 +63,13 @@ typedef struct {
struct switch_rtp {
switch_raw_socket_t sock;
switch_socket_t *sock;
struct sockaddr_in local_addr;
switch_sockaddr_t *local_addr;
rtp_msg_t send_msg;
srtp_ctx_t *send_ctx;
struct sockaddr_in remote_addr;
switch_sockaddr_t *remote_addr;
rtp_msg_t recv_msg;
srtp_ctx_t *recv_ctx;
@ -104,6 +81,8 @@ struct switch_rtp {
uint32_t ts;
uint32_t flags;
switch_memory_pool *pool;
switch_sockaddr_t *from_addr;
};
static int global_init = 0;
@ -114,13 +93,6 @@ static void init_rtp(void)
return;
}
#ifdef RTPW_USE_WINSOCK2
WORD wVersionRequested = MAKEWORD(2, 0);
WSADATA wsaData;
ret = WSAStartup(wVersionRequested, &wsaData);
#endif
srtp_init();
global_init = 1;
@ -135,9 +107,10 @@ switch_rtp *switch_rtp_new(char *rx_ip,
const char **err,
switch_memory_pool *pool)
{
switch_raw_socket_t sock;
switch_socket_t *sock;
switch_rtp *rtp_session = NULL;
struct in_addr rx_addr, tx_addr;
switch_sockaddr_t *rx_addr;
switch_sockaddr_t *tx_addr;
srtp_policy_t policy;
char key[MAX_KEY_LEN];
uint32_t ssrc = rand() & 0xffff;
@ -146,45 +119,39 @@ switch_rtp *switch_rtp_new(char *rx_ip,
init_rtp();
}
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
*err = "Socket Error!\n";
if (switch_sockaddr_info_get(&rx_addr, rx_ip, SWITCH_UNSPEC, rx_port, 0, pool) != SWITCH_STATUS_SUCCESS) {
*err = "RX Address Error!";
return NULL;
}
if (!inet_aton(rx_ip, &rx_addr)) {
*err = "RX Address Error!\n";
if (switch_sockaddr_info_get(&tx_addr, tx_ip, SWITCH_UNSPEC, tx_port, 0, pool) != SWITCH_STATUS_SUCCESS) {
*err = "TX Address Error!";
return NULL;
}
}
if (!inet_aton(tx_ip, &tx_addr)) {
*err = "TX Address Error!\n";
if (switch_socket_create(&sock, AF_INET, SOCK_DGRAM, 0, pool) != SWITCH_STATUS_SUCCESS) {
*err = "Socket Error!";
return NULL;
}
}
if (switch_socket_bind(sock, rx_addr) != SWITCH_STATUS_SUCCESS) {
*err = "Bind Error!";
return NULL;
}
if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session)))) {
*err = "Memory Error!";
return NULL;
}
rtp_session->flags = flags;
rtp_session->local_addr.sin_addr = rx_addr;
rtp_session->local_addr.sin_family = PF_INET;
rtp_session->local_addr.sin_port = htons(rx_port);
rtp_session->remote_addr.sin_addr = tx_addr;
rtp_session->remote_addr.sin_family = PF_INET;
rtp_session->remote_addr.sin_port = htons(tx_port);
rtp_session->sock = sock;
rtp_session->local_addr = rx_addr;
rtp_session->remote_addr = tx_addr;
rtp_session->pool = pool;
switch_sockaddr_info_get(&rtp_session->from_addr, NULL, SWITCH_UNSPEC, 0, 0, rtp_session->pool);
if (bind(sock, (struct sockaddr *)&rtp_session->local_addr, sizeof(rtp_session->local_addr)) < 0) {
*err = "Bind Err!";
close(sock);
return NULL;
}
if switch_test_flag(rtp_session, SWITCH_RTP_NOBLOCK) {
fcntl(sock, F_SETFL, O_NONBLOCK);
switch_socket_opt_set(rtp_session->sock, APR_SO_NONBLOCK, TRUE);
}
policy.key = (uint8_t *)key;
@ -236,8 +203,9 @@ switch_rtp *switch_rtp_new(char *rx_ip,
void switch_rtp_killread(switch_rtp *rtp_session)
{
apr_socket_shutdown(rtp_session->sock, APR_SHUTDOWN_READWRITE);
switch_clear_flag(rtp_session, SWITCH_RTP_FLAG_IO);
shutdown(rtp_session->sock, SHUT_RDWR);
}
@ -245,12 +213,12 @@ void switch_rtp_destroy(switch_rtp **rtp_session)
{
switch_rtp_killread(*rtp_session);
do_close((*rtp_session)->sock);
switch_socket_close((*rtp_session)->sock);
*rtp_session = NULL;
return;
}
switch_raw_socket_t switch_rtp_get_rtp_socket(switch_rtp *rtp_session)
switch_socket_t *switch_rtp_get_rtp_socket(switch_rtp *rtp_session)
{
return rtp_session->sock;
}
@ -262,22 +230,23 @@ void switch_rtp_set_invald_handler(switch_rtp *rtp_session, switch_rtp_invalid_h
int switch_rtp_read(switch_rtp *rtp_session, void *data, uint32_t datalen, int *payload_type)
{
int32_t bytes;
struct sockaddr_in in;
unsigned int len = sizeof(struct sockaddr_in);
switch_size_t bytes;
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
return -1;
}
bytes = sizeof(rtp_msg_t);
switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock, 0, (void *)&rtp_session->recv_msg, &bytes);
bytes = recvfrom(rtp_session->sock, (void *)&rtp_session->recv_msg, sizeof(rtp_msg_t), 0, (struct sockaddr *) &in, &len);
if (bytes <= 0) {
return 0;
}
if (rtp_session->recv_msg.header.version != 2) {
if (rtp_session->invalid_handler) {
rtp_session->invalid_handler(rtp_session, rtp_session->sock, (void *) &rtp_session->recv_msg, bytes, in.sin_addr.s_addr, in.sin_port);
rtp_session->invalid_handler(rtp_session, rtp_session->sock, (void *) &rtp_session->recv_msg, bytes, rtp_session->from_addr);
}
return 0;
}
@ -290,23 +259,23 @@ int switch_rtp_read(switch_rtp *rtp_session, void *data, uint32_t datalen, int *
int switch_rtp_zerocopy_read(switch_rtp *rtp_session, void **data, int *payload_type)
{
int32_t bytes;
struct sockaddr_in in;
unsigned int len = sizeof(struct sockaddr_in);
switch_size_t bytes;
*data = NULL;
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
return -1;
}
bytes = recvfrom(rtp_session->sock, (void *)&rtp_session->recv_msg, sizeof(rtp_msg_t), 0, (struct sockaddr *) &in, &len);
bytes = sizeof(rtp_msg_t);
switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock, 0, (void *)&rtp_session->recv_msg, &bytes);
if (bytes <= 0) {
return 0;
}
if (rtp_session->recv_msg.header.version != 2) {
if (rtp_session->invalid_handler) {
rtp_session->invalid_handler(rtp_session, rtp_session->sock, (void *) &rtp_session->recv_msg, bytes, in.sin_addr.s_addr, ntohs(in.sin_port));
rtp_session->invalid_handler(rtp_session, rtp_session->sock, (void *) &rtp_session->recv_msg, bytes, rtp_session->from_addr);
}
return 0;
}
@ -318,7 +287,7 @@ int switch_rtp_zerocopy_read(switch_rtp *rtp_session, void **data, int *payload_
int switch_rtp_write(switch_rtp *rtp_session, void *data, int datalen, uint32_t ts)
{
int32_t bytes;
switch_size_t bytes;
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
return -1;
@ -331,15 +300,16 @@ int switch_rtp_write(switch_rtp *rtp_session, void *data, int datalen, uint32_t
rtp_session->payload = htonl(rtp_session->payload);
memcpy(rtp_session->send_msg.body, data, datalen);
bytes = sendto(rtp_session->sock, (void*)&rtp_session->send_msg,
datalen + rtp_header_len, 0, (struct sockaddr *)&rtp_session->remote_addr, sizeof (struct sockaddr_in));
bytes = datalen + rtp_header_len;
switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void*)&rtp_session->send_msg, &bytes);
return bytes;
}
int switch_rtp_write_payload(switch_rtp *rtp_session, void *data, int datalen, int payload, uint32_t ts, uint32_t mseq)
{
int32_t bytes;
switch_size_t bytes;
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
return -1;
@ -350,8 +320,8 @@ int switch_rtp_write_payload(switch_rtp *rtp_session, void *data, int datalen, i
rtp_session->send_msg.header.pt = htonl(payload);
memcpy(rtp_session->send_msg.body, data, datalen);
bytes = sendto(rtp_session->sock, (void*)&rtp_session->send_msg,
datalen + rtp_header_len, 0, (struct sockaddr *)&rtp_session->remote_addr, sizeof (struct sockaddr_in));
bytes = datalen + rtp_header_len;
switch_socket_sendto(rtp_session->sock, rtp_session->remote_addr, 0, (void*)&rtp_session->send_msg, &bytes);
return bytes;
}