Browse Source

- added grep_sock_info and updated check_self to use it

Andrei Pelinescu-Onciul 21 years ago
parent
commit
463fdeec3d
5 changed files with 107 additions and 88 deletions
  1. 1 1
      Makefile.defs
  2. 3 86
      forward.c
  3. 7 0
      name_alias.h
  4. 94 0
      socket_info.c
  5. 2 1
      socket_info.h

+ 1 - 1
Makefile.defs

@@ -50,7 +50,7 @@ MAIN_NAME=ser
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL =   99
-EXTRAVERSION = -dev9
+EXTRAVERSION = -dev10
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")

+ 3 - 86
forward.c

@@ -44,6 +44,7 @@
  *  2003-08-21  check_self properly handles ipv6 addresses & refs   (andrei)
  *  2003-10-21  check_self updated to handle proto (andrei)
  *  2003-10-24  converted to the new socket_info lists (andrei)
+ *  2004-10-10  modified check_self to use grep_sock_info (andrei)
  */
 
 
@@ -230,93 +231,9 @@ struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
  */
 int check_self(str* host, unsigned short port, unsigned short proto)
 {
-	char* hname;
-	int h_len;
-	struct socket_info* si;
-	unsigned short c_proto;
-#ifdef USE_IPV6
-	struct ip_addr* ip6;
-#endif
-	
-	h_len=host->len;
-	hname=host->s;
-#ifdef USE_IPV6
-	if ((h_len>2)&&((*hname)=='[')&&(hname[h_len-1]==']')){
-		/* ipv6 reference, skip [] */
-		hname++;
-		h_len-=2;
-	}
-#endif
-	c_proto=proto?proto:PROTO_UDP;
-	do{
-		/* get the proper sock list */
-		switch(c_proto){
-			case PROTO_NONE: /* we'll use udp and not all the lists FIXME: */
-			case PROTO_UDP:
-				si=udp_listen;
-				break;
-#ifdef USE_TCP
-			case PROTO_TCP:
-				si=tcp_listen;
-				break;
-#endif
-#ifdef USE_TLS
-			case PROTO_TLS:
-				si=tls_listen;
-				break;
-#endif
-			default:
-				/* unknown proto */
-				LOG(L_WARN, "WARNING: check_self: "
-							"unknown proto %d\n", c_proto);
-				return 0; /* false */
-		}
-		for (; si; si=si->next){
-			DBG("check_self - checking if host==us: %d==%d && "
-					" [%.*s] == [%.*s]\n", 
-						h_len,
-						si->name.len,
-						h_len, hname,
-						si->name.len, si->name.s
-				);
-			if (port) {
-				DBG("check_self - checking if port %d matches port %d\n", 
-						si->port_no, port);
-				if (si->port_no!=port) {
-					continue;
-				}
-			}
-			if ( (h_len==si->name.len) && 
-				(strncasecmp(hname, si->name.s,
-						 si->name.len)==0) /*slower*/)
-				/* comp. must be case insensitive, host names
-				 * can be written in mixed case, it will also match
-				 * ipv6 addresses if we are lucky*/
-				goto found;
-		/* check if host == ip address */
-#ifdef USE_IPV6
-			/* ipv6 case is uglier, host can be [3ffe::1] */
-			ip6=str2ip6(host);
-			if (ip6){
-				if (ip_addr_cmp(ip6, &si->address))
-					goto found; /* match */
-				else
-					continue; /* no match, but this is an ipv6 address
-								 so no point in trying ipv4 */
-			}
-#endif
-			/* ipv4 */
-			if ( 	(!(si->flags&SI_IS_IP)) &&
-					(h_len==si->address_str.len) && 
-				(memcmp(hname, si->address_str.s, 
-									si->address_str.len)==0)
-				)
-				goto found;
-		}
-	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
-	
+	if (grep_sock_info(host, port, proto)) goto found;
 	/* try to look into the aliases*/
-	if (grep_aliases(hname, h_len, port, proto)==0){
+	if (grep_aliases(host->s, host->len, port, proto)==0){
 		DBG("check_self: host != me\n");
 		return 0;
 	}

+ 7 - 0
name_alias.h

@@ -59,6 +59,13 @@ static inline int grep_aliases(char* name, int len, unsigned short port,
 {
 	struct  host_alias* a;
 	
+#ifdef USE_IPV6
+	if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
+		/* ipv6 reference, skip [] */
+		name++;
+		len-=2;
+	}
+#endif
 	for(a=aliases;a;a=a->next)
 		if ((a->alias.len==len) && ((a->port==0) || (port==0) || 
 				(a->port==port)) && ((a->proto==0) || (proto==0) || 

+ 94 - 0
socket_info.c

@@ -32,6 +32,7 @@
  * History:
  * --------
  *  2003-10-22  created by andrei
+ *  2004-10-10  added grep_sock_info (andrei)
  */
 
 
@@ -185,6 +186,99 @@ static struct socket_info** get_sock_info_list(unsigned short proto)
 
 
 
+/* checks if the proto: host:port is one of the address we listen on
+ * and returns the corresponding socket_info structure.
+ * if port==0, the  port number is ignored
+ * if proto==0 (PROTO_NONE) the protocol is ignored
+ * returns  0 if not found
+ * WARNING: uses str2ip6 so it will overwrite any previous
+ *  unsaved result of this function (static buffer)
+ */
+struct socket_info* grep_sock_info(str* host, unsigned short port,
+												unsigned short proto)
+{
+	char* hname;
+	int h_len;
+	struct socket_info* si;
+	struct socket_info** list;
+	unsigned short c_proto;
+#ifdef USE_IPV6
+	struct ip_addr* ip6;
+#endif
+
+	h_len=host->len;
+	hname=host->s;
+#ifdef USE_IPV6
+	if ((h_len>2)&&((*hname)=='[')&&(hname[h_len-1]==']')){
+		/* ipv6 reference, skip [] */
+		hname++;
+		h_len-=2;
+	}
+#endif
+	c_proto=proto?proto:PROTO_UDP;
+	do{
+		/* get the proper sock_list */
+		if (c_proto==PROTO_NONE)
+			list=&udp_listen;
+		else
+			list=get_sock_info_list(c_proto);
+	
+		if (list==0){
+			LOG(L_WARN, "WARNING: grep_sock_info: "
+						"unknown proto %d\n", c_proto);
+			goto not_found; /* false */
+		}
+		for (si=*list; si; si=si->next){
+			DBG("grep_sock_info - checking if host==us: %d==%d && "
+					" [%.*s] == [%.*s]\n", 
+						h_len,
+						si->name.len,
+						h_len, hname,
+						si->name.len, si->name.s
+				);
+			if (port) {
+				DBG("grep_sock_info - checking if port %d matches port %d\n", 
+						si->port_no, port);
+				if (si->port_no!=port) {
+					continue;
+				}
+			}
+			if ( (h_len==si->name.len) && 
+				(strncasecmp(hname, si->name.s,
+						 si->name.len)==0) /*slower*/)
+				/* comp. must be case insensitive, host names
+				 * can be written in mixed case, it will also match
+				 * ipv6 addresses if we are lucky*/
+				goto found;
+		/* check if host == ip address */
+#ifdef USE_IPV6
+			/* ipv6 case is uglier, host can be [3ffe::1] */
+			ip6=str2ip6(host);
+			if (ip6){
+				if (ip_addr_cmp(ip6, &si->address))
+					goto found; /* match */
+				else
+					continue; /* no match, but this is an ipv6 address
+								 so no point in trying ipv4 */
+			}
+#endif
+			/* ipv4 */
+			if ( 	(!(si->flags&SI_IS_IP)) &&
+					(h_len==si->address_str.len) && 
+				(memcmp(hname, si->address_str.s, 
+									si->address_str.len)==0)
+				)
+				goto found;
+		}
+	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
+not_found:
+	return 0;
+found:
+	return si;
+}
+
+
+
 /* adds a new sock_info structure to the corresponding list
  * return  0 on success, -1 on error */
 int new_sock2list(char* name, unsigned short port, unsigned short proto,

+ 2 - 1
socket_info.h

@@ -57,6 +57,8 @@ int fix_all_socket_lists();
 void print_all_socket_lists();
 void print_aliases();
 
+struct socket_info* grep_sock_info(str* host, unsigned short port,
+										unsigned short proto);
 
 /* helper function:
  * returns next protocol, if the last one is reached return 0
@@ -108,5 +110,4 @@ inline static struct socket_info* get_first_socket()
 }
 
 
-
 #endif