Browse code

added functions to allow myself check ony based on port numbers

Nils Ohlmeier authored on 17/04/2008 20:52:47
Showing 4 changed files
... ...
@@ -276,6 +276,19 @@ found:
276 276
 	return 1;
277 277
 }
278 278
 
279
+/* checks if the proto:port is one of the ports we listen on;
280
+ * if proto==0 (PROTO_NONE) the protocol is ignored
281
+ * returns 1 if true, 0 if false, -1 on error
282
+ */
283
+int check_self_port(unsigned short port, unsigned short proto)
284
+{
285
+	if (grep_sock_info_by_port(port, proto))
286
+		/* as aliases do not contain different ports we can skip them */
287
+		return 1;
288
+	else
289
+		return 0;
290
+}
291
+
279 292
 
280 293
 
281 294
 /* forwards a request to dst
... ...
@@ -64,6 +64,7 @@ struct socket_info* get_send_socket(struct sip_msg* msg,
64 64
 									union sockaddr_union* su, int proto);
65 65
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
66 66
 int check_self(str* host, unsigned short port, unsigned short proto);
67
+int check_self_port(unsigned short port, unsigned short proto);
67 68
 int forward_request( struct sip_msg* msg, str* dst,  unsigned short port,
68 69
 						struct dest_info* send_info);
69 70
 int update_sock_struct_from_via( union sockaddr_union* to,
... ...
@@ -188,7 +188,6 @@ static struct socket_info** get_sock_info_list(unsigned short proto)
188 188
 }
189 189
 
190 190
 
191
-
192 191
 /* checks if the proto: host:port is one of the address we listen on
193 192
  * and returns the corresponding socket_info structure.
194 193
  * if port==0, the  port number is ignored
... ...
@@ -279,6 +278,48 @@ found:
279 278
 	return si;
280 279
 }
281 280
 
281
+/* checks if the proto:port is one of the ports we listen on
282
+ * and returns the corresponding socket_info structure.
283
+ * if proto==0 (PROTO_NONE) the protocol is ignored
284
+ * returns  0 if not found
285
+ */
286
+struct socket_info* grep_sock_info_by_port(unsigned short port, 
287
+											unsigned short proto)
288
+{
289
+	struct socket_info* si;
290
+	struct socket_info** list;
291
+	unsigned short c_proto;
292
+
293
+	if (!port) {
294
+		goto not_found;
295
+	}
296
+	c_proto=proto?proto:PROTO_UDP;
297
+	do{
298
+		/* get the proper sock_list */
299
+		if (c_proto==PROTO_NONE)
300
+			list=&udp_listen;
301
+		else
302
+			list=get_sock_info_list(c_proto);
303
+	
304
+		if (list==0){
305
+			LOG(L_WARN, "WARNING: grep_sock_info_by_port: "
306
+						"unknown proto %d\n", c_proto);
307
+			goto not_found; /* false */
308
+		}
309
+		for (si=*list; si; si=si->next){
310
+			DBG("grep_sock_info_by_port - checking if port %d matches port %d\n", 
311
+					si->port_no, port);
312
+			if (si->port_no==port) {
313
+				goto found;
314
+			}
315
+		}
316
+	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
317
+not_found:
318
+	return 0;
319
+found:
320
+	return si;
321
+}
322
+
282 323
 
283 324
 
284 325
 /* checks if the proto: ip:port is one of the address we listen on
... ...
@@ -69,6 +69,8 @@ void print_aliases();
69 69
 
70 70
 struct socket_info* grep_sock_info(str* host, unsigned short port,
71 71
 										unsigned short proto);
72
+struct socket_info* grep_sock_info_by_port(unsigned short port,
73
+											unsigned short proto);
72 74
 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
73 75
 												unsigned short proto);
74 76