2012-05-02 16:58:39 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2012 Paul Gardner-Stephen, Serval Project.
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Android does unix domain sockets, but only stream sockets, not datagram sockets.
|
|
|
|
So we need a separate monitor interface for Android. A bit of a pain, but in
|
|
|
|
fact it lets us make a very Android/Java-friendly interface, without any binary
|
|
|
|
data structures (except for a binary extent for an audio sample block).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serval.h"
|
2012-05-11 21:54:52 +00:00
|
|
|
#include "rhizome.h"
|
2012-05-02 16:58:39 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-05-21 02:52:50 +00:00
|
|
|
#if defined(LOCAL_PEERCRED) && !defined(SO_PEERCRED)
|
|
|
|
#define SO_PEERCRED LOCAL_PEERCRED
|
|
|
|
#endif
|
2012-05-03 02:41:13 +00:00
|
|
|
|
2012-05-03 17:46:06 +00:00
|
|
|
#define MONITOR_LINE_LENGTH 160
|
2012-05-02 16:58:39 +00:00
|
|
|
#define MONITOR_DATA_SIZE MAX_AUDIO_BYTES
|
|
|
|
struct monitor_context {
|
|
|
|
#define MONITOR_VOMP (1<<0)
|
|
|
|
#define MONITOR_RHIZOME (1<<1)
|
2012-05-21 03:25:25 +00:00
|
|
|
#define MONITOR_PEERS (1<<2)
|
2012-05-02 16:58:39 +00:00
|
|
|
int flags;
|
|
|
|
int socket;
|
|
|
|
char line[MONITOR_LINE_LENGTH];
|
|
|
|
int line_length;
|
|
|
|
#define MONITOR_STATE_COMMAND 1
|
|
|
|
#define MONITOR_STATE_DATA 2
|
|
|
|
int state;
|
|
|
|
unsigned char buffer[MONITOR_DATA_SIZE];
|
|
|
|
int data_expected;
|
|
|
|
int data_offset;
|
|
|
|
int sample_codec;
|
|
|
|
int sample_call_session_token;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_MONITOR_SOCKETS 8
|
|
|
|
int monitor_socket_count=0;
|
|
|
|
struct monitor_context monitor_sockets[MAX_MONITOR_SOCKETS];
|
2012-05-02 17:30:34 +00:00
|
|
|
long long monitor_last_update_time=0;
|
2012-05-02 16:58:39 +00:00
|
|
|
|
|
|
|
int monitor_process_command(int index,char *cmd);
|
|
|
|
int monitor_process_data(int index);
|
2012-06-25 07:20:23 +00:00
|
|
|
static void monitor_new_client(int s);
|
2012-05-02 16:58:39 +00:00
|
|
|
|
|
|
|
int monitor_named_socket=-1;
|
|
|
|
int monitor_setup_sockets()
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
|
|
|
int len;
|
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
bzero(&name, sizeof(name));
|
2012-05-02 16:58:39 +00:00
|
|
|
name.sun_family = AF_UNIX;
|
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
if (monitor_named_socket!=-1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((monitor_named_socket = socket(AF_UNIX, SOCK_STREAM, 0))==-1) {
|
|
|
|
WHY_perror("socket");
|
|
|
|
goto error;
|
|
|
|
}
|
2012-05-03 18:14:41 +00:00
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
#ifdef linux
|
2012-06-21 05:05:05 +00:00
|
|
|
/* Use abstract namespace as Android has no writable FS which supports sockets.
|
|
|
|
Abstract namespace is just plain better, anyway, as no dead files end up
|
|
|
|
hanging around. */
|
2012-05-28 05:24:33 +00:00
|
|
|
name.sun_path[0]=0;
|
|
|
|
/* XXX: 104 comes from OSX sys/un.h - no #define (note Linux has UNIX_PATH_MAX and it's 108(!)) */
|
2012-06-21 05:05:05 +00:00
|
|
|
snprintf(&name.sun_path[1],104-2,
|
|
|
|
confValueGet("monitor.socket",DEFAULT_MONITOR_SOCKET_NAME));
|
2012-05-28 05:24:33 +00:00
|
|
|
/* Doesn't include trailing nul */
|
|
|
|
len = 1+strlen(&name.sun_path[1]) + sizeof(name.sun_family);
|
|
|
|
#else
|
2012-06-22 10:04:01 +00:00
|
|
|
snprintf(name.sun_path,104-1,"%s/%s",
|
2012-06-21 05:05:05 +00:00
|
|
|
serval_instancepath(),
|
|
|
|
confValueGet("monitor.socket",DEFAULT_MONITOR_SOCKET_NAME));
|
2012-05-28 05:24:33 +00:00
|
|
|
unlink(name.sun_path);
|
|
|
|
/* Includes trailing nul */
|
2012-05-29 00:18:22 +00:00
|
|
|
len = 1+strlen(name.sun_path) + sizeof(name.sun_family);
|
2012-05-28 05:24:33 +00:00
|
|
|
#endif
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
if(bind(monitor_named_socket, (struct sockaddr *)&name, len)==-1) {
|
|
|
|
WHY_perror("bind");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(listen(monitor_named_socket,MAX_MONITOR_SOCKETS)==-1) {
|
|
|
|
WHY_perror("listen");
|
|
|
|
goto error;
|
|
|
|
}
|
2012-05-17 03:43:42 +00:00
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
int reuseP=1;
|
|
|
|
if(setsockopt(monitor_named_socket, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&reuseP, sizeof(reuseP)) < 0) {
|
|
|
|
WHY_perror("setsockopt");
|
|
|
|
WHY("Could not indicate reuse addresses. Not necessarily a problem (yet)");
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
2012-05-28 05:24:33 +00:00
|
|
|
|
|
|
|
int send_buffer_size=64*1024;
|
|
|
|
if(setsockopt(monitor_named_socket, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
&send_buffer_size, sizeof(send_buffer_size))==-1)
|
|
|
|
WHY_perror("setsockopt");
|
|
|
|
if (debug&(DEBUG_IO|DEBUG_VERBOSE_IO)) WHY("Monitor server socket setup");
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-06-22 05:06:29 +00:00
|
|
|
fd_watch(monitor_named_socket,monitor_poll,POLL_IN);
|
2012-05-02 16:58:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
error:
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(monitor_named_socket);
|
2012-05-28 05:24:33 +00:00
|
|
|
monitor_named_socket=-1;
|
|
|
|
return -1;
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 03:55:41 +00:00
|
|
|
void monitor_poll(int ignored_fd)
|
2012-05-02 16:58:39 +00:00
|
|
|
{
|
2012-06-25 07:20:23 +00:00
|
|
|
int s,i,m;
|
2012-05-08 22:05:05 +00:00
|
|
|
unsigned char buffer[1024];
|
2012-06-25 07:20:23 +00:00
|
|
|
char msg[1024];
|
2012-05-08 22:05:05 +00:00
|
|
|
struct sockaddr *ignored_address=(struct sockaddr *)&buffer[0];
|
2012-05-02 16:58:39 +00:00
|
|
|
socklen_t ignored_length=sizeof(ignored_address);
|
|
|
|
|
2012-05-02 17:30:34 +00:00
|
|
|
/* tell all monitor clients about status of all calls periodically */
|
2012-06-25 07:20:23 +00:00
|
|
|
long long now = overlay_gettime_ms();
|
2012-06-22 11:06:59 +00:00
|
|
|
if (monitor_last_update_time > (now + 1000)) {
|
2012-05-08 06:28:37 +00:00
|
|
|
WHY("Fixed run away monitor_last_update_time");
|
2012-06-22 11:06:59 +00:00
|
|
|
monitor_last_update_time = now + 1000;
|
2012-05-08 06:28:37 +00:00
|
|
|
}
|
2012-06-22 11:06:59 +00:00
|
|
|
|
|
|
|
if (now > (monitor_last_update_time + 1000)) {
|
2012-05-08 22:10:29 +00:00
|
|
|
// WHY("Send keep alives");
|
2012-06-22 11:06:59 +00:00
|
|
|
monitor_last_update_time = now;
|
|
|
|
for(i = 0; i < vomp_call_count; i++) {
|
2012-05-06 23:31:46 +00:00
|
|
|
/* Push out any undelivered status changes */
|
2012-05-02 17:30:34 +00:00
|
|
|
monitor_call_status(&vomp_call_states[i]);
|
2012-05-08 06:17:39 +00:00
|
|
|
WHYF("Sending keepalives for call #%d",i);
|
2012-05-08 22:05:05 +00:00
|
|
|
|
2012-05-06 23:31:46 +00:00
|
|
|
/* And let far-end know that call is still alive */
|
2012-06-22 11:06:59 +00:00
|
|
|
snprintf(msg,sizeof(msg) -1,"\nKEEPALIVE:%06x\n", vomp_call_states[i].local.session);
|
|
|
|
for(m = 0;m < monitor_socket_count; m++)
|
|
|
|
WRITE_STR(monitor_sockets[m].socket,msg);
|
2012-05-06 23:31:46 +00:00
|
|
|
}
|
2012-05-02 17:30:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 16:58:39 +00:00
|
|
|
/* Check for new connections */
|
2012-06-22 11:06:59 +00:00
|
|
|
fcntl(monitor_named_socket, F_SETFL,
|
|
|
|
fcntl(monitor_named_socket, F_GETFL, NULL) | O_NONBLOCK);
|
|
|
|
/* We don't care about the peer's address */
|
2012-06-25 07:20:23 +00:00
|
|
|
ignored_length = 0;
|
2012-05-10 04:37:11 +00:00
|
|
|
while (
|
2012-05-03 03:59:48 +00:00
|
|
|
#ifdef HAVE_LINUX_IF_H
|
2012-06-25 07:20:23 +00:00
|
|
|
(s = accept4(monitor_named_socket, NULL, &ignored_length,O_NONBLOCK))
|
2012-05-03 03:59:48 +00:00
|
|
|
#else
|
2012-06-25 07:20:23 +00:00
|
|
|
(s = accept(monitor_named_socket,NULL, &ignored_length))
|
2012-05-03 03:59:48 +00:00
|
|
|
#endif
|
2012-05-10 04:37:11 +00:00
|
|
|
!= -1
|
|
|
|
) {
|
2012-06-25 07:20:23 +00:00
|
|
|
monitor_new_client(s);
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
2012-06-22 11:06:59 +00:00
|
|
|
if (errno != EAGAIN)
|
|
|
|
WHY_perror("accept");
|
2012-06-22 03:55:41 +00:00
|
|
|
}
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-06-22 03:55:41 +00:00
|
|
|
void monitor_client_poll(int fd)
|
|
|
|
{
|
2012-05-02 16:58:39 +00:00
|
|
|
/* Read from any open connections */
|
|
|
|
int i;
|
2012-06-22 11:06:59 +00:00
|
|
|
for(i = 0;i < monitor_socket_count; i++) {
|
2012-05-02 16:58:39 +00:00
|
|
|
nextInSameSlot:
|
|
|
|
errno=0;
|
|
|
|
int bytes;
|
2012-05-03 06:46:36 +00:00
|
|
|
struct monitor_context *c=&monitor_sockets[i];
|
2012-06-22 03:55:41 +00:00
|
|
|
if (c->socket!=fd) continue;
|
2012-05-03 03:34:17 +00:00
|
|
|
fcntl(c->socket,F_SETFL,
|
2012-06-22 11:06:59 +00:00
|
|
|
fcntl(c->socket, F_GETFL, NULL) | O_NONBLOCK);
|
2012-05-02 16:58:39 +00:00
|
|
|
switch(c->state) {
|
|
|
|
case MONITOR_STATE_COMMAND:
|
2012-06-22 11:06:59 +00:00
|
|
|
bytes = 1;
|
|
|
|
while(bytes == 1) {
|
|
|
|
if (c->line_length >= MONITOR_LINE_LENGTH) {
|
2012-05-02 16:58:39 +00:00
|
|
|
/* line too long */
|
2012-06-22 11:06:59 +00:00
|
|
|
c->line[MONITOR_LINE_LENGTH-1] = 0;
|
|
|
|
monitor_process_command(i, c->line);
|
|
|
|
bytes = -1;
|
2012-05-02 16:58:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-06-22 11:06:59 +00:00
|
|
|
bytes = read(c->socket, &c->line[c->line_length], 1);
|
|
|
|
if (bytes < 1) {
|
2012-05-02 16:58:39 +00:00
|
|
|
switch(errno) {
|
2012-05-10 14:47:02 +00:00
|
|
|
case EINTR:
|
|
|
|
case ENOTRECOVERABLE:
|
2012-05-02 16:58:39 +00:00
|
|
|
/* transient errors */
|
2012-05-10 14:47:02 +00:00
|
|
|
WHY_perror("read");
|
|
|
|
break;
|
|
|
|
case EAGAIN:
|
2012-05-03 17:46:06 +00:00
|
|
|
break;
|
2012-05-02 16:58:39 +00:00
|
|
|
default:
|
2012-05-10 14:47:02 +00:00
|
|
|
WHY_perror("read");
|
2012-05-02 16:58:39 +00:00
|
|
|
/* all other errors; close socket */
|
2012-05-05 09:58:31 +00:00
|
|
|
WHYF("Tearing down monitor client #%d due to errno=%d (%s)",
|
2012-05-08 22:05:05 +00:00
|
|
|
i,errno,strerror(errno)?strerror(errno):"<unknown error>");
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(c->socket);
|
2012-05-02 16:58:39 +00:00
|
|
|
if (i==monitor_socket_count-1) {
|
|
|
|
monitor_socket_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
bcopy(&monitor_sockets[monitor_socket_count-1],
|
|
|
|
&monitor_sockets[i],
|
|
|
|
sizeof(struct monitor_context));
|
|
|
|
monitor_socket_count--;
|
|
|
|
goto nextInSameSlot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-22 11:06:59 +00:00
|
|
|
if (bytes > 0 && (c->line[c->line_length] != '\r')) {
|
|
|
|
c->line_length += bytes;
|
|
|
|
if (c->line[c->line_length-1] == '\n') {
|
2012-05-03 17:46:06 +00:00
|
|
|
/* got command */
|
2012-06-22 11:06:59 +00:00
|
|
|
c->line[c->line_length-1] = 0; /* trim new line for easier parsing */
|
|
|
|
monitor_process_command(i, c->line);
|
2012-05-03 17:46:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MONITOR_STATE_DATA:
|
2012-06-22 11:06:59 +00:00
|
|
|
bytes = read(c->socket,
|
|
|
|
&c->buffer[c->data_offset],
|
|
|
|
c->data_expected-c->data_offset);
|
|
|
|
if (bytes < 1) {
|
2012-05-02 16:58:39 +00:00
|
|
|
switch(errno) {
|
|
|
|
case EAGAIN: case EINTR:
|
|
|
|
/* transient errors */
|
2012-05-03 17:46:06 +00:00
|
|
|
break;
|
2012-05-02 16:58:39 +00:00
|
|
|
default:
|
|
|
|
/* all other errors; close socket */
|
2012-05-03 07:32:57 +00:00
|
|
|
WHYF("Tearing down monitor client #%d due to errno=%d",
|
|
|
|
i,errno);
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(c->socket);
|
2012-05-02 16:58:39 +00:00
|
|
|
if (i==monitor_socket_count-1) {
|
|
|
|
monitor_socket_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
2012-06-22 11:06:59 +00:00
|
|
|
bcopy(&monitor_sockets[monitor_socket_count - 1],
|
2012-05-02 16:58:39 +00:00
|
|
|
&monitor_sockets[i],
|
|
|
|
sizeof(struct monitor_context));
|
|
|
|
monitor_socket_count--;
|
|
|
|
goto nextInSameSlot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-22 11:06:59 +00:00
|
|
|
c->data_offset += bytes;
|
|
|
|
if (c->data_offset >= c->data_expected)
|
2012-05-02 16:58:39 +00:00
|
|
|
{
|
|
|
|
/* we have the binary data we were expecting. */
|
|
|
|
monitor_process_data(i);
|
2012-06-22 11:06:59 +00:00
|
|
|
c->state = MONITOR_STATE_COMMAND;
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-05-03 06:46:36 +00:00
|
|
|
default:
|
2012-06-22 11:06:59 +00:00
|
|
|
c->state = MONITOR_STATE_COMMAND;
|
2012-05-03 06:46:36 +00:00
|
|
|
WHY("fixed monitor connection state");
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-06-22 03:55:41 +00:00
|
|
|
return;
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
2012-06-25 07:20:23 +00:00
|
|
|
|
|
|
|
static void monitor_new_client(int s) {
|
2012-06-22 11:06:59 +00:00
|
|
|
#ifdef linux
|
|
|
|
struct ucred ucred;
|
|
|
|
socklen_t len;
|
|
|
|
#else
|
|
|
|
gid_t othergid;
|
|
|
|
#endif
|
|
|
|
int res;
|
|
|
|
uid_t otheruid;
|
|
|
|
struct monitor_context *c;
|
|
|
|
|
|
|
|
#ifndef HAVE_LINUX_IF_H
|
|
|
|
if ((res = fcntl(s, F_SETFL, O_NONBLOCK)) == -1) {
|
|
|
|
WHY_perror("fcntl()");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
len = sizeof(ucred);
|
|
|
|
res = getsockopt(s, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
|
|
|
|
if (res) {
|
|
|
|
WHY_perror("getsockopt(SO_PEERCRED)");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (len < sizeof(ucred)) {
|
|
|
|
WHYF("getsockopt(SO_PEERCRED) returned the wrong size (Got %d expected %d)", len, sizeof(ucred));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
otheruid = ucred.uid;
|
|
|
|
#else
|
|
|
|
if (getpeereid(s, &otheruid, &othergid) != 0) {
|
|
|
|
WHY_perror("getpeereid()");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (otheruid != getuid()) {
|
|
|
|
WHYF("monitor.socket client has wrong uid (%d versus %d)", otheruid,getuid());
|
|
|
|
WRITE_STR(s, "\nCLOSE:Incorrect UID\n");
|
|
|
|
goto error;
|
|
|
|
} else if (monitor_socket_count >= MAX_MONITOR_SOCKETS
|
|
|
|
||monitor_socket_count < 0) {
|
|
|
|
WRITE_STR(s, "\nCLOSE:All sockets busy\n");
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
c = &monitor_sockets[monitor_socket_count];
|
|
|
|
c->socket = s;
|
|
|
|
c->line_length = 0;
|
|
|
|
c->state = MONITOR_STATE_COMMAND;
|
|
|
|
monitor_socket_count++;
|
|
|
|
WRITE_STR(s,"\nMONITOR:You are talking to servald\n");
|
2012-06-22 11:12:05 +00:00
|
|
|
INFOF("Got %d clients", monitor_socket_count);
|
2012-06-22 11:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(monitor_named_socket,F_SETFL,
|
|
|
|
fcntl(monitor_named_socket, F_GETFL, NULL)|O_NONBLOCK);
|
|
|
|
|
2012-06-26 02:50:49 +00:00
|
|
|
fd_watch(s, monitor_client_poll, POLL_IN);
|
|
|
|
|
2012-06-22 11:06:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-02 16:58:39 +00:00
|
|
|
int monitor_process_command(int index,char *cmd)
|
|
|
|
{
|
|
|
|
int callSessionToken,sampleType,bytes;
|
2012-05-03 17:46:06 +00:00
|
|
|
char sid[MONITOR_LINE_LENGTH],localDid[MONITOR_LINE_LENGTH];
|
2012-05-03 18:14:41 +00:00
|
|
|
char remoteDid[MONITOR_LINE_LENGTH],digits[MONITOR_LINE_LENGTH];
|
2012-05-02 17:30:34 +00:00
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
mdp.packetTypeAndFlags=MDP_VOMPEVENT;
|
2012-05-02 16:58:39 +00:00
|
|
|
|
|
|
|
struct monitor_context *c=&monitor_sockets[index];
|
|
|
|
c->line_length=0;
|
|
|
|
|
2012-05-03 03:34:17 +00:00
|
|
|
fcntl(c->socket,F_SETFL,
|
|
|
|
fcntl(c->socket, F_GETFL, NULL)|O_NONBLOCK);
|
|
|
|
|
2012-05-03 17:46:06 +00:00
|
|
|
if (strlen(cmd)>MONITOR_LINE_LENGTH) {
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(c->socket,"\nERROR:Command too long\n");
|
2012-05-02 17:30:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-03 17:54:53 +00:00
|
|
|
char msg[1024];
|
2012-05-10 22:30:09 +00:00
|
|
|
int flag;
|
2012-05-03 17:54:53 +00:00
|
|
|
|
2012-05-03 18:14:41 +00:00
|
|
|
if (cmd[0]=='*') {
|
|
|
|
/* command with content */
|
|
|
|
int ofs=0;
|
|
|
|
if (sscanf(cmd,"*%d:%n",&bytes,&ofs)==1) {
|
|
|
|
/* work out rest of command */
|
|
|
|
cmd=&cmd[ofs];
|
2012-05-02 16:58:39 +00:00
|
|
|
c->state=MONITOR_STATE_DATA;
|
|
|
|
c->data_expected=bytes;
|
|
|
|
c->data_offset=0;
|
2012-05-03 18:14:41 +00:00
|
|
|
c->sample_codec=-1;
|
|
|
|
|
|
|
|
if (sscanf(cmd,"AUDIO:%x:%d",
|
|
|
|
&callSessionToken,&sampleType)==2)
|
|
|
|
{
|
|
|
|
/* Start getting sample */
|
|
|
|
c->sample_call_session_token=callSessionToken;
|
|
|
|
c->sample_codec=sampleType;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-02 16:58:39 +00:00
|
|
|
}
|
2012-05-03 18:14:41 +00:00
|
|
|
}
|
2012-05-02 16:58:39 +00:00
|
|
|
else if (!strcasecmp(cmd,"monitor vomp"))
|
|
|
|
c->flags|=MONITOR_VOMP;
|
|
|
|
else if (!strcasecmp(cmd,"ignore vomp"))
|
|
|
|
c->flags&=~MONITOR_VOMP;
|
|
|
|
else if (!strcasecmp(cmd,"monitor rhizome"))
|
|
|
|
c->flags|=MONITOR_RHIZOME;
|
|
|
|
else if (!strcasecmp(cmd,"ignore rhizome"))
|
|
|
|
c->flags&=~MONITOR_RHIZOME;
|
2012-05-21 03:34:54 +00:00
|
|
|
else if (!strcasecmp(cmd,"monitor peers"))
|
|
|
|
c->flags|=MONITOR_PEERS;
|
|
|
|
else if (!strcasecmp(cmd,"ignore peers"))
|
|
|
|
c->flags&=~MONITOR_PEERS;
|
2012-05-10 22:30:09 +00:00
|
|
|
else if (sscanf(cmd,"FASTAUDIO:%x:%d",&callSessionToken,&flag)==2)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<vomp_call_count;i++)
|
|
|
|
if (vomp_call_states[i].local.session==callSessionToken
|
|
|
|
||callSessionToken==0) {
|
|
|
|
vomp_call_states[i].fast_audio=flag;
|
|
|
|
vomp_call_states[i].local.last_state=-1;
|
|
|
|
monitor_call_status(&vomp_call_states[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-05-03 17:46:06 +00:00
|
|
|
else if (sscanf(cmd,"call %s %s %s",sid,localDid,remoteDid)==3) {
|
2012-05-14 06:22:15 +00:00
|
|
|
WHY("here");
|
2012-05-08 05:02:13 +00:00
|
|
|
if (sid[0]=='*') {
|
|
|
|
/* For testing, pick a peer and call them */
|
|
|
|
int bin,slot;
|
|
|
|
for(bin=0;bin<overlay_bin_count;bin++)
|
|
|
|
for(slot=0;slot<overlay_bin_size;slot++)
|
|
|
|
{
|
|
|
|
if (!overlay_nodes[bin][slot].sid[0])
|
|
|
|
{
|
|
|
|
continue; }
|
|
|
|
strcpy(sid,overlay_render_sid(overlay_nodes[bin][slot].sid));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 17:30:34 +00:00
|
|
|
mdp.vompevent.flags=VOMPEVENT_DIAL;
|
2012-05-03 17:54:53 +00:00
|
|
|
int cn=0,in=0,kp=0;
|
|
|
|
if(!keyring_next_identity(keyring,&cn,&in,&kp))
|
|
|
|
{
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(c->socket,"\nERROR:no local identity, so cannot place call\n");
|
2012-05-03 17:54:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
bcopy(keyring->contexts[cn]->identities[in]
|
|
|
|
->keypairs[kp]->public_key,
|
|
|
|
&mdp.vompevent.local_sid[0],SID_SIZE);
|
|
|
|
stowSid(&mdp.vompevent.remote_sid[0],0,sid);
|
|
|
|
vomp_mdp_event(&mdp,NULL,0);
|
|
|
|
}
|
2012-05-14 06:22:15 +00:00
|
|
|
WHY("here");
|
2012-05-02 17:30:34 +00:00
|
|
|
}
|
2012-05-06 23:01:53 +00:00
|
|
|
else if (sscanf(cmd,"status %x",&callSessionToken)==1) {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<vomp_call_count;i++)
|
|
|
|
if (vomp_call_states[i].local.session==callSessionToken
|
|
|
|
||callSessionToken==0) {
|
|
|
|
vomp_call_states[i].local.last_state=0;
|
|
|
|
monitor_call_status(&vomp_call_states[i]);
|
|
|
|
}
|
|
|
|
} else if (sscanf(cmd,"pickup %x",&callSessionToken)==1) {
|
2012-05-02 17:30:34 +00:00
|
|
|
mdp.vompevent.flags=VOMPEVENT_PICKUP;
|
|
|
|
mdp.vompevent.call_session_token=callSessionToken;
|
|
|
|
vomp_mdp_event(&mdp,NULL,0);
|
|
|
|
}
|
2012-05-03 17:46:06 +00:00
|
|
|
else if (sscanf(cmd,"hangup %x",&callSessionToken)==1) {
|
2012-05-02 17:30:34 +00:00
|
|
|
mdp.vompevent.flags=VOMPEVENT_HANGUP;
|
|
|
|
mdp.vompevent.call_session_token=callSessionToken;
|
|
|
|
vomp_mdp_event(&mdp,NULL,0);
|
2012-05-03 18:14:41 +00:00
|
|
|
} else if (sscanf(cmd,"dtmf %x %s",&callSessionToken,digits)==2) {
|
|
|
|
mdp.vompevent.flags=VOMPEVENT_AUDIOPACKET;
|
|
|
|
mdp.vompevent.call_session_token=callSessionToken;
|
|
|
|
|
|
|
|
/* One digit per sample block. */
|
|
|
|
mdp.vompevent.audio_sample_codec=VOMP_CODEC_DTMF;
|
|
|
|
mdp.vompevent.audio_sample_bytes=1;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i=0;i<strlen(digits);i++) {
|
|
|
|
int digit=vomp_parse_dtmf_digit(digits[i]);
|
|
|
|
if (digit<0) {
|
2012-05-08 03:25:57 +00:00
|
|
|
snprintf(msg,1024,"\nERROR: invalid DTMF digit 0x%02x\n",digit);
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(c->socket,msg);
|
2012-05-03 18:14:41 +00:00
|
|
|
}
|
|
|
|
mdp.vompevent.audio_bytes[mdp.vompevent.audio_sample_bytes]
|
|
|
|
=(digit<<4); /* 80ms standard tone duration, so that it is a multiple
|
|
|
|
of the majority of codec time units (70ms is the nominal
|
|
|
|
DTMF tone length for most systems). */
|
|
|
|
if (overlay_mdp_send(&mdp,0,0)) WHY("Send DTMF failed.");
|
|
|
|
}
|
|
|
|
|
2012-05-02 17:30:34 +00:00
|
|
|
}
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-05-03 03:34:17 +00:00
|
|
|
fcntl(c->socket,F_SETFL,
|
|
|
|
fcntl(c->socket, F_GETFL, NULL)|O_NONBLOCK);
|
|
|
|
|
2012-05-08 03:25:57 +00:00
|
|
|
snprintf(msg,1024,"\nMONITORSTATUS:%d\n",c->flags);
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(c->socket,msg);
|
2012-05-02 16:58:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int monitor_process_data(int index)
|
|
|
|
{
|
|
|
|
/* Called when we have received an entire data sample */
|
|
|
|
struct monitor_context *c=&monitor_sockets[index];
|
|
|
|
c->state=MONITOR_STATE_COMMAND;
|
|
|
|
|
|
|
|
if (vomp_sample_size(c->sample_codec)!=c->data_offset)
|
2012-05-07 07:20:49 +00:00
|
|
|
return
|
2012-06-18 00:07:24 +00:00
|
|
|
WHYF("Ignoring sample block of incorrect size (expected %d, got %d bytes for codec %d)",
|
|
|
|
vomp_sample_size(c->sample_codec), c->data_offset, c->sample_codec);
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-05-03 03:34:17 +00:00
|
|
|
fcntl(c->socket,F_SETFL,
|
|
|
|
fcntl(c->socket, F_GETFL, NULL)|O_NONBLOCK);
|
|
|
|
|
2012-05-02 16:58:39 +00:00
|
|
|
vomp_call_state *call=vomp_find_call_by_session(c->sample_call_session_token);
|
|
|
|
if (!call) {
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(c->socket,"\nERROR:No such call\n");
|
2012-05-02 16:58:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
mdp.packetTypeAndFlags=MDP_VOMPEVENT;
|
|
|
|
mdp.vompevent.flags=VOMPEVENT_AUDIOPACKET;
|
|
|
|
mdp.vompevent.call_session_token=c->sample_call_session_token;
|
|
|
|
mdp.vompevent.audio_sample_codec=c->sample_codec;
|
|
|
|
bcopy(&c->buffer[0],&mdp.vompevent.audio_bytes[0],
|
|
|
|
vomp_sample_size(c->sample_codec));
|
2012-05-06 12:53:58 +00:00
|
|
|
mdp.vompevent.audio_sample_bytes=vomp_sample_size(c->sample_codec);
|
2012-05-02 16:58:39 +00:00
|
|
|
|
2012-05-06 12:53:58 +00:00
|
|
|
if (overlay_mdp_send(&mdp,0,0)) WHY("Send audio failed.");
|
2012-05-02 16:58:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-02 17:30:34 +00:00
|
|
|
|
2012-05-11 21:54:52 +00:00
|
|
|
int monitor_announce_bundle(rhizome_manifest *m)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char msg[1024];
|
2012-05-23 06:34:00 +00:00
|
|
|
const char *service = rhizome_manifest_get(m, "service", NULL, 0);
|
|
|
|
const char *sender = rhizome_manifest_get(m, "sender", NULL, 0);
|
|
|
|
const char *recipient = rhizome_manifest_get(m, "recipient", NULL, 0);
|
|
|
|
snprintf(msg,1024,"\nBUNDLE:%s:%s:%lld:%lld:%s:%s:%s\n",
|
|
|
|
/* XXX bit of a hack here, since SIDs and cryptosign public keys have the same length */
|
2012-05-11 21:54:52 +00:00
|
|
|
overlay_render_sid(m->cryptoSignPublic),
|
2012-05-23 06:34:00 +00:00
|
|
|
service ? service : "",
|
2012-05-11 21:54:52 +00:00
|
|
|
m->version,
|
|
|
|
m->fileLength,
|
2012-05-23 06:34:00 +00:00
|
|
|
sender,
|
|
|
|
recipient,
|
2012-05-11 21:54:52 +00:00
|
|
|
m->dataFileName?m->dataFileName:"");
|
|
|
|
for(i=0;i<monitor_socket_count;i++)
|
|
|
|
{
|
|
|
|
if (!(monitor_sockets[i].flags&MONITOR_RHIZOME))
|
|
|
|
continue;
|
|
|
|
nextInSameSlot:
|
|
|
|
errno=0;
|
|
|
|
fcntl(monitor_sockets[i].socket,F_SETFL,
|
|
|
|
fcntl(monitor_sockets[i].socket, F_GETFL, NULL)|O_NONBLOCK);
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(monitor_sockets[i].socket,msg);
|
2012-05-11 21:54:52 +00:00
|
|
|
if (errno&&(errno!=EINTR)&&(errno!=EAGAIN)) {
|
|
|
|
/* error sending update, so kill monitor socket */
|
|
|
|
WHYF("Tearing down monitor client #%d due to errno=%d",
|
|
|
|
i,errno);
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(monitor_sockets[i].socket);
|
2012-05-11 21:54:52 +00:00
|
|
|
if (i==monitor_socket_count-1) {
|
|
|
|
monitor_socket_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
bcopy(&monitor_sockets[monitor_socket_count-1],
|
|
|
|
&monitor_sockets[i],
|
|
|
|
sizeof(struct monitor_context));
|
|
|
|
monitor_socket_count--;
|
|
|
|
goto nextInSameSlot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-02 17:30:34 +00:00
|
|
|
int monitor_call_status(vomp_call_state *call)
|
|
|
|
{
|
2012-05-02 18:07:03 +00:00
|
|
|
int i;
|
|
|
|
char msg[1024];
|
2012-05-03 18:28:34 +00:00
|
|
|
int show=0;
|
|
|
|
if (call->local.state>call->local.last_state) show=1;
|
|
|
|
if (call->remote.state>call->remote.last_state) show=1;
|
|
|
|
call->local.last_state=call->local.state;
|
|
|
|
call->remote.last_state=call->remote.state;
|
|
|
|
if (show) {
|
2012-05-06 23:01:53 +00:00
|
|
|
if (0) WHYF("sending call status to monitor");
|
2012-05-10 22:30:09 +00:00
|
|
|
snprintf(msg,1024,"\nCALLSTATUS:%06x:%06x:%d:%d:%d:%s:%s:%s:%s\n",
|
2012-05-03 18:28:34 +00:00
|
|
|
call->local.session,call->remote.session,
|
2012-05-04 18:28:02 +00:00
|
|
|
call->local.state,call->remote.state,
|
2012-05-10 22:30:09 +00:00
|
|
|
call->fast_audio,
|
2012-05-04 18:28:02 +00:00
|
|
|
overlay_render_sid(call->local.sid),
|
|
|
|
overlay_render_sid(call->remote.sid),
|
|
|
|
call->local.did,call->remote.did);
|
2012-05-08 22:05:05 +00:00
|
|
|
msg[1023]=0;
|
2012-05-03 18:28:34 +00:00
|
|
|
for(i=0;i<monitor_socket_count;i++)
|
|
|
|
{
|
|
|
|
if (!(monitor_sockets[i].flags&MONITOR_VOMP))
|
2012-05-02 18:07:03 +00:00
|
|
|
continue;
|
2012-05-03 18:28:34 +00:00
|
|
|
nextInSameSlot:
|
|
|
|
errno=0;
|
|
|
|
fcntl(monitor_sockets[i].socket,F_SETFL,
|
|
|
|
fcntl(monitor_sockets[i].socket, F_GETFL, NULL)|O_NONBLOCK);
|
2012-06-22 11:06:59 +00:00
|
|
|
WRITE_STR(monitor_sockets[i].socket,msg);
|
2012-05-03 18:28:34 +00:00
|
|
|
if (errno&&(errno!=EINTR)&&(errno!=EAGAIN)) {
|
|
|
|
/* error sending update, so kill monitor socket */
|
|
|
|
WHYF("Tearing down monitor client #%d due to errno=%d",
|
|
|
|
i,errno);
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(monitor_sockets[i].socket);
|
2012-05-03 18:28:34 +00:00
|
|
|
if (i==monitor_socket_count-1) {
|
|
|
|
monitor_socket_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
bcopy(&monitor_sockets[monitor_socket_count-1],
|
|
|
|
&monitor_sockets[i],
|
|
|
|
sizeof(struct monitor_context));
|
|
|
|
monitor_socket_count--;
|
|
|
|
goto nextInSameSlot;
|
|
|
|
}
|
2012-05-02 18:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 18:28:34 +00:00
|
|
|
}
|
2012-05-02 17:30:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:50:18 +00:00
|
|
|
int monitor_announce_peer(unsigned char *sid)
|
|
|
|
{
|
2012-06-25 05:30:07 +00:00
|
|
|
char msg[1024];
|
|
|
|
int n = snprintf(msg, sizeof msg, "\nNEWPEER:%s\n",overlay_render_sid(sid));
|
|
|
|
monitor_tell_clients(msg, n, MONITOR_PEERS);
|
2012-05-18 10:50:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-02 17:30:34 +00:00
|
|
|
int monitor_send_audio(vomp_call_state *call,overlay_mdp_frame *audio)
|
|
|
|
{
|
2012-05-08 22:05:05 +00:00
|
|
|
if (0) WHYF("Tell call monitor about audio for call %06x:%06x",
|
|
|
|
call->local.session,call->remote.session);
|
2012-05-02 18:07:03 +00:00
|
|
|
int sample_bytes=vomp_sample_size(audio->vompevent.audio_sample_codec);
|
2012-06-25 05:30:07 +00:00
|
|
|
char msg[1024 + MAX_AUDIO_BYTES];
|
2012-05-03 18:14:41 +00:00
|
|
|
/* All commands followed by binary data start with *len:, so that
|
2012-05-08 03:25:57 +00:00
|
|
|
they can be easily parsed at the far end, even if not supported.
|
|
|
|
Put newline at start of these so that receiving data in command
|
|
|
|
mode doesn't confuse the parser. */
|
2012-06-25 05:30:07 +00:00
|
|
|
int msglen = snprintf(msg, 1024,
|
2012-05-08 03:25:57 +00:00
|
|
|
"\n*%d:AUDIOPACKET:%06x:%06x:%d:%d:%d:%lld:%lld\n",
|
2012-05-03 18:14:41 +00:00
|
|
|
sample_bytes,
|
2012-05-02 18:07:03 +00:00
|
|
|
call->local.session,call->remote.session,
|
|
|
|
call->local.state,call->remote.state,
|
|
|
|
audio->vompevent.audio_sample_codec,
|
|
|
|
audio->vompevent.audio_sample_starttime,
|
|
|
|
audio->vompevent.audio_sample_endtime);
|
2012-06-25 05:30:07 +00:00
|
|
|
bcopy(&audio->vompevent.audio_bytes[0], &msg[msglen], sample_bytes);
|
2012-05-02 18:07:03 +00:00
|
|
|
msglen+=sample_bytes;
|
2012-06-26 05:04:39 +00:00
|
|
|
msg[msglen++]='\n';
|
2012-06-25 05:30:07 +00:00
|
|
|
monitor_tell_clients(msg, msglen, MONITOR_VOMP);
|
2012-05-18 10:50:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-25 05:30:07 +00:00
|
|
|
int monitor_tell_clients(char *msg, int msglen, int mask)
|
2012-05-18 10:50:18 +00:00
|
|
|
{
|
2012-05-02 18:07:03 +00:00
|
|
|
int i;
|
|
|
|
for(i=0;i<monitor_socket_count;i++)
|
|
|
|
{
|
2012-05-21 03:17:19 +00:00
|
|
|
if (!(monitor_sockets[i].flags&mask))
|
2012-05-02 18:11:26 +00:00
|
|
|
continue;
|
2012-05-02 18:07:03 +00:00
|
|
|
nextInSameSlot:
|
|
|
|
errno=0;
|
2012-05-03 03:34:17 +00:00
|
|
|
fcntl(monitor_sockets[i].socket,F_SETFL,
|
|
|
|
fcntl(monitor_sockets[i].socket, F_GETFL, NULL)|O_NONBLOCK);
|
2012-06-26 05:04:39 +00:00
|
|
|
write(monitor_sockets[i].socket, msg, msglen);
|
2012-05-08 22:05:05 +00:00
|
|
|
// WHYF("Writing AUDIOPACKET to client");
|
2012-05-02 18:07:03 +00:00
|
|
|
if (errno&&(errno!=EINTR)&&(errno!=EAGAIN)) {
|
|
|
|
/* error sending update, so kill monitor socket */
|
2012-05-03 07:32:57 +00:00
|
|
|
WHYF("Tearing down monitor client #%d due to errno=%d",
|
|
|
|
i,errno);
|
2012-06-22 03:55:41 +00:00
|
|
|
fd_teardown(monitor_sockets[i].socket);
|
2012-05-02 18:07:03 +00:00
|
|
|
if (i==monitor_socket_count-1) {
|
|
|
|
monitor_socket_count--;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
bcopy(&monitor_sockets[monitor_socket_count-1],
|
|
|
|
&monitor_sockets[i],
|
|
|
|
sizeof(struct monitor_context));
|
|
|
|
monitor_socket_count--;
|
|
|
|
goto nextInSameSlot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 17:30:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|