diff --git a/linphone/console/shell.c b/linphone/console/shell.c new file mode 100644 index 000000000..80ee23b29 --- /dev/null +++ b/linphone/console/shell.c @@ -0,0 +1,158 @@ +/**************************************************************************** + * Copyright (C) 2009 Simon MORLAT + * + **************************************************************************** + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ****************************************************************************/ + + +#include +#include + + +#ifdef WIN32 +#include +#else +#include +#include +#endif + +#include "ortp/ortp.h" + +#define DEFAULT_TCP_PORT "32333" +#define DEFAULT_REPLY_SIZE 4096 + +static int send_command(const char *command, const char * port, char *reply, int reply_len, int print_errors){ + ortp_socket_t sock; + struct addrinfo *ai=NULL; + struct addrinfo hints; + int err; + int i; + memset(&hints,0,sizeof(hints)); + hints.ai_family=AF_INET; + hints.ai_socktype=SOCK_STREAM; + err=getaddrinfo("127.0.0.1",port,&hints,&ai); + if (err!=0){ + if (print_errors) fprintf(stderr,"ERROR: getaddrinfo failed: error %i\n", err); + return -1; + } + sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); + if (connect(sock,ai->ai_addr,ai->ai_addrlen)!=0){ + if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket.\n"); + freeaddrinfo(ai); + return -1; + } + freeaddrinfo(ai); + if (send(sock,command,strlen(command),0)<0){ + if (print_errors) fprintf(stderr,"ERROR: Fail to send command to remote linphonec\n"); + close_socket(sock); + return -1; + } + /*wait for replies */ + i=0; + while ((err=recv(sock,&reply[i],reply_len-i-1,0))>0){ + i+=err; + } + reply[i]='\0'; + close_socket(sock); + return 0; +} + +static void print_usage(void){ + fprintf(stderr,"Usage:\nlinphonecsh [arguments]\n" + "where action is one of\n" + "\tinit : spawn a linphonec daemon (first step to make other actions)\n" + "\t\tfollowed by the arguments sent to linphonec\n" + "\tgeneric : sends a generic command to the running linphonec daemon\n" + "\t\tfollowed by the generic command surrounded by quotes, for example \"call sip:joe@example.net\"\n" + "\tregister : register with specified proxy\n"); + exit(-1); +} + +#define MAX_ARGS 10 + +static void spawn_linphonec(int argc, char *argv[]){ + char * args[10]; + int i,j; + pid_t pid; + j=0; + args[j++]="linphonec"; + args[j++]="--tcp"; + args[j++]=DEFAULT_TCP_PORT; + for(i=0;i