Browse code

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

Andrei Pelinescu-Onciul authored on 23/08/2007 20:43:58
Showing 3 changed files
... ...
@@ -84,7 +84,8 @@ union sockaddr_union{
84 84
 
85 85
 
86 86
 
87
-enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4 };
87
+enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
88
+				 SI_IS_ANY=8 };
88 89
 
89 90
 struct socket_info{
90 91
 	int socket;
... ...
@@ -34,6 +34,7 @@
34 34
  *  2003-10-22  created by andrei
35 35
  *  2004-10-10  added grep_sock_info (andrei)
36 36
  *  2004-11-08  added find_si (andrei)
37
+ *  2007-08-23  added detection for INADDR_ANY types of sockets (andrei)
37 38
  */
38 39
 
39 40
 
... ...
@@ -646,6 +647,10 @@ static int fix_socket_list(struct socket_info **list, int* type_flags)
646 646
 			si->flags |= SI_IS_MCAST;
647 647
 		}
648 648
 #endif /* USE_MCAST */
649
+		
650
+		/* check if INADDR_ANY */
651
+		if (ip_addr_any(&si->address))
652
+			si->flags|=SI_IS_ANY;
649 653
 
650 654
 #ifdef EXTRA_DEBUG
651 655
 		printf("              %.*s [%s]:%s%s\n", si->name.len, 
... ...
@@ -77,6 +77,7 @@
77 77
  *                source addr/port (andrei)
78 78
  *  2007-07-26   tcp_send() and tcpconn_get() can now use a specified source
79 79
  *                addr./port (andrei)
80
+ *  2007-08-23   getsockname() for INADDR_ANY(SI_IS_ANY) sockets (andrei)
80 81
  */
81 82
 
82 83
 
... ...
@@ -1608,6 +1609,9 @@ inline static int send2child(struct tcp_connection* tcpconn)
1608 1608
 static inline int handle_new_connect(struct socket_info* si)
1609 1609
 {
1610 1610
 	union sockaddr_union su;
1611
+	union sockaddr_union sock_name;
1612
+	unsigned sock_name_len;
1613
+	union sockaddr_union* dst_su;
1611 1614
 	struct tcp_connection* tcpconn;
1612 1615
 	socklen_t su_len;
1613 1616
 	int new_sock;
... ...
@@ -1635,8 +1639,21 @@ static inline int handle_new_connect(struct socket_info* si)
1635 1635
 	}
1636 1636
 	(*tcp_connections_no)++;
1637 1637
 	
1638
+	dst_su=&si->su;
1639
+	if (si->flags & SI_IS_ANY){
1640
+		/* INADDR_ANY => get local dst */
1641
+		sock_name_len=sizeof(sock_name);
1642
+		if (getsockname(new_sock, &sock_name.s, &sock_name_len)!=0){
1643
+			LOG(L_ERR, "ERROR: handle_new_connect:"
1644
+						" getsockname failed: %s(%d)\n",
1645
+						strerror(errno), errno);
1646
+			/* go on with the 0.0.0.0 dst from the sock_info */
1647
+		}else{
1648
+			dst_su=&sock_name;
1649
+		}
1650
+	}
1638 1651
 	/* add socket to list */
1639
-	tcpconn=tcpconn_new(new_sock, &su, &si->su, si, si->proto, S_CONN_ACCEPT);
1652
+	tcpconn=tcpconn_new(new_sock, &su, dst_su, si, si->proto, S_CONN_ACCEPT);
1640 1653
 	if (tcpconn){
1641 1654
 #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
1642 1655
 		io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);