⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upnptools.c

📁 电驴下载工具eMule0.47aVeryCD的源代码,可作分析测试也可用于P2P软件的开发研究.
💻 C
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2003 Intel Corporation 
// All rights reserved. 
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met: 
//
// * Redistributions of source code must retain the above copyright notice, 
// this list of conditions and the following disclaimer. 
// * Redistributions in binary form must reproduce the above copyright notice, 
// this list of conditions and the following disclaimer in the documentation 
// and/or other materials provided with the distribution. 
// * Neither name of Intel Corporation nor the names of its contributors 
// may be used to endorse or promote products derived from this software 
// without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////

#if EXCLUDE_DOM == 0
#include <stdarg.h>
#include "config.h"
#include "upnptools.h"
#include "uri.h"
#define HEADER_LENGTH 2000

//Structure to maintain a error code and string associated with the 
// error code
struct ErrorString {
    int rc;                     /* error code */
    const char *rcError;        /* error description */

};

//Intializing the array of error structures. 
struct ErrorString ErrorMessages[] = { {UPNP_E_SUCCESS, "UPNP_E_SUCCESS"},
{UPNP_E_INVALID_HANDLE, "UPNP_E_INVALID_HANDLE"},
{UPNP_E_INVALID_PARAM, "UPNP_E_INVALID_PARAM"},
{UPNP_E_OUTOF_HANDLE, "UPNP_E_OUTOF_HANDLE"},
{UPNP_E_OUTOF_CONTEXT, "UPNP_E_OUTOF_CONTEXT"},
{UPNP_E_OUTOF_MEMORY, "UPNP_E_OUTOF_MEMOR"},
{UPNP_E_INIT, "UPNP_E_INIT"},
{UPNP_E_BUFFER_TOO_SMALL, "UPNP_E_BUFFER_TOO_SMALL"},
{UPNP_E_INVALID_DESC, "UPNP_E_INVALID_DESC"},
{UPNP_E_INVALID_URL, "UPNP_E_INVALID_URL"},
{UPNP_E_INVALID_SID, "UPNP_E_INVALID_SID"},
{UPNP_E_INVALID_DEVICE, "UPNP_E_INVALID_DEVICE"},
{UPNP_E_INVALID_SERVICE, "UPNP_E_INVALID_SERVICE"},
{UPNP_E_BAD_RESPONSE, "UPNP_E_BAD_RESPONSE"},
{UPNP_E_BAD_REQUEST, "UPNP_E_BAD_REQUEST"},
{UPNP_E_INVALID_ACTION, "UPNP_E_INVALID_ACTION"},
{UPNP_E_FINISH, "UPNP_E_FINISH"},
{UPNP_E_INIT_FAILED, "UPNP_E_INIT_FAILED"},
{UPNP_E_NETWORK_ERROR, "UPNP_E_NETWORK_ERROR"},
{UPNP_E_SOCKET_WRITE, "UPNP_E_SOCKET_WRITE"},
{UPNP_E_SOCKET_READ, "UPNP_E_SOCKET_READ"},
{UPNP_E_SOCKET_BIND, "UPNP_E_SOCKET_BIND"},
{UPNP_E_SOCKET_CONNECT, "UPNP_E_SOCKET_CONNECT"},
{UPNP_E_OUTOF_SOCKET, "UPNP_E_OUTOF_SOCKET"},
{UPNP_E_LISTEN, "UPNP_E_LISTEN"},
{UPNP_E_EVENT_PROTOCOL, "UPNP_E_EVENT_PROTOCOL"},
{UPNP_E_SUBSCRIBE_UNACCEPTED, "UPNP_E_SUBSCRIBE_UNACCEPTED"},
{UPNP_E_UNSUBSCRIBE_UNACCEPTED, "UPNP_E_UNSUBSCRIBE_UNACCEPTED"},
{UPNP_E_NOTIFY_UNACCEPTED, "UPNP_E_NOTIFY_UNACCEPTED"},
{UPNP_E_INTERNAL_ERROR, "UPNP_E_INTERNAL_ERROR"},
{UPNP_E_INVALID_ARGUMENT, "UPNP_E_INVALID_ARGUMENT"}
};

/************************************************************************
* Function : UpnpGetErrorMessage											
*																	
* Parameters:														
*	IN int rc: error code
*																	
* Description:														
*	This functions returns the error string mapped to the error code 
* Returns: const char *
*	return either the right string or "Unknown Error"
***************************************************************************/
const char *
UpnpGetErrorMessage( IN int rc )
{
    int i;

    for( i = 0; i < sizeof( ErrorMessages ) / sizeof( ErrorMessages[0] );
         i++ ) {
        if( rc == ErrorMessages[i].rc )
            return ErrorMessages[i].rcError;

    }

    return "Unknown Error";

}

