Exemplo n.º 1
0
/*!
  The function maintains a list of previously loaded comm-layers locally.
  A mutex is used to ensure proper access to this list. If a comm-layer
  is found within the list, it is being unloaded.

  \param comm_id the name of the communication layer to be looked up.
  \param broker broker handle as passed to the init function.
  \param ctx context as passed to the cleanup function.

 */
void unload_provider_comms ( void )
{
	provider_comm * tmp;
	provider_comm * next;
	TRACE_VERBOSE(("entered function."));
	TRACE_NORMAL(("unloading remote communication layers"));

        INIT_LOCK(__mutex);
        CMPI_BrokerExt_Ftab->lockMutex(__mutex);

	for ( tmp = __comm_layers; tmp != NULL; tmp = next ) {
		TRACE_INFO(("unloading comm-layer: %s.", tmp->id));
		next = tmp->next;
	    	// IBMKR: Call the cleanup function of comm library.
	    	tmp->terminate();
		// IBMKR: free the library name.
		free (tmp->id); tmp->id = 0;
		unload_comm_library(tmp);
	}

	__comm_layers = NULL;
        CMPI_BrokerExt_Ftab->unlockMutex(__mutex);

	TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 2
0
/*!
  The function maintains a list of previously loaded comm-layers locally.
  A mutex is used to ensure proper access to this list. If a comm-layer
  cannot be found within the list, it is being loaded and inserted at
  the begininng.

  \param comm_id the name of the communication layer to be looked up.
  \param broker broker handle as passed to the init function.
  \param ctx context as passed to the init function.

  \return the comm-layer matching the id or NULL if it cannot be loaded.
 */
provider_comm * load_provider_comm ( const char * comm_id,
				     CONST CMPIBroker * broker,
				     CONST CMPIContext * ctx )
{
	provider_comm * tmp;

	TRACE_VERBOSE(("entered function."));
	TRACE_NORMAL(("loading remote communication layer: %s", comm_id ));

        INIT_LOCK(__mutex);
        CMPI_BrokerExt_Ftab->lockMutex(__mutex);

	for ( tmp = __comm_layers; tmp != NULL; tmp = tmp->next ) {

		if ( strcmp ( tmp->id, comm_id ) == 0 ) {
                        CMPI_BrokerExt_Ftab->unlockMutex(__mutex);

			TRACE_INFO(("found previously loaded comm-layer."));
			TRACE_VERBOSE(("leaving function."));
			return tmp;
		}
	}

	tmp = load_comm_library ( comm_id, broker, ctx );

	if ( tmp != NULL ) {
		tmp->next     = __comm_layers;
		__comm_layers = tmp;
	}

        CMPI_BrokerExt_Ftab->unlockMutex(__mutex);

	TRACE_VERBOSE(("leaving function."));
  	return tmp;
}
Exemplo n.º 3
0
/*!
  The function unloads "lib<id>.so".

  \param id comm the provider_comm structure that can be unloaded

 */
static void unload_comm_library ( provider_comm * comm )
{
	TRACE_VERBOSE(("entered function."));
	TRACE_NORMAL(("unloading comm-layer library: %s", comm->id));

	_unload_lib ( comm->handle );

	TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 4
0
/* create and bind master socket */
int masterSocket(const int port) {
    int rc;
    struct sockaddr_in addr;

    DEBUG_ENTER;

    /* create master socket */
    rc = socket(AF_INET, SOCK_STREAM, 0);
    if (rc == SOCKET_ERROR) {
	TRACE_ERROR("master socket create (err:%i): %s\n",
		    SOCKET_ERROR_CODE, socketError());
	DEBUG_LEAVE;
	return 0;
    }

    TRACE_VERBOSE("master socket created: %i\n", rc);

    DEBUG;

    /* set socket non blocking */
#ifdef WIN32
    unsigned long nValue = 1;
    ioctlsocket(rc, FIONBIO, &nValue);
#endif

    DEBUG;

    /* bind master socket */
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if (bind
	(rc, (struct sockaddr *) &addr,
	 sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
	TRACE_ERROR("failure on binding socket (err:%i): %s\n",
		    SOCKET_ERROR_CODE, socketError());
	DEBUG_LEAVE;
	exit(1);
    }
    TRACE_VERBOSE("master socket bound: %i\n", rc);

    DEBUG;

    /* listen master socket */
    if (listen(rc, 128) == SOCKET_ERROR) {
	TRACE_ERROR("failure on listening (err:%i): %s\n",
		    SOCKET_ERROR_CODE, socketError());
	DEBUG_LEAVE;
	exit(1);
    }

    TRACE_INFO("listening on *:%d (socket:%i)\n", port, rc);
    DEBUG_LEAVE;
    return rc;
}
Exemplo n.º 5
0
/*!
  The function tries to load "lib<id>.so" looking for an entry point
  "<id>_InitCommLayer". The latter one is called to obtain a provider_comm
  structure including the function pointer table for MI calls towards
  remote providers.

  \param id the name of the comm-layer for which the library has to be loaded.
  \param broker broker handle as passed to the init function.
  \param ctx context as passed to the init function.

  \return pointer to the provider_comm structure from the comm-layer, or NULL.
 */
static provider_comm * load_comm_library ( const char * id,
					   CONST CMPIBroker * broker,
					   CONST CMPIContext * ctx )
{
	void * hLibrary;
    CMPIStatus rc = {CMPI_RC_OK, NULL};

	TRACE_VERBOSE(("entered function."));
	TRACE_NORMAL(("loading comm-layer library: lib%s.so", id));

	hLibrary = _load_lib ( id );

	if ( hLibrary != NULL ) {
		char function[255];
		INIT_COMM_LAYER fp;
		sprintf ( function, "%s_InitCommLayer", id );
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
		fp = (INIT_COMM_LAYER) dllqueryfn ( (dllhandle *) hLibrary, function );
#else
		fp = (INIT_COMM_LAYER) dlsym ( hLibrary, function );
#endif

		if ( fp != NULL ) {
			provider_comm * result = fp ( broker, ctx );
			result->id = strdup ( id );
			result->handle = hLibrary;

			TRACE_INFO(("comm-layer successfully initialized."));
			TRACE_VERBOSE(("leaving function."));
			return result;
		}
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
		dllfree ( (dllhandle*) hLibrary );
#else
		dlclose ( hLibrary );
#endif
	}
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
	error_at_line ( 0, errno, __FILE__, __LINE__,
			"Unable to load/init communication-layer library.");
#else
	error_at_line ( 0, 0, __FILE__, __LINE__,
			"Unable to load/init communication-layer library: %s",
			dlerror () );
#endif
	TRACE_VERBOSE(("leaving function."));
	return NULL;
}
Exemplo n.º 6
0
/*!
    The function tries to load "lib<id>.so" looking for an entry point
    "<id>_InitCommLayer". The latter one is called to obtain a provider_comm
    structure including the function pointer table for MI calls towards
    remote providers.

    \param id the name of the comm-layer for which the library has to be loaded.
    \param broker broker handle as passed to the init function.
    \param ctx context as passed to the init function.

    \return pointer to the provider_comm structure from the comm-layer, or NULL.
*/
static provider_comm * load_comm_library (
    const char * id,
    CONST CMPIBroker * broker,
    CONST CMPIContext * ctx )
{
    void * hLibrary;
    char function[255];
    CMPIStatus rc = {CMPI_RC_OK, NULL};

    TRACE_VERBOSE(("entered function."));
    TRACE_NORMAL(("loading comm-layer library: lib%s.so", id));

    hLibrary = _load_lib ( id );

    if (hLibrary != NULL)
    {
        INIT_COMM_LAYER fp;
        sprintf ( function, "%s_InitCommLayer", id );
        //invokes dlsym on unix and GetProcAddress on windows
        fp = (INIT_COMM_LAYER)PEGASUS_CMPIR_GETPROCADDRESS(hLibrary,function);
        if (fp != NULL)
        {
            provider_comm * result = fp(broker, ctx);
            result->id = strdup ( id );
            result->handle = hLibrary;

            TRACE_INFO(("comm-layer successfully initialized."));
            TRACE_VERBOSE(("leaving function."));
            return result;
        }
        //invokes dlfree call on unix and FreeLibrary on windows
        PEGASUS_CMPIR_FREELIBRARY( hLibrary );
    }
    error_at_line (
        0,
        errno,
        __FILE__,
        __LINE__,
        "Unable to load/init communication-layer library.%s Error",
        id);
    TRACE_VERBOSE(("leaving function."));
    return NULL;
}
Exemplo n.º 7
0
BOOL WINAPI new_SetWindowText(HWND h, LPCTSTR text) {
    BOOL rc;
    DEBUG_ENTER;
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("org title: %s\n", text);
#endif
    /* update title */
    if (strstr((char *) text, TITLE_APPEND) == NULL)
	text = strcat((char *) text, TITLE_APPEND);
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("new title: %s\n", text);
#endif
    /* set window text */
    _unhook(3);
    rc = SetWindowText(h, text);
    _hook(3);
    DEBUG_LEAVE;
    return rc;
}
Exemplo n.º 8
0
/*!
  The function evaluates the up-call context id to set up
  the basic entities broker and context, before actually
  spawning a separate thread to handle the request.

  \param socket the connection socket.
  \param broker the broker to forward the request to.

  \sa __dispatch_MB_function
  \sa __verify_MB_call
 */
static void __handle_MB_call(int socket, CONST CMPIBroker * broker)
{
    CONST CMPIContext *ctx;

    struct accept_thread *athread = (struct accept_thread *)
                                    malloc(sizeof(struct accept_thread));

    TRACE_VERBOSE(("entered function."));
    TRACE_NORMAL(("handling incoming broker service request."));

    ctx = get_context(__sft->deserialize_UINT32(socket));

    athread->socket = socket;
    athread->broker = broker;
    athread->context = CBPrepareAttachThread(broker, ctx);

    CMPI_BrokerExt_Ftab->newThread((void*(*)(void*))__dispatch_MB_function,athread,1);

    TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 9
0
/*!
  The function evaluates the up-call ticket, retrieves the appropriate
  broker handle, and passes on the request to __handle_MB_call(), if
  successful.

  \param socket the connection socket.
 */
static void __verify_MB_call(int socket)
{
    comm_ticket ticket;
    CONST CMPIBroker *broker;

    TRACE_VERBOSE(("entered function."));
    TRACE_NORMAL(("handling incoming broker service request."));

    io_read_fixed_length(socket, &ticket, sizeof(comm_ticket));
    broker = verify_ticket(&ticket);

    if (broker == NULL) {

        TRACE_CRITICAL(("ticket invalid, could not obtain a broker"
                        " handle."));
        // return bad status.
        close(socket);

    } else
        __handle_MB_call(socket, broker);

    TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 10
0
/*!
  This function is running in a separate thread and sets up the environment
  for the actual request handler before calling it. This comprises reading
  the function name and deserializing the calling context.

  \param athread contains socket, broker and context handles.

  \sa __mb_functions
  \sa __handle_MB_call
 */
static void __dispatch_MB_function(struct accept_thread *athread)
{
    char *function;
    unsigned int i;

    TRACE_VERBOSE(("entered function."));
    TRACE_NORMAL(("dispatching broker service function request."));

    CBAttachThread(athread->broker, athread->context);
    function =
        __sft->deserialize_string(athread->socket, athread->broker);

    socketcomm_deserialize_context(athread->socket,  __sft, athread->broker,
                                   athread->context);

    for (i = 0;
            i < sizeof(__mb_functions) / sizeof(struct socket_mb_function);
            i++) {

        if (strcmp(function, __mb_functions[i].name) == 0) {

            TRACE_INFO(("calling %s to handle request.",
                        __mb_functions[i].name));

            __mb_functions[i].function(athread->socket, athread->broker,
                                       athread->context);
            break;
        }
    }

    CBDetachThread(athread->broker, athread->context);
    close(athread->socket);
    free(athread);

    TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 11
0
/*!
  Since results from remote providers cannot be sent as CMPIResult directly,
  they are serialized using arrays, which can be transferred to the result
  using this method.

  \param array the source array.
  \param result the destination result.
 */
void socketcomm_array2result ( CMPIArray * array, CONST CMPIResult * result )
{
	TRACE_VERBOSE(("entered function."));

	if ( array != NULL && result != NULL ) {

		CMPICount size = CMGetArrayCount ( array, NULL );
		CMPICount i;

		TRACE_NORMAL(("Transferring %d array elements to CMPIResult.",
			      size));

		for ( i = 0; i < size; i++ ) {

			CMPIData data = CMGetArrayElementAt ( array, i, NULL );

			if ( data.type == CMPI_instance ) {

				TRACE_INFO(("transferring instance."));
				CMReturnInstance ( result, data.value.inst );
			} else if ( data.type == CMPI_ref ) {

				TRACE_INFO(("transferring object path."));
				CMReturnObjectPath ( result, data.value.ref );
			} else {

				TRACE_INFO(("transferring CMPIData."));
				CMReturnData ( result,
					       &data.value,
					       data.type );
			}
		}
	}

	TRACE_VERBOSE(("leaving function."));
}
Exemplo n.º 12
0
/* wrapper for socket receiving */
SOCKET_DATA_LEN socksswitch_recv(const int sock, char *buf) {
    int rc = 0;

    DEBUG_ENTER;

    if (sock <= 0) {
	DEBUG_LEAVE;
	return 0;
    }

    rc = recv(sock, buf, SOCKET_DATA_MAX, 0);

    /* data received */
    if (rc > 0) {
	TRACE_VERBOSE
	    ("recv from %s (socket:%i length:%i)\n",
	     socksswitch_addr(sock), sock, rc);
	DUMP(buf, rc);
    }

    /* socket closed */
#ifdef WIN32
    else if (rc == 0 || SOCKET_ERROR_CODE == WSAECONNRESET) {
#else
    else if (rc == 0) {
#endif
	TRACE_VERBOSE
	    ("recv disconnect from %s (socket:%i)\n",
	     socksswitch_addr(sock), sock);
    }

    /* error */
    else {
	TRACE_WARNING
	    ("failure on receiving from %s (socket:%i err:%i): %s\n",
	     socksswitch_addr(sock), sock, SOCKET_ERROR_CODE,
	     socketError());
	socketError();
	DEBUG_LEAVE;
	return SOCKET_ERROR;
    }

    DEBUG_LEAVE;
    return rc;
}

/* wrapper for socket sending */
int
socksswitch_send(const int sock,
		 const char *buf, const SOCKET_DATA_LEN len) {
    int rc = 0, bytessend = 0;

    DEBUG_ENTER;

    if (sock <= 0)
	return SOCKET_ERROR;

    /* send data */
    while (bytessend < len) {
	rc = send(sock, buf + bytessend, len - bytessend, 0);

	/* error */
	if (rc <= 0) {
	    TRACE_WARNING
		("failure on sending to %s (socket:%i: err:%i): %s\n",
		 socksswitch_addr(sock), sock, SOCKET_ERROR_CODE,
		 socketError());
	    socketError();
	    DEBUG_LEAVE;
	    return SOCKET_ERROR;
	}
	bytessend += rc;
    }

    /* successful */
    if (rc >= 0) {
	TRACE_VERBOSE
	    ("send to %s (socket:%i length:%i rc:%i)\n",
	     socksswitch_addr(sock), sock, len, rc);
	DUMP(buf, len);
    }

    DEBUG_LEAVE;
    return rc;
}
Exemplo n.º 13
0
BOOL WINAPI DllMain(HINSTANCE hinstDLL,
		    DWORD fdwReason, LPVOID lpvReserved) {
    DWORD protect, jmp_size;
    HWND h;
    int i, pid;
    char txt[1024] = "";

#if defined(_DEBUG) || defined(_DEBUG_)
    putenv("LOGFILE=c:\\socksswitchdrv.log");
    putenv("TRACE=4");
    putenv("DUMP=1");
#endif

    DEBUG_ENTER;

    switch (fdwReason) {

	/* hook */
    case DLL_PROCESS_ATTACH:

	/* init */
	for (i = 0; i < COUNT; i++) {
	    memset(bytes[i], 0, SIZE);
	    memcpy(jmp[i], "\xE9\x90\x90\x90\x90\xC3", SIZE);
	}

	/* function address */
	addr[0] = GetProcAddress(GetModuleHandle("ws2_32.dll"), "connect");
	addr[1] =
	    GetProcAddress(GetModuleHandle("ws2_32.dll"), "WSAConnect");
	addr[2] =
	    GetProcAddress(GetModuleHandle("user32.dll"),
			   "SetWindowTextA");
	addr_new[0] = new_connect;
	addr_new[1] = new_WSAConnect;
	addr_new[2] = new_SetWindowText;

#if defined(_DEBUG) || defined(_DEBUG_)
	for (i = 0; i < COUNT; i++) {
	    TRACE_VERBOSE("pid:%i org:%p new:%p\n", GetCurrentProcessId(),
			  addr[i], addr_new[i]);
	}
#endif

	/* save bytes */
	for (i = 0; i < COUNT; i++) {
	    if (addr[i] != NULL) {
		VirtualProtect(addr[i], SIZE,
			       PAGE_EXECUTE_READWRITE, &protect);
		memcpy(bytes[i], addr[i], SIZE);
		VirtualProtect(addr[i], SIZE, protect, NULL);
		jmp_size = (DWORD) addr_new[i] - (DWORD) addr[i] - 5;
		memcpy(&jmp[i][1], &jmp_size, 4);
	    }
	}

	_hook(0);

	/* window text */
	h = GetTopWindow(0);
	while (h) {
	    GetWindowThreadProcessId(h, (PDWORD) & pid);
	    if (pid == GetCurrentProcessId() && IsWindow(h)
		&& IsWindowVisible(h)) {
		GetWindowText(h, txt, sizeof(txt));
		SetWindowText(h, txt);
	    }
	    h = GetNextWindow(h, GW_HWNDNEXT);
	}

	for (i = 0; i < COUNT; i++) {
	    DUMP(bytes[i], SIZE);
	    DUMP(jmp[i], SIZE);
	}

	break;

	/* unhook */
    case DLL_PROCESS_DETACH:
	_unhook(0);
	break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
	break;
    }

    DEBUG_LEAVE;

    return TRUE;
}
Exemplo n.º 14
0
int WINAPI new_WSAConnect(SOCKET s,
			  const struct sockaddr *name,
			  int namelen,
			  LPWSABUF lpCallerData,
			  LPWSABUF lpCalleeData,
			  LPQOS lpSQOS, LPQOS lpGQOS) {
    int rc, err;
    struct sockaddr_in in_addr;
    struct sockaddr_in out_addr;
    char buf[256];
    fd_set set;

    DEBUG_ENTER;

    memcpy(&in_addr, name, sizeof(in_addr));

    /* change non-local */
    if (in_addr.sin_addr.s_addr != inet_addr("127.0.0.1")) {
	DEBUG;
	out_addr.sin_family = AF_INET;
	out_addr.sin_port = htons(1080);
	out_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    }

    DEBUG;

    /* connect */
    _unhook(2);
    rc = WSAConnect(s, (struct sockaddr *) (&out_addr),
		    sizeof(struct sockaddr_in), lpCallerData,
		    lpCalleeData, lpSQOS, lpGQOS);
    err = WSAGetLastError();
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_NO("connect (rc:%i err:%i pid:%i): %s\n", rc, err,
	     GetCurrentProcessId(), socketError());
#endif
    _hook(2);
    if (rc != 0 && err != WSAEWOULDBLOCK) {
	closesocket(s);
	DEBUG_LEAVE;
	return rc;
    }

    /* pass through local */
    if (in_addr.sin_addr.s_addr == inet_addr("127.0.0.1")) {
	DEBUG_LEAVE;
	return rc;
    }

    /* fd_set */
    FD_ZERO(&set);
    FD_SET(s, &set);
    /* sock5 handshake */
    select(s + 1, NULL, &set, NULL, 0);
    rc = send(s, "\x05\x01\x00", 3, 0);
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("send (rc:%i err:%i pid:%i): %s\n", rc,
		  WSAGetLastError(), GetCurrentProcessId(), socketError());
#endif
    if (rc != 3) {
	closesocket(s);
	DEBUG_LEAVE;
	return SOCKET_ERROR;
    }

    /* socks5 ack */
    select(s + 1, &set, NULL, NULL, 0);
    rc = recv(s, buf, 256, 0);
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("recv (rc:%i err:%i pid:%i): %s\n", rc,
		  WSAGetLastError(), GetCurrentProcessId(), socketError());
    DUMP(buf, rc);
#endif
    if (rc != 2) {
	closesocket(s);
	DEBUG_LEAVE;
	return SOCKET_ERROR;
    }

    /* socks5 request */
    memcpy(buf, "\x05\x01\x00\x01", 4);
    memcpy(buf + 4, &in_addr.sin_addr.s_addr, 4);
    memcpy(buf + 8, &in_addr.sin_port, 2);
    select(s + 1, NULL, &set, NULL, 0);
    rc = send(s, buf, 10, 0);
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("send (rc:%i err:%i pid:%i): %s\n", rc,
		  WSAGetLastError(), GetCurrentProcessId(), socketError());
#endif
    if (rc != 10) {
	closesocket(s);
	DEBUG_LEAVE;
	return SOCKET_ERROR;
    }

    /* socks5 ack */
    select(s + 1, &set, NULL, NULL, 0);
    rc = recv(s, buf, 256, 0);
#if defined(_DEBUG) || defined(_DEBUG_)
    TRACE_VERBOSE("recv (rc:%i err:%i pid:%i): %s\n", rc,
		  WSAGetLastError(), GetCurrentProcessId(), socketError());
    DUMP(buf, rc);
#endif
    if (rc != 10) {
	closesocket(s);
	DEBUG_LEAVE;
	return SOCKET_ERROR;
    }

    DEBUG_LEAVE;
    return 0;
}