[Spread-cvs] commit: r543 - trunk/libspread

jschultz at spread.org jschultz at spread.org
Tue Mar 12 15:47:49 EDT 2013


Author: jschultz
Date: 2013-03-12 15:47:49 -0400 (Tue, 12 Mar 2013)
New Revision: 543

Modified:
   trunk/libspread/sp.c
Log:
Cleanup of top of SP_connect_timeout. Also change win32 logic to automatically convert a spread name of <port> to <port>@localhost.  This is to help encourage everyone to only use the <port> format for local connections to spread.


Modified: trunk/libspread/sp.c
===================================================================
--- trunk/libspread/sp.c	2013-03-12 19:37:50 UTC (rev 542)
+++ trunk/libspread/sp.c	2013-03-12 19:47:49 UTC (rev 543)
@@ -201,27 +201,28 @@
 
 static  void    sp_atfork_prepare(void)
 {
-
         Mutex_lock( &Struct_mutex );
 }
+
 /* parent unlocks Struct_mutex when fork is complete */
 static  void    sp_atfork_parent(void)
 {
         Mutex_unlock( &Struct_mutex );
 }
+
 /* Child unlocks Struct_mutex, and also unlocks all session locks so
  * it can acces all of the connections.
  */
 static  void    sp_atfork_child(void)
 {
         int ses;
+
         Mutex_unlock( &Struct_mutex );
         for( ses=0; ses < MAX_LIB_SESSIONS; ++ses )
         {
             Mutex_unlock( &Sessions[ses].recv_mutex );
 	    Mutex_unlock( &Sessions[ses].send_mutex );
         }
-
 }
 
 #endif  /* HAVE_PTHREAD_ATFORK */
