shell.c 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/****************************************************************************
 *  Copyright (C) 2009  Simon MORLAT <simon.morlat@linphone.org>
 *
 ****************************************************************************
 *
 * 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20 21 22 23 24
 *
 ****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
25 26
#include <string.h>
#include <assert.h>
27

28
#ifdef _WIN32
29
#include <ws2tcpip.h>
30 31
#include <windows.h>
#include <winbase.h>
32 33
#include <ctype.h>
#include <conio.h>
34 35 36
#else
#include <sys/socket.h>
#include <netdb.h>
37 38
#include <sys/un.h>

39 40 41
#endif

#include "ortp/ortp.h"
42
#include <bctoolbox/port.h>
43 44 45

#define DEFAULT_REPLY_SIZE 4096

46 47 48 49 50 51
#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 */
52
#define STATUS_IN_COMING (1<<6) /*incoming call pending */
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74


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;
	}
75 76 77
	if (strstr(status_string,"Incoming call from ")){
		ret|=STATUS_IN_COMING;
	}
78 79 80
	return ret;
}

81 82
static int send_command(const char *command, char *reply, int reply_len, int print_errors){
	ortp_pipe_t pp;
83
	int i;
84
	int err;
85
	char path[128];
86
#ifndef _WIN32
87
	snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
88
#else
89 90 91 92 93 94
	{
		char username[128];
		DWORD size=sizeof(username)-1;
		GetUserName(username,&size);
		snprintf(path,sizeof(path)-1,"linphonec-%s",username);
	}
95
#endif
96 97 98
	if ((pp=ortp_client_pipe_connect(path))==ORTP_PIPE_INVALID){
		if (print_errors) fprintf(stderr,"ERROR: Failed to connect pipe: %s\n",strerror(errno));
		return -1;
99
	}
smorlat's avatar
smorlat committed
100
	if (ortp_pipe_write(pp,(uint8_t*)command,strlen(command))==-1){
101
		if (print_errors) fprintf(stderr,"ERROR: Fail to send command to remote linphonec\n");
102
		ortp_client_pipe_close(pp);
103 104 105 106
		return -1;
	}
	/*wait for replies */
	i=0;
smorlat's avatar
smorlat committed
107
	while ((err=ortp_pipe_read(pp,(uint8_t*)&reply[i],reply_len-i-1))>0){
108 109 110
		i+=err;
	}
	reply[i]='\0';
111
	ortp_client_pipe_close(pp);
112 113 114 115 116 117
	return 0;
}

static void print_usage(void){
	fprintf(stderr,"Usage:\nlinphonecsh <action> [arguments]\n"
			"where action is one of\n"
smorlat's avatar
smorlat committed
118 119 120 121 122 123 124 125
			"\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 <host>\n\t\t\t--username <username>\n\t\t\t--password <password>\n"
			"\tunregister\t: unregister\n"
			"\tdial\t\t: dial <sip uri or number>\n"
			"\tstatus\t\t: can be 'status register', 'status autoanswer' or 'status hook'\n"
126 127 128
			"\tsoundcard\t: can be 'soundcard capture', 'soundcard playback', 'soundcard ring',\n"
			"\t\t\t followed by an optional number representing the index of the soundcard,\n"
			"\t\t\t in which case the soundcard is set instead of just read.\n"
smorlat's avatar
smorlat committed
129 130
			"\texit\t\t: make the linphonec daemon to exit.\n"
	);
131 132 133
	exit(-1);
}

134
#ifdef _WIN32
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
static char *argv_to_line(int argc, char *argv[]) {
	int i;
	int line_length;
	char *line;

	assert( argc>=0 );

	if(argc == 0) return NULL;

	line_length = strlen(argv[0]);
	for(i=1; i<argc; i++) {
		line_length += strlen(argv[i]) + 1;
	}

	line = ortp_malloc0((line_length +1) * sizeof(char));
	strcat(line, argv[0]);
	for(i=1; i<argc; i++) {
		strcat(line, " ");
		strcat(line, argv[i]);
	}
	return line;
}
#endif

Simon Morlat's avatar
Simon Morlat committed
159
#define MAX_ARGS 20
160

