Browse code

core: removed STUN from core

- Added hooks so that STUN messages can be passed to a module
- Removed STUN configuration file parameters

Peter Dunkley authored on 29/03/2013 21:47:57
Showing 12 changed files
... ...
@@ -647,8 +647,6 @@ data_target = $(prefix)/$(data_dir)
647 647
 # -DPROFILING
648 648
 #		if enabled profiling will be enabled for child processes
649 649
 #		Don't forget to set PROFILE (see below)
650
-# -DUSE_STUN
651
-#		compiles in stun support
652 650
 # -DNO_SIG_DEBUG
653 651
 #        turns off debugging messages in signal handlers (which might be 
654 652
 #         unsafe)
... ...
@@ -752,10 +750,6 @@ ifeq ($(WITHAS), 1)
752 752
 	C_DEFS+= -DWITH_AS_SUPPORT
753 753
 endif
754 754
 
755
-ifneq ($(STUN),)
756
-	C_DEFS+= -DUSE_STUN
757
-endif
758
-
759 755
 ifeq ($(mode),)
760 756
 	mode = release
761 757
 endif
... ...
@@ -2100,11 +2094,6 @@ LIBS+= -L$(LOCALBASE)/lib -L$(LOCALBASE)/ssl/lib -lssl -lcrypto \
2100 2100
 #       E.g.: make CORE_TLS=1 EXTRA_TLS_LIBS="-lz -lkrb5"
2101 2101
 endif
2102 2102
 
2103
-ifneq ($(STUN),)
2104
-C_INCLUDES+= -I$(LOCALBASE)/ssl/include
2105
-LIBS+= -L$(LOCALBASE)/lib -L$(LOCALBASE)/ssl/lib -lcrypto
2106
-endif
2107
-
2108 2103
 ifneq ($(found_lock_method), yes)
2109 2104
 $(warning	No locking method found so far, trying SYS V sems)
2110 2105
 		C_DEFS+= -DUSE_SYSV_SEM  # try sys v sems
... ...
@@ -498,11 +498,6 @@ PVBUFSLOTS		"pv_buffer_slots"
498 498
 HTTP_REPLY_HACK		"http_reply_hack"
499 499
 VERSION_TABLE_CFG		"version_table"
500 500
 
501
-/* stun config variables */
502
-STUN_REFRESH_INTERVAL "stun_refresh_interval"
503
-STUN_ALLOW_STUN "stun_allow_stun"
504
-STUN_ALLOW_FP "stun_allow_fp"
505
-
506 501
 SERVER_ID     "server_id"
507 502
 
508 503
 LATENCY_LOG				latency_log
... ...
@@ -985,10 +980,6 @@ IMPORTFILE      "import_file"
985 985
 <INITIAL>{LOADPATH}		{ count(); yylval.strval=yytext; return LOADPATH; }
986 986
 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
987 987
 
988
-<INITIAL>{STUN_REFRESH_INTERVAL} { count(); yylval.strval=yytext; return STUN_REFRESH_INTERVAL;}
989
-<INITIAL>{STUN_ALLOW_STUN} { count(); yylval.strval=yytext; return STUN_ALLOW_STUN;}
990
-<INITIAL>{STUN_ALLOW_FP} { count(); yylval.strval=yytext; return STUN_ALLOW_FP;}
991
-
992 988
 <INITIAL>{EQUAL}	{ count(); return EQUAL; }
993 989
 <INITIAL>{ADDEQ}          { count(); return ADDEQ; }
994 990
 <INITIAL>{EQUAL_T}	{ count(); return EQUAL_T; }
... ...
@@ -195,12 +195,6 @@
195 195
 	#define IF_DST_BLACKLIST(x) warn("dst blacklist support not compiled in")
196 196
 #endif
197 197
 
198
-#ifdef USE_STUN
199
-	#define IF_STUN(x) x
200
-#else 
201
-	#define IF_STUN(x) warn("stun support not compiled in")
202
-#endif
203
-
204 198
 #ifdef USE_SCTP
205 199
 	#define IF_SCTP(x) x
206 200
 #else
... ...
@@ -586,10 +580,6 @@ extern char *finame;
586 586
 %token ADDEQ
587 587
 
588 588
 
589
-%token STUN_REFRESH_INTERVAL
590
-%token STUN_ALLOW_STUN
591
-%token STUN_ALLOW_FP
592
-
593 589
 /*pre-processor*/
594 590
 %token SUBST
595 591
 %token SUBSTDEF
... ...
@@ -1708,12 +1698,6 @@ assign_stm:
1708 1708
 	| PVBUFSLOTS EQUAL error { yyerror("number expected"); }
