source: mediastreamer2/linphone/console/shell.c @ 240:02d6c792d6e6

Last change on this file since 240:02d6c792d6e6 was 240:02d6c792d6e6, checked in by smorlat <smorlat@…>, 4 years ago

merge g722 stuff for msrtp.c
wip for linphonecsh

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@243 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 5.5 KB
Line 
1/****************************************************************************
2 *  Copyright (C) 2009  Simon MORLAT <simon.morlat@linphone.org>
3 *
4 ****************************************************************************
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 ****************************************************************************/
21
22
23#include <stdio.h>
24#include <stdlib.h>
25
26
27#ifdef WIN32
28#include <ws2tcpip.h>
29#else
30#include <sys/socket.h>
31#include <netdb.h>
32#endif
33
34#include "ortp/ortp.h"
35
36#define DEFAULT_TCP_PORT "32333"
37#define DEFAULT_REPLY_SIZE 4096
38
39static int send_command(const char *command, const char * port, char *reply, int reply_len, int print_errors){
40        ortp_socket_t sock;
41        struct addrinfo *ai=NULL;
42        struct addrinfo hints;
43        int err;
44        int i;
45        memset(&hints,0,sizeof(hints));
46        hints.ai_family=AF_INET;
47        hints.ai_socktype=SOCK_STREAM;
48        err=getaddrinfo("127.0.0.1",port,&hints,&ai);
49        if (err!=0){
50                if (print_errors) fprintf(stderr,"ERROR: getaddrinfo failed: error %i\n", err);
51                return -1;
52        }
53        sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
54        if (connect(sock,ai->ai_addr,ai->ai_addrlen)!=0){
55                if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket.\n");
56                freeaddrinfo(ai);
57                return -1;
58        }
59        freeaddrinfo(ai);
60        if (send(sock,command,strlen(command),0)<0){
61                if (print_errors) fprintf(stderr,"ERROR: Fail to send command to remote linphonec\n");
62                close_socket(sock);
63                return -1;
64        }
65        /*wait for replies */
66        i=0;
67        while ((err=recv(sock,&reply[i],reply_len-i-1,0))>0){
68                i+=err;
69        }
70        reply[i]='\0';
71        close_socket(sock);
72        return 0;
73}
74
75static void print_usage(void){
76        fprintf(stderr,"Usage:\nlinphonecsh <action> [arguments]\n"
77                        "where action is one of\n"
78                        "\tinit     : spawn a linphonec daemon (first step to make other actions)\n"
79                        "\t\tfollowed by the arguments sent to linphonec\n"
80                        "\tgeneric  : sends a generic command to the running linphonec daemon\n"
81                        "\t\tfollowed by the generic command surrounded by quotes, for example \"call sip:joe@example.net\"\n"
82                        "\tregister : register with specified proxy\n");
83        exit(-1);
84}
85
86#define MAX_ARGS 10
87
88static void spawn_linphonec(int argc, char *argv[]){
89        char * args[10];
90        int i,j;
91        pid_t pid;
92        j=0;
93        args[j++]="linphonec";
94        args[j++]="--tcp";
95        args[j++]=DEFAULT_TCP_PORT;
96        for(i=0;i<argc;++i){
97                args[j++]=argv[i];
98        }
99        args[j++]=NULL;
100
101        pid = fork();
102        if (pid < 0){
103                fprintf(stderr,"Could not fork\n");
104                exit(-1);
105        }
106        if (pid == 0) {
107                int fd;
108                /*we are the new process*/
109                setsid();
110               
111                fd = open("/dev/null", O_RDWR);
112                if (fd==-1){
113                        fprintf(stderr,"Could not open /dev/null\n");
114                        exit(-1);
115                }
116                dup2(fd, 0);
117                dup2(fd, 1);
118                dup2(fd, 2);
119                close(fd);
120               
121                if (execvp("linphonec",args)==-1){
122                        fprintf(stderr,"Fail to spawn linphonec: %s\n",strerror(errno));
123                        exit(-1);
124                }
125        }
126}
127
128static int send_generic_command(const char *command, int print_result){
129        char reply[DEFAULT_REPLY_SIZE];
130        int err;
131        err=send_command(command,DEFAULT_TCP_PORT,reply,sizeof(reply),print_result);
132        if (err==0 && print_result) printf("%s",reply);
133        return err;
134}
135
136static int register_execute(int argc, char *argv[]){
137        char cmd[512];
138        char *username=NULL;
139        char *host=NULL;
140        char *passwd=NULL;
141        int i;
142        for(i=0;i<argc;++i){
143                if (strcmp(argv[i],"--host")==0){
144                        i++;
145                        if (i<argc){
146                                host=argv[i];
147                        }else print_usage();
148                }else if (strcmp(argv[i],"--username")==0){
149                        i++;
150                        if (i<argc){
151                                username=argv[i];
152                        }else print_usage();
153                }else if (strcmp(argv[i],"--password")==0){
154                        i++;
155                        if (i<argc){
156                                passwd=argv[i];
157                        }else print_usage();
158                }else print_usage();
159        }
160        if (username==NULL) {
161                fprintf(stderr,"Missing --username\n");
162                print_usage();
163        }
164        if (host==NULL) {
165                fprintf(stderr,"Missing --host\n");
166                print_usage();
167        }
168        if (passwd) snprintf(cmd,sizeof(cmd),"register sip:%s@%s sip:%s %s",username,host,host,passwd);
169        else snprintf(cmd,sizeof(cmd),"register sip:%s@%s sip:%s",username,host,host);
170        return send_generic_command(cmd,TRUE);
171}
172
173static int dial_execute(int argc, char *argv[]){
174        char cmd[512];
175        if (argc==1){
176                snprintf(cmd,sizeof(cmd),"call %s",argv[0]);
177                return send_generic_command(cmd,TRUE);
178        }else{
179                print_usage();
180        }
181        return -1;
182}
183int main(int argc, char *argv[]){
184        int argi;
185        if (argc<2){
186                print_usage();
187                return -1;
188        }
189        ortp_init();
190        for(argi=1;argi<argc;++argi){
191                if (strcmp(argv[argi],"init")==0){
192                        /*check if there is running instance*/
193                        if (send_generic_command("help",0)==0){
194                                fprintf(stderr,"A running linphonec has been found, not spawning a second one.\n");
195                        }
196                        spawn_linphonec(argc-argi-1,&argv[argi+1]);
197                        return 0;
198                }else if (strcmp(argv[argi],"generic")==0){
199                        if (argi+1<argc){
200                                return send_generic_command(argv[argi+1],1);
201                        }else print_usage();
202                }else if (strcmp(argv[argi],"register")==0){
203                        return register_execute(argc-argi-1,&argv[argi+1]);
204                }else if (strcmp(argv[argi],"dial")==0){
205                        return dial_execute(argc-argi-1,&argv[argi+1]);
206                }
207        }
208        return 0;
209}
Note: See TracBrowser for help on using the repository browser.