[Opensrf-commits] r1013 - in trunk: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Jul 8 13:44:41 EDT 2007


Author: miker
Date: 2007-07-08 13:40:23 -0400 (Sun, 08 Jul 2007)
New Revision: 1013

Modified:
   trunk/include/opensrf/socket_bundle.h
   trunk/src/libopensrf/socket_bundle.c
Log:
Patch from Scott McKellar for declaration cleanup and improved error handling:

1. I added the const qualifier to several function parameters.

2. I replaced inet_addr() with inet_aton().

According to the man page, inet_addr() is obsolete.  It reports an
error by returning -1, which however is a valid IP address
(255.255.255.255).  inet_aton reports errors in a more robust way.

3. I check the return value from inet_aton().  If the input address is
invalid, I log a message and return -1.

Without such a check, an invalid listener address leads to a server
that is completely unresponsive.  The user has no way to know what's
wrong unless he spots the mistake himself in the relevant
configuration file.



Modified: trunk/include/opensrf/socket_bundle.h
===================================================================
--- trunk/include/opensrf/socket_bundle.h	2007-07-08 17:39:48 UTC (rev 1012)
+++ trunk/include/opensrf/socket_bundle.h	2007-07-08 17:40:23 UTC (rev 1013)
@@ -67,19 +67,19 @@
 /* creates a new server socket node and adds it to the socket set.
 	returns socket id on success.  -1 on failure.
 	socket_type is one of INET or UNIX  */
-int socket_open_tcp_server(socket_manager*, int port, char* listen_ip );
+int socket_open_tcp_server(socket_manager*, int port, const char* listen_ip );
 
 int socket_open_unix_server(socket_manager* mgr, char* path);
 
-int socket_open_udp_server( socket_manager* mgr, int port, char* listen_ip );
+int socket_open_udp_server( socket_manager* mgr, int port, const char* listen_ip );
 
 /* creates a client TCP socket and adds it to the socket set.
 	returns 0 on success.  -1 on failure.  */
-int socket_open_tcp_client(socket_manager*, int port, char* dest_addr);
+int socket_open_tcp_client(socket_manager*, int port, const char* dest_addr);
 
 /* creates a client UNIX socket and adds it to the socket set.
 	returns 0 on success.  -1 on failure.  */
-int socket_open_unix_client(socket_manager*, char* sock_path);
+int socket_open_unix_client(socket_manager*, const char* sock_path);
 
 int socket_open_udp_client( socket_manager* mgr, int port, char* dest_addr);
 

Modified: trunk/src/libopensrf/socket_bundle.c
===================================================================
--- trunk/src/libopensrf/socket_bundle.c	2007-07-08 17:39:48 UTC (rev 1012)
+++ trunk/src/libopensrf/socket_bundle.c	2007-07-08 17:40:23 UTC (rev 1013)
@@ -77,7 +77,7 @@
 /* creates a new server socket node and adds it to the socket set.
 	returns new socket fd on success.  -1 on failure.
 	socket_type is one of INET or UNIX  */
-int socket_open_tcp_server(socket_manager* mgr, int port, char* listen_ip) {
+int socket_open_tcp_server(socket_manager* mgr, int port, const char* listen_ip) {
 
 	if( mgr == NULL ) {
 		osrfLogWarning( OSRF_LOG_MARK, "socket_open_tcp_server(): NULL mgr"); 
@@ -98,7 +98,13 @@
 	server_addr.sin_family = AF_INET;
 
 	if(listen_ip != NULL) {
-		server_addr.sin_addr.s_addr = inet_addr(listen_ip);
+		struct in_addr addr;
+		if( inet_aton( listen_ip, &addr ) )
+			server_addr.sin_addr.s_addr = addr.s_addr;
+		else {
+			osrfLogError( OSRF_LOG_MARK, "Listener address is invalid: %s", listen_ip );
+			return -1;
+		}
 	} else {
 		server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 	}
@@ -175,7 +181,7 @@
 
 
 int socket_open_udp_server( 
-		socket_manager* mgr, int port, char* listen_ip ) {
+		socket_manager* mgr, int port, const char* listen_ip ) {
 
 	int sockfd;
 	struct sockaddr_in server_addr;
@@ -188,8 +194,15 @@
 
 	server_addr.sin_family = AF_INET;
 	server_addr.sin_port = htons(port);
-	if(listen_ip) server_addr.sin_addr.s_addr = inet_addr(listen_ip);
-	else server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+	if(listen_ip) {
+		struct in_addr addr;
+		if( inet_aton( listen_ip, &addr ) )
+			server_addr.sin_addr.s_addr = addr.s_addr;
+		else {
+			osrfLogError( OSRF_LOG_MARK, "UDP listener address is invalid: %s", listen_ip );
+			return -1;
+		}
+	} else server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
 	errno = 0;
 	if( (bind (sockfd, (struct sockaddr *) &server_addr,sizeof(server_addr))) ) {
@@ -203,7 +216,7 @@
 }
 
 
-int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) {
+int socket_open_tcp_client(socket_manager* mgr, int port, const char* dest_addr) {
 
 	struct sockaddr_in remoteAddr, localAddr;
    struct hostent *hptr;



More information about the opensrf-commits mailing list