Просмотр исходного кода

- detect INADDR_ANY (0.0.0.0 or ::) listen addresses
- basic tcp support for INADDR_ANY

Andrei Pelinescu-Onciul 18 лет назад
Родитель
Сommit
a6357e2582
3 измененных файлов с 25 добавлено и 2 удалено
  1. 2 1
      ip_addr.h
  2. 5 0
      socket_info.c
  3. 18 1
      tcp_main.c

+ 2 - 1
ip_addr.h

@@ -84,7 +84,8 @@ union sockaddr_union{
 
 
 
-enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4 };
+enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
+				 SI_IS_ANY=8 };
 
 struct socket_info{
 	int socket;

+ 5 - 0
socket_info.c

@@ -34,6 +34,7 @@
  *  2003-10-22  created by andrei
  *  2004-10-10  added grep_sock_info (andrei)
  *  2004-11-08  added find_si (andrei)
+ *  2007-08-23  added detection for INADDR_ANY types of sockets (andrei)
  */
 
 
@@ -646,6 +647,10 @@ static int fix_socket_list(struct socket_info **list, int* type_flags)
 			si->flags |= SI_IS_MCAST;
 		}
 #endif /* USE_MCAST */
+		
+		/* check if INADDR_ANY */
+		if (ip_addr_any(&si->address))
+			si->flags|=SI_IS_ANY;
 
 #ifdef EXTRA_DEBUG
 		printf("              %.*s [%s]:%s%s\n", si->name.len, 

+ 18 - 1
tcp_main.c

@@ -77,6 +77,7 @@
  *                source addr/port (andrei)
  *  2007-07-26   tcp_send() and tcpconn_get() can now use a specified source
  *                addr./port (andrei)
+ *  2007-08-23   getsockname() for INADDR_ANY(SI_IS_ANY) sockets (andrei)
  */
 
 
@@ -1608,6 +1609,9 @@ inline static int send2child(struct tcp_connection* tcpconn)
 static inline int handle_new_connect(struct socket_info* si)
 {
 	union sockaddr_union su;
+	union sockaddr_union sock_name;
+	unsigned sock_name_len;
+	union sockaddr_union* dst_su;
 	struct tcp_connection* tcpconn;
 	socklen_t su_len;
 	int new_sock;
@@ -1635,8 +1639,21 @@ static inline int handle_new_connect(struct socket_info* si)
 	}
 	(*tcp_connections_no)++;
 	
+	dst_su=&si->su;
+	if (si->flags & SI_IS_ANY){
+		/* INADDR_ANY => get local dst */
+		sock_name_len=sizeof(sock_name);
+		if (getsockname(new_sock, &sock_name.s, &sock_name_len)!=0){
+			LOG(L_ERR, "ERROR: handle_new_connect:"
+						" getsockname failed: %s(%d)\n",
+						strerror(errno), errno);
+			/* go on with the 0.0.0.0 dst from the sock_info */
+		}else{
+			dst_su=&sock_name;
+		}
+	}
 	/* add socket to list */
-	tcpconn=tcpconn_new(new_sock, &su, &si->su, si, si->proto, S_CONN_ACCEPT);
+	tcpconn=tcpconn_new(new_sock, &su, dst_su, si, si->proto, S_CONN_ACCEPT);
 	if (tcpconn){
 #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
 		io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);