/************************************************************************
* Function : UpnpResolveURL											
*																	
* Parameters:														
*	IN char * BaseURL: Base URL string
*	IN char * RelURL: relative URL string
*	OUT char * AbsURL: Absolute URL string
* Description:														
*	This functions concatinates the base URL and relative URL to generate 
*	the absolute URL
* Returns: int
*	return either UPNP_E_SUCCESS or appropriate error
***************************************************************************/
int
UpnpResolveURL( IN char *BaseURL,
                IN char *RelURL,
                OUT char *AbsURL )
{
    // There is some unnecessary allocation and
    // deallocation going on here because of the way
    // resolve_rel_url was originally written and used
    // in the future it would be nice to clean this up

    char *tempRel;

    if( RelURL == NULL )
        return UPNP_E_INVALID_PARAM;

    tempRel = NULL;

    tempRel = resolve_rel_url( BaseURL, RelURL );

    if( tempRel ) {
        strcpy( AbsURL, tempRel );
        free( tempRel );
    } else {
        return UPNP_E_INVALID_URL;
    }

    return UPNP_E_SUCCESS;

}

/************************************************************************
* Function : addToAction											
*																	
* Parameters:														
*	IN int response: flag to tell if the ActionDoc is for response 
*					or request
*	INOUT IXML_Document **ActionDoc: request or response document
*	IN char *ActionName: Name of the action request or response
*	IN char *ServType: Service type
*	IN char * ArgName: Name of the argument
*	IN char * ArgValue: Value of the argument
*
* Description:		
*	This function adds the argument in the action request or response. 
* This function creates the action request or response if it is a first
* argument else it will add the argument in the document
*
* Returns: int
*	returns UPNP_E_SUCCESS if successful else returns appropriate error
***************************************************************************/
static int
addToAction( IN int response,
             INOUT IXML_Document ** ActionDoc,
             IN char *ActionName,
             IN char *ServType,
             IN char *ArgName,
             IN char *ArgValue )
{
    char *ActBuff = NULL;
    IXML_Node *node = NULL;
    IXML_Element *Ele = NULL;
    IXML_Node *Txt = NULL;
    int rc = 0;

    if( ActionName == NULL || ServType == NULL ) {
        return UPNP_E_INVALID_PARAM;
    }

    if( *ActionDoc == NULL ) {
        ActBuff = ( char * )malloc( HEADER_LENGTH );
        if( ActBuff == NULL ) {
            return UPNP_E_OUTOF_MEMORY;
        }

        if( response ) {
            sprintf( ActBuff,
                     "<u:%sResponse xmlns:u=\"%s\"></u:%sResponse>",
                     ActionName, ServType, ActionName );
        } else {
            sprintf( ActBuff, "<u:%s xmlns:u=\"%s\"></u:%s>",
                     ActionName, ServType, ActionName );
        }

        rc = ixmlParseBufferEx( ActBuff, ActionDoc );
        free( ActBuff );
        if( rc != IXML_SUCCESS ) {
            if( rc == IXML_INSUFFICIENT_MEMORY ) {
                return UPNP_E_OUTOF_MEMORY;
            } else {
                return UPNP_E_INVALID_DESC;
            }
        }
    }

    if( ArgName != NULL /*&& ArgValue != NULL */  ) {
        node = ixmlNode_getFirstChild( ( IXML_Node * ) * ActionDoc );
        Ele = ixmlDocument_createElement( *ActionDoc, ArgName );
        if( ArgValue ) {
            Txt = ixmlDocument_createTextNode( *ActionDoc, ArgValue );
            ixmlNode_appendChild( ( IXML_Node * ) Ele, Txt );
        }

        ixmlNode_appendChild( node, ( IXML_Node * ) Ele );
    }

    return UPNP_E_SUCCESS;
}

/************************************************************************
* Function : makeAction											
*																	
* Parameters:														
*	IN int response: flag to tell if the ActionDoc is for response 
*					or request
*	IN char * ActionName: Name of the action request or response
*	IN char * ServType: Service type
*	IN int NumArg :Number of arguments in the action request or response
*	IN char * Arg : pointer to the first argument
*	IN va_list ArgList: Argument list
*
* Description:		
*	This function creates the action request or response from the argument
* list.
* Returns: IXML_Document *
*	returns action request or response document if successful 
*	else returns NULL
***************************************************************************/
static IXML_Document *
makeAction( IN int response,
            IN char *ActionName,
            IN char *ServType,
            IN int NumArg,
            IN char *Arg,
            IN va_list ArgList )
{
    char *ArgName,
     *ArgValue;
    char *ActBuff;
    int Idx = 0;
    IXML_Document *ActionDoc;
    IXML_Node *node;
    IXML_Element *Ele;
    IXML_Node *Txt = NULL;

    if( ActionName == NULL || ServType == NULL ) {
        return NULL;
    }

    ActBuff = ( char * )malloc( HEADER_LENGTH );
    if( ActBuff == NULL ) {
        return NULL;
    }

    if( response ) {
        sprintf( ActBuff, "<u:%sResponse xmlns:u=\"%s\"></u:%sResponse>",
                 ActionName, ServType, ActionName );
    } else {
        sprintf( ActBuff, "<u:%s xmlns:u=\"%s\"></u:%s>",
                 ActionName, ServType, ActionName );
    }

    if( ixmlParseBufferEx( ActBuff, &ActionDoc ) != IXML_SUCCESS ) {
        free( ActBuff );
        return NULL;
    }

    free( ActBuff );

    if( ActionDoc == NULL ) {
        return NULL;
    }

    if( NumArg > 0 ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -