source: roaraudio/roard/clients.c @ 439:452cf038155c

Last change on this file since 439:452cf038155c was 439:452cf038155c, checked in by phi, 16 years ago

added calls to set UID and GID

File size: 7.3 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_set_uid   (int id, int    uid) {
115 if ( g_clients[id] == NULL )
116  return -1;
117
118 g_clients[id]->uid = uid;
119
120 return 0;
121}
122
123int clients_set_gid   (int id, int    gid) {
124 if ( g_clients[id] == NULL )
125  return -1;
126
127 g_clients[id]->gid = gid;
128
129 return 0;
130}
131
132int clients_check_all (void) {
133 struct timeval tv;
134 fd_set r, e;
135 int i, j;
136 int ret;
137 int fh;
138 int max_fh = -1;
139 int have = 0;
140
141 FD_ZERO(&r);
142 FD_ZERO(&e);
143
144 tv.tv_sec  = 0;
145 tv.tv_usec = 1;
146
147 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
148  if ( g_clients[i] == NULL )
149   continue;
150
151  if ( (fh = g_clients[i]->fh) != -1 ) {
152   have++;
153
154   FD_SET(fh, &r);
155   FD_SET(fh, &e);
156
157   if ( fh > max_fh )
158    max_fh = fh;
159  }
160
161  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
162   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
163    FD_SET(fh, &r);
164
165    if ( fh > max_fh )
166     max_fh = fh;
167   }
168   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
169  }
170
171 }
172
173 if ( max_fh == -1 )
174  return 0;
175
176 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
177  return ret < 0 ? ret : have;
178 }
179
180 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
181  if ( g_clients[i] == NULL )
182   continue;
183
184  if ( (fh = g_clients[i]->fh) != -1 ) {
185   if ( FD_ISSET(fh, &r) ) {
186    if ( g_clients[i]->execed == -1 ) {
187     clients_check(i);
188/*
189    } else {
190     streams_check(g_clients[i]->execed);
191*/
192    }
193   }
194
195   if ( FD_ISSET(fh, &e) ) {
196    clients_delete(i);
197    continue;
198   }
199  }
200
201  if ( g_clients[i] == NULL )
202   continue;
203
204  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
205   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
206   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
207    break;
208   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
209    if ( FD_ISSET(fh, &r) ) {
210     streams_check(g_clients[i]->streams[j]);
211    }
212   }
213  }
214 }
215
216 ROAR_DBG("clients_check_all(void) = %i // have value", have);
217 return have;
218}
219
220int clients_check     (int id) {
221 struct roar_message    m;
222 struct roar_connection con;
223 char * data = NULL;
224 int oldcmd;
225 int r;
226
227 if ( g_clients[id] == NULL )
228  return -1;
229 if ( g_clients[id]->fh == -1 )
230  return -1;
231
232 con.fh = g_clients[id]->fh;
233
234 r = roar_recv_message(&con, &m, &data);
235
236 if ( r == -1 ) { // should we drop the client?
237  clients_delete(id);
238  return -1;
239 }
240
241 roar_debug_message_print(&m);
242
243 oldcmd = m.cmd;
244
245 if ( (r = command_exec(id, &m, data)) == -1 ) {
246  m.cmd     = ROAR_CMD_ERROR;
247  m.datalen = 0;
248  ROAR_DBG("clients_check(*): Exec of command faild!");
249 } else {
250  if ( m.cmd == oldcmd ) {
251   m.cmd     = ROAR_CMD_OK;
252   m.datalen = 0;
253  }
254 }
255
256 roar_send_message(&con, &m, NULL);
257
258 if ( data )
259  free(data);
260
261 ROAR_DBG("clients_check(id=%i) = 0", id);
262 return 0;
263}
264
265int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
266 int i;
267 int fh;
268
269 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
270  if ( g_clients[i] == NULL )
271   continue;
272
273  if ( (fh = g_clients[i]->fh) == -1 )
274   continue;
275
276  if ( g_clients[i]->execed == -1 ) {
277   // TODO: add some code to send a message to the client insetd of the raw data.
278  } else {
279//   streams_check(g_clients[i]->execed);
280   streams_send_mon(g_clients[i]->execed);
281//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
282//    clients_delete(i); // delete client in case we could not write
283  }
284 }
285
286 return -1;
287}
288
289int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
290 int i;
291 int fh;
292
293 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
294  if ( g_clients[i] == NULL )
295   continue;
296
297  if ( (fh = g_clients[i]->fh) == -1 )
298   continue;
299
300  if ( g_clients[i]->execed == -1 ) {
301   // TODO: add some code to send a message to the client insetd of the raw data.
302  } else {
303//   streams_check(g_clients[i]->execed);
304   streams_send_filter(g_clients[i]->execed);
305//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
306//    clients_delete(i); // delete client in case we could not write
307  }
308 }
309
310 return -1;
311}
312
313int client_stream_exec   (int client, int stream) {
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   g_clients[client]->execed = stream;
322   streams_set_fh(stream, g_clients[client]->fh);
323   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
324   return 0;
325  }
326 }
327
328 return -1;
329}
330
331int client_stream_set_fh (int client, int stream, int fh) {
332 int i;
333
334 if ( g_clients[client] == NULL )
335  return -1;
336
337 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
338  if ( g_clients[client]->streams[i] == stream ) {
339   streams_set_fh(stream, fh);
340   return 0;
341  }
342 }
343
344 return -1;
345}
346
347int client_stream_add    (int client, int stream) {
348 int i;
349
350 if ( g_clients[client] == NULL )
351  return -1;
352
353 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
354  if ( g_clients[client]->streams[i] == -1 ) {
355   g_clients[client]->streams[i] = stream;
356   streams_set_client(stream, client);
357   return 0;
358  }
359 }
360
361 return -1;
362}
363
364int client_stream_delete (int client, int stream) {
365 int i;
366
367 if ( g_clients[client] == NULL )
368  return -1;
369
370 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
371  if ( g_clients[client]->streams[i] == stream ) {
372   g_clients[client]->streams[i] = -1;
373
374   if ( stream == g_clients[client]->execed ) {
375    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
376    clients_delete(client);
377   }
378
379   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
380   return 0;
381  }
382 }
383
384 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
385 return -1;
386}
387
388//ll
Note: See TracBrowser for help on using the repository browser.