提交 a8177018 authored 作者: Mike Jerris's avatar Mike Jerris

Merge pull request #290 in FS/freeswitch from ~SAFAROV/freeswitch2:mod_erlang_socket to master

* commit '97c25275':
  FS-7628: mod_erlang_event - added ipv6 support
......@@ -1103,6 +1103,7 @@ SWITCH_DECLARE(switch_status_t) switch_socket_connect(switch_socket_t *sock, swi
SWITCH_DECLARE(uint16_t) switch_sockaddr_get_port(switch_sockaddr_t *sa);
SWITCH_DECLARE(const char *) switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in);
SWITCH_DECLARE(switch_status_t) switch_getnameinfo(char **hostname, switch_sockaddr_t *sa, int32_t flags);
SWITCH_DECLARE(int32_t) switch_sockaddr_get_family(switch_sockaddr_t *sa);
SWITCH_DECLARE(switch_status_t) switch_sockaddr_ip_get(char **addr, switch_sockaddr_t *sa);
SWITCH_DECLARE(int) switch_sockaddr_equal(const switch_sockaddr_t *sa1, const switch_sockaddr_t *sa2);
......
......@@ -64,6 +64,8 @@ void ei_link(listener_t *listener, erlang_pid * from, erlang_pid * to)
char msgbuf[2048];
char *s;
int index = 0;
switch_socket_t *sock = NULL;
switch_os_sock_put(&sock, &listener->sockdes, listener->pool);
index = 5; /* max sizes: */
ei_encode_version(msgbuf, &index); /* 1 */
......@@ -79,13 +81,9 @@ void ei_link(listener_t *listener, erlang_pid * from, erlang_pid * to)
/* sum: 542 */
switch_mutex_lock(listener->sock_mutex);
#ifdef WIN32
send(listener->sockfd, msgbuf, index, 0);
#else
if (write(listener->sockfd, msgbuf, index) == -1) {
if (switch_socket_send(sock, msgbuf, (switch_size_t *) &index)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to link to process on %s\n", listener->peer_nodename);
}
#endif
switch_mutex_unlock(listener->sock_mutex);
}
......@@ -329,30 +327,17 @@ int ei_decode_string_or_binary(char *buf, int *index, int maxlen, char *dst)
switch_status_t initialise_ei(struct ei_cnode_s *ec)
{
int rv;
struct sockaddr_in server_addr;
struct hostent *nodehost;
char thishostname[EI_MAXHOSTNAMELEN + 1] = "";
char *thishostname = NULL;
char thisnodename[MAXNODELEN + 1];
char thisalivename[MAXNODELEN + 1];
char *atsign;
/* zero out the struct before we use it */
memset(&server_addr, 0, sizeof(server_addr));
/* convert the configured IP to network byte order, handing errors */
rv = switch_inet_pton(AF_INET, prefs.ip, &server_addr.sin_addr.s_addr);
if (rv == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not parse invalid ip address: %s\n", prefs.ip);
return SWITCH_STATUS_FALSE;
} else if (rv == -1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error when parsing ip address %s : %s\n", prefs.ip, strerror(errno));
return SWITCH_STATUS_FALSE;
if (zstr(listen_list.hostname) || !strncasecmp(prefs.ip, "0.0.0.0", 7) || !strncasecmp(prefs.ip, "::", 2)) {
listen_list.hostname=(char *) switch_core_get_hostname();
}
if (strlen(listen_list.hostname) > EI_MAXHOSTNAMELEN) {
*(listen_list.hostname+EI_MAXHOSTNAMELEN) = '\0';
}
/* set the address family and port */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(prefs.port);
/* copy the prefs.nodename into something we can modify */
strncpy(thisalivename, prefs.nodename, MAXNODELEN);
......@@ -363,39 +348,19 @@ switch_status_t initialise_ei(struct ei_cnode_s *ec)
/* truncate the alivename at the @ */
*atsign = '\0';
} else {
#ifdef WIN32
if ((nodehost = gethostbyaddr((const char *) &server_addr.sin_addr.s_addr, sizeof(server_addr.sin_addr.s_addr), AF_INET)))
#else
if ((nodehost = gethostbyaddr((const char *) &server_addr.sin_addr.s_addr, sizeof(server_addr.sin_addr.s_addr), AF_INET)))
#endif
memcpy(thishostname, nodehost->h_name, EI_MAXHOSTNAMELEN);
if (zstr_buf(thishostname) || !strncasecmp(prefs.ip, "0.0.0.0", 7)) {
gethostname(thishostname, EI_MAXHOSTNAMELEN);
}
if (prefs.shortname) {
char *off;
if ((off = strchr(thishostname, '.'))) {
if ((off = strchr(listen_list.hostname, '.'))) {
*off = '\0';
}
} else {
if (!(_res.options & RES_INIT)) {
// init the resolver
res_init();
}
if (_res.dnsrch[0] && !zstr_buf(_res.dnsrch[0])) {
strncat(thishostname, ".", 1);
strncat(thishostname, _res.dnsrch[0], EI_MAXHOSTNAMELEN - strlen(thishostname));
}
}
snprintf(thisnodename, MAXNODELEN + 1, "%s@%s", prefs.nodename, thishostname);
snprintf(thisnodename, MAXNODELEN + 1, "%s@%s", prefs.nodename, listen_list.hostname);
}
/* init the ei stuff */
if (ei_connect_xinit(ec, thishostname, thisalivename, thisnodename, (Erl_IpAddr) (&server_addr.sin_addr.s_addr), prefs.cookie, 0) < 0) {
if (ei_connect_xinit(ec, listen_list.hostname, thisalivename, thisnodename, (Erl_IpAddr) listen_list.addr, prefs.cookie, 0) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to init ei connection\n");
return SWITCH_STATUS_FALSE;
}
......
......@@ -111,7 +111,7 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
_ei_x_encode_string(&ebuf, reply);
switch_mutex_lock(acs->listener->sock_mutex);
ei_send(acs->listener->sockfd, &acs->pid, ebuf.buff, ebuf.index);
ei_send(acs->listener->sockdes, &acs->pid, ebuf.buff, ebuf.index);
switch_mutex_unlock(acs->listener->sock_mutex);
#ifdef EI_DEBUG
ei_x_print_msg(&ebuf, &acs->pid, 1);
......@@ -139,7 +139,7 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
switch_mutex_lock(acs->listener->sock_mutex);
ei_send(acs->listener->sockfd, &acs->pid, rbuf.buff, rbuf.index);
ei_send(acs->listener->sockdes, &acs->pid, rbuf.buff, rbuf.index);
switch_mutex_unlock(acs->listener->sock_mutex);
#ifdef EI_DEBUG
ei_x_print_msg(&rbuf, &acs->pid, 1);
......@@ -1332,7 +1332,7 @@ static switch_status_t handle_net_kernel_msg(listener_t *listener, erlang_msg *
ei_x_encode_atom(rbuf, "yes");
switch_mutex_lock(listener->sock_mutex);
ei_send(listener->sockfd, &pid, rbuf->buff, rbuf->index);
ei_send(listener->sockdes, &pid, rbuf->buff, rbuf->index);
switch_mutex_unlock(listener->sock_mutex);
#ifdef EI_DEBUG
ei_x_print_msg(rbuf, &pid, 1);
......@@ -1398,7 +1398,7 @@ int handle_msg(listener_t *listener, erlang_msg * msg, ei_x_buff * buf, ei_x_buf
return 0;
} else if (rbuf->index > 1) {
switch_mutex_lock(listener->sock_mutex);
ei_send(listener->sockfd, &msg->from, rbuf->buff, rbuf->index);
ei_send(listener->sockdes, &msg->from, rbuf->buff, rbuf->index);
switch_mutex_unlock(listener->sock_mutex);
#ifdef EI_DEBUG
ei_x_print_msg(rbuf, &msg->from, 1);
......
......@@ -112,11 +112,7 @@ typedef enum {
5 call sessions will be "attached" to the same listener.
*/
struct listener {
#ifdef WIN32
SOCKET sockfd;
#else
int sockfd;
#endif
switch_os_socket_t sockdes;
struct ei_cnode_s *ec;
struct erlang_process log_process;
struct erlang_process event_process;
......@@ -170,6 +166,7 @@ struct api_command_struct {
};
struct globals_struct {
switch_mutex_t *listener_mutex;
switch_thread_rwlock_t *listener_rwlock;
switch_thread_rwlock_t *bindings_rwlock;
switch_event_node_t *node;
......@@ -187,11 +184,10 @@ struct globals_struct {
typedef struct globals_struct globals_t;
struct listen_list_struct {
#ifdef WIN32
SOCKET sockfd;
#else
int sockfd;
#endif
char *hostname;
char addr[64];
switch_socket_t *sock;
switch_mutex_t *sock_mutex;
listener_t *listeners;
uint8_t ready;
};
......@@ -220,6 +216,7 @@ struct prefs_struct {
int compat_rel;
int max_event_bulk;
int max_log_bulk;
int stop_on_bind_error;
};
typedef struct prefs_struct prefs_t;
......
......@@ -913,6 +913,11 @@ SWITCH_DECLARE(int32_t) switch_sockaddr_get_family(switch_sockaddr_t *sa)
return sa->family;
}
SWITCH_DECLARE(switch_status_t) switch_getnameinfo(char **hostname, switch_sockaddr_t *sa, int32_t flags)
{
return apr_getnameinfo(hostname, sa, flags);
}
SWITCH_DECLARE(switch_status_t) switch_socket_atmark(switch_socket_t *sock, int *atmark)
{
return apr_socket_atmark(sock, atmark);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论