~ubuntu-branches/ubuntu/vivid/linphone/vivid

« back to all changes in this revision

Viewing changes to console/shell.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2009-10-14 08:26:02 UTC
  • mfrom: (1.3.1 upstream) (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20091014082602-751618nxdjooja3l
Tags: 3.2.1-1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#include "ortp/ortp.h"
41
41
 
42
 
#define DEFAULT_TCP_PORT "32333"
43
42
#define DEFAULT_REPLY_SIZE 4096
44
43
 
45
44
#define STATUS_REGISTERED (1<<0)
49
48
#define STATUS_IN_CONNECTED (1<<4) /* incoming call accepted */
50
49
#define STATUS_OUT_CONNECTED (1<<5) /*outgoing call accepted */
51
50
 
52
 
#ifndef WIN32
53
 
static int tcp=0;
54
 
#else
55
 
static int tcp=1;
56
 
#endif
57
51
 
58
52
static int make_status_value(const char *status_string){
59
53
        int ret=0;
78
72
        return ret;
79
73
}
80
74
 
81
 
static int send_command(const char *command, const char * port, char *reply, int reply_len, int print_errors){
82
 
        ortp_socket_t sock;
 
75
static int send_command(const char *command, char *reply, int reply_len, int print_errors){
 
76
        ortp_pipe_t pp;
83
77
        int i;
84
78
        int err;
85
 
        if (tcp){
86
 
                struct addrinfo *ai=NULL;
87
 
                struct addrinfo hints;
88
 
                memset(&hints,0,sizeof(hints));
89
 
                hints.ai_family=AF_INET;
90
 
                hints.ai_socktype=SOCK_STREAM;
91
 
                err=getaddrinfo("127.0.0.1",port,&hints,&ai);
92
 
                if (err!=0){
93
 
                        if (print_errors) fprintf(stderr,"ERROR: getaddrinfo failed: error %i\n", err);
94
 
                        return -1;
95
 
                }
96
 
                sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
97
 
                if (connect(sock,ai->ai_addr,ai->ai_addrlen)!=0){
98
 
                        if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket.\n");
99
 
                        freeaddrinfo(ai);
100
 
                        return -1;
101
 
                }
102
 
                freeaddrinfo(ai);
103
 
        }else{
 
79
        char path[128];
104
80
#ifndef WIN32
105
 
                struct sockaddr_un sa;
106
 
                char path[128];
107
 
                sock=socket(AF_UNIX,SOCK_STREAM,0);
108
 
                sa.sun_family=AF_UNIX;
109
 
                snprintf(path,sizeof(path)-1,"/tmp/linphonec-%i",getuid());
110
 
                strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
111
 
                if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){
112
 
                        if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket: %s\n",getSocketError());
113
 
                        return -1;
114
 
                }
 
81
        snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
115
82
#else
116
 
                fprintf(stderr,"ERROR: windows pipes communication not yet implemented.\n");
 
83
        {
 
84
                char username[128];
 
85
                DWORD size=sizeof(username)-1;
 
86
                GetUserName(username,&size);
 
87
                snprintf(path,sizeof(path)-1,"linphonec-%s",username);
 
88
        }
 
89
#endif
 
90
        if ((pp=ortp_client_pipe_connect(path))==ORTP_PIPE_INVALID){
 
91
                if (print_errors) fprintf(stderr,"ERROR: Failed to connect pipe: %s\n",strerror(errno));
117
92
                return -1;
118
 
#endif
119
93
        }
120
 
        if (send(sock,command,strlen(command),0)<0){
 
94
        if (ortp_pipe_write(pp,(uint8_t*)command,strlen(command))==-1){
121
95
                if (print_errors) fprintf(stderr,"ERROR: Fail to send command to remote linphonec\n");
122
 
                close_socket(sock);
 
96
                ortp_client_pipe_close(pp);
123
97
                return -1;
124
98
        }
125
99
        /*wait for replies */
126
100
        i=0;
127
 
        while ((err=recv(sock,&reply[i],reply_len-i-1,0))>0){
 
101
        while ((err=ortp_pipe_read(pp,(uint8_t*)&reply[i],reply_len-i-1))>0){
128
102
                i+=err;
129
103
        }
130
104
        reply[i]='\0';
131
 
        close_socket(sock);
 
105
        ortp_client_pipe_close(pp);
132
106
        return 0;
133
107
}
134
108
 
160
134
        pid_t pid;
161
135
        j=0;
162
136
        args[j++]="linphonec";
163
 
        if (tcp){
164
 
                args[j++]="--tcp";
165
 
                args[j++]=DEFAULT_TCP_PORT;
166
 
        }else args[j++]="--pipe";
 
137
        args[j++]="--pipe";
167
138
        args[j++]="-c";
168
139
        args[j++]="/dev/null";
169
140
        for(i=0;i<argc;++i){
203
174
        PROCESS_INFORMATION pinfo;
204
175
        STARTUPINFO si;
205
176
        ZeroMemory( &si, sizeof(si) );
206
 
    si.cb = sizeof(si);
207
 
    ZeroMemory( &pinfo, sizeof(pinfo) );
208
 
 
209
 
 
210
 
        BOOL ret=CreateProcess(NULL,"linphonec.exe --tcp " DEFAULT_TCP_PORT " -c NUL",
 
177
        si.cb = sizeof(si);
 
178
        ZeroMemory( &pinfo, sizeof(pinfo) );
 
179
 
 
180
 
 
181
        BOOL ret=CreateProcess(NULL,"linphoned.exe --pipe -c NUL",
211
182
                NULL,
212
183
                NULL,
213
184
                FALSE,
228
199
static int send_generic_command(const char *command, int print_result){
229
200
        char reply[DEFAULT_REPLY_SIZE];
230
201
        int err;
231
 
        err=send_command(command,DEFAULT_TCP_PORT,reply,sizeof(reply),print_result);
 
202
        err=send_command(command,reply,sizeof(reply),print_result);
232
203
        if (err==0 && print_result) {
233
204
                printf("%s",reply);
234
205
                fflush(stdout);
296
267
        
297
268
        if (argc==1){
298
269
                snprintf(cmd,sizeof(cmd),"status %s",argv[0]);
299
 
                err=send_command(cmd,DEFAULT_TCP_PORT,reply,sizeof(reply),TRUE);
 
270
                err=send_command(cmd,reply,sizeof(reply),TRUE);
300
271
                if (err==0) {
301
272
                        printf("%s",reply);
302
273
                        err=make_status_value(reply);
323
294
        int err;
324
295
        if (argc==1){
325
296
                snprintf(cmd,sizeof(cmd),"soundcard %s",argv[0]);
326
 
                err=send_command(cmd,DEFAULT_TCP_PORT,reply,sizeof(reply),TRUE);
 
297
                err=send_command(cmd,reply,sizeof(reply),TRUE);
327
298
                if (err==0) {
328
299
                        printf("%s",reply);
329
300
                        return parse_card_index(reply);
330
301
                }
331
302
        }else if (argc==2){/*setting a soundcard */
332
303
                snprintf(cmd,sizeof(cmd),"soundcard %s %s",argv[0],argv[1]);
333
 
                err=send_command(cmd,DEFAULT_TCP_PORT,reply,sizeof(reply),TRUE);
 
304
                err=send_command(cmd,reply,sizeof(reply),TRUE);
334
305
                if (err==0) {
335
306
                        printf("%s",reply);
336
307
                        return 0;
349
320
        }
350
321
        ortp_init();
351
322
        for(argi=1;argi<argc;++argi){
352
 
                if (strcmp(argv[argi],"--tcp")==0){
353
 
                        tcp=1;
354
 
                }else if (strcmp(argv[argi],"init")==0){
 
323
                if (strcmp(argv[argi],"init")==0){
355
324
                        /*check if there is running instance*/
356
325
                        if (send_generic_command("help",0)==0){
357
326
                                fprintf(stderr,"A running linphonec has been found, not spawning a second one.\n");
358
327
                                return 0;
359
328
                        }
360
329
                        spawn_linphonec(argc-argi-1,&argv[argi+1]);
361
 
                        if (tcp) fprintf(stderr,"WARNING: using --tcp is unsafe: unprivilegied users can make calls.\n");
362
330
                        return 0;
363
331
                }else if (strcmp(argv[argi],"generic")==0){
364
332
                        if (argi+1<argc){