summaryrefslogtreecommitdiffstats
path: root/package/fonera-mp3/src/cli/main_tcp.c
blob: 02fc41ea41ea6420af5ee9e0802c5baaf0b58492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* FOXMP3 
* Copyright (c) 2006 acmesystems.it - john@acmesystems.it
*
* 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, MA02111-1307USA
*
* Feedback, Bugs...  info@acmesystems.it 
*
*/ 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define SOCKET_PORT 369


void print_usage(void){
	printf("mp3_play_tcp IP COMANND PARAMETERS\n");
	printf("    Commands :\n");
	printf("  	PLAY filename\n");	
	printf("	STREAM url [URL OF STREAM]\n");	
	printf("	STREAM pls [URL PLS FILE]\n");	
	printf("	VOLUME [0-255]\n");	
	printf("	STOP\n");	
	printf("	STATE\n");
	printf("	BASS [0-255]\n");
}

void issue_command(unsigned char *str, unsigned char *ip){
	int s, t, len;
	struct sockaddr_in remote;
	struct hostent *he;

	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}
	
	printf("Connecting to FOXMP3 on IP/DNS : %s  ...\n", ip);
	if((he=gethostbyname(ip)) == NULL) { 
		herror("gethostbyname");
		exit(1);
	}
	if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}
	remote.sin_family = AF_INET;
	remote.sin_port = htons(SOCKET_PORT);
	remote.sin_addr = *((struct in_addr *)he->h_addr);
	memset(&(remote.sin_zero), '\0', 8);

	if (connect(s, (struct sockaddr *)&remote,
			sizeof(struct sockaddr)) == -1) {
		perror("connect");
		exit(1);
	}
	printf("Connected ...\n\nSending command -> \n%s\n\n", str);
	if (send(s, str, strlen(str), 0) == -1) {
		perror("send");
		exit(1);	
	}
	if ((t=recv(s, str, 2048, 0)) > 0) {
		str[t] = '\0';
		printf("The answer was -> \n%s\n", str);
	} else {
		if (t < 0){
			perror("recv");
		} else {
			printf("Server closed connection\n");
		};
		exit(1);
	}
	close(s);
}

int main(int argc, char **argv){
	unsigned char buffer[2048];
	buffer[0] = '\0';
	if(argc > 2){
		if(((!strcmp(argv[2], "STOP")) || (!strcmp(argv[2], "STATE"))) 
				&& (argc == 3)){
			sprintf(buffer, "%s", argv[2]);
		} else if(((!strcmp(argv[2], "PLAY")) || (!strcmp(argv[2], "VOLUME")) 
			|| (!strcmp(argv[2], "BASS"))) && (argc == 4)){
			sprintf(buffer, "%s %s", argv[2], argv[3]);	
		} else if((!strcmp(argv[2], "STREAM")) && (argc == 5)
				&& ((!strcmp(argv[3], "url")) || (!strcmp(argv[3], "pls")))){
			sprintf(buffer, "%s %s %s", argv[2], argv[3], 
					argv[4]);	
		}
	};
	if(buffer[0] != '\0'){
		issue_command(buffer, argv[1]);
	} else {
		print_usage();
	};
	return 0;
}