1709 1709
 	| HTTP_REPLY_HACK EQUAL NUMBER { http_reply_hack=$3; }
1710 1710
 	| HTTP_REPLY_HACK EQUAL error { yyerror("boolean value expected"); }
1711
-	| STUN_REFRESH_INTERVAL EQUAL NUMBER { IF_STUN(stun_refresh_interval=$3); }
1712
-	| STUN_REFRESH_INTERVAL EQUAL error{ yyerror("number expected"); }
1713
-	| STUN_ALLOW_STUN EQUAL NUMBER { IF_STUN(stun_allow_stun=$3); }
1714
-	| STUN_ALLOW_STUN EQUAL error{ yyerror("number expected"); }
1715
-	| STUN_ALLOW_FP EQUAL NUMBER { IF_STUN(stun_allow_fp=$3) ; }
1716
-	| STUN_ALLOW_FP EQUAL error{ yyerror("number expected"); }
1717 1711
     | SERVER_ID EQUAL NUMBER { server_id=$3; }
1718 1712
     | LATENCY_LOG EQUAL NUMBER { default_core_cfg.latency_log=$3; }
1719 1713
 	| LATENCY_LOG EQUAL error  { yyerror("number  expected"); }
... ...
@@ -106,6 +106,11 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
106 106
 					_sr_events_list.tcp_ws_frame_out = f;
107 107
 				else return -1;
108 108
 			break;
109
+		case SREV_STUN_IN:
110
+				if(_sr_events_list.stun_in==0)
111
+					_sr_events_list.stun_in = f;
112
+				else return -1;
113
+			break;
109 114
 		default:
110 115
 			return -1;
111 116
 	}
... ...
@@ -209,6 +214,12 @@ int sr_event_exec(int type, void *data)
209 209
 					ret = _sr_events_list.tcp_ws_frame_out(data);
210 210
 					return ret;
211 211
 				} else return 1;
212
+		case SREV_STUN_IN:
213
+				if(unlikely(_sr_events_list.stun_in!=0))
214
+				{
215
+					ret = _sr_events_list.stun_in(data);
216
+					return ret;
217
+				} else return 1;
212 218
 		default:
213 219
 			return -1;
214 220
 	}
... ...
@@ -242,6 +253,8 @@ int sr_event_enabled(int type)
242 242
 				return (_sr_events_list.tcp_ws_frame_in!=0)?1:0;
243 243
 		case SREV_TCP_WS_FRAME_OUT:
244 244
 				return (_sr_events_list.tcp_ws_frame_out!=0)?1:0;
245
+		case SREV_STUN_IN:
246
+				return (_sr_events_list.stun_in!=0)?1:0;
245 247
 	}
246 248
 	return 0;
247 249
 }
... ...
@@ -28,12 +28,13 @@
28 28
 #define SREV_CORE_STATS			3
29 29
 #define SREV_CFG_RUN_ACTION		4
30 30
 #define SREV_PKG_SET_USED		5
31
-#define SREV_PKG_SET_REAL_USED	6
31
+#define SREV_PKG_SET_REAL_USED		6
32 32
 #define SREV_NET_DGRAM_IN		7
33 33
 #define SREV_TCP_HTTP_100C		8
34 34
 #define SREV_TCP_MSRP_FRAME		9
35 35
 #define SREV_TCP_WS_FRAME_IN		10
36 36
 #define SREV_TCP_WS_FRAME_OUT		11
37
+#define SREV_STUN_IN			12
37 38
 
38 39
 
39 40
 typedef int (*sr_event_cb_f)(void *data);
... ...
@@ -50,6 +51,7 @@ typedef struct sr_event_cb {
50 50
 	sr_event_cb_f tcp_msrp_frame;
51 51
 	sr_event_cb_f tcp_ws_frame_in;
52 52
 	sr_event_cb_f tcp_ws_frame_out;
53
+	sr_event_cb_f stun_in;
53 54
 } sr_event_cb_t;
54 55
 
55 56
 void sr_event_cb_init(void);
... ...
@@ -136,12 +136,6 @@ extern int mcast_loopback;
136 136
 extern int mcast_ttl;
137 137
 #endif /* USE_MCAST */
138 138
 
139
-#ifdef USE_STUN
140
-extern unsigned int stun_refresh_interval;
141
-extern int stun_allow_stun;
142
-extern int stun_allow_fp;
143
-#endif
144
-
145 139
 #ifdef USE_IPV6
146 140
 extern int auto_bind_ipv6;
147 141
 #endif
... ...
@@ -504,15 +504,6 @@ unsigned short port_no=0; /* default port*/
504 504
 unsigned short tls_port_no=0; /* default port */
