/**************************************************************************** * 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 #include #include #include #include #else #include #include #include #endif #include "ortp/ortp.h" #define DEFAULT_TCP_PORT "32333" #define DEFAULT_REPLY_SIZE 4096 #define STATUS_REGISTERED (1<<0) #define STATUS_REGISTERING (1<<1) #define STATUS_DIALING (1<<2) #define STATUS_AUTOANSWER (1<<3) #define STATUS_IN_CONNECTED (1<<4) /* incoming call accepted */ #define STATUS_OUT_CONNECTED (1<<5) /*outgoing call accepted */ #ifndef WIN32 static int tcp=0; #else static int tcp=1; #endif static int make_status_value(const char *status_string){ int ret=0; if (strstr(status_string,"registered, identity=")){ ret|=STATUS_REGISTERED; } if (strstr(status_string,"registered=-1")){ ret|=STATUS_REGISTERING; } if (strstr(status_string,"autoanswer=1")){ ret|=STATUS_AUTOANSWER; } if (strstr(status_string,"dialing")){ ret|=STATUS_DIALING; } if (strstr(status_string,"Call out")){ ret|=STATUS_OUT_CONNECTED; } if (strstr(status_string,"hook=answered")){ ret|=STATUS_IN_CONNECTED; } return ret; } static int send_command(const char *command, const char * port, char *reply, int reply_len, int print_errors){ ortp_socket_t sock; int i; int err; if (tcp){ struct addrinfo *ai=NULL; struct addrinfo hints; 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); }else{ #ifndef WIN32 struct sockaddr_un sa; char path[128]; sock=socket(AF_UNIX,SOCK_STREAM,0); sa.sun_family=AF_UNIX; snprintf(path,sizeof(path)-1,"/tmp/linphonec-%i",getuid()); strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){ if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket: %s\n",getSocketError()); return -1; } #else fprintf(stderr,"ERROR: windows pipes communication not yet implemented.\n"); return -1; #endif } 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\t\t: spawn a linphonec daemon (first step to make other actions)\n" "\t\t\tfollowed by the arguments sent to linphonec\n" "\tgeneric\t\t: sends a generic command to the running linphonec daemon\n" "\t\t\tfollowed by the generic command surrounded by quotes,\n\t\t\t for example \"call sip:joe@example.net\"\n" "\tregister\t: register; arguments are \n\t\t\t--host \n\t\t\t--username \n\t\t\t--password \n" "\tunregister\t: unregister\n" "\tdial\t\t: dial \n" "\tstatus\t\t: can be 'status register', 'status autoanswer' or 'status hook'\n" "\texit\t\t: make the linphonec daemon to exit.\n" ); exit(-1); } #define MAX_ARGS 10 #ifndef WIN32 static void spawn_linphonec(int argc, char *argv[]){ char * args[10]; int i,j; pid_t pid; j=0; args[j++]="linphonec"; if (tcp){ args[j++]="--tcp"; args[j++]=DEFAULT_TCP_PORT; }else args[j++]="--pipe"; args[j++]="-c"; args[j++]="/dev/null"; for(i=0;i