mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-30 16:13:51 +00:00
Outbound gateway now works with batphone on android with asterisk hooks.
This commit is contained in:
parent
7e3e6201ed
commit
ba758f71ca
83
gateway.c
83
gateway.c
@ -1,8 +1,12 @@
|
|||||||
#include "mphlr.h"
|
#include "mphlr.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
char *asterisk_extensions_conf="/data/data/org.servalproject/var/extensions_dna.conf";
|
char *asterisk_extensions_conf="/data/data/org.servalproject/etc/asterisk/gatewayextensions.conf";
|
||||||
char *asterisk_binary="/data/data/org.servalproject/sbin/asterisk";
|
char *asterisk_binary="/data/data/org.servalproject/sbin/asterisk";
|
||||||
char *temp_file="/data/data/org.servalproject/var/temp.out";
|
char *temp_file="/data/data/org.servalproject/var/temp.out";
|
||||||
|
char *cmd_file="/data/data/org.servalproject/var/temp.cmd";
|
||||||
|
char *shell="/system/bin/sh";
|
||||||
|
|
||||||
typedef struct dna_gateway_extension {
|
typedef struct dna_gateway_extension {
|
||||||
char did[64];
|
char did[64];
|
||||||
@ -16,6 +20,43 @@ typedef struct dna_gateway_extension {
|
|||||||
#define MAX_CURRENT_EXTENSIONS 1024
|
#define MAX_CURRENT_EXTENSIONS 1024
|
||||||
dna_gateway_extension extensions[MAX_CURRENT_EXTENSIONS];
|
dna_gateway_extension extensions[MAX_CURRENT_EXTENSIONS];
|
||||||
|
|
||||||
|
int safeSystem(char *cmd_file)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
int pid = fork();
|
||||||
|
|
||||||
|
if(pid == -1) {
|
||||||
|
fprintf(stderr, "Unable to fork.\n");
|
||||||
|
return -1;
|
||||||
|
} else if(pid == 0) {
|
||||||
|
execlp(shell, shell, "-c", cmd_file,NULL);
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
// Wait for child to finish
|
||||||
|
int status;
|
||||||
|
wait(&status);
|
||||||
|
return WEXITSTATUS(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int runCommand(char *cmd)
|
||||||
|
{
|
||||||
|
FILE *f=fopen(cmd_file,"w");
|
||||||
|
if (!f) {
|
||||||
|
if (debug) fprintf(stderr,"%s:%d: Could not write to command file '%s'.\n",__FUNCTION__,__LINE__,cmd_file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
fprintf(f,"#!%s\n%s\n",shell,cmd);
|
||||||
|
fclose(f);
|
||||||
|
if (chmod(cmd_file,0000700))
|
||||||
|
{
|
||||||
|
if (debug) fprintf(stderr,"%s:%d: Could not chmod command file '%s'.\n",__FUNCTION__,__LINE__,cmd_file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return safeSystem(cmd_file);
|
||||||
|
}
|
||||||
|
|
||||||
int gatewayReadSettings(char *file)
|
int gatewayReadSettings(char *file)
|
||||||
{
|
{
|
||||||
char line[1024];
|
char line[1024];
|
||||||
@ -35,6 +76,12 @@ int gatewayReadSettings(char *file)
|
|||||||
line[0]=0; fgets(line,1024,f);
|
line[0]=0; fgets(line,1024,f);
|
||||||
temp_file=strdup(line);
|
temp_file=strdup(line);
|
||||||
|
|
||||||
|
/* Temporary file for running asterisk commands (since system() cannot do redirection on android) */
|
||||||
|
line[0]=0; fgets(line,1024,f);
|
||||||
|
cmd_file=strdup(line);
|
||||||
|
|
||||||
|
line[0]=0; fgets(line,1024,f);
|
||||||
|
shell=strdup(line);
|
||||||
|
|
||||||
/* XXX Need more here I suspect */
|
/* XXX Need more here I suspect */
|
||||||
|
|
||||||
@ -63,12 +110,12 @@ int asteriskCreateExtension(char *requestor_sid,char *did,char *uri_out)
|
|||||||
int index=random()%MAX_CURRENT_EXTENSIONS;
|
int index=random()%MAX_CURRENT_EXTENSIONS;
|
||||||
|
|
||||||
bcopy(requestor_sid,extensions[index].requestor_sid,SID_SIZE);
|
bcopy(requestor_sid,extensions[index].requestor_sid,SID_SIZE);
|
||||||
strcpy(did,extensions[index].did);
|
strcpy(extensions[index].did,did);
|
||||||
extensions[index].created=time(0);
|
extensions[index].created=time(0);
|
||||||
extensions[index].expires=time(0)+3600;
|
extensions[index].expires=time(0)+3600;
|
||||||
snprintf(extensions[index].uri,sizeof(extensions[index].uri),"4101*%08x%08x%08x@%s",
|
snprintf(extensions[index].uri,sizeof(extensions[index].uri),"4101*%08x%04x@",
|
||||||
(unsigned int)random(),(unsigned int)random(),(unsigned int)random(),gatewayuri);
|
(unsigned int)random(),(unsigned int)random()&0xffff);
|
||||||
extensions[index].uriprefixlen=strlen(extensions[index].uri)-strlen(gatewayuri)-1;
|
extensions[index].uriprefixlen=strlen(extensions[index].uri)-1;
|
||||||
if (extensions[index].uriprefixlen<0) {
|
if (extensions[index].uriprefixlen<0) {
|
||||||
/* Whoops - something wrong with the extension/uri, so kill the record and fail. */
|
/* Whoops - something wrong with the extension/uri, so kill the record and fail. */
|
||||||
extensions[index].expires=1;
|
extensions[index].expires=1;
|
||||||
@ -77,6 +124,7 @@ int asteriskCreateExtension(char *requestor_sid,char *did,char *uri_out)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (debug) fprintf(stderr,"Created extension '%s' to dial %s\n",extensions[index].uri,did);
|
if (debug) fprintf(stderr,"Created extension '%s' to dial %s\n",extensions[index].uri,did);
|
||||||
|
strcpy(uri_out,extensions[index].uri);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -106,8 +154,8 @@ int asteriskWriteExtensions()
|
|||||||
{
|
{
|
||||||
extensions[i].uri[extensions[i].uriprefixlen]=0;
|
extensions[i].uri[extensions[i].uriprefixlen]=0;
|
||||||
fprintf(out,
|
fprintf(out,
|
||||||
"exten => _%s., 1, Dial(SIP/sdnagatewayout/%s)\n"
|
"exten => %s, 1, Dial(SIP/dnagateway/%s)\n"
|
||||||
"exten => _%s., 2, Hangup()\n",
|
"exten => %s, 2, Hangup()\n",
|
||||||
extensions[i].uri,
|
extensions[i].uri,
|
||||||
extensions[i].did,
|
extensions[i].did,
|
||||||
extensions[i].uri);
|
extensions[i].uri);
|
||||||
@ -122,14 +170,14 @@ int asteriskWriteExtensions()
|
|||||||
int asteriskReloadExtensions()
|
int asteriskReloadExtensions()
|
||||||
{
|
{
|
||||||
char cmd[8192];
|
char cmd[8192];
|
||||||
snprintf(cmd,8192,"%s -rx \"dialplan reload\"",asterisk_binary);
|
snprintf(cmd,8192,"%s -rx 'dialplan reload'",asterisk_binary);
|
||||||
if (system(cmd))
|
if (runCommand(cmd))
|
||||||
{
|
{
|
||||||
if (debug) fprintf(stderr,"%s:%d: Dialplan reload failed.\n",__FUNCTION__,__LINE__);
|
if (debug) fprintf(stderr,"%s:%d: Dialplan reload failed.\n",__FUNCTION__,__LINE__);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else
|
if (debug) fprintf(stderr,"%s:%d: Dialplan reload appeared to succeed.\n",__FUNCTION__,__LINE__);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int asteriskGatewayUpP()
|
int asteriskGatewayUpP()
|
||||||
@ -149,11 +197,17 @@ int asteriskGatewayUpP()
|
|||||||
being ours, then we can ignore the hostname.
|
being ours, then we can ignore the hostname.
|
||||||
*/
|
*/
|
||||||
char cmd[8192];
|
char cmd[8192];
|
||||||
snprintf(cmd,8192,"%s -rx \"sip show registry\" > %s",asterisk_binary,temp_file);
|
|
||||||
system(cmd);
|
unlink(temp_file);
|
||||||
|
snprintf(cmd,8192,"%s -rx 'sip show registry' > %s",asterisk_binary,temp_file);
|
||||||
|
|
||||||
|
if (runCommand(cmd)) {
|
||||||
|
if (debug) { fprintf(stderr,"%s:%d: system(%s) might have failed.\n",__FUNCTION__,__LINE__,cmd_file);
|
||||||
|
perror("system"); }
|
||||||
|
}
|
||||||
FILE *f=fopen(temp_file,"r");
|
FILE *f=fopen(temp_file,"r");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (debug) fprintf(stderr,"%s:%d: Could not read result from \"sip show registry\".\n",__FUNCTION__,__LINE__);
|
if (debug) fprintf(stderr,"%s:%d: Could not read result of \"sip show registry\" from '%s'.\n",__FUNCTION__,__LINE__,temp_file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,5 +264,6 @@ int asteriskObtainGateway(char *requestor_sid,char *did,char *uri_out)
|
|||||||
if (debug) fprintf(stderr,"asteriskReloadExtensions() failed, so not offering gateway.\n");
|
if (debug) fprintf(stderr,"asteriskReloadExtensions() failed, so not offering gateway.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (debug) fprintf(stderr,"asteriskReloadExtensions() suceeded, offering gateway.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user