161
#ifndef _WIN32
162
static void spawn_linphonec(int argc, char *argv[]){
Simon Morlat's avatar
Simon Morlat committed
163
	char * args[MAX_ARGS+1];
164 165 166 167
	int i,j;
	pid_t pid;
	j=0;
	args[j++]="linphonec";
168
	args[j++]="--pipe";
smorlat's avatar
smorlat committed
169 170
	args[j++]="-c";
	args[j++]="/dev/null";
Simon Morlat's avatar
Simon Morlat committed
171
	for(i=0;i<argc && i<MAX_ARGS;++i){
172 173
		args[j++]=argv[i];
	}
Simon Morlat's avatar
Simon Morlat committed
174
	args[j]=NULL;
175

Simon Morlat's avatar
Simon Morlat committed
176
#ifdef __uClinux__
177
	pid = vfork();
Simon Morlat's avatar
Simon Morlat committed
178 179 180
#else
	pid = fork();
#endif
181 182 183 184 185 186 187 188
	if (pid < 0){
		fprintf(stderr,"Could not fork\n");
		exit(-1);
	}
	if (pid == 0) {
		int fd;
		/*we are the new process*/
		setsid();
189

190 191 192 193 194 195 196 197 198
		fd = open("/dev/null", O_RDWR);
		if (fd==-1){
			fprintf(stderr,"Could not open /dev/null\n");
			exit(-1);
		}
		dup2(fd, 0);
		dup2(fd, 1);
		dup2(fd, 2);
		close(fd);
199

200 201 202 203 204 205
		if (execvp("linphonec",args)==-1){
			fprintf(stderr,"Fail to spawn linphonec: %s\n",strerror(errno));
			exit(-1);
		}
	}
}
206 207 208
#else

static void spawn_linphonec(int argc, char *argv[]){
209
	PROCESS_INFORMATION pinfo;
210
	STARTUPINFO si;
211
	BOOL ret;
212 213 214
	const char *cmd = "linphoned.exe --pipe -c NUL";
	char *args_in_line = argv_to_line(argc, argv);
	char *cmd_with_args;
215

216
	ZeroMemory( &si, sizeof(si) );
217 218
	si.cb = sizeof(si);
	ZeroMemory( &pinfo, sizeof(pinfo) );
219

220 221 222 223 224
	if(args_in_line) {
		cmd_with_args = ortp_strdup_printf("%s %s", cmd, args_in_line);
	} else {
		cmd_with_args = ortp_strdup(cmd);
	}
225

226
	ret=CreateProcess(NULL, cmd_with_args,
227 228 229 230 231 232 233
		NULL,
		NULL,
		FALSE,
		0,
		NULL,
		NULL,
		&si,
234
		&pinfo);
235 236 237 238

	if(args_in_line) ortp_free(args_in_line);
	ortp_free(cmd_with_args);

239
	if (!ret){
240
		fprintf(stderr,"Spawning of linphoned.exe failed.\n");
241 242
	}else{
		WaitForInputIdle(pinfo.hProcess,1000);
243 244 245 246
	}
}

#endif
247 248 249 250

static int send_generic_command(const char *command, int print_result){
	char reply[DEFAULT_REPLY_SIZE];
	int err;
251
	err=send_command(command,reply,sizeof(reply),print_result);
252 253 254 255
	if (err==0 && print_result) {
		printf("%s",reply);
		fflush(stdout);
	}
256 257 258
	return err;
}

smorlat's avatar
smorlat committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static int register_execute(int argc, char *argv[]){
	char cmd[512];
	char *username=NULL;
	char *host=NULL;
	char *passwd=NULL;
	int i;
	for(i=0;i<argc;++i){
		if (strcmp(argv[i],"--host")==0){
			i++;
			if (i<argc){
				host=argv[i];
			}else print_usage();
		}else if (strcmp(argv[i],"--username")==0){
			i++;
			if (i<argc){
				username=argv[i];
			}else print_usage();
		}else if (strcmp(argv[i],"--password")==0){
			i++;
			if (i<argc){
				passwd=argv[i];
			}else print_usage();
		}else print_usage();
	}
	if (username==NULL) {
		fprintf(stderr,"Missing --username\n");
		print_usage();
	}
	if (host==NULL) {
		fprintf(stderr,"Missing --host\n");
		print_usage();
	}
	if (passwd) snprintf(cmd,sizeof(cmd),"register sip:%s@%s sip:%s %s",username,host,host,passwd);
	else snprintf(cmd,sizeof(cmd),"register sip:%s@%s sip:%s",username,host,host);
	return send_generic_command(cmd,TRUE);
}

