[Spread-cvs] commit: r410 - trunk/daemon

jonathan at spread.org jonathan at spread.org
Sun May 3 23:23:29 EDT 2009


Author: jonathan
Date: 2009-05-03 23:23:28 -0400 (Sun, 03 May 2009)
New Revision: 410

Modified:
   trunk/daemon/data_link.c
   trunk/daemon/data_link.h
Log:
Add DL_recvfrom() call to return sender address and port number to caller. 
Fix portability bug by zeroing the soc_addr struct before filling it out and binding. 
Add REUSE_ADDR option to DL_init_channel() to set the REUSEADDR socket option on the underlying socket. 



Modified: trunk/daemon/data_link.c
===================================================================
--- trunk/daemon/data_link.c	2009-05-01 15:15:13 UTC (rev 409)
+++ trunk/daemon/data_link.c	2009-05-04 03:23:28 UTC (rev 410)
@@ -95,6 +95,7 @@
 
 	if ( channel_type & RECV_CHANNEL )
 	{
+                memset(&soc_addr, 0, sizeof(soc_addr));
         	soc_addr.sin_family    	= AF_INET;
         	soc_addr.sin_port	= htons(port);
                 memset(&soc_addr.sin_zero, 0, sizeof(soc_addr.sin_zero));
@@ -106,6 +107,15 @@
                 else 
                         soc_addr.sin_addr.s_addr= htonl(interface_address);
 
+                if ( channel_type & REUSE_ADDR ) 
+                {
+                        if(setsockopt(chan, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) < 0) 
+                        {
+                            Alarm( EXIT, "DL_init_channel: Failed to set socket option REUSEADDR, errno: %d\n", errno);
+                        }
+                }
+
+
 	        if(bind( chan, (struct sockaddr *) &soc_addr, 
 				sizeof(soc_addr)) == -1) 
 		{
@@ -179,6 +189,7 @@
         /* Check that the scatter passed is small enough to be a valid system scatter */
         assert(scat->num_elements <= ARCH_SCATTER_SIZE);
 
+        memset(&soc_addr, 0, sizeof(soc_addr));
 	soc_addr.sin_family 	= AF_INET;
 	soc_addr.sin_addr.s_addr= htonl(address);
 	soc_addr.sin_port	= htons(port);
@@ -259,9 +270,16 @@
 
 int	DL_recv( channel chan, sys_scatter *scat )
 {
+    int ret;
+    ret = DL_recvfrom( chan, scat, NULL, NULL );
+
+    return(ret);
+}
+
+int	DL_recvfrom( channel chan, sys_scatter *scat, int *src_address, unsigned short *src_port )
+{
 #ifndef ARCH_SCATTER_NONE
 static	struct	msghdr	msg;
-        struct  sockaddr_in     source_address;
 #else	/* ARCH_SCATTER_NONE */
 static	char		pseudo_scat[MAX_PACKET_SIZE];
 	int		bytes_to_copy;
@@ -269,7 +287,10 @@
 	int		start;
 	int		i;
 #endif	/* ARCH_SCATTER_NONE */
-
+        struct  sockaddr_in     source_address;
+        int             sip;
+        unsigned short  sport;
+        socklen_t       sa_len;
 	int		ret;
 
         /* check the scat is small enough to be a sys_scatter */
@@ -293,6 +314,7 @@
 
 #ifndef ARCH_SCATTER_NONE
 	ret = recvmsg( chan, &msg, 0 ); 
+        sa_len = msg.msg_namelen;
 #else	/* ARCH_SCATTER_NONE */
         
 	total_len = 0;                             /*This is for TCP, to not receive*/
@@ -302,7 +324,8 @@
         if(total_len>MAX_PACKET_SIZE)
            total_len = MAX_PACKET_SIZE;
 
-	ret = recvfrom( chan, pseudo_scat, total_len, 0, 0, 0 );
+        sa_len = sizeof(source_address);
+	ret = recvfrom( chan, pseudo_scat, total_len, 0, &source_address, &sa_len);
 	
 	for( i=0, total_len = ret, start =0; total_len > 0; i++)
 	{
@@ -352,6 +375,17 @@
                 Alarm( DATA_LINK, "\n");
         }
 #endif
+        /* Report the source address and port if requested by caller */
+        if (sa_len >= sizeof(struct sockaddr_in) ) {
+            memcpy(&sip, &source_address.sin_addr, sizeof(int32) );
+            sip =  Flip_int32(sip);
+            if (src_address != NULL)
+                *src_address = sip;
+            sport = Flip_int16(source_address.sin_port);
+            if (src_port != NULL)
+                *src_port = sport;
+            Alarm( DATA_LINK, "\tfrom ("IPF") with family %d port %d\n", IP(sip), source_address.sin_family, sport );
+        }
 	Alarm( DATA_LINK, "DL_recv: received %d bytes on channel %d\n",
 			ret, chan );
 

Modified: trunk/daemon/data_link.h
===================================================================
--- trunk/daemon/data_link.h	2009-05-01 15:15:13 UTC (rev 409)
+++ trunk/daemon/data_link.h	2009-05-04 03:23:28 UTC (rev 410)
@@ -43,10 +43,13 @@
 
 #define		SEND_CHANNEL	0x00000001
 #define		RECV_CHANNEL    0x00000002
+#define         RESERVED        0x00000004
+#define         REUSE_ADDR      0x00000008
 
 channel	DL_init_channel( int32 channel_type, int16 port, int32 mcast_address, int32 interface_address );
 void    DL_close_channel(channel chan);
 int	DL_send( channel chan, int32 address, int16 port, sys_scatter *scat );
 int	DL_recv( channel chan, sys_scatter *scat );
+int	DL_recvfrom( channel chan, sys_scatter *scat, int *src_address, unsigned short *src_port );
 
 #endif  /* INC_DATA_LINK */




More information about the Spread-cvs mailing list