ip_hpux.cpp
来自「Pegasus is an open-source implementation」· C++ 代码 · 共 1,169 行 · 第 1/3 页
CPP
1,169 行
NAME : get_LANInterfaceNameDESCRIPTION : ASSUMPTIONS : PRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/String IPInterface::get_LANInterfaceName(void) const{ // Get rid of everything after the colon (":") if the name is of the // form "lanX:Y", e.g. "lan0:1". Uint32 pos = _simpleIfName.find(":"); if (pos == PEG_NOT_FOUND) return _simpleIfName; else { String s = _simpleIfName; s.remove(pos,PEG_NOT_FOUND); return s; }}/*================================================================================NAME : bindsToLANInterfaceDESCRIPTION : ASSUMPTIONS : PRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/Boolean IPInterface::bindsToLANInterface(void) const{ // if this is a local ("lo") interface, then it doesn't bind to // an actual LAN Interface if (_simpleIfName.find("lo") == PEG_NOT_FOUND) return true; else return false;}/*================================================================================NAME : initSystemNameDESCRIPTION : Platform-specific routine to get the System NameASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/void IPInterface::initSystemName(void){}/*================================================================================NAME : set_addressDESCRIPTION : Platform-specific routine to set the IP AddressASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/void IPInterface::set_address(const String& addr){ _address = addr;}/*================================================================================NAME : set_subnetMaskDESCRIPTION : Platform-specific routine to set the Subnet MaskASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/void IPInterface::set_subnetMask(const String& snm){ _subnetMask = snm;}/*================================================================================NAME : set_protocolDESCRIPTION : Platform-specific routine to set the IP ProtocolASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/void IPInterface::set_protocol(const String& proto){ _protocol = proto;}/*================================================================================NAME : set_simpleIfNameDESCRIPTION : Platform-specific routine to set the Interface NameASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/void IPInterface::set_simpleIfName(const String& name){ _simpleIfName = name;}/*================================================================================NAME : InterfaceList ConstructorDESCRIPTION : Build the list IP InterfacesASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/InterfaceList::InterfaceList(){ int fd, // file descriptor i, j, // general purpose indicies numif, // number of interfaces numip; // number of IP addresses struct ifconf ifconf; // interface configuration unsigned int len; // length of get_mib_info() buffer struct nmparms parms; // get_mib_info() arguments mib_ipAdEnt * addr_buf; // IP Address Buffer struct in_addr t; // temporary variable for extracting // IP Address Buffer contents struct sockaddr_in *sin; // temporary variable for extracting // interface name#ifdef DEBUG cout << "InterfaceList::InterfaceList()" << endl;#endif // Load the interface name structures. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { throw CIMOperationFailedException("Error in opening socket: " + String(strerror(errno))); } if (ioctl(fd, SIOCGIFNUM, &numif) < 0) { throw CIMOperationFailedException ("Error in ioctl() request SIOCGIFNUM: " + String(strerror(errno))); } ifconf.ifc_len = numif * sizeof (struct ifreq); ifconf.ifc_req = (struct ifreq *) calloc(numif, sizeof (struct ifreq)); if (ioctl (fd, SIOCGIFCONF, &ifconf) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException ("Error in ioctl() request SIOCGIFCONF: " + String(strerror(errno))); } close(fd); if ((fd = open_mib("/dev/ip", O_RDONLY, 0, 0)) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException("Can't open /dev/ip: " + String(strerror(errno))); } parms.objid = ID_ipAddrNumEnt; parms.buffer = (char *) &numip; len = sizeof(numip); parms.len = &len; if (get_mib_info (fd, &parms) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException( "Can't get ID_ipAddrNumEnt from get_mib_info(): " + String(strerror(errno))); } addr_buf = (mib_ipAdEnt *)malloc(numip*sizeof(mib_ipAdEnt)); if (addr_buf == 0) { free (ifconf.ifc_req); free (addr_buf); throw CIMOperationFailedException( "Error in allocating space for the kernel interface table: " + String(strerror(errno))); } parms.objid = ID_ipAddrTable; parms.buffer = (char *) addr_buf; len = numip * sizeof(mib_ipAdEnt); parms.len = &len; if (get_mib_info (fd, &parms) < 0) { free (ifconf.ifc_req); free (addr_buf); throw CIMOperationFailedException( "Can't get ID_ipAddrTable from get_mib_info(): " + String(strerror(errno))); } // Create the interface list entries for (i=0; i < numip ; i++) { IPInterface _ipif; t.s_addr = addr_buf[i].Addr; _ipif.set_address(inet_ntoa(t)); // ATTN-LEW-2002-07-30: Enhance this to deal with IPv6 too. _ipif.set_protocol(PROTOCOL_IPV4); for (j = 0; j < (int)(numif-1); j++) { sin = reinterpret_cast<struct sockaddr_in*>( &ifconf.ifc_req[j].ifr_addr); if (sin->sin_addr.s_addr == t.s_addr) { _ipif.set_simpleIfName(ifconf.ifc_req[j].ifr_name); } } /* for */ t.s_addr = addr_buf[i].NetMask; _ipif.set_subnetMask(inet_ntoa(t)); _ifl.push_back(_ipif); // Add another IP interface to the list } /* for */ close_mib(fd); free (ifconf.ifc_req); free (addr_buf);#ifdef DEBUG cout << "InterfaceList::InterfaceList() -- done" << endl;#endif}/*================================================================================NAME : InterfaceList DestructorDESCRIPTION : NoneASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/InterfaceList::~InterfaceList(){}/*================================================================================NAME : findInterfaceDESCRIPTION : find the requested interfaceASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/Boolean InterfaceList::findInterface(const String &ifName, IPInterface &ipIfInst) const{ // ifName has the format <Protocol>_<InterfaceName>, // for example "IPv4_lan0". int i; for (i = 0; i < _ifl.size(); i++) { String s; if (_ifl[i].getName(s) && String::equal(s,ifName)) { ipIfInst = _ifl[i]; return true; } } // Interface not found return false;}/*================================================================================NAME : getInterfaceDESCRIPTION : Get an interface based on an index.ASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/IPInterface InterfaceList::getInterface(const int index) const{ return _ifl[index];}/*================================================================================NAME : sizeDESCRIPTION : Find the size of the Interface List.ASSUMPTIONS : NonePRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/int InterfaceList::size(void) const{ return _ifl.size();}/////////////////////////////////////////////////////////////////////////IPRoute::IPRoute(){}IPRoute::~IPRoute(){}/*================================================================================NAME : getCaptionDESCRIPTION :ASSUMPTIONS : PRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/Boolean IPRoute::getCaption(String& s) const{ s = _destAddr; return true;}/*================================================================================NAME : getDescriptionDESCRIPTION :ASSUMPTIONS : PRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/Boolean IPRoute::getDescription(String& s) const{ s = "IP Route for Destination Address: " + _destAddr + " (" + _protocolType + ")"; return true;}/*================================================================================NAME : getInstallDateDESCRIPTION : ASSUMPTIONS : PRE-CONDITIONS :POST-CONDITIONS : NOTES : ================================================================================*/Boolean IPRoute::getInstallDate(CIMDateTime& d) const{ // Not supported. This property is inherited from // CIM_ManagedSystemElement, but has no useful meaning
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?