505 505
 #endif
506 506
 
507
-#ifdef USE_STUN
508
-/* refresh interval in miliseconds */
509
-unsigned int stun_refresh_interval=0;
510
-/* stun can be switch off even if it is compiled */
511
-int stun_allow_stun=1;
512
-/* use or don't use fingerprint */
513
-int stun_allow_fp=1;
514
-#endif
515
-
516 507
 struct host_alias* aliases=0; /* name aliases list */
517 508
 
518 509
 /* Parameter to child_init */
519 510
new file mode 100644
... ...
@@ -0,0 +1,46 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2013 Crocodile RCS Ltd
4
+ *
5
+ * This file is part of Kamailio, a free SIP server.
6
+ *
7
+ * Kamailio is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * Kamailio is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License 
18
+ * along with this program; if not, write to the Free Software 
19
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
+ *
21
+ */
22
+
23
+#include "events.h"
24
+#include "ip_addr.h"
25
+#include "stun.h"
26
+
27
+int stun_process_msg(char* buf, unsigned int len, struct receive_info* ri)
28
+{
29
+        int ret;
30
+	stun_event_info_t sev;
31
+
32
+        ret = 0;
33
+        LM_DBG("STUN Message: [[>>>\n%.*s<<<]]\n", len, buf);
34
+        if(likely(sr_event_enabled(SREV_STUN_IN))) {
35
+		memset(&sev, 0, sizeof(stun_event_info_t));
36
+		sev.buf = buf;
37
+		sev.len = len;
38
+		sev.rcv = ri;
39
+                ret = sr_event_exec(SREV_STUN_IN, (void *) &sev);
40
+        } else {
41
+                LM_DBG("no callback registering for handling STUN -"
42
+			" dropping!\n");
43
+        }
44
+        return ret;
45
+}
0 46
new file mode 100644
... ...
@@ -0,0 +1,64 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Portions Copyright (C) 2013 Crocodile RCS Ltd
4
+ *
5
+ * Based on "ser_stun.h". Copyright (C) 2001-2003 FhG Fokus
6
+ *
7
+ * This file is part of Kamailio, a free SIP server.
8
+ *
9
+ * Kamailio is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * Kamailio 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 program; if not, write to the Free Software 
21
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
+ *
23
+ */
24
+
25
+#ifndef _STUN_H
26
+#define _STUN_H
27
+
28
+#include "ip_addr.h"
29
+
30
+/* type redefinition */
31
+typedef unsigned char UCHAR_T;
32
+typedef unsigned short USHORT_T;
33
+typedef unsigned int UINT_T;
34
+typedef unsigned long ULONG_T;
35
+
36
+#define MAGIC_COOKIE	0x2112A442
37
+#define TRANSACTION_ID	12
38
+
39
+struct transaction_id {
40
+        UINT_T magic_cookie;
41
+        UCHAR_T id[TRANSACTION_ID];
42
+};
43
+
44
+struct stun_hdr {
45
+        USHORT_T type;
46
+        USHORT_T len;
47
+        struct transaction_id id;
48
+};
49
+
50
+struct stun_attr {
51
+        USHORT_T type;
52
+        USHORT_T len;
53
+};
54
+
55
+typedef struct stun_event_info {
56
+	char *buf;
57
+	unsigned int len;
58
+	struct receive_info *rcv;
59
+} stun_event_info_t;
60
+
61
+int stun_process_msg(char* buf, unsigned int len, struct receive_info* ri);
62
+
63
+#endif /* _STUN_H */
... ...
@@ -97,13 +97,7 @@
97 97
 #include "tsend.h"
98 98
 #include "forward.h"
99 99
 #include "events.h"
100
-
101
-#ifdef USE_STUN
102
-#include "ser_stun.h"
103
-
104
-int is_msg_complete(struct tcp_req* r);
105
-
106
-#endif /* USE_STUN */
100
+#include "stun.h"
107 101
 
108 102
 #ifdef READ_HTTP11
109 103
 #define HTTP11CONTINUE	"HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"
... ...
@@ -123,6 +117,8 @@ static int tcpmain_sock=-1;
123 123
 static struct local_timer tcp_reader_ltimer;
124 124
 static ticks_t tcp_reader_prev_ticks;
125 125
 
126
+int is_msg_complete(struct tcp_req* r);
127
+
126 128
 /**
127 129
  * control cloning of TCP receive buffer
128 130
  * - needed for operations working directly inside the buffer
... ...
@@ -373,11 +369,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
373 373
 	int bytes, remaining;
374 374
 	char *p;
375 375
 	struct tcp_req* r;
376
-
377
-#ifdef USE_STUN
378 376
 	unsigned int mc;   /* magic cookie */