@@ -231,6 +232,11 @@
 {
         int ses;
 
+#ifdef ENABLEDEBUG
+        Alarm_set_types(SESSION | DEBUG);
+        Alarm_set_priority(SPLOG_DEBUG);
+#endif
+
 	Mutex_init( &Struct_mutex );
 	for( ses=0; ses < MAX_LIB_SESSIONS; ++ses )
 	{
@@ -242,11 +248,13 @@
 
 	Mutex_atfork( sp_atfork_prepare, sp_atfork_parent, sp_atfork_child );
 
-#ifdef ARCH_PC_WIN95
+#ifndef ARCH_PC_WIN95
+	signal( SIGPIPE, SIG_IGN );
+#else	/* ARCH_PC_WIN95 */
 	{
 	        int ret = WSAStartup( MAKEWORD(2,0), &WSAData );
-		if( ret != 0 )
-		  Alarm( EXIT, "sp_initialize_locks: winsock initialization error %d\n", ret );
+
+		if( ret != 0 ) Alarm( EXIT, "sp_initialize: winsock initialization error %d\n", ret );
 	}
 #endif	/* ARCH_PC_WIN95 */
 
@@ -549,23 +557,16 @@
 		    int priority, int group_membership, mailbox *mbox,
 		    char *private_group, sp_time time_out )
 {
-	/* struct	hostent		*host_ptr, *gethostbyname(); */
-	struct	hostent		*host_ptr;
-	int16			port;
-	int32			host_address;
-	int32			lport, i1, i2, i3, i4;
-	char			*c_ptr;
-	char			host_name[MAXHOSTNAMELEN];
-	char			s_name[SPREAD_MAXCONNECT_NAMELEN + 1];
-	char			dummy_s[20];
+	struct	hostent	       *host_ptr;
+	int16u			port;
+	char			host_name[SPREAD_MAXCONNECT_NAMELEN + 1];
 	char			conn[MAX_PRIVATE_NAME+5];
-        signed char                    auth_list_len;
+        signed char             auth_list_len;
         char                    auth_list[MAX_AUTH_NAME * MAX_AUTH_METHODS];
         char                    auth_choice[MAX_AUTH_NAME * MAX_AUTH_METHODS];
         bool                    failed;
         int                     num_auth_methods;
         struct auth_method_info auth_methods[MAX_AUTH_METHODS];
-	int			p;
 	int			s;
 	int                     ses;
 	int                     base_ses;
@@ -575,128 +576,95 @@
 	char		        cval;
 	int32			on;
 
-	struct	sockaddr_in	inet_addr;
-
+	struct sockaddr_in	inet_addr;
+	struct sockaddr        *sock_addr = NULL;
+	int                     sock_len;
+	
 #ifndef	ARCH_PC_WIN95
-
 	struct	sockaddr_un	unix_addr;
-
 #endif	/* ARCH_PC_WIN95 */
 
-#ifndef	ARCH_PC_WIN95
-
-	signal( SIGPIPE, SIG_IGN );
-
-#endif	/* ARCH_PC_WIN95 */
-
-#ifdef ENABLEDEBUG
-        Alarm_set_types(SESSION | DEBUG);
-        Alarm_set_priority(SPLOG_DEBUG);
-#endif
-
         Once_execute( &Init_once, sp_initialize );
 
 	/* 
-	 * There are 3 possibilities for a name:
-	 * 	3333
-	 *	3333 at commedia.cs.jhu.edu or 3333 at fault
-	 *	3333 at 128.220.221.1
+	 * There are 4 options for a spread daemon name:
+	 *      NULL
+	 *      ""
+	 * 	<port_num>
+	 *	<port_num>@<host_name>
 	 */
 
-	if( spread_name == NULL || (!strcmp( spread_name, "" ) ) ) {
-		strcpy( s_name, DEFAULT_SPREAD_CONNECTION );
-        } else {
-            if (strlen(spread_name) > SPREAD_MAXCONNECT_NAMELEN) 
-                return( ILLEGAL_SPREAD );
-            strcpy( s_name, spread_name );
-        }
-	c_ptr = strchr( s_name, ' ');
-	if( c_ptr != NULL ) return ( ILLEGAL_SPREAD );
-	c_ptr = strchr( s_name, '@');
+	/* options NULL and "" */
+	if( spread_name == NULL || !strcmp( spread_name, "" ) ) spread_name = DEFAULT_SPREAD_CONNECTION;
 
-	if( c_ptr == NULL )
+	/* ensure sscanf is safe */
+	if( strlen( spread_name ) >= sizeof( host_name ) ) return( ILLEGAL_SPREAD );
+
+	switch ( sscanf( spread_name, "%hu%s", &port, host_name ) )
 	{
+	case 1:  /* option <port_num> */
 
 #ifndef	ARCH_PC_WIN95
-		p = AF_UNIX;
-		ret = sscanf( s_name, "%d%s", &lport, dummy_s );
-		if( ret != 1 ) return( ILLEGAL_SPREAD );
-#else	/* ARCH_PC_WIN95 */
-		return( ILLEGAL_SPREAD );
-#endif	/* ARCH_PC_WIN95 */
+		memset( &unix_addr, 0, sizeof( unix_addr ) );
+		unix_addr.sun_family = AF_UNIX;
+		sprintf( unix_addr.sun_path, "%s/%hu", SP_UNIX_SOCKET, port );
+		sock_addr = (struct sockaddr *) &unix_addr;
+		sock_len  = sizeof( unix_addr );
 
-	}else{
-		p = AF_INET;
-		*c_ptr = ' ';
-		ret = sscanf( s_name,"%d%s", &lport, host_name );
-		if( ret != 2) return( ILLEGAL_SPREAD );
-		host_ptr = gethostbyname( host_name );
-		if( host_ptr != NULL )
-		{
-			/* option 3333 at commedia.cs.jhu.edu */
-			memcpy( &host_address, host_ptr->h_addr, sizeof(int32) );
-		}else{
-			/* option 3333 at 128.220.221.1 */
-			for(i=0; i< 3; i++)
-			{
-				c_ptr = strchr(host_name, '.' );
-				if ( c_ptr == NULL) return( ILLEGAL_SPREAD );
-				*c_ptr = ' ';
-			}
-			ret = sscanf( host_name, "%d%d%d%d%s", &i1, &i2, &i3, &i4, dummy_s );
-			if( ret != 4 ) return( ILLEGAL_SPREAD );
-			host_address = ( (i1 << 24 ) | (i2 << 16 ) | (i3 << 8) | i4 );
-		}
-	}
-	if( lport < 0 || lport >= 32*1024 ) return( ILLEGAL_SPREAD );
-	port = lport;
-	if( p == AF_INET )
-	{
-		s = socket( AF_INET, SOCK_STREAM, 0 );
+		s = socket( AF_UNIX, SOCK_STREAM, 0 );
 		if( s < 0 )
 		{
 			Alarm( DEBUG, "SP_connect: unable to create mailbox %d\n", s );
 			return( COULD_NOT_CONNECT );
 		}
 
-                set_large_socket_buffers(s);
+		break;
 
-                on = 1;
-                ret = setsockopt( s, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on) );
-                if (ret < 0) 
-                        Alarm(PRINT, "Setting TCP_NODELAY failed with error: %s\n", sock_strerror(sock_errno));
-                else 
-                        Alarm( SESSION, "SP_connect: set TCP_NODELAY for socket %d\n", s);
+#else	/* ARCH_PC_WIN95 */
+		/* win32: intentional fall through this case to option <port_num>@<host_name> where <host_name> = localhost */
+		strcpy( hostname, "@localhost" );
+#endif	/* ARCH_PC_WIN95 */
 
-                on = 1;
-                ret = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on) );
-                if (ret < 0) 
-                        Alarm(PRINT, "Setting SO_KEEPALIVE failed with error: %s\n", sock_strerror(sock_errno));
-                else 
-                        Alarm( SESSION, "SP_connect: set SO_KEEPALIVE for socket %d\n", s);
+	case 2:  /* option <port_num>@<host_name> */
 
