File: server.c

package info (click to toggle)
cthugha 1.1a-7
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,904 kB
  • ctags: 2,155
  • sloc: ansic: 22,340; makefile: 443; sh: 75; perl: 15
file content (252 lines) | stat: -rw-r--r-- 5,172 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "cthugha.h"
#include "sound.h"
#include "options.h"
#include "server.h"
#include "keys.h"
#include "network.h"
#include "display.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


int sound_sock;					/* used for broadcast */
int request_socket;				/* use to recv. requests */

struct sockaddr_in client_addrs[MAX_CLIENTS];
int nr_clients = 0;

int srv_wait_time = 100;			/* time to wait after send */
int server = 0;					/* server active */

int remove_client(long addr, short port);

/*
 * create socket to transmit the sound
 */
int init_sound_socket() {

    if( (sound_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
	printfee("Can not open socket");
	return 1;
    }
    fcntl( sound_sock, F_SETFL, O_NONBLOCK);
		
    return 0;
}
/*
 * close the socket for transmission of the sound
 */
int exit_sound_socket() {
    close(sound_sock);
    return 0;
}

/*
 * create a socket to receive requests from clients
 */
int init_request_socket() {
    struct sockaddr_in s_addr;

    /* create request-socket */
    if( (request_socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
	printfee("Can not open request socket");
	return 1;
    }
    /* bind */
    s_addr.sin_family = AF_INET;
    s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    s_addr.sin_port = htons(REQ_PORT);
    if( bind(request_socket, (struct sockaddr*)&s_addr, sizeof(s_addr)) < 0) {
	printfee("Can not bind to request socket.");
	close(request_socket);
	return 1;
    }
    /* change to non-blocking */
    fcntl( request_socket, F_SETFL, O_NONBLOCK); 

    /* listen */
    if ( listen(request_socket, 1) < 0) {
	printfee("Can not listen.\n");
	close(request_socket);
	return 1;
    }
    return 0;
}

/*
 * close the socket to receive requests
 */
int exit_request_socket() {
    shutdown(request_socket, 2);
    close(request_socket);
    return 0;
}

/*
 * Broadcast the sound_data to all clients
 */
int broadcast_sound() {
    int i;

    for(i=0; i < nr_clients; i++) {

	if( sendto(sound_sock, &(sound_data[0][0]), 2*BUFF_WIDTH, 0,
		   (struct sockaddr*)&client_addrs[i], 
		   sizeof(struct sockaddr_in)) <= 0) {
	    if ( errno != 111)
		printfee("Can not write to client.")
	    else
		fprintf(stderr, "x");
	    return 0;
	}
    }
    return 0;
}


/*
 * include a new client in the list
 */
int add_client(long addr, short port) {
    struct sockaddr_in s_addr;

    printfv(1,"adding %lx:%d\n", addr, port);

    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(port); 
    s_addr.sin_addr.s_addr = addr;

    /* remove old entry */
    remove_client(addr,port);

    /* check for full memory */
    if ( nr_clients >= MAX_CLIENTS)
	return 1;

    /* append new entry to list */
    memcpy( &(client_addrs[nr_clients]), &s_addr, sizeof(struct sockaddr_in));

    nr_clients ++;

    return 0;
}

/*
 *remove a client from the list
 */
int remove_client(long addr, short port) {
    struct sockaddr_in s_addr;
    int i;

    printfv(1,"removing %lx:%d\n", addr, port);

    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(port); 
    s_addr.sin_addr.s_addr = addr;

    for(i=0; i < nr_clients; i++) {
	if( (s_addr.sin_port == client_addrs[i].sin_port) &&
	    (s_addr.sin_addr.s_addr == client_addrs[i].sin_addr.s_addr)) {
	    printfv(1,"Removing %lx:%d at position %d\n", addr,port,i);
	    memcpy( &(client_addrs[i]), &(client_addrs[i+1]), 
		   sizeof(struct sockaddr_in) * (nr_clients - i));
	    nr_clients --;
	}
    }

    return 0;
}

/*
 * print a list of all clients
 */
int print_clients() {
    int i;
    char str[256];

    for(i=0; i < nr_clients; i++) {
	sprintf(str, "Client %d: %x:%d\n", 
		i, client_addrs[i].sin_addr.s_addr, 
		client_addrs[i].sin_port);
	display_print(str, -2, 'l', 0, 0);
    }

    return 0;
}


/*
 * initialize the sound server
 */
int init_server() {

    /* prepare socket for boradcast */
    if ( init_sound_socket() ) {
	return 1;
    }

    /* prepare socket to receive requests */
    if ( init_request_socket() ) {
	exit_sound_socket();
	return 1;
    }

    return 0;
}
/* 
 * close down the sound server
 */
int exit_server() {

    exit_sound_socket();
    exit_request_socket();

    return 0;
}

/*
 * accept new connection (check for new requests)
 */
int server_accept() {
    int  nr_read;
    char data[512];
    int client_port;
    long client_addr;
    struct sockaddr_in s_addr;
    int size;
    int acc_socket;

    if ( (acc_socket = accept(request_socket, 
			      (struct sockaddr*)&s_addr, &size) ) >= 0) {
	
	nr_read = recv(acc_socket, data, 64, 0);
	
	printfv(1,"\nreceived %d bytes: %s", nr_read, data);
	
	if( sscanf(data, "connect %ld %d", 
		   &client_addr, &client_port) > 0)
	    add_client( client_addr, client_port );
	
	if( sscanf(data, "disconnect %ld %d", 
		   &client_addr,  &client_port) >0)
	    remove_client( client_addr, client_port );
	
	close(acc_socket);
    } else
	if ( errno != EWOULDBLOCK)
	    printfe("Can not accept.");
    return 0;
}