aboutsummaryrefslogtreecommitdiffstats
path: root/package/fonera-mp3/src/lib/mp3_nix_socket.c
blob: 964690d6470edd7ea0e4d8e07a8b7bf5945fab1a (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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 <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/poll.h>
#include <stdarg.h>
#include <fcntl.h>
#include "mp3.h"

#define SOCKET_PATH "/tmp/foxmp3"

typedef struct _MP3_NIX_SOCKET {
	fd_set master;
	fd_set clients;
	int max;
	int listener;
} MP3_NIX_SOCKET;

static MP3_NIX_SOCKET mp3_nix_socket;

int mp3_nix_socket_setup(void){
	struct sockaddr_un myaddr;
	int yes=1; 	
	int len;
	FD_ZERO(&mp3_nix_socket.master); 
	FD_ZERO(&mp3_nix_socket.clients);
	
	if ((mp3_nix_socket.listener = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		return MP3_ERROR;
	}
	if (setsockopt(mp3_nix_socket.listener, SOL_SOCKET, 
				SO_REUSEADDR, &yes, sizeof(int)) == -1) {
		perror("setsockopt");
		return MP3_ERROR;
	}
	myaddr.sun_family = AF_UNIX;
	strcpy(myaddr.sun_path, SOCKET_PATH);
	unlink(myaddr.sun_path);
	len = strlen(myaddr.sun_path) + sizeof(myaddr.sun_family);
	if (bind(mp3_nix_socket.listener, (struct sockaddr *)&myaddr, len) == -1) {
		perror("bind");
		return MP3_ERROR;
	}
	if (listen(mp3_nix_socket.listener, 3) == -1) {
		perror("listen");
		return MP3_ERROR;
	}
	FD_SET(mp3_nix_socket.listener, &mp3_nix_socket.master);
	mp3_nix_socket.max = mp3_nix_socket.listener;

	return MP3_OK;
};

int mp3_nix_socket_handle(void){
	struct sockaddr_un remoteaddr; 
	socklen_t addrlen;
	int i;
	int newfd; 
	char buf[1024]; 
	int nbytes;	
	char buf_out[1024];
	struct timeval tv;
	
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	mp3_nix_socket.clients = mp3_nix_socket.master;
	
	if (select(mp3_nix_socket.max + 1, &mp3_nix_socket.clients,
				NULL, NULL, &tv) == -1) {
		// sometimes the select is interrupted, because of the alarm signal used by the playtime counter
		//perror("error whilst selecting socket");
		return MP3_ERROR;
	}
	for(i = 0; i <= mp3_nix_socket.max; i++) {
		if (FD_ISSET(i, &mp3_nix_socket.clients)) { 
			if (i == mp3_nix_socket.listener) {
				addrlen = sizeof(remoteaddr);
				if ((newfd = accept(mp3_nix_socket.listener, 
							(struct sockaddr *)&remoteaddr, 
							&addrlen)) == -1) {
					perror("error whilst accepting socket");
					return MP3_ERROR;
				} else {
					FD_SET(newfd, &mp3_nix_socket.master); 
					if (newfd > mp3_nix_socket.max) { 
						mp3_nix_socket.max = newfd;
					}
					fcntl(newfd, F_SETFL, O_NONBLOCK);
					printf("New socket client on %d\n", newfd);
				}
			} else {
				if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
					if (nbytes == 0) {
						printf("selectserver: socket hung up %d\n", i);
						close(i); 
						FD_CLR(i, &mp3_nix_socket.master);
					} else {
						printf("error whilst receiving socket %d\n", i);
					}
				} else {
					buf[nbytes] = '\0';
					printf("Got data : %s\n", buf);
					mp3_parser_incoming(buf,buf_out);
					if(*buf_out != '\0'){
						send(i, buf_out, strlen(buf_out), 0);
					}
				}
			}
		}	
	}
	return MP3_OK;
}

void mp3_nix_socket_write(unsigned char *data, ...){
	unsigned int i;
	unsigned char t[2048];	
	va_list ap;
	
	// clear possible dead sockets
	mp3_nix_socket_handle();
	memset(t, 0, 2048);	
	va_start(ap, data);
  	vsprintf(t, data, ap);
   	va_end(ap);
	printf("Sending data --> %s\n", t);	
	for(i = 0; i <= mp3_nix_socket.max; i++) {
		if (FD_ISSET(i, &mp3_nix_socket.master)) { 
			if (i != mp3_nix_socket.listener) {
				printf("Sending on socket %d\n", i);
				send(i, t, strlen(t), 0);
			}
		}	
	}
	printf("Finished sending\n");
}

int mp3_nix_socket_cleanup(void){
	return MP3_OK;
};