Browse code

core: new core event for received datagrams

- event type defined as SREV_NET_DGRAM_IN
- allow modules to get a callback when a udp datagram is received
- offers the possibility to handle different content sent via datagram,
such as HEP from Homer project, without invading the core with new
code

Daniel-Constantin Mierla authored on 14/08/2011 20:59:53
Showing 3 changed files
... ...
@@ -81,6 +81,11 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
81 81
 					_sr_events_list.pkg_set_real_used = f;
82 82
 				else return -1;
83 83
 			break;
84
+		case SREV_NET_DGRAM_IN:
85
+				if(_sr_events_list.net_dgram_in==0)
86
+					_sr_events_list.net_dgram_in = f;
87
+				else return -1;
88
+			break;
84 89
 		default:
85 90
 			return -1;
86 91
 	}
... ...
@@ -154,6 +159,12 @@ int sr_event_exec(int type, void *data)
154 154
 					ret = _sr_events_list.pkg_set_real_used(data);
155 155
 					return ret;
156 156
 				} else return 1;
157
+		case SREV_NET_DGRAM_IN:
158
+				if(unlikely(_sr_events_list.net_dgram_in!=0))
159
+				{
160
+					ret = _sr_events_list.net_dgram_in(data);
161
+					return ret;
162
+				} else return 1;
157 163
 		default:
158 164
 			return -1;
159 165
 	}
... ...
@@ -29,6 +29,7 @@
29 29
 #define SREV_CFG_RUN_ACTION		4
30 30
 #define SREV_PKG_SET_USED		5
31 31
 #define SREV_PKG_SET_REAL_USED	6
32
+#define SREV_NET_DGRAM_IN		7
32 33
 
33 34
 typedef int (*sr_event_cb_f)(void *data);
34 35
 
... ...
@@ -39,6 +40,7 @@ typedef struct sr_event_cb {
39 39
 	sr_event_cb_f run_action;
40 40
 	sr_event_cb_f pkg_set_used;
41 41
 	sr_event_cb_f pkg_set_real_used;
42
+	sr_event_cb_f net_dgram_in;
42 43
 } sr_event_cb_t;
43 44
 
44 45
 void sr_event_cb_init(void);
... ...
@@ -75,6 +75,7 @@
75 75
 #include "mem/mem.h"
76 76
 #include "ip_addr.h"
77 77
 #include "cfg/cfg_struct.h"
78
+#include "events.h"
78 79
 #ifdef USE_RAW_SOCKS
79 80
 #include "raw_sock.h"
80 81
 #endif /* USE_RAW_SOCKS */
... ...
@@ -475,6 +476,17 @@ int udp_rcv_loop()
475 475
 		su2ip_addr(&ri.src_ip, from);
476 476
 		ri.src_port=su_getport(from);
477 477
 
478
+		if(unlikely(sr_event_enabled(SREV_NET_DGRAM_IN)))
479
+		{
480
+			void *sredp[3];
481
+			sredp[0] = (void*)buf;
482
+			sredp[1] = (void*)(&len);
483
+			sredp[2] = (void*)(&ri);
484
+			if(sr_event_exec(SREV_NET_DGRAM_IN, (void*)sredp)<0) {
485
+				/* data handled by callback - continue to next packet */
486
+				continue;
487
+			}
488
+		}
478 489
 #ifndef NO_ZERO_CHECKS
479 490
 #ifdef USE_STUN
480 491
 		/* STUN support can be switched off even if it's compiled */