source: roaraudio/roard/clients.c @ 437:38e4b190c0e8

Last change on this file since 437:38e4b190c0e8 was 437:38e4b190c0e8, checked in by phi, 16 years ago

added support UID/GID of client

File size: 7.1 KB
Line 
1//clients.c:
2
3#include "roard.h"
4
5int clients_init (void) {
6 int i;
7
8 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
9  g_clients[i] = NULL;
10
11 return 0;
12}
13
14int clients_free (void) {
15 int i;
16
17 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
18  if ( g_clients[i] )
19   clients_delete(i);
20
21 return 0;
22}
23
24int clients_new (void) {
25 int i;
26 int s;
27 struct roar_client * n;
28
29 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
30  if ( g_clients[i] == NULL ) {
31   n = malloc(sizeof(struct roar_client));
32   if ( n != NULL ) {
33    n->pid    = -1;
34    n->uid    = -1;
35    n->gid    = -1;
36    n->fh     = -1;
37
38    *n->name = 0;
39    *n->host = 0;
40
41    n->acl   = NULL;
42
43    n->execed = -1;
44    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
45     n->streams[s] = -1;
46
47    g_clients[i] = n;
48
49    ROAR_DBG("clients_new(void) = %i", i);
50    return i;
51   } else {
52    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
53    ROAR_ERR("clients_new(void) = -1");
54    return -1;
55   }
56  }
57 }
58
59 return -1;
60}
61
62int clients_delete (int id) {
63 int i;
64
65 if ( g_clients[id] == NULL )
66  return -1;
67
68 if (g_clients[id]->execed != -1) {
69//  return streams_delete(g_clients[id]->execed);
70  g_clients[id]->execed = -1;
71 }
72
73 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
74  streams_delete(g_clients[id]->streams[i]);
75 }
76
77 if ( g_clients[id]->fh != -1 )
78  close(g_clients[id]->fh);
79
80 free(g_clients[id]);
81 g_clients[id] = NULL;
82
83 ROAR_DBG("clients_delete(id=%i) = 0", id);
84 return 0;
85}
86
87int clients_get       (int id, struct roar_client ** client) {
88 *client = g_clients[id];
89
90 if ( *client == NULL )
91  return -1;
92
93 return 0;
94}
95
96int clients_set_fh    (int id, int    fh) {
97 if ( g_clients[id] == NULL )
98  return -1;
99
100 g_clients[id]->fh = fh;
101
102 return 0;
103}
104
105int clients_set_pid   (int id, int    pid) {
106 if ( g_clients[id] == NULL )
107  return -1;
108
109 g_clients[id]->pid = pid;
110
111 return 0;
112}
113
114int clients_check_all (void) {
115 struct timeval tv;
116 fd_set r, e;
117 int i, j;
118 int ret;
119 int fh;
120 int max_fh = -1;
121 int have = 0;
122
123 FD_ZERO(&r);
124 FD_ZERO(&e);
125
126 tv.tv_sec  = 0;
127 tv.tv_usec = 1;
128
129 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
130  if ( g_clients[i] == NULL )
131   continue;
132
133  if ( (fh = g_clients[i]->fh) != -1 ) {
134   have++;
135
136   FD_SET(fh, &r);
137   FD_SET(fh, &e);
138
139   if ( fh > max_fh )
140    max_fh = fh;
141  }
142
143  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
144   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
145    FD_SET(fh, &r);
146
147    if ( fh > max_fh )
148     max_fh = fh;
149   }
150   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
151  }
152
153 }
154
155 if ( max_fh == -1 )
156  return 0;
157
158 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
159  return ret < 0 ? ret : have;
160 }
161
162 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
163  if ( g_clients[i] == NULL )
164   continue;
165
166  if ( (fh = g_clients[i]->fh) != -1 ) {
167   if ( FD_ISSET(fh, &r) ) {
168    if ( g_clients[i]->execed == -1 ) {
169     clients_check(i);
170/*
171    } else {
172     streams_check(g_clients[i]->execed);
173*/
174    }
175   }
176
177   if ( FD_ISSET(fh, &e) ) {
178    clients_delete(i);
179    continue;
180   }
181  }
182
183  if ( g_clients[i] == NULL )
184   continue;
185
186  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
187   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
188   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
189    break;
190   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
191    if ( FD_ISSET(fh, &r) ) {
192     streams_check(g_clients[i]->streams[j]);
193    }
194   }
195  }
196 }
197
198 ROAR_DBG("clients_check_all(void) = %i // have value", have);
199 return have;
200}
201
202int clients_check     (int id) {
203 struct roar_message    m;
204 struct roar_connection con;
205 char * data = NULL;
206 int oldcmd;
207 int r;
208
209 if ( g_clients[id] == NULL )
210  return -1;
211 if ( g_clients[id]->fh == -1 )
212  return -1;
213
214 con.fh = g_clients[id]->fh;
215
216 r = roar_recv_message(&con, &m, &data);
217
218 if ( r == -1 ) { // should we drop the client?
219  clients_delete(id);
220  return -1;
221 }
222
223 roar_debug_message_print(&m);
224
225 oldcmd = m.cmd;
226
227 if ( (r = command_exec(id, &m, data)) == -1 ) {
228  m.cmd     = ROAR_CMD_ERROR;
229  m.datalen = 0;
230  ROAR_DBG("clients_check(*): Exec of command faild!");
231 } else {
232  if ( m.cmd == oldcmd ) {
233   m.cmd     = ROAR_CMD_OK;
234   m.datalen = 0;
235  }
236 }
237
238 roar_send_message(&con, &m, NULL);
239
240 if ( data )
241  free(data);
242
243 ROAR_DBG("clients_check(id=%i) = 0", id);
244 return 0;
245}
246
247int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
248 int i;
249 int fh;
250
251 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
252  if ( g_clients[i] == NULL )
253   continue;
254
255  if ( (fh = g_clients[i]->fh) == -1 )
256   continue;
257
258  if ( g_clients[i]->execed == -1 ) {
259   // TODO: add some code to send a message to the client insetd of the raw data.
260  } else {
261//   streams_check(g_clients[i]->execed);
262   streams_send_mon(g_clients[i]->execed);
263//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
264//    clients_delete(i); // delete client in case we could not write
265  }
266 }
267
268 return -1;
269}
270
271int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
272 int i;
273 int fh;
274
275 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
276  if ( g_clients[i] == NULL )
277   continue;
278
279  if ( (fh = g_clients[i]->fh) == -1 )
280   continue;
281
282  if ( g_clients[i]->execed == -1 ) {
283   // TODO: add some code to send a message to the client insetd of the raw data.
284  } else {
285//   streams_check(g_clients[i]->execed);
286   streams_send_filter(g_clients[i]->execed);
287//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
288//    clients_delete(i); // delete client in case we could not write
289  }
290 }
291
292 return -1;
293}
294
295int client_stream_exec   (int client, int stream) {
296 int i;
297
298 if ( g_clients[client] == NULL )
299  return -1;
300
301 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
302  if ( g_clients[client]->streams[i] == stream ) {
303   g_clients[client]->execed = stream;
304   streams_set_fh(stream, g_clients[client]->fh);
305   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
306   return 0;
307  }
308 }
309
310 return -1;
311}
312
313int client_stream_set_fh (int client, int stream, int fh) {
314 int i;
315
316 if ( g_clients[client] == NULL )
317  return -1;
318
319 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
320  if ( g_clients[client]->streams[i] == stream ) {
321   streams_set_fh(stream, fh);
322   return 0;
323  }
324 }
325
326 return -1;
327}
328
329int client_stream_add    (int client, int stream) {
330 int i;
331
332 if ( g_clients[client] == NULL )
333  return -1;
334
335 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
336  if ( g_clients[client]->streams[i] == -1 ) {
337   g_clients[client]->streams[i] = stream;
338   streams_set_client(stream, client);
339   return 0;
340  }
341 }
342
343 return -1;
344}
345
346int client_stream_delete (int client, int stream) {
347 int i;
348
349 if ( g_clients[client] == NULL )
350  return -1;
351
352 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
353  if ( g_clients[client]->streams[i] == stream ) {
354   g_clients[client]->streams[i] = -1;
355
356   if ( stream == g_clients[client]->execed ) {
357    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
358    clients_delete(client);
359   }
360
361   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
362   return 0;
363  }
364 }
365
366 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
367 return -1;
368}
369
370//ll
Note: See TracBrowser for help on using the repository browser.