(file) Return to ip.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / ProviderManager2 / CMPIR

File: [Pegasus] / pegasus / src / Pegasus / ProviderManager2 / CMPIR / ip.c (download)
Revision: 1.11, Fri Sep 29 18:32:37 2006 UTC (17 years, 9 months ago) by dave.sudlik
Branch: MAIN
CVS Tags: TASK-TASK-BUG4011_WinLocalConnect-branch-New-root, TASK-TASK-BUG4011_WinLocalConnect-branch-New-merged_out_to_branch, TASK-TASK-BUG4011_WinLocalConnect-branch-New-merged_out_from_trunk, TASK-TASK-BUG4011_WinLocalConnect-branch-New-merged_in_to_trunk, TASK-TASK-BUG4011_WinLocalConnect-branch-New-merged_in_from_branch, TASK-TASK-BUG4011_WinLocalConnect-branch-New-branch, TASK-PEP268_SSLClientCertificatePropagation-root, TASK-PEP268_SSLClientCertificatePropagation-merged_out_to_branch, TASK-PEP268_SSLClientCertificatePropagation-merged_out_from_trunk, TASK-PEP268_SSLClientCertificatePropagation-merged_in_to_trunk, TASK-PEP268_SSLClientCertificatePropagation-merged_in_from_branch, TASK-PEP268_SSLClientCertificatePropagation-branch, TASK-PEP267_SLPReregistrationSupport-root, TASK-PEP267_SLPReregistrationSupport-merging_out_to_branch, TASK-PEP267_SLPReregistrationSupport-merged_out_to_branch, TASK-PEP267_SLPReregistrationSupport-merged_out_from_trunk, TASK-PEP267_SLPReregistrationSupport-merged_in_to_trunk, TASK-PEP267_SLPReregistrationSupport-merged_in_from_branch, TASK-PEP267_SLPReregistrationSupport-branch, TASK-Bugs5690_3913_RemoteCMPI-root, TASK-Bugs5690_3913_RemoteCMPI-merged_out_to_branch, TASK-Bugs5690_3913_RemoteCMPI-merged_out_from_trunk, TASK-Bugs5690_3913_RemoteCMPI-merged_in_to_trunk, TASK-Bugs5690_3913_RemoteCMPI-merged_in_from_branch, TASK-Bug2021_RemoteCMPIonWindows-root, TASK-Bug2021_RemoteCMPIonWindows-merged_out_to_branch, TASK-Bug2021_RemoteCMPIonWindows-merged_out_from_trunk, TASK-Bug2021_RemoteCMPIonWindows-merged_in_to_trunk, TASK-Bug2021_RemoteCMPIonWindows-merged_in_from_branch, TASK-BUG4011_WinLocalConnect-branch-New
Branch point for: TASK-Bugs5690_3913_RemoteCMPI-branch, TASK-Bug2021_RemoteCMPIonWindows-branch
Changes since 1.10: +1 -0 lines
BUG#: 5629
TITLE: socket leak when connect() fails

DESCRIPTION: committing fix

//%2006////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
// IBM Corp.; EMC Corporation, The Open Group.
// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
// EMC Corporation; VERITAS Software Corporation; The Open Group.
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
// EMC Corporation; Symantec Corporation; The Open Group.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//==============================================================================
//
//%/////////////////////////////////////////////////////////////////////////////

/*!
  \file ip.c
  \brief General TCP/IP routines.

*/

#include <stdio.h>
#include <stdlib.h>
#if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)
  #include <error.h>
#else
  #include "debug.h"
#endif
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
#define _XOPEN_SOURCE_EXTENDED 1
#include <arpa/inet.h>
#endif
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>


#ifndef CMPI_VER_100
#define CMPI_VER_100
#endif

#include <Pegasus/Provider/CMPI/cmpimacs.h>
#include <Pegasus/Provider/CMPI/cmpidt.h>
#include <Pegasus/Provider/CMPI/cmpift.h>

#include "ip.h"
#include "tcpcomm.h"

struct linger __linger = {
	1,
	15
};

extern CMPIBrokerExtFT *CMPI_BrokerExt_Ftab;
static int _die = 0;
/****************************************************************************/

static struct hostent * _getHostByName (
    const char *hname,
    struct hostent *hbuf,
    char *tmphbuf,
    int hbuflen)
{
    struct hostent  *hptr;
    int herr,rc=0;
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
    extern int h_errno;
#endif

#if defined(PEGASUS_OS_LINUX)
    rc = gethostbyname_r (hname,hbuf,tmphbuf,hbuflen,&hptr,&herr);
#elif defined(PEGASUS_OS_SOLARIS)
    hptr = gethostbyname_r(hname,hbuf,tmphbuf,hbuflen,&herr);
#else
    hptr = gethostbyname(hname);
#endif

    if (hptr==NULL)
    {
        error_at_line (0, 0, __FILE__, __LINE__,
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
        strerror (h_errno));
#else
        hstrerror (h_errno));
#endif
    }   

    return hptr;
}