+		if( host_name[0] != '@' ) return( ILLEGAL_SPREAD );
+
+		host_ptr = gethostbyname( host_name + 1 );
+		if( host_ptr == NULL ) return( ILLEGAL_SPREAD );
+
+		memset( &inet_addr, 0, sizeof( inet_addr ) );
 		inet_addr.sin_family = AF_INET;
 		inet_addr.sin_port   = htons( port );
-	        memcpy( &inet_addr.sin_addr, &host_address, sizeof(int32) );
-		ret = connect_nointr_timeout( s, (struct sockaddr *)&inet_addr, sizeof(inet_addr), &time_out);
-	}else{
+		memcpy( &inet_addr.sin_addr, host_ptr->h_addr, sizeof( int32 ) );
+		sock_addr = (struct sockaddr *) &inet_addr;
+		sock_len  = sizeof(inet_addr);
 
-#ifndef	ARCH_PC_WIN95
-		s = socket( AF_UNIX, SOCK_STREAM, 0 );
+		s = socket( AF_INET, SOCK_STREAM, 0 );
 		if( s < 0 )
 		{
 			Alarm( DEBUG, "SP_connect: unable to create mailbox %d\n", s );
 			return( COULD_NOT_CONNECT );
 		}
 
-                set_large_socket_buffers(s);
+                on = 1;
+                ret = setsockopt( s, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on) );
+                if( ret < 0 ) Alarm( PRINT,   "SP_connect: setting TCP_NODELAY failed with error: %s\n", sock_strerror( sock_errno ) );
+                else          Alarm( SESSION, "SP_connect: set TCP_NODELAY for socket %d\n", s );
 
-		unix_addr.sun_family = AF_UNIX;
-		sprintf( unix_addr.sun_path, "%s/%d", SP_UNIX_SOCKET, port );
-		ret = connect_nointr_timeout( s, (struct sockaddr *)&unix_addr, sizeof(unix_addr), &time_out);
-#endif	/* !ARCH_PC_WIN95 */
+                on = 1;
+                ret = setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on) );
+                if( ret < 0 ) Alarm( PRINT,   "SP_connect: setting SO_KEEPALIVE failed with error: %s\n", sock_strerror( sock_errno ) );
+                else          Alarm( SESSION, "SP_connect: set SO_KEEPALIVE for socket %d\n", s );
+
+		break;
+
+	default:  /* didn't start with a legal port # */
+		return( ILLEGAL_SPREAD );
 	}
 
+	set_large_socket_buffers(s);
+
+	ret = connect_nointr_timeout( s, sock_addr, sock_len, &time_out);
 	if( ret < 0 )
 	{
 		Alarm( SESSION, "SP_connect: unable to connect mailbox %d: %s\n", s, sock_strerror(sock_errno));




More information about the Spread-cvs mailing list