char *upnp_get_string(struct action_event *event, const char *key) { IXML_Node *node; node = (IXML_Node *) event->request->ActionRequest; if (node == NULL) { upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Invalid action request document"); return NULL; } node = ixmlNode_getFirstChild(node); if (node == NULL) { upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Invalid action request document"); return NULL; } node = ixmlNode_getFirstChild(node); for (; node != NULL; node = ixmlNode_getNextSibling(node)) { if (strcmp(ixmlNode_getNodeName(node), key) == 0) { node = ixmlNode_getFirstChild(node); if (node == NULL) { /* Are we sure empty arguments are reported like this? */ return strdup(""); } return strdup(ixmlNode_getNodeValue(node)); } } upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Missing action request argument (%s)", key); return NULL; }
/*! * \brief Separates the action node from the root DOM node. * * \return 0 if successful, or -1 if fails. */ static UPNP_INLINE int get_action_node( /*! [in] The root DOM node. */ IXML_Document *TempDoc, /*! [in] IXML_Node name to be searched. */ char *NodeName, /*! [out] Response/Output node. */ IXML_Document **RespNode) { IXML_Node *EnvpNode = NULL; IXML_Node *BodyNode = NULL; IXML_Node *ActNode = NULL; DOMString ActNodeName = NULL; const DOMString nodeName; int ret_code = -1; /* error, by default */ IXML_NodeList *nl = NULL; UpnpPrintf(UPNP_INFO, SOAP, __FILE__, __LINE__, "get_action_node(): node name =%s\n ", NodeName); *RespNode = NULL; /* Got the Envelope node here */ EnvpNode = ixmlNode_getFirstChild((IXML_Node *) TempDoc); if (!EnvpNode) goto error_handler; nl = ixmlElement_getElementsByTagNameNS((IXML_Element *)EnvpNode, "*", "Body"); if (!nl) goto error_handler; BodyNode = ixmlNodeList_item(nl, 0); if (!BodyNode) goto error_handler; /* Got action node here */ ActNode = ixmlNode_getFirstChild(BodyNode); if (!ActNode) goto error_handler; /* Test whether this is the action node */ nodeName = ixmlNode_getNodeName(ActNode); if (!nodeName) goto error_handler; if (!strstr(nodeName, NodeName)) goto error_handler; else { ActNodeName = ixmlPrintNode(ActNode); if (!ActNodeName) goto error_handler; ret_code = ixmlParseBufferEx(ActNodeName, RespNode); if (ret_code != IXML_SUCCESS) { ixmlFreeDOMString(ActNodeName); ret_code = -1; goto error_handler; } } /* success */ ret_code = 0; error_handler: ixmlFreeDOMString(ActNodeName); if (nl) ixmlNodeList_free(nl); return ret_code; }
/************************************************************************ * Function : getSubElement * * Parameters : * const char *element_name ; sub element name to be searched for * IXML_Node *node ; Input node which provides the list of child * nodes * IXML_Node **out ; Ouput node to which the matched child node is * returned. * * Description : Traverses through a list of XML nodes to find the * node with the known element name. * * Return : int ; * 1 - On Success * 0 - On Failure * * Note : ************************************************************************/ int getSubElement( const char *element_name, IXML_Node * node, IXML_Node ** out ) { const DOMString NodeName = NULL; int found = 0; IXML_Node *child = ( IXML_Node * ) ixmlNode_getFirstChild( node ); ( *out ) = NULL; while( ( child != NULL ) && ( !found ) ) { switch ( ixmlNode_getNodeType( child ) ) { case eELEMENT_NODE: NodeName = ixmlNode_getNodeName( child ); if( !strcmp( NodeName, element_name ) ) { ( *out ) = child; found = 1; return found; } break; default: break; } child = ( IXML_Node * ) ixmlNode_getNextSibling( child ); } return found; }
/**************************************************************************** * Function : dom_cmp_name * * Parameters : * IN char *name : lookup name * IN IXML_Node *node : xml node * * Description : This function compares 'name' and node's name * * Return : int * 0 if both are equal; 1 if not equal, and UPNP_E_OUTOF_MEMORY * * Note : ****************************************************************************/ static int dom_cmp_name( IN char *name, IN IXML_Node * node ) { const DOMString node_name = NULL; memptr nameptr, dummy; int ret_code; assert( name ); assert( node ); node_name = ixmlNode_getNodeName( node ); if( node_name == NULL ) { return UPNP_E_OUTOF_MEMORY; } if( strcmp( name, node_name ) == 0 ) { ret_code = 0; } else if( matchstr( ( char * )node_name, strlen( node_name ), "%s:%s%0", &dummy, &nameptr ) == PARSE_OK && strcmp( nameptr.buf, name ) == 0 ) { ret_code = 0; } else { ret_code = 1; // names are not the same } return ret_code; }
IXML_Element* XMLUtil_FindFirstElement (const IXML_Node* const node, const char* const tagname, bool const deep, bool const log_error) { IXML_Element* res = NULL; if (node == NULL || tagname == NULL) { Log_Printf (LOG_ERROR, "GetFirstElementByTagName invalid NULL parameter"); } else { IXML_Node* const n = discard_const_p (IXML_Node, node); if (ixmlNode_getNodeType (n) == eELEMENT_NODE) { const char* const name = ixmlNode_getNodeName (n); if (name && strcmp (tagname, name) == 0) { res = (IXML_Element*) n; } } if (res == NULL) { res = findFirstElementRecursive (n, tagname, deep); } if (res == NULL) { Log_Printf ((log_error ? LOG_ERROR : LOG_DEBUG), "Can't find '%s' element in XML Node" " (deep search=%d)", tagname, (int) deep); } } return res; }
void printNodes(IXML_Node *tmpRoot, int depth) { unsigned long i; IXML_NodeList *NodeList1; IXML_Node *ChildNode1; unsigned short NodeType; const DOMString NodeValue; const DOMString NodeName; NodeList1 = ixmlNode_getChildNodes(tmpRoot); for (i = 0; i < 100; ++i) { ChildNode1 = ixmlNodeList_item(NodeList1, i); if (ChildNode1 == NULL) { break; } printNodes(ChildNode1, depth+1); NodeType = ixmlNode_getNodeType(ChildNode1); NodeValue = ixmlNode_getNodeValue(ChildNode1); NodeName = ixmlNode_getNodeName(ChildNode1); IxmlPrintf(__FILE__, __LINE__, "printNodes", "DEPTH-%2d-IXML_Node Type %d, " "IXML_Node Name: %s, IXML_Node Value: %s\n", depth, NodeType, NodeName, NodeValue); } }
char *upnp_get_string(struct action_event *event, const char *key) { IXML_Node *node; node = (IXML_Node *) event->request->ActionRequest; if (node == NULL) { upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Invalid action request document"); return NULL; } node = ixmlNode_getFirstChild(node); if (node == NULL) { upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Invalid action request document"); return NULL; } node = ixmlNode_getFirstChild(node); for (/**/; node != NULL; node = ixmlNode_getNextSibling(node)) { if (strcmp(ixmlNode_getNodeName(node), key) == 0) { node = ixmlNode_getFirstChild(node); const char *node_value = (node != NULL ? ixmlNode_getNodeValue(node) : NULL); return strdup(node_value != NULL ? node_value : ""); } } upnp_set_error(event, UPNP_SOAP_E_INVALID_ARGS, "Missing action request argument (%s)", key); return NULL; }
/*! * \brief Compares 'name' and node's name. * * \return 0 if both are equal; 1 if not equal, and UPNP_E_OUTOF_MEMORY. */ static int dom_cmp_name( /* [in] lookup name. */ const char *name, /* [in] xml node. */ IXML_Node *node) { const DOMString node_name = NULL; memptr nameptr; memptr dummy; int ret_code; assert(name); assert(node); node_name = ixmlNode_getNodeName(node); if (node_name == NULL) return UPNP_E_OUTOF_MEMORY; if (strcmp(name, node_name) == 0) ret_code = 0; else if (matchstr((char *)node_name, strlen(node_name), "%s:%s%0", &dummy, &nameptr) == PARSE_OK && strcmp(nameptr.buf, name) == 0) ret_code = 0; else /* names are not the same */ ret_code = 1; return ret_code; }
/*! * \brief Returns the element tag (name) */ const DOMString CUPnPLib::Element_GetTag( IXML_Element *element) const { IXML_Node *node = REINTERPRET_CAST(IXML_Node *)(element); const DOMString tag = ixmlNode_getNodeName(node); return tag; }
static struct xmlelement *find_element(IXML_Node *node, const char *key) { node = ixmlNode_getFirstChild(node); for (/**/; node != NULL; node = ixmlNode_getNextSibling(node)) { if (strcmp(ixmlNode_getNodeName(node), key) == 0) { return (struct xmlelement*) node; } } return NULL; }
/*! * \brief Returns the first child element that matches the requested tag or * NULL if not found. */ IXML_Element *CUPnPLib::Element_GetFirstChildByTag( IXML_Element *element, const DOMString tag) const { if (!element || !tag) { return NULL; } IXML_Node *node = REINTERPRET_CAST(IXML_Node *)(element); IXML_Node *child = ixmlNode_getFirstChild(node); const DOMString childTag = ixmlNode_getNodeName(child); while(child && childTag && strcmp(tag, childTag)) { child = ixmlNode_getNextSibling(child); childTag = ixmlNode_getNodeName(child); } return REINTERPRET_CAST(IXML_Element *)(child); }
v = (char*) ixmlNode_getNodeValue(l1_1_node); LoadConfigItem(Conf, sq_conf, n, v); } if (node_list) ixmlNodeList_free(node_list); } return node; } /*----------------------------------------------------------------------------*/ void *LoadConfig(char *name, tMRConfig *Conf, sq_dev_param_t *sq_conf) { IXML_Element *elm; IXML_Document *doc; doc = ixmlLoadDocument(name); if (!doc) return NULL; elm = ixmlDocument_getElementById(doc, "squeeze2upnp"); if (elm) { unsigned i; char *n, *v; IXML_NodeList *l1_node_list; l1_node_list = ixmlNode_getChildNodes((IXML_Node*) elm); for (i = 0; i < ixmlNodeList_length(l1_node_list); i++) { IXML_Node *l1_node, *l1_1_node; l1_node = ixmlNodeList_item(l1_node_list, i); n = (char*) ixmlNode_getNodeName(l1_node); l1_1_node = ixmlNode_getFirstChild(l1_node); v = (char*) ixmlNode_getNodeValue(l1_1_node); LoadGlobalItem(n, v); } if (l1_node_list) ixmlNodeList_free(l1_node_list); } elm = ixmlDocument_getElementById((IXML_Document *)elm, "common"); if (elm) { char *n, *v; IXML_NodeList *l1_node_list; unsigned i; l1_node_list = ixmlNode_getChildNodes((IXML_Node*) elm); for (i = 0; i < ixmlNodeList_length(l1_node_list); i++) { IXML_Node *l1_node, *l1_1_node; l1_node = ixmlNodeList_item(l1_node_list, i);
/**************************************************************************** * Function : get_var_name * * Parameters : * IN IXML_Document *TempDoc : Document containing variable request * OUT char* VarName : Name of the state varible * * Description : This function finds the name of the state variable * asked in the SOAP request. * * Return : int * returns 0 if successful else returns -1. * Note : ****************************************************************************/ static UPNP_INLINE int get_var_name( IN IXML_Document * TempDoc, OUT char *VarName ) { IXML_Node *EnvpNode = NULL; IXML_Node *BodyNode = NULL; IXML_Node *StNode = NULL; IXML_Node *VarNameNode = NULL; IXML_Node *VarNode = NULL; const DOMString StNodeName = NULL; const DOMString Temp = NULL; int ret_val = -1; // Got the Envelop node here EnvpNode = ixmlNode_getFirstChild( ( IXML_Node * ) TempDoc ); if( EnvpNode == NULL ) { goto error_handler; } // Got Body here BodyNode = ixmlNode_getFirstChild( EnvpNode ); if( BodyNode == NULL ) { goto error_handler; } // Got action node here StNode = ixmlNode_getFirstChild( BodyNode ); if( StNode == NULL ) { goto error_handler; } //Test whether this is the action node StNodeName = ixmlNode_getNodeName( StNode ); if( StNodeName == NULL || strstr( StNodeName, "QueryStateVariable" ) == NULL ) { goto error_handler; } VarNameNode = ixmlNode_getFirstChild( StNode ); if( VarNameNode == NULL ) { goto error_handler; } VarNode = ixmlNode_getFirstChild( VarNameNode ); Temp = ixmlNode_getNodeValue( VarNode ); linecopy( VarName, Temp ); UpnpPrintf( UPNP_INFO, SOAP, __FILE__, __LINE__, "Received query for variable name %s\n", VarName ); ret_val = 0; // success error_handler: return ret_val; }
/*================================================================ * ixmlDocument_getElementById * Returns the element whose ID is given by tagName. If no such * element exists, returns null. * External function. * Parameter: * tagName: the tag name for an element. * Return Values: * The matching element. * *=================================================================*/ IXML_Element * ixmlDocument_getElementById( IN IXML_Document * doc, IN DOMString tagName ) { IXML_Element *rtElement = NULL; IXML_Node *nodeptr = ( IXML_Node * ) doc; const char *name; if( ( nodeptr == NULL ) || ( tagName == NULL ) ) { return rtElement; } if( ixmlNode_getNodeType( nodeptr ) == eELEMENT_NODE ) { name = ixmlNode_getNodeName( nodeptr ); if( name == NULL ) { return rtElement; } if( strcmp( tagName, name ) == 0 ) { rtElement = ( IXML_Element * ) nodeptr; return rtElement; } else { rtElement = ixmlDocument_getElementById( ( IXML_Document * ) ixmlNode_getFirstChild ( nodeptr ), tagName ); if( rtElement == NULL ) { rtElement = ixmlDocument_getElementById( ( IXML_Document * ) ixmlNode_getNextSibling ( nodeptr ), tagName ); } } } else { rtElement = ixmlDocument_getElementById( ( IXML_Document * ) ixmlNode_getFirstChild ( nodeptr ), tagName ); if( rtElement == NULL ) { rtElement = ixmlDocument_getElementById( ( IXML_Document * ) ixmlNode_getNextSibling ( nodeptr ), tagName ); } } return rtElement; }
/*! * \brief Finds the name of the state variable asked in the SOAP request. * * \return 0 if successful else returns -1. */ static UPNP_INLINE int get_var_name( /*! [in] Document containing variable request. */ IXML_Document *TempDoc, /*! [out] Name of the state varible. */ char *VarName) { IXML_Node *EnvpNode = NULL; IXML_Node *BodyNode = NULL; IXML_Node *StNode = NULL; IXML_Node *VarNameNode = NULL; IXML_Node *VarNode = NULL; const DOMString StNodeName = NULL; const DOMString Temp = NULL; int ret_val = -1; /* Got the Envelop node here */ EnvpNode = ixmlNode_getFirstChild((IXML_Node *) TempDoc); if (EnvpNode == NULL) goto error_handler; /* Got Body here */ BodyNode = ixmlNode_getFirstChild(EnvpNode); if (BodyNode == NULL) goto error_handler; /* Got action node here */ StNode = ixmlNode_getFirstChild(BodyNode); if (StNode == NULL) goto error_handler; /* Test whether this is the action node */ StNodeName = ixmlNode_getNodeName(StNode); if (StNodeName == NULL || strstr(StNodeName, "QueryStateVariable") == NULL) goto error_handler; VarNameNode = ixmlNode_getFirstChild(StNode); if (VarNameNode == NULL) goto error_handler; VarNode = ixmlNode_getFirstChild(VarNameNode); Temp = ixmlNode_getNodeValue(VarNode); linecopy(VarName, Temp); UpnpPrintf(UPNP_INFO, SOAP, __FILE__, __LINE__, "Received query for variable name %s\n", VarName); /* success */ ret_val = 0; error_handler: return ret_val; }
void ixmlNode_getElementsByTagName( IXML_Node *n, const char *tagname, IXML_NodeList **list) { const char *name; assert(n != NULL && tagname != NULL); if (ixmlNode_getNodeType(n) == eELEMENT_NODE) { name = ixmlNode_getNodeName(n); if (strcmp(tagname, name) == 0 || strcmp(tagname, "*") == 0) { ixmlNodeList_addToNodeList(list, n); } } ixmlNode_getElementsByTagNameRecursive(ixmlNode_getFirstChild(n), tagname, list); }
/*! * \brief Recursively traverse the whole tree, search for element with the * given tagname. */ static void ixmlNode_getElementsByTagNameRecursive( /*! [in] The \b Node tree. */ IXML_Node *n, /*! [in] The tag name to match. */ const char *tagname, /*! [out] The output \b NodeList. */ IXML_NodeList **list) { const char *name; if (n != NULL) { if (ixmlNode_getNodeType(n) == eELEMENT_NODE) { name = ixmlNode_getNodeName(n); if (strcmp(tagname, name) == 0 || strcmp(tagname, "*") == 0) { ixmlNodeList_addToNodeList(list, n); } } ixmlNode_getElementsByTagNameRecursive(ixmlNode_getFirstChild(n), tagname, list); ixmlNode_getElementsByTagNameRecursive(ixmlNode_getNextSibling(n), tagname, list); } }
static IXML_Element* findFirstElementRecursive (const IXML_Node* const node, const char* const tagname, bool const deep) { IXML_Element* res = NULL; IXML_Node* n = ixmlNode_getFirstChild (discard_const_p (IXML_Node, node)); while (n && !res) { if (ixmlNode_getNodeType (n) == eELEMENT_NODE) { const char* const name = ixmlNode_getNodeName (n); if (name && strcmp (tagname, name) == 0) { res = (IXML_Element*) n; } } if (deep && !res) { res = findFirstElementRecursive (n, tagname, deep); } n = ixmlNode_getNextSibling (n); } return res; }
/*================================================================ * ixmlNode_getElementsByTagNameRecursive * Recursively traverse the whole tree, search for element * with the given tagname. * Internal to parser. * *=================================================================*/ void ixmlNode_getElementsByTagNameRecursive( IN IXML_Node * n, IN char *tagname, OUT IXML_NodeList ** list ) { const char *name; if( n != NULL ) { if( ixmlNode_getNodeType( n ) == eELEMENT_NODE ) { name = ixmlNode_getNodeName( n ); if( strcmp( tagname, name ) == 0 || strcmp( tagname, "*" ) == 0 ) { ixmlNodeList_addToNodeList( list, n ); } } ixmlNode_getElementsByTagNameRecursive( ixmlNode_getFirstChild ( n ), tagname, list ); ixmlNode_getElementsByTagNameRecursive( ixmlNode_getNextSibling ( n ), tagname, list ); } }
if (v && !strcmp(v, UDN)) { device = ixmlNode_getParentNode(l1_node); break; } } if (l1_node_list) ixmlNodeList_free(l1_node_list); return device; } /*----------------------------------------------------------------------------*/ void *LoadMRConfig(void *ref, char *UDN, tMRConfig *Conf, sq_dev_param_t *sq_conf) { IXML_NodeList *node_list; IXML_Document *doc = (IXML_Document*) ref; IXML_Node *node; char *n, *v; unsigned i; node = (IXML_Node*) FindMRConfig(doc, UDN); if (node) { node_list = ixmlNode_getChildNodes(node); for (i = 0; i < ixmlNodeList_length(node_list); i++) { IXML_Node *l1_node, *l1_1_node; l1_node = ixmlNodeList_item(node_list, i);
char * upnp_get_string (struct Upnp_Action_Request *request, const char *key) { IXML_Node *node = NULL; if (!request || !request->ActionRequest || !key) return NULL; node = (IXML_Node *) request->ActionRequest; if (!node) { log_verbose ("Invalid action request document\n"); return NULL; } node = ixmlNode_getFirstChild (node); if (!node) { log_verbose ("Invalid action request document\n"); return NULL; } node = ixmlNode_getFirstChild (node); for (; node; node = ixmlNode_getNextSibling (node)) if (!strcmp (ixmlNode_getNodeName (node), key)) { node = ixmlNode_getFirstChild (node); if (!node) return strdup (""); return strdup (ixmlNode_getNodeValue (node)); } log_verbose ("Missing action request argument (%s)\n", key); return NULL; }
/************************************************************************** * Function: prepareParameter * Functionality: it will get the parameter from pipe and check it * @IN : fd: the file description id for the pipe * enable: enable or disable flag * @OUT: 0 success, else failed. * Used description: The parameter is a xml, it will get from the pipe * Date: 20080108 * Changed history: * Date Who Reason * 20080108 kelly First creation ***************************************************************************/ int prepareParameter(PSystemConfig **pList,int *count) { IXML_Document *rootDom = NULL; IXML_NodeList *nodeList = NULL; IXML_NodeList *nodeList2 = NULL; IXML_Node *node = NULL; IXML_Node *cmdNode = NULL; char buffer[1024] = {0}; char Str[1024] = {0}; int i = 0; int nodeLen = 0; PSystemConfig *pConfigList = NULL; while(1) { fgets(Str, 1024, stdin); strcat(buffer, Str); if(strstr(buffer,EndFlag)) break; } if(strlen(buffer) == 0) goto failed; if((rootDom = ixmlParseBuffer(buffer)) == NULL) goto failed; if((nodeList = ixmlDocument_getElementsByTagName(rootDom,"cmd")) != NULL) { cmdNode = ixmlGetFirstNodeByTagName(rootDom,"cmd"); nodeList2 = ixmlNode_getChildNodes(cmdNode); nodeLen = ixmlNodeList_length(nodeList2); /* fprintf(stderr, "=======nodeLen -> [%d]\n", nodeLen);*/ pConfigList = (PSystemConfig*)malloc(sizeof(PSystemConfig) * nodeLen); memset(pConfigList, 0, sizeof(PSystemConfig)*nodeLen); node = ixmlNodeList_item(nodeList, 0); node = ixmlNode_getFirstChild(node); for(i=0; i<nodeLen; i++) { if(node){ pConfigList[i] = (PSystemConfig)malloc(sizeof(SystemConfig)); memset(pConfigList[i],0,sizeof(SystemConfig)); pConfigList[i]->name = strdup(ixmlNode_getNodeName(node)); if(i != nodeLen-1) node = ixmlNode_getNextSibling(node); } else{ fprintf(stderr, "===== node is empty. i->[%d]\n", i); } } } *count = nodeLen; if(pConfigList){ *pList = pConfigList; } else{ fprintf(stderr, " !!!!!!pLIST is NULL"); } if (rootDom) ixmlDocument_free(rootDom); if (nodeList) ixmlNodeList_free(nodeList); if (nodeList2) ixmlNodeList_free(nodeList2); return i; failed: fprintf(stderr,"Got a command parameter->%s", buffer); //fprintf(stderr,"Got a command parameter->%s", buffer); return i; }
/**************************************************************************** * Function : get_action_node * * Parameters : * IN IXML_Document *TempDoc : The root DOM node. * IN char *NodeName : IXML_Node name to be searched. * OUT IXML_Document ** RespNode : Response/Output node. * * Description : This function separates the action node from * the root DOM node. * * Return : static UPNP_INLINE int * 0 if successful, or -1 if fails. * * Note : ****************************************************************************/ static UPNP_INLINE int get_action_node( IN IXML_Document * TempDoc, IN char *NodeName, OUT IXML_Document ** RespNode ) { IXML_Node *EnvpNode = NULL; IXML_Node *BodyNode = NULL; IXML_Node *ActNode = NULL; DOMString ActNodeName = NULL; const DOMString nodeName; int ret_code = -1; // error, by default IXML_NodeList *nl = NULL; UpnpPrintf( UPNP_INFO, SOAP, __FILE__, __LINE__, "get_action_node(): node name =%s\n ", NodeName ); *RespNode = NULL; // Got the Envelope node here EnvpNode = ixmlNode_getFirstChild( ( IXML_Node * ) TempDoc ); if( EnvpNode == NULL ) { goto error_handler; } nl = ixmlElement_getElementsByTagNameNS( ( IXML_Element * ) EnvpNode, "*", "Body" ); if( nl == NULL ) { goto error_handler; } BodyNode = ixmlNodeList_item( nl, 0 ); if( BodyNode == NULL ) { goto error_handler; } // Got action node here ActNode = ixmlNode_getFirstChild( BodyNode ); if( ActNode == NULL ) { goto error_handler; } //Test whether this is the action node nodeName = ixmlNode_getNodeName( ActNode ); if( nodeName == NULL ) { goto error_handler; } if( strstr( nodeName, NodeName ) == NULL ) { goto error_handler; } else { ActNodeName = ixmlPrintNode( ActNode ); if( ActNodeName == NULL ) { goto error_handler; } ret_code = ixmlParseBufferEx( ActNodeName, RespNode ); if( ret_code != IXML_SUCCESS ) { ixmlFreeDOMString( ActNodeName ); ret_code = -1; goto error_handler; } } ret_code = 0; // success error_handler: ixmlFreeDOMString( ActNodeName ); if( nl ) ixmlNodeList_free( nl ); return ret_code; }
/*! * \brief Recursive function to print all the node in a tree. * Internal to parser only. */ static void ixmlPrintDomTreeRecursive( /*! [in] \todo documentation. */ IXML_Node *nodeptr, /*! [in] \todo documentation. */ ixml_membuf *buf) { const char *nodeName = NULL; const char *nodeValue = NULL; IXML_Node *child = NULL, *sibling = NULL; if (nodeptr != NULL) { nodeName = (const char *)ixmlNode_getNodeName(nodeptr); nodeValue = ixmlNode_getNodeValue(nodeptr); switch (ixmlNode_getNodeType(nodeptr)) { case eTEXT_NODE: copy_with_escape(buf, nodeValue); break; case eCDATA_SECTION_NODE: ixml_membuf_append_str(buf, "<![CDATA["); ixml_membuf_append_str(buf, nodeValue); ixml_membuf_append_str(buf, "]]>"); break; case ePROCESSING_INSTRUCTION_NODE: ixml_membuf_append_str(buf, "<?"); ixml_membuf_append_str(buf, nodeName); ixml_membuf_append_str(buf, " "); copy_with_escape(buf, nodeValue); ixml_membuf_append_str(buf, "?>\n"); break; case eDOCUMENT_NODE: ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild(nodeptr), buf); break; case eATTRIBUTE_NODE: ixml_membuf_append_str(buf, nodeName); ixml_membuf_append_str(buf, "=\""); copy_with_escape(buf, nodeValue); ixml_membuf_append_str(buf, "\""); if (nodeptr->nextSibling != NULL) { ixml_membuf_append_str(buf, " "); ixmlPrintDomTreeRecursive(nodeptr->nextSibling, buf); } break; case eELEMENT_NODE: ixml_membuf_append_str(buf, "<"); ixml_membuf_append_str(buf, nodeName); if (nodeptr->firstAttr != NULL) { ixml_membuf_append_str(buf, " "); ixmlPrintDomTreeRecursive(nodeptr->firstAttr, buf); } child = ixmlNode_getFirstChild(nodeptr); if (child != NULL && ixmlNode_getNodeType(child) == eELEMENT_NODE) { ixml_membuf_append_str(buf, ">\r\n"); } else { ixml_membuf_append_str(buf, ">"); } // output the children ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild(nodeptr), buf); // Done with children. Output the end tag. ixml_membuf_append_str(buf, "</"); ixml_membuf_append_str(buf, nodeName); sibling = ixmlNode_getNextSibling(nodeptr); if (sibling != NULL && ixmlNode_getNodeType(sibling) == eTEXT_NODE) { ixml_membuf_append_str( buf, ">" ); } else { ixml_membuf_append_str( buf, ">\r\n" ); } ixmlPrintDomTreeRecursive( ixmlNode_getNextSibling(nodeptr), buf); break; default: IxmlPrintf("(%s::ixmlPrintDomTreeRecursive) line %d: " "Warning, unknown node type %d\n", __FILE__, __LINE__, ixmlNode_getNodeType(nodeptr)); break; } } }
/************************************************************************** * Function: prepareParameter * Functionality: it will get the parameter from pipe and check it * @IN : fd: the file description id for the pipe * enable: enable or disable flag * @OUT: 0 success, else failed. * Used description: The parameter is a xml, it will get from the pipe * Date: 20080108 * Changed history: * Date Who Reason * 20080108 kelly First creation ***************************************************************************/ int prepareParameter(PSystemConfig **pList,int *count) { IXML_Document *rootDom = NULL; IXML_NodeList *nodeList = NULL; IXML_NodeList *nodeList2 = NULL; IXML_Node *node = NULL; IXML_Node *cmdNode = NULL; IXML_Node *node2 = NULL; char buffer[1024] = {0}; char Str[1024] = {0}; int i = 0; int i_ret=0; int nodeLen = 0; PSystemConfig *pConfigList = NULL; while(1) { fgets(Str, 1024, stdin); strcat(buffer, Str); if(strstr(buffer,EndFlag)) break; } if(strlen(buffer) == 0) goto failed; if((rootDom = ixmlParseBuffer(buffer)) == NULL){ i_ret = -1; goto Err_handler; } if((nodeList = ixmlDocument_getElementsByTagName(rootDom,"cmd")) != NULL) { cmdNode = ixmlGetFirstNodeByTagName(rootDom,"cmd"); nodeList2 = ixmlNode_getChildNodes(cmdNode); nodeLen = ixmlNodeList_length(nodeList2); pConfigList = (PSystemConfig*)malloc(sizeof(PSystemConfig) * nodeLen); memset(pConfigList, 0, sizeof(PSystemConfig)*nodeLen); node = ixmlNodeList_item(nodeList, 0); node = ixmlNode_getFirstChild(node); while (node != NULL) { pConfigList[i] = (PSystemConfig)malloc(sizeof(SystemConfig)); memset(pConfigList[i],0,sizeof(SystemConfig)); pConfigList[i]->name = strdup(ixmlNode_getNodeName(node)); node2 = ixmlNode_getFirstChild(node); pConfigList[i]->value = (ixmlNode_getNodeValue(node2))?strdup(ixmlNode_getNodeValue(node2)):strdup(""); /* fprintf(stderr, "name->%s , value ->%s\n" , pConfigList[i]->name , pConfigList[i]->value); */ i_ret=CheckVariable(pConfigList[i]->name,pConfigList[i]->value); if(i_ret<0) { i_ret = -3; goto Err_handler; } node = ixmlNode_getNextSibling(node); i++; } } *count = nodeLen; if(pConfigList){ *pList = pConfigList; } else{ fprintf(stderr, " !!!!!!pLIST is NULL"); } if (rootDom) ixmlDocument_free(rootDom); if (nodeList) ixmlNodeList_free(nodeList); if (nodeList2) ixmlNodeList_free(nodeList2); return i; failed: fprintf(stderr,"Got a command parameter->%s", buffer); //fprintf(stderr,"Got a command parameter->%s", buffer); return i; Err_handler: if (rootDom) ixmlDocument_free(rootDom); if (nodeList) ixmlNodeList_free(nodeList); if (nodeList2) ixmlNodeList_free(nodeList2); return i_ret; }
/*================================================================ * ixmlDomTreetoString * Converts a DOM tree into a text string * Element, and Attribute nodes are handled differently. * We don't want to print the Element and Attribute nodes' sibling. * External function. * *=================================================================*/ void ixmlDomTreetoString( IN IXML_Node * nodeptr, IN ixml_membuf * buf ) { char *nodeName = NULL; char *nodeValue = NULL; IXML_Node *child = NULL; if( ( nodeptr == NULL ) || ( buf == NULL ) ) { return; } nodeName = ( char * )ixmlNode_getNodeName( nodeptr ); nodeValue = ixmlNode_getNodeValue( nodeptr ); switch ( ixmlNode_getNodeType( nodeptr ) ) { case eTEXT_NODE: case eCDATA_SECTION_NODE: case ePROCESSING_INSTRUCTION_NODE: case eDOCUMENT_NODE: ixmlPrintDomTreeRecursive( nodeptr, buf ); break; case eATTRIBUTE_NODE: ixml_membuf_append_str( buf, nodeName ); ixml_membuf_append_str( buf, "=\"" ); ixml_membuf_append_str( buf, nodeValue ); ixml_membuf_append_str( buf, "\"" ); break; case eELEMENT_NODE: ixml_membuf_append_str( buf, "<" ); ixml_membuf_append_str( buf, nodeName ); if( nodeptr->firstAttr != NULL ) { ixml_membuf_append_str( buf, " " ); ixmlPrintDomTreeRecursive( nodeptr->firstAttr, buf ); } child = ixmlNode_getFirstChild( nodeptr ); if( ( child != NULL ) && ( ixmlNode_getNodeType( child ) == eELEMENT_NODE ) ) { ixml_membuf_append_str( buf, ">" ); } else { ixml_membuf_append_str( buf, ">" ); } // output the children ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild( nodeptr ), buf ); // Done with children. Output the end tag. ixml_membuf_append_str( buf, "</" ); ixml_membuf_append_str( buf, nodeName ); ixml_membuf_append_str( buf, ">" ); break; default: break; } }
/*================================================================ * ixmlPrintDomTreeRecursive * It is a recursive function to print all the node in a tree. * Internal to parser only. * *=================================================================*/ void ixmlPrintDomTreeRecursive( IN IXML_Node * nodeptr, IN ixml_membuf * buf ) { char *nodeName = NULL; char *nodeValue = NULL; IXML_Node *child = NULL, *sibling = NULL; if( nodeptr != NULL ) { nodeName = ( char * )ixmlNode_getNodeName( nodeptr ); nodeValue = ixmlNode_getNodeValue( nodeptr ); switch ( ixmlNode_getNodeType( nodeptr ) ) { case eTEXT_NODE: copy_with_escape( buf, nodeValue ); break; case eCDATA_SECTION_NODE: ixml_membuf_append_str( buf, nodeValue ); break; case ePROCESSING_INSTRUCTION_NODE: ixml_membuf_append_str( buf, "<?" ); ixml_membuf_append_str( buf, nodeName ); ixml_membuf_append_str( buf, " " ); ixml_membuf_append_str( buf, nodeValue ); ixml_membuf_append_str( buf, "?>\n" ); break; case eDOCUMENT_NODE: ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild ( nodeptr ), buf ); break; case eATTRIBUTE_NODE: ixml_membuf_append_str( buf, nodeName ); ixml_membuf_append_str( buf, "=\"" ); if( nodeValue != NULL ) { ixml_membuf_append_str( buf, nodeValue ); } ixml_membuf_append_str( buf, "\"" ); if( nodeptr->nextSibling != NULL ) { ixml_membuf_append_str( buf, " " ); ixmlPrintDomTreeRecursive( nodeptr->nextSibling, buf ); } break; case eELEMENT_NODE: ixml_membuf_append_str( buf, "<" ); ixml_membuf_append_str( buf, nodeName ); if( nodeptr->firstAttr != NULL ) { ixml_membuf_append_str( buf, " " ); ixmlPrintDomTreeRecursive( nodeptr->firstAttr, buf ); } child = ixmlNode_getFirstChild( nodeptr ); if( ( child != NULL ) && ( ixmlNode_getNodeType( child ) == eELEMENT_NODE ) ) { ixml_membuf_append_str( buf, ">\n" ); } else { ixml_membuf_append_str( buf, ">" ); } // output the children ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild ( nodeptr ), buf ); // Done with children. Output the end tag. ixml_membuf_append_str( buf, "</" ); ixml_membuf_append_str( buf, nodeName ); sibling = ixmlNode_getNextSibling( nodeptr ); if( sibling != NULL && ixmlNode_getNodeType( sibling ) == eTEXT_NODE ) { ixml_membuf_append_str( buf, ">" ); } else { ixml_membuf_append_str( buf, ">\n" ); } ixmlPrintDomTreeRecursive( ixmlNode_getNextSibling ( nodeptr ), buf ); break; default: break; } } }
int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, enum SsdpSearchType SearchType, struct sockaddr *DestAddr, char *DeviceType, char *DeviceUDN, char *ServiceType, int Exp) { int retVal = UPNP_E_SUCCESS; long unsigned int i; long unsigned int j; int defaultExp = DEFAULT_MAXAGE; struct Handle_Info *SInfo = NULL; char UDNstr[100]; char devType[100]; char servType[100]; IXML_NodeList *nodeList = NULL; IXML_NodeList *tmpNodeList = NULL; IXML_Node *tmpNode = NULL; IXML_Node *tmpNode2 = NULL; IXML_Node *textNode = NULL; const DOMString tmpStr; const DOMString dbgStr; int NumCopy = 0; memset(UDNstr, 0, sizeof(UDNstr)); memset(devType, 0, sizeof(devType)); memset(servType, 0, sizeof(servType)); UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Inside AdvertiseAndReply with AdFlag = %d\n", AdFlag); /* Use a read lock */ HandleReadLock(); if (GetHandleInfo(Hnd, &SInfo) != HND_DEVICE) { retVal = UPNP_E_INVALID_HANDLE; goto end_function; } defaultExp = SInfo->MaxAge; /* parse the device list and send advertisements/replies */ while (NumCopy == 0 || (AdFlag && NumCopy < NUM_SSDP_COPY)) { if (NumCopy != 0) imillisleep(SSDP_PAUSE); NumCopy++; for (i = 0lu;; i++) { UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Entering new device list with i = %lu\n\n", i); tmpNode = ixmlNodeList_item(SInfo->DeviceList, i); if (!tmpNode) { UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Exiting new device list with i = %lu\n\n", i); break; } dbgStr = ixmlNode_getNodeName(tmpNode); UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "Extracting device type once for %s\n", dbgStr); ixmlNodeList_free(nodeList); nodeList = ixmlElement_getElementsByTagName((IXML_Element *) tmpNode, "deviceType"); if (!nodeList) continue; UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Extracting UDN for %s\n", dbgStr); UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Extracting device type\n"); tmpNode2 = ixmlNodeList_item(nodeList, 0lu); if (!tmpNode2) continue; textNode = ixmlNode_getFirstChild(tmpNode2); if (!textNode) continue; UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Extracting device type \n"); tmpStr = ixmlNode_getNodeValue(textNode); if (!tmpStr) continue; strncpy(devType, tmpStr, sizeof(devType) - 1); UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Extracting device type = %s\n", devType); if (!tmpNode) { UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "TempNode is NULL\n"); } dbgStr = ixmlNode_getNodeName(tmpNode); UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Extracting UDN for %s\n", dbgStr); ixmlNodeList_free(nodeList); nodeList = ixmlElement_getElementsByTagName((IXML_Element *) tmpNode, "UDN"); if (!nodeList) { UpnpPrintf(UPNP_CRITICAL, API, __FILE__, __LINE__, "UDN not found!\n"); continue; } tmpNode2 = ixmlNodeList_item(nodeList, 0lu); if (!tmpNode2) { UpnpPrintf(UPNP_CRITICAL, API, __FILE__, __LINE__, "UDN not found!\n"); continue; } textNode = ixmlNode_getFirstChild(tmpNode2); if (!textNode) { UpnpPrintf(UPNP_CRITICAL, API, __FILE__, __LINE__, "UDN not found!\n"); continue; } tmpStr = ixmlNode_getNodeValue(textNode); if (!tmpStr) { UpnpPrintf(UPNP_CRITICAL, API, __FILE__, __LINE__, "UDN not found!\n"); continue; } strncpy(UDNstr, tmpStr, sizeof(UDNstr) - 1); UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "Sending UDNStr = %s \n", UDNstr); if (AdFlag) { /* send the device advertisement */ if (AdFlag == 1) { DeviceAdvertisement(devType, i == 0lu, NumCopy - 1, UDNstr, SInfo->DescURL, Exp, SInfo->DeviceAf, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else { /* AdFlag == -1 */ DeviceShutdown(devType, i == 0lu, NumCopy - 1, UDNstr, SInfo->DescURL, Exp, SInfo->DeviceAf, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } } else { switch (SearchType) { case SSDP_ALL: DeviceReply(DestAddr, devType, i == 0lu, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); break; case SSDP_ROOTDEVICE: if (i == 0lu) { SendReply(DestAddr, devType, 1, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, 0, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } break; case SSDP_DEVICEUDN: { if (DeviceUDN && strlen(DeviceUDN) != (size_t)0) { if (strcasecmp(DeviceUDN, UDNstr)) { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceUDN=%s and search UDN=%s DID NOT match\n", UDNstr, DeviceUDN); break; } else { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceUDN=%s and search UDN=%s MATCH\n", UDNstr, DeviceUDN); SendReply(DestAddr, devType, 0, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, 0, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); break; } } } case SSDP_DEVICETYPE: { if (!strncasecmp(DeviceType, devType, strlen(DeviceType) - (size_t)2)) { if (atoi(strrchr(DeviceType, ':') + 1) < atoi(&devType[strlen(devType) - (size_t)1])) { /* the requested version is lower than the device version * must reply with the lower version number and the lower * description URL */ UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceType=%s and search devType=%s MATCH\n", devType, DeviceType); SendReply(DestAddr, DeviceType, 0, NumCopy - 1, UDNstr, SInfo->LowerDescURL, defaultExp, 1, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else if (atoi(strrchr(DeviceType, ':') + 1) == atoi(&devType[strlen(devType) - (size_t)1])) { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceType=%s and search devType=%s MATCH\n", devType, DeviceType); SendReply(DestAddr, DeviceType, 0, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, 1, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceType=%s and search devType=%s DID NOT MATCH\n", devType, DeviceType); } } else { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "DeviceType=%s and search devType=%s DID NOT MATCH\n", devType, DeviceType); } break; } default: break; } } /* send service advertisements for services corresponding * to the same device */ UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "Sending service Advertisement\n"); /* Correct service traversal such that each device's serviceList * is directly traversed as a child of its parent device. This * ensures that the service's alive message uses the UDN of * the parent device. */ tmpNode = ixmlNode_getFirstChild(tmpNode); while (tmpNode) { dbgStr = ixmlNode_getNodeName(tmpNode); if (!strncmp (dbgStr, SERVICELIST_STR, sizeof SERVICELIST_STR)) { break; } tmpNode = ixmlNode_getNextSibling(tmpNode); } ixmlNodeList_free(nodeList); if (!tmpNode) { nodeList = NULL; continue; } nodeList = ixmlElement_getElementsByTagName((IXML_Element *) tmpNode, "service"); if (!nodeList) { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "Service not found 3\n"); continue; } for (j = 0lu;; j++) { tmpNode = ixmlNodeList_item(nodeList, j); if (!tmpNode) { break; } ixmlNodeList_free(tmpNodeList); tmpNodeList = ixmlElement_getElementsByTagName((IXML_Element *) tmpNode, "serviceType"); if (!tmpNodeList) { UpnpPrintf(UPNP_CRITICAL, API, __FILE__, __LINE__, "ServiceType not found \n"); continue; } tmpNode2 = ixmlNodeList_item(tmpNodeList, 0lu); if (!tmpNode2) continue; textNode = ixmlNode_getFirstChild(tmpNode2); if (!textNode) continue; /* servType is of format Servicetype:ServiceVersion */ tmpStr = ixmlNode_getNodeValue(textNode); if (!tmpStr) continue; strncpy(servType, tmpStr, sizeof(servType) - 1); UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "ServiceType = %s\n", servType); if (AdFlag) { if (AdFlag == 1) { ServiceAdvertisement(NumCopy - 1, UDNstr, servType, SInfo->DescURL, Exp, SInfo->DeviceAf, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else { /* AdFlag == -1 */ ServiceShutdown(NumCopy - 1, UDNstr, servType, SInfo->DescURL, Exp, SInfo->DeviceAf, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } } else { switch (SearchType) { case SSDP_ALL: ServiceReply(DestAddr, servType, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); break; case SSDP_SERVICE: if (ServiceType) { if (!strncasecmp(ServiceType, servType, strlen(ServiceType) - (size_t)2)) { if (atoi(strrchr(ServiceType, ':') + 1) < atoi(&servType[strlen(servType) - (size_t)1])) { /* the requested version is lower than the service version * must reply with the lower version number and the lower * description URL */ UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "ServiceType=%s and search servType=%s MATCH\n", ServiceType, servType); SendReply(DestAddr, ServiceType, 0, NumCopy - 1, UDNstr, SInfo->LowerDescURL, defaultExp, 1, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else if (atoi(strrchr (ServiceType, ':') + 1) == atoi(&servType[strlen(servType) - (size_t)1])) { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "ServiceType=%s and search servType=%s MATCH\n", ServiceType, servType); SendReply(DestAddr, ServiceType, 0, NumCopy - 1, UDNstr, SInfo->DescURL, defaultExp, 1, SInfo->PowerState, SInfo->SleepPeriod, SInfo->RegistrationState); } else { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "ServiceType=%s and search servType=%s DID NOT MATCH\n", ServiceType, servType); } } else { UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__, "ServiceType=%s and search servType=%s DID NOT MATCH\n", ServiceType, servType); } } break; default: break; } } } ixmlNodeList_free(tmpNodeList); tmpNodeList = NULL; ixmlNodeList_free(nodeList); nodeList = NULL; } } end_function: ixmlNodeList_free(tmpNodeList); ixmlNodeList_free(nodeList); UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Exiting AdvertiseAndReply.\n"); HandleUnlock(); return retVal; }