Browse code

- aliases proto support

Andrei Pelinescu-Onciul authored on 21/10/2003 23:28:46
Showing 7 changed files
... ...
@@ -2,6 +2,9 @@ $Id$
2 2
 
3 3
 ( - todo, x - done)
4 4
 
5
+- fix aliases for tls_port (add_them?)
6
+- fix check_sel_op -- add proto for uri proto checks
7
+
5 8
 - alias support fot tcp/tls port numbers
6 9
 - warning builder set_advertised address support
7 10
 - grep parse_uri & replace with parse_sip_msg_uri (e.g do_action!)
... ...
@@ -551,7 +551,7 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
551 551
 		| ALIAS EQUAL  id_lst { 
552 552
 							for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next)
553 553
 								add_alias(lst_tmp->s, strlen(lst_tmp->s), 
554
-											lst_tmp->port);
554
+											lst_tmp->port, lst_tmp->proto);
555 555
 							  }
556 556
 		| ALIAS  EQUAL error  { yyerror(" hostname expected"); }
557 557
 		| ADVERTISED_ADDRESS EQUAL listen_id {
... ...
@@ -42,6 +42,7 @@
42 42
  *  2003-04-12  update_sock_struct_form via uses also FL_FORCE_RPORT for
43 43
  *               local replies (andrei)
44 44
  *  2003-08-21  check_self properly handles ipv6 addresses & refs   (andrei)
45
+ *  2003-10-21  check_self updated to handle proto (andrei)
45 46
  */
46 47
 
47 48
 
... ...
@@ -218,17 +219,19 @@ struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
218 218
 
219 219
 
220 220
 
221
-/* checks if the host:port is one of the address we listen on;
221
+/* checks if the proto: host:port is one of the address we listen on;
222 222
  * if port==0, the  port number is ignored
223
+ * if proto==0 (PROTO_NONE) the protocol is ignored
223 224
  * returns 1 if true, 0 if false, -1 on error
224 225
  * WARNING: uses str2ip6 so it will overwrite any previous
225 226
  *  unsaved result of this function (static buffer)
226 227
  */
227
-int check_self(str* host, unsigned short port)
228
+int check_self(str* host, unsigned short port, unsigned short proto)
228 229
 {
229 230
 	int r;
230 231
 	char* hname;
231 232
 	int h_len;
233
+	struct socket_info* si;
232 234
 #ifdef USE_IPV6
233 235
 	struct ip_addr* ip6;
234 236
 #endif
... ...
@@ -242,22 +245,39 @@ int check_self(str* host, unsigned short port)
242 242
 		h_len-=2;
243 243
 	}
244 244
 #endif
245
+	/* get teh proper sock list */
246
+	switch(proto){
247
+		case PROTO_NONE: /* we'll use udp and not all the lists FIXME: */
248
+		case PROTO_UDP:
249
+			si=sock_info;
250
+			break;
251
+#ifdef USE_TCP
252
+		case PROTO_TCP:
253
+			si=tcp_info;
254
+			break;
255
+#endif
256
+#ifdef USE_TLS
257
+		case PROTO_TLS:
258
+			si=tls_info;
259
+			break;
260
+#endif
261
+		default:
262
+			/* unknown proto */
263
+			LOG(L_WARN, "WARNING: check_self: unknown proto %d\n", proto);
264
+			return 0; /* false */
265
+	}
245 266
 	for (r=0; r<sock_no; r++){
246 267
 		DBG("check_self - checking if host==us: %d==%d && "
247 268
 				" [%.*s] == [%.*s]\n", 
248 269
 					h_len,
249
-					sock_info[r].name.len,
270
+					si[r].name.len,
250 271
 					h_len, hname,
251
-					sock_info[r].name.len, sock_info[r].name.s
272
+					si[r].name.len, si[r].name.s
252 273
 			);
253 274
 		if (port) {
254 275
 			DBG("check_self - checking if port %d matches port %d\n", 
255
-					sock_info[r].port_no, port);
256
-#ifdef USE_TLS
257
-			if  ((sock_info[r].port_no!=port) && (tls_info[r].port_no!=port)) {
258
-#else
259
-			if (sock_info[r].port_no!=port) {
260
-#endif
276
+					si[r].port_no, port);
277
+			if (si[r].port_no!=port) {
261 278
 				continue;
262 279
 			}
263 280
 		}
... ...
@@ -290,7 +310,7 @@ int check_self(str* host, unsigned short port)
290 290
 	}
291 291
 	if (r==sock_no){
292 292
 		/* try to look into the aliases*/
293
-		if (grep_aliases(hname, h_len, port)==0){
293
+		if (grep_aliases(hname, h_len, port, proto)==0){
294 294
 			DBG("check_self: host != me\n");
295 295
 			return 0;
296 296
 		}
... ...
@@ -487,7 +507,8 @@ int forward_reply(struct sip_msg* msg)
487 487
 	/*check if first via host = us */
488 488
 	if (check_via){
489 489
 		if (check_self(&msg->via1->host,
490
-					msg->via1->port?msg->via1->port:SIP_PORT)!=1){
490
+					msg->via1->port?msg->via1->port:SIP_PORT,
491
+					msg->via1->proto)!=1){
491 492
 			LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
492 493
 					" %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
493 494
 									msg->via1->port);
... ...
@@ -55,7 +55,7 @@
55 55
 
56 56
 struct socket_info* get_send_socket(union sockaddr_union* su, int proto);
57 57
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
58
-int check_self(str* host, unsigned short port);
58
+int check_self(str* host, unsigned short port, unsigned short proto);
59 59
 int forward_request( struct sip_msg* msg,  struct proxy_l* p, int proto);
60 60
 int update_sock_struct_from_via( union sockaddr_union* to,
61 61
 								 struct sip_msg* msg,
... ...
@@ -1601,7 +1601,7 @@ try_again:
1601 1601
 		/* check if we got the official name */
1602 1602
 		if (strcasecmp(he->h_name, sock_info[r].name.s)!=0){
1603 1603
 			if (add_alias(sock_info[r].name.s, sock_info[r].name.len,
1604
-							sock_info[r].port_no)<0){
1604
+							sock_info[r].port_no, 0)<0){
1605 1605
 				LOG(L_ERR, "ERROR: main: add_alias failed\n");
1606 1606
 			}
1607 1607
 			/* change the oficial name */
... ...
@@ -1616,7 +1616,7 @@ try_again:
1616 1616
 		}
1617 1617
 		/* add the aliases*/
1618 1618
 		for(h=he->h_aliases; h && *h; h++)
1619
-			if (add_alias(*h, strlen(*h), sock_info[r].port_no)<0){
1619
+			if (add_alias(*h, strlen(*h), sock_info[r].port_no, 0)<0){
1620 1620
 				LOG(L_ERR, "ERROR: main: add_alias failed\n");
1621 1621
 			}
1622 1622
 		hostent2ip_addr(&sock_info[r].address, he, 0); /*convert to ip_addr 
... ...
@@ -1643,11 +1643,11 @@ try_again:
1643 1643
 				}else{
1644 1644
 					/* add the aliases*/
1645 1645
 					if (add_alias(he->h_name, strlen(he->h_name),
1646
-									sock_info[r].port_no)<0){
1646
+									sock_info[r].port_no, 0)<0){
1647 1647
 						LOG(L_ERR, "ERROR: main: add_alias failed\n");
1648 1648
 					}
1649 1649
 					for(h=he->h_aliases; h && *h; h++)
1650
-						if (add_alias(*h,strlen(*h),sock_info[r].port_no)<0){
1650
+						if (add_alias(*h,strlen(*h),sock_info[r].port_no,0)<0){
1651 1651
 							LOG(L_ERR, "ERROR: main: add_alias failed\n");
1652 1652
 						}
1653 1653
 				}
... ...
@@ -1680,7 +1680,7 @@ try_again:
1680 1680
 								 sock_info[r].name.len)!=0))
1681 1681
 					)