379 377
 	unsigned short body_len;
380
-#endif
381 378
 
382 379
 #ifdef READ_MSRP
383 380
 	char *mfline;
... ...
@@ -610,18 +603,16 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
610 610
 						r->start=p;
611 611
 						break;
612 612
 					default:
613
-#ifdef USE_STUN
614
-						/* STUN support can be switched off even if it's compiled */
615 613
 						/* stun test */						
616
-						if (stun_allow_stun && (unsigned char)*p == 0x00) {
614
+						if (unlikely(sr_event_enabled(SREV_STUN_IN)) && (unsigned char)*p == 0x00) {
617 615
 							r->state=H_STUN_MSG;
618 616
 						/* body will used as pointer to the last used byte */
619 617
 							r->body=p;
620 618
 							r->content_len = 0;
621 619
 							DBG("stun msg detected\n");
622
-						}else
623
-#endif
624
-						r->state=H_SKIP;
620
+						} else {
621
+							r->state=H_SKIP;
622
+						}
625 623
 						r->start=p;
626 624
 				};
627 625
 				p++;
... ...
@@ -656,7 +647,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
656 656
 					r->state = H_SKIP_EMPTY;
657 657
 				}
658 658
 				break;
659
-#ifdef USE_STUN
659
+
660 660
 			case H_STUN_MSG:
661 661
 				if ((r->pos - r->body) >= sizeof(struct stun_hdr)) {
662 662
 					/* copy second short from buffer where should be body 
... ...
@@ -688,8 +679,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
688 688
 						}
689 689
 						else {
690 690
 							/* set content_len to length of fingerprint */
691
-							body_len = sizeof(struct stun_attr) + 
692
-									   SHA_DIGEST_LENGTH;
691
+							body_len = sizeof(struct stun_attr) + 20;
692
+							/* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
693 693
 						}
694 694
 					}
695 695
 					r->content_len=body_len;
... ...
@@ -711,7 +702,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
711 711
 					}
712 712
 					else {
713 713
 						/* set content_len to length of fingerprint */
714
-						body_len = sizeof(struct stun_attr)+SHA_DIGEST_LENGTH;
714
+						body_len = sizeof(struct stun_attr) + 20;
715
+						/* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
715 716
 						r->content_len=body_len;
716 717
 					}
717 718
 				}
... ...
@@ -736,7 +728,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
736 736
 					p = r->pos;
737 737
 				}
738 738
 				break;
739
-#endif /* USE_STUN */
739
+
740 740
 			change_state_case(H_CONT_LEN1,  'O', 'o', H_CONT_LEN2);
741 741
 			change_state_case(H_CONT_LEN2,  'N', 'n', H_CONT_LEN3);
742 742
 			change_state_case(H_CONT_LEN3,  'T', 't', H_CONT_LEN4);
... ...
@@ -1350,14 +1342,11 @@ again:
1350 1350
 					LOG(L_ERR, "CRLF ping: tcp_send() failed\n");
1351 1351
 				}
1352 1352
 				ret = 0;
1353
-			}else
1354
-#ifdef USE_STUN
1355
-			if (unlikely(req->state==H_STUN_END)){
1353
+			} else if (unlikely(req->state==H_STUN_END)) {
1356 1354
 				/* stun request */
1357 1355
 				ret = stun_process_msg(req->start, req->parsed-req->start,
1358 1356
 									 &con->rcv);
1359
-			}else
1360
-#endif
1357
+			} else
1361 1358
 #ifdef READ_MSRP
1362 1359
 			// if (unlikely(req->flags&F_TCP_REQ_MSRP_FRAME)){
1363 1360
 			if (unlikely(req->state==H_MSRP_FINISH)){
... ...
@@ -1762,8 +1751,6 @@ error:
1762 1762
 }
1763 1763
 
1764 1764
 
1765
-
1766
-#ifdef USE_STUN
1767 1765
 int is_msg_complete(struct tcp_req* r)
1768 1766
 {
1769 1767
 	if (TCP_REQ_HAS_CLEN(r)) {
... ...
@@ -1778,6 +1765,5 @@ int is_msg_complete(struct tcp_req* r)
1778 1778
 		return 1;
1779 1779
 	}
1780 1780
 }
1781
-#endif
1782 1781
 
1783 1782
 #endif /* USE_TCP */
... ...
@@ -76,15 +76,12 @@
76 76
 #include "ip_addr.h"
77 77
 #include "cfg/cfg_struct.h"
78 78
 #include "events.h"
