source: roaraudio/libroar/notify.c @ 5019:86da2c425dd0

Last change on this file since 5019:86da2c425dd0 was 5019:86da2c425dd0, checked in by phi, 13 years ago

fixed a int overflow in length test

File size: 12.8 KB
Line 
1//notify.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38struct roar_subscriber {
39 uint32_t flags;
40 uint32_t event;
41 int emitter;
42 int target;
43 int target_type;
44 void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata);
45 void * userdata;
46 size_t refc;
47 unsigned int hash;
48// struct roar_subscriber * prev;
49 struct roar_subscriber * next;
50};
51
52struct roar_notify_core {
53 size_t refc;
54 size_t listc;
55 struct roar_subscriber ** lists;
56 void (*proxy)(struct roar_notify_core * core, struct roar_event * event, void * userdata);
57 void * proxy_userdata;
58};
59
60static struct roar_notify_core * _libroar_notify_core = NULL;
61
62#define _CKRCORE(ret) if ( core == NULL ) { if ( _libroar_notify_core == NULL ) { roar_err_set(ROAR_ERROR_INVAL); return (ret); } else { core = _libroar_notify_core; } }
63#define _CKICORE() _CKRCORE(-1)
64
65static unsigned int _hash_event (struct roar_notify_core * core, struct roar_event * event) {
66 unsigned int hash = 0;
67 unsigned int mask = core->listc - 1;
68
69 hash ^= event->event >>  0;
70 hash ^= event->event >>  8;
71 hash ^= event->event >> 16;
72 hash ^= event->event >> 24;
73
74 return hash & mask;
75}
76
77struct roar_notify_core * roar_notify_core_new(ssize_t lists) {
78 struct roar_notify_core * core = NULL;
79
80 switch (lists) {
81  case    1:
82  case    2:
83  case    4:
84  case    8:
85  case   16:
86  case   32:
87  case   64:
88  case  128:
89  case  256:
90  case  512:
91  case 1024:
92    // they all are ok.
93   break;
94  case  -1:
95    // handle defult:
96    lists = 16;
97   break;
98  default:
99    roar_err_set(ROAR_ERROR_INVAL);
100    return NULL;
101   break;
102 }
103
104 if ( (core = roar_mm_malloc(sizeof(struct roar_notify_core))) == NULL ) {
105  return NULL;
106 }
107
108 memset(core, 0, sizeof(struct roar_notify_core));
109
110 core->refc  = 1;
111 core->listc = lists;
112 core->lists = roar_mm_malloc(lists*sizeof(struct roar_subscriber *));
113 core->proxy = NULL;
114 core->proxy_userdata = NULL;
115
116 if ( core->lists == NULL ) {
117  roar_mm_free(core);
118  return NULL;
119 }
120
121 memset(core->lists, 0, lists*sizeof(struct roar_subscriber *));
122
123 return core;
124}
125
126int roar_notify_core_ref(struct roar_notify_core * core) {
127 _CKICORE();
128
129 core->refc++;
130
131 return 0;
132}
133
134int roar_notify_core_unref(struct roar_notify_core * core) {
135 struct roar_subscriber * cur, * next;
136 size_t i;
137
138 _CKICORE();
139
140 if ( core->refc == 0 ) {
141  ROAR_ERR("roar_notify_core_unref(core=%p): refc is zero, must be greater zero. resolving by setting to one.", core);
142  core->refc = 1;
143  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
144 }
145
146 core->refc--;
147
148 if ( core->refc > 0 ) {
149  return 0;
150 }
151
152 // if we work on the global core, reset it to NULL if we free it.
153 if ( core == _libroar_notify_core ) {
154  _libroar_notify_core = NULL;
155 }
156
157 for (i = 0; i < core->listc; i++) {
158  cur = core->lists[i];
159  while (cur != NULL) {
160   next = cur->next;
161
162   roar_mm_free(cur);
163
164   cur = next;
165  }
166 }
167
168 roar_mm_free(core->lists);
169 roar_mm_free(core);
170 return 0;
171}
172
173int roar_notify_core_new_global(ssize_t lists) {
174 if ( _libroar_notify_core != NULL ) {
175  roar_err_set(ROAR_ERROR_INVAL);
176  return -1;
177 }
178
179 if ( (_libroar_notify_core = roar_notify_core_new(lists)) == NULL )
180  return -1;
181
182 return 0;
183}
184
185int roar_notify_core_register_proxy(struct roar_notify_core * core, void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata), void * userdata) {
186 _CKICORE();
187
188 core->proxy = cb;
189 core->proxy_userdata = userdata;
190
191 return 0;
192}
193
194
195struct roar_subscriber * roar_notify_core_subscribe(struct roar_notify_core * core, struct roar_event * event, void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata), void * userdata) {
196 struct roar_subscriber * subs = NULL;
197 struct roar_subscriber * cur, * old;
198
199 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata);
200
201 _CKRCORE(NULL);
202
203 if ( event == NULL || cb == NULL ) {
204  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata);
205  roar_err_set(ROAR_ERROR_FAULT);
206  return NULL;
207 }
208
209 if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) {
210  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata);
211  return NULL;
212 }
213
214 memset(subs, 0, sizeof(struct roar_subscriber));
215
216 subs->flags       = 0;
217 subs->event       = event->event;
218 subs->emitter     = event->emitter;
219 subs->target      = event->target;
220 subs->target_type = event->target_type;
221 subs->cb          = cb;
222 subs->userdata    = userdata;
223 subs->refc        = 1;
224 subs->hash        = _hash_event(core, event);
225 subs->next        = NULL;
226
227 if ( (cur = core->lists[subs->hash]) == NULL ) {
228  core->lists[subs->hash] = subs;
229 } else {
230  old = cur;
231  while (cur != NULL) {
232   old = cur;
233   cur = cur->next;
234  }
235
236  old->next = subs;
237 }
238
239 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs);
240 return subs;
241}
242
243int roar_notify_core_unsubscribe(struct roar_notify_core * core, struct roar_subscriber * subscriber) {
244 struct roar_subscriber * cur, * old;
245
246 _CKICORE();
247
248 if ( subscriber == NULL ) {
249  roar_err_set(ROAR_ERROR_FAULT);
250  return -1;
251 }
252
253 if ( (cur = core->lists[subscriber->hash]) == subscriber ) {
254  core->lists[subscriber->hash] = core->lists[subscriber->hash]->next;
255 } else {
256  old = NULL;
257  while (cur != NULL && cur != subscriber ) {
258   old = cur;
259   cur = cur->next;
260  }
261
262  if ( cur != subscriber )
263   return -1;
264
265  old->next = cur->next;
266  roar_mm_free(cur);
267 }
268
269 return 0;
270}
271
272int roar_notify_core_emit(struct roar_notify_core * core, struct roar_event * event) {
273 struct roar_subscriber * cur;
274
275 _CKICORE();
276
277 if ( event == NULL ) {
278  roar_err_set(ROAR_ERROR_FAULT);
279  return -1;
280 }
281
282 if ( core->proxy != NULL && !(event->flags & ROAR_EVENT_FLAG_PROXYEVENT) ) {
283  core->proxy(core, event, core->proxy_userdata);
284 }
285
286 cur = core->lists[_hash_event(core, event)];
287 ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
288
289 while (cur != NULL) {
290  ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
291
292  // test if we can skip this one:
293  if ( !( (cur->event       == ROAR_NOTIFY_SPECIAL || cur->event       == event->event)   &&
294          (cur->emitter     == -1                  || cur->emitter     == event->emitter) &&
295          (cur->target      == -1                  || cur->target      == event->target)  &&
296          (cur->target_type == -1                  || cur->target_type == event->target_type)
297        ) ) {
298   cur = cur->next;
299   continue;
300  }
301
302  if ( cur->cb == NULL ) {
303   ROAR_ERR("roar_notify_core_emit(core=%p, event=%p): cur=%p, cb is set NULL, bad.", core, event, cur);
304  } else {
305   cur->cb(core, event, cur->userdata);
306  }
307  cur = cur->next;
308 }
309
310 return 0;
311}
312
313int roar_notify_core_emit_simple(uint32_t event, int emitter, int target, int target_type, int arg0, int arg1, void * arg2, ssize_t arg2_len) {
314 struct roar_event locevent;
315
316 memset(&locevent, 0, sizeof(locevent));
317
318 locevent.event = event;
319 locevent.emitter = emitter;
320 locevent.target = target;
321 locevent.target_type = target_type;
322 locevent.arg0 = arg0;
323 locevent.arg1 = arg1;
324 locevent.arg2 = arg2;
325 locevent.arg2_len = arg2_len;
326
327 if ( arg2 == NULL && (arg2_len == 0 || arg2_len == -1) ) {
328  locevent.flags |= ROAR_EVENT_FLAG_NETTRANS; // we guss packages with only int args are network transparent.
329 }
330
331 return roar_notify_core_emit(NULL, &locevent);
332}
333
334
335int roar_event_to_blob(struct roar_event * event, void * blob, size_t * len) {
336 size_t needed_len = (2 * 4) + (4 * 2);
337 uint32_t * u32 = blob;
338 uint16_t * u16 = blob;
339 size_t data_offset_neg = 0;
340
341 u16 += 4;
342
343 // error checking:
344 if ( event == NULL || blob == NULL || len == NULL )
345  return -1;
346
347 if ( *len == 0 )
348  return -1;
349
350 // calc and check length:
351 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
352  needed_len += 4;
353
354 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
355
356  // this is not a real maximum but a value good to detect all kinds of errors.
357  // a notify event should not be longer anyway.
358  if ( event->arg2_len > 32767 )
359   return -1;
360
361  needed_len += 4;
362  if ( event->arg2_len > 0 ) {
363    needed_len += event->arg2_len;
364    data_offset_neg = event->arg2_len;
365  }
366 }
367
368 if ( *len < needed_len )
369  return -1;
370
371 *len = 0;
372
373 // fill in the data...
374 memset(blob, 0, needed_len);
375
376 u32[1] = ROAR_HOST2NET32(0);
377
378 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
379  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT);
380  u16 += 2;
381 }
382
383 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
384  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA);
385 }
386
387 u32[1] = ROAR_HOST2NET32(event->event);
388
389 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
390  u32[2] = ROAR_HOST2NET32(event->event_proxy);
391
392 u16[0] = ROAR_HOST2NET16(event->emitter);
393 u16[1] = ROAR_HOST2NET16(event->target);
394 u16[2] = ROAR_HOST2NET16(event->target_type);
395 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
396  u16[3] = ROAR_HOST2NET16(event->arg2_len);
397  u16[4] = ROAR_HOST2NET16(event->arg0);
398  u16[5] = ROAR_HOST2NET16(event->arg1);
399 } else {
400  u16[3] = ROAR_HOST2NET16(0);
401 }
402
403 memcpy(blob + needed_len - data_offset_neg, event->arg2, event->arg2_len);
404
405 *len = needed_len;
406
407 return 0;
408}
409
410int roar_event_from_blob(struct roar_event * event, void * blob, size_t * len) {
411 size_t needed_len = (2 * 4) + (4 * 2);
412 uint32_t * u32 = blob;
413 uint16_t * u16 = blob;
414 uint32_t flags;
415
416 u16 += 4;
417
418 // error checking:
419 if ( event == NULL || blob == NULL || len == NULL )
420  return -1;
421
422 // check for minimum length.
423 if ( *len < needed_len )
424  return -1;
425
426 flags = ROAR_NET2HOST32(u32[0]);
427
428#if 0
429 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
430  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT);
431  u16 += 2;
432 }
433
434 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
435  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA);
436 }
437#endif
438
439 if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) {
440  needed_len += 4;
441  u16 += 2;
442 }
443
444 if ( flags & ROAR_EVENT_NETFLAG_DATA ) {
445  needed_len += 4;
446 }
447
448 // do we have a full header?
449 if ( *len < needed_len )
450  return -1;
451
452 needed_len += ROAR_NET2HOST16(u16[3]);
453
454 // is all of the event complet?
455 if ( *len < needed_len )
456  return -1;
457
458 // now we know everything is complet we can start to extract data...
459
460 *len = 0;
461
462 memset(event, 0, sizeof(struct roar_event));
463
464 if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) {
465  flags -= ROAR_EVENT_NETFLAG_PROXYEVENT;
466  event->flags |= ROAR_EVENT_FLAG_PROXYEVENT;
467 }
468
469 if ( flags & ROAR_EVENT_NETFLAG_DATA ) {
470  flags -= ROAR_EVENT_NETFLAG_DATA;
471  event->flags |= ROAR_EVENT_FLAG_NETTRANS;
472 }
473
474 // test if there are flags left we do not understand:
475 if ( flags ) {
476  return -1;
477 }
478
479 event->event = ROAR_NET2HOST32(u32[1]);
480
481 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
482  event->event_proxy = ROAR_NET2HOST32(u32[2]);
483
484 event->emitter     = ROAR_NET2HOST16(u16[0]);
485 event->target      = ROAR_NET2HOST16(u16[1]);
486 event->target_type = ROAR_NET2HOST16(u16[2]);
487 event->arg2_len    = ROAR_NET2HOST16(u16[3]);
488
489 if ( event->emitter == (uint16_t)-1)
490  event->emitter = -1;
491
492 if ( event->target == (uint16_t)-1)
493  event->target = -1;
494
495 if ( event->target_type == (uint16_t)-1)
496  event->target_type = -1;
497
498 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
499  event->arg0 = ROAR_NET2HOST16(u16[4]);
500  event->arg1 = ROAR_NET2HOST16(u16[5]);
501
502  if ( event->arg0 == (uint16_t)-1)
503   event->arg0 = -1;
504
505  if ( event->arg1 == (uint16_t)-1)
506   event->arg1 = -1;
507
508  event->arg2 = blob + needed_len - event->arg2_len;
509 } else {
510  event->arg0 = -1;
511  event->arg1 = -1;
512  event->arg2 = NULL;
513 }
514
515 *len = needed_len;
516
517 return 0;
518}
519
520
521//ll
Note: See TracBrowser for help on using the repository browser.