1682 1682
 					add_alias(sock_info[t].name.s, sock_info[t].name.len,
1683
-								sock_info[t].port_no);
1683
+								sock_info[t].port_no, 0);
1684 1684
 						
1685 1685
 				/* free space*/
1686 1686
 				pkg_free(sock_info[t].name.s);
... ...
@@ -29,6 +29,7 @@
29 29
  * History:
30 30
  * --------
31 31
  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
32
+ *  2003-10-21  support for proto added: proto:host:port (andrei)
32 33
  */
33 34
 
34 35
 
... ...
@@ -42,6 +43,7 @@
42 42
 struct host_alias{
43 43
 	str alias;
44 44
 	unsigned short port;
45
+	unsigned short proto;
45 46
 	struct host_alias* next;
46 47
 };
47 48
 
... ...
@@ -50,19 +52,17 @@ extern struct host_alias* aliases;
50 50
 
51 51
 
52 52
 
53
-/* returns 1 if  name is in the alias list; if port=0, port no is ignored*/
54
-static inline int grep_aliases(char* name, int len, unsigned short port)
53
+/* returns 1 if  name is in the alias list; if port=0, port no is ignored
54
+ * if proto=0, proto is ignored*/
55
+static inline int grep_aliases(char* name, int len, unsigned short port,
56
+								unsigned short proto)
55 57
 {
56 58
 	struct  host_alias* a;
57 59
 	
58 60
 	for(a=aliases;a;a=a->next)
59
-#ifdef USE_TLS
60 61
 		if ((a->alias.len==len) && ((a->port==0) || (port==0) || 
61
-					(port==tls_port_no) ||
62
-#else
63
-		if ((a->alias.len==len) && ((a->port==0) || (port==0) || 
64
-#endif
65
-				(a->port==port)) && (strncasecmp(a->alias.s, name, len)==0))
62
+				(a->port==port)) && ((a->proto==0) || (proto==0) || 
63
+				(a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0))
66 64
 			return 1;
67 65
 	return 0;
68 66
 }
... ...
@@ -71,14 +71,24 @@ static inline int grep_aliases(char* name, int len, unsigned short port)
71 71
 
72 72
 /* adds an alias to the list (only if it isn't already there)
73 73
  * if port==0, the alias will match all the ports
74
- * returns 1 if a new alias was added, 0 if the alias was already on the list
75
- * and  -1 on error */
76
-static inline int add_alias(char* name, int len, unsigned short port)
74
+ * if proto==0, the alias will match all the protocols
75
+ * returns 1 if a new alias was added, 0 if a matching alias was already on
76
+ * the list and  -1 on error */
77
+static inline int add_alias(char* name, int len, unsigned short port, 
78
+								unsigned short proto)
77 79
 {
78 80
 	struct host_alias* a;
79 81
 	
80
-	if ((port) && grep_aliases(name,len, port)) return 0;
81
-	a=0;
82
+	if ((port) && (proto)){
83
+		/* don't add if there is already an alias matching it */
84
+		if (grep_aliases(name,len, port, proto)) return 0;
85
+	}else{
86
+		/* don't add if already in the list with port or proto ==0*/
87
+		for(a=aliases;a;a=a->next)
88
+			if ((a->alias.len==len) && (a->port==port) && (a->proto==proto) &&
89
+					(strncasecmp(a->alias.s, name, len)==0))
90
+				return 0;
91
+	}
82 92
 	a=(struct host_alias*)pkg_malloc(sizeof(struct host_alias));
83 93
 	if(a==0) goto error;
84 94
 	a->alias.s=(char*)pkg_malloc(len+1);
... ...
@@ -87,6 +97,7 @@ static inline int add_alias(char* name, int len, unsigned short port)
87 87
 	memcpy(a->alias.s, name, len);
88 88
 	a->alias.s[len]=0; /* null terminate for easier printing*/
89 89
 	a->port=port;
90
+	a->proto=proto;
90 91
 	a->next=aliases;
91 92
 	aliases=a;
92 93
 	return 1;
... ...
@@ -376,7 +376,7 @@ inline static int check_self_op(int op, str* s, unsigned short p)
376 376
 {
377 377
 	int ret;
378 378
 	
379
-	ret=check_self(s, p);
379
+	ret=check_self(s, p, 0);
380 380
 	switch(op){
381 381
 		case EQUAL_OP:
382 382
 			break;