int open_connection ( const char * address, int port )
{
    int sockfd;
    struct sockaddr_in sin;
    struct hostent * server_host_name;
    struct hostent hbuf;
    char tempbuf[8192];

// masking unability to transform an ip-address via gethostbyname()
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
	extern int h_errno;
	in_addr_t broker_ip_address;
	broker_ip_address = inet_addr(address);

if ( broker_ip_address != INADDR_NONE )
{
	// HERE COMES THE CALL TO GETHOSTBYADDR
	server_host_name = gethostbyaddr( &(broker_ip_address), sizeof(broker_ip_address), AF_INET);
	if (server_host_name == NULL )
	{
		error_at_line ( 0, 0, __FILE__, __LINE__,strerror(h_errno));
		return -1;
	}
} else {
#endif
    if ((server_host_name = _getHostByName (
        address,
        &hbuf,
        tempbuf,
        sizeof(tempbuf))) == NULL) 
    {
        return -1;
    }
// masking end of if case for differing between ip-address and host
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
}
#endif

	sin.sin_family      = AF_INET;
	sin.sin_port        = htons ( port );
	sin.sin_addr.s_addr =
		( (struct in_addr *) ( server_host_name->h_addr ) )->s_addr;

	if ( ( sockfd = socket ( PF_INET,
				 SOCK_STREAM,
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
				 0 ) ) == -1 ) {
#else
				 IPPROTO_TCP ) ) == -1 ) {
#endif

		error_at_line ( 0, errno, __FILE__, __LINE__,
				"failed to create socket" );
		return -1;
	}

	setsockopt ( sockfd,
		     SOL_SOCKET,
		     SO_LINGER,
		     &__linger,
		     sizeof ( struct linger ) );

	if ( connect ( sockfd,
		       (struct sockaddr *) &sin,
		       sizeof ( sin ) ) == -1 ) {

                close(sockfd);
		error_at_line ( 0, errno, __FILE__, __LINE__,
				"could not connect to %s:%d",
				address,
				port );
		return -1;
	}

	return sockfd;
}


void accept_connections ( int port,
			  void (* __connection_handler) ( int ),
			  int multithreaded )
{
	CMPI_THREAD_TYPE t;

	int in_socket, listen_socket =
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
		socket ( PF_INET, SOCK_STREAM, 0 );
#else
		socket ( PF_INET, SOCK_STREAM, IPPROTO_TCP );
#endif
	struct sockaddr_in sin;
	int sin_len = sizeof ( sin );

	int ru = 1;

	setsockopt ( listen_socket,
		     SOL_SOCKET,
		     SO_REUSEADDR, (char *) &ru,
		     sizeof ( ru ) );

	bzero ( &sin, sin_len );

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons ( port );

	if ( bind ( listen_socket, (struct sockaddr *) &sin, sin_len ) ||
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
	     listen ( listen_socket, 15 ) ) {
#else
	     listen ( listen_socket, 0 ) ) {
#endif
		error_at_line ( -1, errno, __FILE__, __LINE__, "cannot listen on port %d", port );

	}
	_die = 0;
	while ( ( in_socket = accept ( listen_socket,
				       (struct sockaddr *) &sin,
				       (size_t *) &sin_len ) ) > 0 ) {

		if (_die == 1) {
			close (in_socket);
			break;
		}
		setsockopt ( in_socket,
			     SOL_SOCKET,
			     SO_LINGER,
			     &__linger,
			     sizeof ( struct linger ) );

		if ( multithreaded ) {

                        t=CMPI_BrokerExt_Ftab->newThread(
			        (void *(*)(void *))__connection_handler,
				(void *) in_socket,1);
		} else __connection_handler ( in_socket );
	}
	if (in_socket < 0)
		{
			error_at_line ( -1, errno, __FILE__, __LINE__, "invalid socket descriptor (%d) ", in_socket);
		}
	close (listen_socket);
	listen_socket = 0;
    _die = 0;
}


int close_connection (int port ) 
{
	int socket = 0;

	_die = 1;

	// "tickle" the connection.
	socket = open_connection("127.0.0.1", port);
	if (socket)
	{
		close (socket);

		while (_die == 1)
		{
			sleep(1);
		}
	}
	return _die;
	
}
void get_peer_address ( int socket, char * buf )
{

#define UC(b)   ( ( (int) b ) & 0xFF )

	struct sockaddr_in sin;
	socklen_t sinlen = sizeof ( sin );
        char * p = (char *) &sin.sin_addr;

	getpeername ( socket,
		      (struct sockaddr *) &sin,
		      &sinlen );

        sprintf ( buf, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]) );
}


/****************************************************************************/

/*** Local Variables:  ***/
/*** mode: C           ***/
/*** c-basic-offset: 8 ***/
/*** End:              ***/

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2