smorlat's avatar
smorlat committed
296 297 298 299 300
static int unregister_execute(int argc, char *argv[]){
	return send_generic_command("unregister",FALSE);
}


smorlat's avatar
smorlat committed
301 302 303 304 305 306 307 308 309 310
static int dial_execute(int argc, char *argv[]){
	char cmd[512];
	if (argc==1){
		snprintf(cmd,sizeof(cmd),"call %s",argv[0]);
		return send_generic_command(cmd,TRUE);
	}else{
		print_usage();
	}
	return -1;
}
smorlat's avatar
smorlat committed
311 312 313

static int status_execute(int argc, char *argv[]){
	char cmd[512];
314 315
	char reply[DEFAULT_REPLY_SIZE];
	int err;
316

smorlat's avatar
smorlat committed
317
	if (argc==1){
smorlat's avatar
smorlat committed
318
		snprintf(cmd,sizeof(cmd),"status %s",argv[0]);
319
		err=send_command(cmd,reply,sizeof(reply),TRUE);
320 321 322 323 324
		if (err==0) {
			printf("%s",reply);
			err=make_status_value(reply);
		}
		return err;
smorlat's avatar
smorlat committed
325 326 327 328 329 330
	}else{
		print_usage();
	}
	return -1;
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
static int parse_card_index(const char *reply){
	int index=-1;
	reply=strstr(reply,"device #");
	if (!reply || sscanf(reply,"device #%i",&index)!=1){
		fprintf(stderr,"Error while parsing linphonec daemon output !\n");
	}
	return index;
}

static int soundcard_execute(int argc, char *argv[]){
	char cmd[512];
	char reply[DEFAULT_REPLY_SIZE];
	int err;
	if (argc==1){
		snprintf(cmd,sizeof(cmd),"soundcard %s",argv[0]);
346
		err=send_command(cmd,reply,sizeof(reply),TRUE);
347 348 349 350 351 352
		if (err==0) {
			printf("%s",reply);
			return parse_card_index(reply);
		}
	}else if (argc==2){/*setting a soundcard */
		snprintf(cmd,sizeof(cmd),"soundcard %s %s",argv[0],argv[1]);
353
		err=send_command(cmd,reply,sizeof(reply),TRUE);
354 355 356 357 358 359 360 361 362 363
		if (err==0) {
			printf("%s",reply);
			return 0;
		}
	}else{
		print_usage();
	}
	return -1;
}

364 365 366 367 368 369 370 371
int main(int argc, char *argv[]){
	int argi;
	if (argc<2){
		print_usage();
		return -1;
	}
	ortp_init();
	for(argi=1;argi<argc;++argi){
372
		if (strcmp(argv[argi],"init")==0){
373 374 375
			/*check if there is running instance*/
			if (send_generic_command("help",0)==0){
				fprintf(stderr,"A running linphonec has been found, not spawning a second one.\n");
376
				return 0;
377 378 379 380 381 382 383
			}
			spawn_linphonec(argc-argi-1,&argv[argi+1]);
			return 0;
		}else if (strcmp(argv[argi],"generic")==0){
			if (argi+1<argc){
				return send_generic_command(argv[argi+1],1);
			}else print_usage();
smorlat's avatar
smorlat committed
384 385
		}else if (strcmp(argv[argi],"register")==0){
			return register_execute(argc-argi-1,&argv[argi+1]);
smorlat's avatar
smorlat committed
386 387
		}else if (strcmp(argv[argi],"unregister")==0){
			return unregister_execute(argc-argi-1,&argv[argi+1]);
smorlat's avatar
smorlat committed
388 389
		}else if (strcmp(argv[argi],"dial")==0){
			return dial_execute(argc-argi-1,&argv[argi+1]);
smorlat's avatar
smorlat committed
390 391 392
		}else if (strcmp(argv[argi],"hangup")==0){
			send_generic_command("terminate",FALSE);
			send_generic_command("duration",TRUE);
smorlat's avatar
smorlat committed
393 394
		}else if (strcmp(argv[argi],"status")==0){
			return status_execute(argc-argi-1,&argv[argi+1]);
395 396
		}else if (strcmp(argv[argi],"soundcard")==0){
			return soundcard_execute(argc-argi-1,&argv[argi+1]);
smorlat's avatar
smorlat committed
397 398 399
		}else if (strcmp(argv[argi],"exit")==0){
			return send_generic_command("quit",TRUE);
		}else print_usage();
400 401 402
	}
  	return 0;
}