79
+#include "stun.h"
79 80
 #ifdef USE_RAW_SOCKS
80 81
 #include "raw_sock.h"
81 82
 #endif /* USE_RAW_SOCKS */
82 83
 
83 84
 
84
-#ifdef USE_STUN
85
-  #include "ser_stun.h"
86
-#endif
87
-
88 85
 #ifdef DBG_MSG_QA
89 86
 /* message quality assurance -- frequently, bugs in ser have
90 87
    been indicated by zero characters or long whitespaces
... ...
@@ -501,19 +498,14 @@ int udp_rcv_loop()
501 501
 			}
502 502
 		}
503 503
 #ifndef NO_ZERO_CHECKS
504
-#ifdef USE_STUN
505
-		/* STUN support can be switched off even if it's compiled */
506
-		if (stun_allow_stun == 0 || (unsigned char)*buf != 0x00) {
507
-#endif
508
-		  if (len<MIN_UDP_PACKET) {
509
-			  tmp=ip_addr2a(&ri.src_ip);
510
-			  DBG("udp_rcv_loop: probing packet received from %s %d\n",
511
-				  	tmp, htons(ri.src_port));
512
-			  continue;
513
-		  }
514
-#ifdef USE_STUN
504
+		if (!unlikely(sr_event_enabled(SREV_STUN_IN)) || (unsigned char)*buf != 0x00) {
505
+			if (len<MIN_UDP_PACKET) {
506
+				tmp=ip_addr2a(&ri.src_ip);
507
+				DBG("udp_rcv_loop: probing packet received from %s %d\n",
508
+					tmp, htons(ri.src_port));
509
+				continue;
510
+			}
515 511
 		}
516
-#endif
517 512
 /* historically, zero-terminated packets indicated a bug in clients
518 513
  * that calculated wrongly packet length and included string-terminating
519 514
  * zero; today clients exist with legitimate binary payloads and we
... ...
@@ -544,17 +536,15 @@ int udp_rcv_loop()
544 544
 		
545 545
 		/* update the local config */
546 546
 		cfg_update();
547
-#ifdef USE_STUN
548
-			/* STUN support can be switched off even if it's compiled */
549
-			if (stun_allow_stun && (unsigned char)*buf == 0x00) {
550
-			    /* stun_process_msg releases buf memory if necessary */
551
-				if ((stun_process_msg(buf, len, &ri)) != 0) {
552
-					continue; /* some error occurred */
553
-				}
554
-			} else
555
-#endif
556
-		/* receive_msg must free buf too!*/
557
-		receive_msg(buf, len, &ri);
547
+		if (unlikely(sr_event_enabled(SREV_STUN_IN)) && (unsigned char)*buf == 0x00) {
548
+			/* stun_process_msg releases buf memory if necessary */
549
+			if ((stun_process_msg(buf, len, &ri)) != 0) {
550
+				continue; /* some error occurred */
551
+			}
552
+		} else {
553
+			/* receive_msg must free buf too!*/
554
+			receive_msg(buf, len, &ri);
555
+		}
558 556
 		
559 557
 	/* skip: do other stuff */
560 558
 		
... ...
@@ -291,12 +291,6 @@
291 291
 #define NO_SIG_DEBUG_STR ""
292 292
 #endif
293 293
 
294
-#ifdef USE_STUN
295
-#define USE_STUN_STR ", USE_STUN"
296
-#else
297
-#define USE_STUN_STR ""
298
-#endif
299
-
300 294
 #ifdef HAVE_RESOLV_RES 
301 295
 #define HAVE_RESOLV_RES_STR ", HAVE_RESOLV_RES"
302 296
 #else
... ...
@@ -336,7 +330,7 @@
336 336
 #define SER_COMPILE_FLAGS \
337 337
 	STATS_STR EXTRA_DEBUG_STR USE_IPV6_STR USE_TCP_STR USE_TLS_STR \
338 338
 	USE_SCTP_STR CORE_TLS_STR TLS_HOOKS_STR  USE_RAW_SOCKS_STR \
339
-	USE_STUN_STR DISABLE_NAGLE_STR USE_MCAST_STR NO_DEBUG_STR NO_LOG_STR \
339
+	DISABLE_NAGLE_STR USE_MCAST_STR NO_DEBUG_STR NO_LOG_STR \
340 340
 	NO_SIG_DEBUG_STR DNS_IP_HACK_STR  SHM_MEM_STR SHM_MMAP_STR PKG_MALLOC_STR \
341 341
 	F_MALLOC_STR DL_MALLOC_STR SF_MALLOC_STR  LL_MALLOC_STR \
342 342
 	USE_SHM_MEM_STR \