/* * tools.cpp * * Created on: 05.08.2012 * Author: savop */ #include "../include/tools.h" #include #include #include #include #include #include #include #include using namespace std; char* substr(const char* str, unsigned int offset, unsigned int length){ if(offset > strlen(str)) return NULL; if(length > strlen(str+offset)) length = strlen(str+offset); char* substring = (char*)malloc(sizeof(substring)*length+1); strncpy(substring, str+offset, length); substring[length] = '\0'; return substring; } namespace upnp { namespace tools { string GetAddressByInterface(string Interface){ string address; if(!Interface.empty()){ int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); /* I want to get an IPv4 IP address */ ifr.ifr_addr.sa_family = AF_INET; /* I want IP address attached to "eth0" */ strncpy(ifr.ifr_name, Interface.c_str(), IFNAMSIZ-1); int ret = ioctl(fd, SIOCGIFADDR, &ifr); close(fd); if(ret==0){ sockaddr_in* inAddr = (sockaddr_in*)&ifr.ifr_addr; address = inet_ntoa(inAddr->sin_addr); } } return address; } string GetNetworkInterfaceByIndex(int Index, bool skipLoop){ return GetNetworkInterfaces(skipLoop)[Index]; } vector GetNetworkInterfaces(bool skipLoop){ vector interfaces; int fd; struct ifconf ifc; struct ifreq ifr[10]; int nifaces, i; memset(&ifc,0,sizeof(ifc)); ifc.ifc_buf = (char*) (ifr); ifc.ifc_len = sizeof(ifr); fd = socket(AF_INET, SOCK_DGRAM, 0); int ret = ioctl(fd, SIOCGIFCONF, &ifc); close(fd); if(ret==0){ nifaces = ifc.ifc_len/sizeof(struct ifreq); for(i = 0; i < nifaces; i++){ if(skipLoop && strcmp("lo", ifr[i].ifr_name)==0) continue; else interfaces.push_back(ifr[i].ifr_name); } } return interfaces; } string ToString(long number){ stringstream ss; ss << number; return ss.str(); } string StringListToCSV(StringList list){ stringstream ss; if(list.empty()) return string(); StringList::iterator it = list.begin(); ss << (*it); for(++it; it != list.end(); ++it){ ss << "," << (*it); } return ss.str(); } string IdListToCSV(IdList list){ stringstream ss; if(list.empty()) return string(); IdList::iterator it = list.begin(); ss << (*it).first << "," << (*it).second; for(++it; it != list.end(); ++it){ ss << "," << (*it).first << "," << (*it).second; } return ss.str(); } string GenerateUUIDFromURL(string url){ boost::uuids::string_generator gen; boost::uuids::uuid urlNamespace = gen(L"6ba7b811-9dad-11d1-80b4-00c04fd430c8"); stringstream uuid; uuid << boost::uuids::name_generator(urlNamespace)(url); return uuid.str(); } string GenerateUUIDRandomly(){ stringstream uuid; uuid << boost::uuids::random_generator()(); return uuid.str(); } } // namespace tools namespace ixml { void XmlEscapeSpecialChars(string& doc){ std::string buffer; buffer.reserve(doc.size()*1.1); for(unsigned int i = 0; i < doc.size(); i++){ switch((long)doc[i]){ case L'€': buffer.append("€"); break; case L'"': buffer.append("""); break; case L'&': buffer.append("&"); break; case L'<': buffer.append("<"); break; case L'>': buffer.append(">"); break; case L'¡': buffer.append("¡"); break; case L'¢': buffer.append("¢"); break; case L'£': buffer.append("£"); break; case L'¤': buffer.append("¤"); break; case L'¥': buffer.append("¥"); break; case L'¦': buffer.append("¦"); break; case L'§': buffer.append("§"); break; case L'¨': buffer.append("¨"); break; case L'©': buffer.append("©"); break; case L'ª': buffer.append("ª"); break; case L'¬': buffer.append("¬"); break; case L'­': buffer.append("­"); break; case L'®': buffer.append("®"); break; case L'¯': buffer.append("¯"); break; case L'°': buffer.append("°"); break; case L'±': buffer.append("±"); break; case L'²': buffer.append("²"); break; case L'³': buffer.append("³"); break; case L'´': buffer.append("´"); break; case L'µ': buffer.append("µ"); break; case L'¶': buffer.append("¶"); break; case L'·': buffer.append("·"); break; case L'¸': buffer.append("¸"); break; case L'¹': buffer.append("¹"); break; case L'º': buffer.append("º"); break; case L'»': buffer.append("»"); break; case L'«': buffer.append("«"); break; case L'¼': buffer.append("¼"); break; case L'½': buffer.append("½"); break; case L'¾': buffer.append("¾"); break; case L'¿': buffer.append("¿"); break; case L'À': buffer.append("À"); break; case L'Á': buffer.append("Á"); break; case L'Â': buffer.append("Â"); break; case L'Ã': buffer.append("Ã"); break; case L'Ä': buffer.append("Ä"); break; case L'Å': buffer.append("Å"); break; case L'Æ': buffer.append("Æ"); break; case L'Ç': buffer.append("Ç"); break; case L'È': buffer.append("È"); break; case L'É': buffer.append("É"); break; case L'Ê': buffer.append("Ê"); break; case L'Ë': buffer.append("Ë"); break; case L'Ì': buffer.append("Ì"); break; case L'Í': buffer.append("Í"); break; case L'Î': buffer.append("Î"); break; case L'Ï': buffer.append("Ï"); break; case L'Ð': buffer.append("Ð"); break; case L'Ñ': buffer.append("Ñ"); break; case L'Ò': buffer.append("Ò"); break; case L'Ó': buffer.append("Ó"); break; case L'Ô': buffer.append("Ô"); break; case L'Õ': buffer.append("Õ"); break; case L'Ö': buffer.append("Ö"); break; case L'×': buffer.append("×"); break; case L'Ø': buffer.append("Ø"); break; case L'Ù': buffer.append("Ù"); break; case L'Ú': buffer.append("Ú"); break; case L'Û': buffer.append("Û"); break; case L'Ü': buffer.append("Ü"); break; case L'Ý': buffer.append("Ý"); break; case L'Þ': buffer.append("Þ"); break; case L'ß': buffer.append("ß"); break; case L'à': buffer.append("à"); break; case L'á': buffer.append("á"); break; case L'â': buffer.append("â"); break; case L'ã': buffer.append("ã"); break; case L'ä': buffer.append("ä"); break; case L'å': buffer.append("å"); break; case L'æ': buffer.append("æ"); break; case L'ç': buffer.append("ç"); break; case L'è': buffer.append("è"); break; case L'é': buffer.append("é"); break; case L'ê': buffer.append("ê"); break; case L'ë': buffer.append("ë"); break; case L'ì': buffer.append("ì"); break; case L'í': buffer.append("í"); break; case L'î': buffer.append("î"); break; case L'ï': buffer.append("ï"); break; case L'ð': buffer.append("ð"); break; case L'ñ': buffer.append("ñ"); break; case L'ò': buffer.append("ò"); break; case L'ó': buffer.append("ó"); break; case L'ô': buffer.append("ô"); break; case L'õ': buffer.append("õ"); break; case L'ö': buffer.append("ö"); break; case L'÷': buffer.append("÷"); break; case L'ø': buffer.append("ø"); break; case L'ù': buffer.append("ù"); break; case L'ú': buffer.append("ú"); break; case L'û': buffer.append("û"); break; case L'ü': buffer.append("ü"); break; case L'ý': buffer.append("ý"); break; case L'þ': buffer.append("þ"); break; default: buffer.append(1, doc[i]); break; } } doc.swap(buffer); } //Function copied from Intel SDK /////////////////////////////////////////////////////////////////////////// // // 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. // /////////////////////////////////////////////////////////////////////////// /******************************************************************************** * SampleUtil_GetFirstDocumentItem * * Description: * Given a document node, this routine searches for the first element * named by the input string item, and returns its value as a string. * String must be freed by caller using free. * Parameters: * doc -- The DOM document from which to extract the value * item -- The item to search for * * ********************************************************************************/ int IxmlGetFirstDocumentItem( IN IXML_Document * doc, IN std::string item, std::string& value ) { IXML_NodeList *nodeList = NULL; IXML_Node *textNode = NULL; IXML_Node *tmpNode = NULL; int error = 0; nodeList = ixmlDocument_getElementsByTagName( doc, item.c_str() ); if( nodeList != NULL ) { if( ( tmpNode = ixmlNodeList_item( nodeList, 0 ) ) ) { textNode = ixmlNode_getFirstChild( tmpNode ); if(textNode != NULL){ value = ixmlNode_getNodeValue( textNode ); } } } else { error = -1; } if( nodeList != NULL) { ixmlNodeList_free( nodeList ); } return error; } //TODO const char* mit std::string ersetzen IXML_Element* IxmlAddProperty(IXML_Document* document, IXML_Element* node, const char* upnpproperty, const char* value){ if(!node) return NULL; IXML_Element* PropertyNode = NULL; //TODO Bug #887 char tvalue[MAX_METADATA_LENGTH]; // trim the value to max metadata size if(value){ strncpy(tvalue, value, MAX_METADATA_LENGTH); } const char* attribute = att(upnpproperty); const char* property = prop(upnpproperty); if(attribute){ if(!strcmp(property,"")){ if(ixmlElement_setAttribute(node, attribute, tvalue)!=IXML_SUCCESS){ return NULL; } } else { IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property); if(NodeList!=NULL){ PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); if(PropertyNode){ if(ixmlElement_setAttribute(PropertyNode, attribute, tvalue)!=IXML_SUCCESS){ return NULL; } } else { ixmlNodeList_free(NodeList); return NULL; } } else { return NULL; } } } else { PropertyNode = ixmlDocument_createElement(document, property); IXML_Node* PropertyText = ixmlDocument_createTextNode(document, tvalue); ixmlNode_appendChild((IXML_Node*) PropertyNode, PropertyText); ixmlNode_appendChild((IXML_Node*) node, (IXML_Node*) PropertyNode); } return PropertyNode; } //TODO const char* mit std::string ersetzen IXML_Element* IxmlAddFilteredProperty(cStringList* Filter, IXML_Document* document, IXML_Element* node, const char* upnpproperty, const char* value){ // leave out empty values. if(!value || !strcmp(value, "") || !strcmp(value, "0")){ return NULL; } if(!Filter || Filter->Find(upnpproperty)) return IxmlAddProperty(document, node, upnpproperty, value); else return NULL; } //TODO const char* mit std::string ersetzen IXML_Element* IxmlReplaceProperty(IXML_Document* document, IXML_Element* node, const char* upnpproperty, const char* newValue){ if(!node) return NULL; IXML_Element* PropertyNode = NULL; //TODO Bug #887 char tvalue[MAX_METADATA_LENGTH]; // trim the value to max metadata size if(newValue){ strncpy(tvalue, newValue, MAX_METADATA_LENGTH); } const char* attribute = att(upnpproperty); const char* property = prop(upnpproperty); if(attribute){ if(!strcmp(property,"")){ if(newValue){ if(ixmlElement_setAttribute(node, attribute, tvalue)!=IXML_SUCCESS){ return NULL; } } else { ixmlElement_removeAttribute(node, attribute); } } else { IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property); if(NodeList!=NULL){ PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); if(PropertyNode){ if(newValue){ if(ixmlElement_setAttribute(PropertyNode, attribute, tvalue)!=IXML_SUCCESS){ return NULL; } } else { ixmlElement_removeAttribute(PropertyNode, attribute); } } else { ixmlNodeList_free(NodeList); return NULL; } } else { return NULL; } } } else { IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property); if(NodeList!=NULL){ PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); IXML_Node* PropertyText = ixmlNode_getFirstChild((IXML_Node*) PropertyNode); if(ixmlNode_removeChild((IXML_Node*) PropertyNode, PropertyText, NULL)!=IXML_SUCCESS){ return NULL; } if(newValue){ PropertyText = ixmlDocument_createTextNode(document, tvalue); } ixmlNode_appendChild((IXML_Node*) PropertyNode, PropertyText); } else { ixmlNodeList_free(NodeList); return NULL; } } return PropertyNode; } } // namespace ixml } // namespace upnp