/*! * @if jp * @brief 宛先アドレスから利用されるエンドポイントアドレスを得る * @else * @brief Getting network interface name from destination address * @endif */ bool dest_to_endpoint(std::string dest_addr, std::string& endpoint) { Winsock winsock; { struct hostent* hp; hp = ::gethostbyname(dest_addr.c_str()); if (hp == 0) { return false; } int i(0); while (hp->h_addr_list[i] != 0) { if(hp->h_addrtype == AF_INET) { struct sockaddr_in addr; memset((char*)&addr, 0, sizeof(addr)); memcpy((char*)&addr.sin_addr, hp->h_addr_list[i], hp->h_length); dest_addr = inet_ntoa(addr.sin_addr); break; } ++i; } } UINT ipaddress(inet_addr(dest_addr.c_str())); if (ipaddress == INADDR_NONE) { return false; } DWORD bestifindex; if (NO_ERROR != GetBestInterface(ipaddress, &bestifindex)) { return false; } PMIB_IPADDRTABLE ipaddr_table; ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE)); if (ipaddr_table == 0) { return false; } // Make an initial call to GetIpAddrTable to get the // necessary size into the size variable DWORD size(0); if (GetIpAddrTable(ipaddr_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER) { FREE(ipaddr_table); ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(size); } if (ipaddr_table == 0) { return false; } if (GetIpAddrTable(ipaddr_table, &size, 0) != NO_ERROR) { return false; } for (int i(0); i < (int) ipaddr_table->dwNumEntries; ++i) { if (bestifindex == ipaddr_table->table[i].dwIndex) { IN_ADDR inipaddr; inipaddr.S_un.S_addr = (u_long) ipaddr_table->table[i].dwAddr; endpoint = inet_ntoa(inipaddr); return true; } } return false; }
string *udpsocket::receive (ipaddress &addr, int timeout_ms) { returnclass (string) res retain; struct sockaddr_storage remote_addr; char buf[2048]; int sz; socklen_t addrsz = sizeof (remote_addr); if (timeout_ms>=0) { fd_set fds; struct timeval tv; tv.tv_sec = timeout_ms/1000; tv.tv_usec = (timeout_ms % 1000) * 1000; FD_ZERO (&fds); FD_SET (sock, &fds); if (select (sock+1, &fds, NULL, NULL, &tv) < 1) { addr = ipaddress (); return &res; } } sz = recvfrom (sock, buf, 2048, 0, (struct sockaddr *) &remote_addr, &addrsz); if (remote_addr.ss_family == AF_INET) { addr = ((sockaddr_in*)&remote_addr)->sin_addr; } else if (remote_addr.ss_family == AF_INET6) { addr = ((sockaddr_in6*)&remote_addr)->sin6_addr; } else { throw udpAddressFamilyException (); } if (sz>0) res.strcpy (buf, sz); return &res; }
// ---------------------------------------------- Record SystemTable::getRecord(ID_t i_record_id) { INF("enter SystemTable::getRecord()."); std::string select_statement = "SELECT * FROM '"; select_statement += this->m_table_name; select_statement += "' WHERE ID == '"; select_statement += std::to_string(i_record_id); select_statement += "';"; this->__prepare_statement__(select_statement); sqlite3_step(this->m_db_statement); ID_t id = sqlite3_column_int64(this->m_db_statement, 0); DBG("Read id [%lli] from table ["%s"] of database ["%s"], input id was [%lli].", id, this->m_table_name.c_str(), this->m_db_name.c_str(), i_record_id); TABLE_ASSERT("Input record id does not equal to primary key value from database!" && id == i_record_id); Record record = Record::EMPTY; if (i_record_id != UNKNOWN_ID) { DBG("Read id [%lli] from table ["%s"] of database ["%s"].", id, this->m_table_name.c_str(), this->m_db_name.c_str()); ID_t extra_id = sqlite3_column_int64(this->m_db_statement, 1); uint64_t timestamp = sqlite3_column_int64(this->m_db_statement, 2); const void* raw_datetime = reinterpret_cast<const char*>(sqlite3_column_text(this->m_db_statement, 3)); WrappedString datetime(raw_datetime); const void* raw_ipaddress = reinterpret_cast<const char*>(sqlite3_column_text(this->m_db_statement, 4)); WrappedString ipaddress(raw_ipaddress); int port = sqlite3_column_int(this->m_db_statement, 5); DBG("Loaded column data: " COLUMN_NAME_EXTRA_ID " [%lli]; " D_COLUMN_NAME_TIMESTAMP " [%lu]; " D_COLUMN_NAME_DATETIME " ["%s"]; " D_COLUMN_NAME_IP_ADDRESS " ["%s"]; " D_COLUMN_NAME_PORT " [%i].", extra_id, timestamp, datetime.c_str(), ipaddress.c_str(), port); record = Record(extra_id, timestamp, ipaddress.get(), port); DBG("Proper record instance has been constructed."); } else { WRN("ID [%lli] is missing in table ["%s"] of database %p!", i_record_id, this->m_table_name.c_str(), this->m_db_handler); } this->__finalize__(select_statement.c_str()); INF("exit SystemTable::getRecord()."); return (record); }
int wpdu::restore_vbs(Pdu& pdu, const snmp_pdu *raw_pdu) const { Vb tempvb; Oid tempoid; struct variable_list *vp; for(vp = raw_pdu->variables; vp; vp = vp->next_variable) { // extract the oid portion tempoid.set_data( (unsigned long *)vp->name, ( unsigned int) vp->name_length); tempvb.set_oid( tempoid); // extract the value portion switch(vp->type) { // octet string case sNMP_SYNTAX_OCTETS: case sNMP_SYNTAX_OPAQUE: { OctetStr octets( (char *) vp->val.string, (long) vp->val_len); tempvb.set_value( octets); } break; // object id case sNMP_SYNTAX_OID: { Oid oid( (unsigned long*) vp->val.objid, (int) vp->val_len); tempvb.set_value( oid); } break; // timeticks case sNMP_SYNTAX_TIMETICKS: { TimeTicks timeticks( (unsigned long) *(vp->val.integer)); tempvb.set_value( timeticks); } break; // Gauge32 case sNMP_SYNTAX_GAUGE32: { Gauge32 gauge32( (unsigned long) *(vp->val.integer)); tempvb.set_value( gauge32); } break; // 32 bit counter case sNMP_SYNTAX_CNTR32: { Counter32 counter32( (unsigned long) *(vp->val.integer)); tempvb.set_value( counter32); } break; // ip address case sNMP_SYNTAX_IPADDR: { char buffer[20]; ACE_OS::sprintf( buffer,"%d.%d.%d.%d", vp->val.string[0], vp->val.string[1], vp->val.string[2], vp->val.string[3]); IpAddress ipaddress( buffer); tempvb.set_value( ipaddress); } break; // 32 bit integer case sNMP_SYNTAX_INT: { SnmpInt32 int32( (long) *(vp->val.integer)); tempvb.set_value( int32); } break; // 32 bit unsigned integer case sNMP_SYNTAX_UINT32: { SnmpUInt32 uint32( (unsigned long) *(vp->val.integer)); tempvb.set_value( uint32); } break; // v2 counter 64's case sNMP_SYNTAX_CNTR64: break; case sNMP_SYNTAX_NULL: tempvb.set_null(); break; // v2 vb exceptions case sNMP_SYNTAX_NOSUCHOBJECT: case sNMP_SYNTAX_NOSUCHINSTANCE: case sNMP_SYNTAX_ENDOFMIBVIEW: set_exception_status( &tempvb, vp->type); break; default: tempvb.set_null(); } // end switch // append the vb to the pdu pdu += tempvb; } return 0; }
/**This method is called when the RTIP DHCP client receives an IP * configuration from the DHCP server. * * It sets IP address, subnet mask and default gateway according to * offer from DHCP server. */ void RTIPDHCPClient::dhcpNewIPCallback(int ifaceno) { IFACE_INFO ifinfo; RTIPDHCPClient *me = static_cast<RTIPDHCPClient*>(RTIPDHCPClient::getInstance()); if ( me->minterfaceno != ifaceno) { return; } if (!me->menabled) { me->mpipstackconfigurator->refreshIPConfig(); //Due to timing, rtip might set the ip address, when we dont want it //this will reset to previous configuration return; } xn_interface_info(me->minterfaceno, &ifinfo); IPAddress ipaddress(ifinfo.my_ip_address); me->mipaddress = ipaddress; IPAddress subnetmask(ifinfo.ip_mask); me->msubnetmask = subnetmask; //Default gateway from dhcp server?: if(me->msession.params.router_option && me->msession.params.router_option_len > 0) { IPAddress defaultgateway(reinterpret_cast<unsigned char*>(me->msession.params.router_option)); //Use first gateway me->mdefaultgateway = defaultgateway; } else { me->mdefaultgateway = IPAddress(0,0,0,0); } if(me->msession.params.dns_server && me->msession.params.dns_server_len > 0) { IPAddress primarydns(reinterpret_cast<unsigned char*>(me->msession.params.dns_server)); me->mprimarydns = primarydns; if(me->msession.params.dns_server_len > 4) { IPAddress secondarydns(reinterpret_cast<unsigned char*>(me->msession.params.dns_server)+4); me->msecondarydns = secondarydns; } else { me->msecondarydns = IPAddress(0,0,0,0); } } else { me->mprimarydns = IPAddress(0,0,0,0); me->msecondarydns = IPAddress(0,0,0,0); } me->mgotlease = true; me->gotNewLease(); }
// unload the data into SNMP++ objects int SnmpMessage::unload(Pdu &pdu, // Pdu object OctetStr &community, // community object snmp_version &version, // SNMP version # OctetStr *engine_id, // optional v3 OctetStr *security_name, // optional v3 long int *security_model, UdpAddress *from_addr, Snmp *snmp_session) { pdu.clear(); if (!valid_flag) return SNMP_CLASS_INVALID; snmp_pdu *raw_pdu; raw_pdu = snmp_pdu_create(0); // do a "snmp_free_pdu( raw_pdu)" before return int status; #ifdef _SNMPv3 OctetStr context_engine_id; OctetStr context_name; long int security_level = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; if ((security_model) && (security_name) && (engine_id) && (snmp_session)) { status = v3MP::I->snmp_parse(snmp_session, raw_pdu, databuff, (int)bufflen, *engine_id, *security_name, context_engine_id, context_name, security_level, *security_model, version, *from_addr); if (status != SNMPv3_MP_OK) { pdu.set_request_id( raw_pdu->reqid); pdu.set_type( raw_pdu->command); snmp_free_pdu( raw_pdu); return status; } pdu.set_context_engine_id(context_engine_id); pdu.set_context_name(context_name); pdu.set_security_level(security_level); pdu.set_message_id(raw_pdu->msgid); pdu.set_maxsize_scopedpdu(raw_pdu->maxsize_scopedpdu); } else { #endif unsigned char community_name[MAX_LEN_COMMUNITY + 1]; int community_len = MAX_LEN_COMMUNITY + 1; status = snmp_parse(raw_pdu, databuff, (int) bufflen, community_name, community_len, version); if (status != SNMP_CLASS_SUCCESS) { snmp_free_pdu(raw_pdu); return status; } community.set_data( community_name, community_len); #ifdef _SNMPv3 } #endif // load up the SNMP++ variables pdu.set_request_id(raw_pdu->reqid); pdu.set_error_status((int) raw_pdu->errstat); pdu.set_error_index((int) raw_pdu->errindex); pdu.set_type( raw_pdu->command); // deal with traps a little different if ( raw_pdu->command == sNMP_PDU_V1TRAP) { // timestamp TimeTicks timestamp; timestamp = raw_pdu->time; pdu.set_notify_timestamp( timestamp); // set the agent address IpAddress agent_addr(inet_ntoa(raw_pdu->agent_addr.sin_addr)); if (agent_addr != "0.0.0.0") { pdu.set_v1_trap_address(agent_addr); LOG_BEGIN(DEBUG_LOG | 4); LOG("SNMPMessage: Trap address of received v1 trap"); LOG(agent_addr.get_printable()); LOG_END; } // set enterprise, notifyid Oid enterprise; if (raw_pdu->enterprise_length >0) { for (int i=0; i< raw_pdu->enterprise_length; i++) { enterprise += (int) (raw_pdu->enterprise[i]); } pdu.set_notify_enterprise(enterprise); } switch (raw_pdu->trap_type) { case 0: pdu.set_notify_id(coldStart); break; case 1: pdu.set_notify_id(warmStart); break; case 2: pdu.set_notify_id(linkDown); break; case 3: pdu.set_notify_id(linkUp); break; case 4: pdu.set_notify_id(authenticationFailure); break; case 5: pdu.set_notify_id(egpNeighborLoss); break; case 6: { // enterprise specific // base id + specific # Oid eOid = enterprise; eOid += 0ul; eOid += raw_pdu->specific_type; pdu.set_notify_id( eOid); break; } default: { LOG_BEGIN(WARNING_LOG | 3); LOG("SNMPMessage: Received trap with illegal trap type"); LOG(raw_pdu->trap_type); LOG_END; } } } // vbs Vb tempvb; Oid tempoid; struct variable_list *vp; int vb_nr = 1; for(vp = raw_pdu->variables; vp; vp = vp->next_variable, vb_nr++) { // extract the oid portion tempoid.set_data( (unsigned long *)vp->name, ( unsigned int) vp->name_length); tempvb.set_oid( tempoid); // extract the value portion switch(vp->type){ // octet string case sNMP_SYNTAX_OCTETS: { OctetStr octets( (unsigned char *) vp->val.string, (unsigned long) vp->val_len); tempvb.set_value( octets); } break; case sNMP_SYNTAX_OPAQUE: { OpaqueStr octets( (unsigned char *) vp->val.string, (unsigned long) vp->val_len); tempvb.set_value( octets); } break; // object id case sNMP_SYNTAX_OID: { Oid oid( (unsigned long*) vp->val.objid, (int) vp->val_len); tempvb.set_value( oid); if ((vb_nr == 2) && ((raw_pdu->command == sNMP_PDU_TRAP) || (raw_pdu->command == sNMP_PDU_INFORM)) && (tempoid == SNMP_MSG_OID_TRAPID)) { // set notify_id pdu.set_notify_id(oid); continue; // don't add vb to pdu } } break; // timeticks case sNMP_SYNTAX_TIMETICKS: { TimeTicks timeticks( (unsigned long) *(vp->val.integer)); tempvb.set_value( timeticks); if ((vb_nr == 1) && ((raw_pdu->command == sNMP_PDU_TRAP) || (raw_pdu->command == sNMP_PDU_INFORM)) && (tempoid == SNMP_MSG_OID_SYSUPTIME)) { // set notify_timestamp pdu.set_notify_timestamp( timeticks); continue; // don't add vb to pdu } } break; // 32 bit counter case sNMP_SYNTAX_CNTR32: { Counter32 counter32( (unsigned long) *(vp->val.integer)); tempvb.set_value( counter32); } break; // 32 bit gauge case sNMP_SYNTAX_GAUGE32: { Gauge32 gauge32( (unsigned long) *(vp->val.integer)); tempvb.set_value( gauge32); } break; // ip address case sNMP_SYNTAX_IPADDR: { char buffer[42]; if (vp->val_len == 16) sprintf( buffer, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:" "%02x%02x:%02x%02x:%02x%02x:%02x%02x", vp->val.string[ 0], vp->val.string[ 1], vp->val.string[ 2], vp->val.string[ 3], vp->val.string[ 4], vp->val.string[ 5], vp->val.string[ 6], vp->val.string[ 7], vp->val.string[ 8], vp->val.string[ 9], vp->val.string[10], vp->val.string[11], vp->val.string[12], vp->val.string[13], vp->val.string[14], vp->val.string[15]); else sprintf( buffer,"%d.%d.%d.%d", vp->val.string[0], vp->val.string[1], vp->val.string[2], vp->val.string[3]); IpAddress ipaddress( buffer); tempvb.set_value( ipaddress); } break; // 32 bit integer case sNMP_SYNTAX_INT: { SnmpInt32 int32( (long) *(vp->val.integer)); tempvb.set_value( int32); } break; // 32 bit unsigned integer /* Not distinguishable from Gauge32 case sNMP_SYNTAX_UINT32: { SnmpUInt32 uint32( (unsigned long) *(vp->val.integer)); tempvb.set_value( uint32); } break; */ // v2 counter 64's case sNMP_SYNTAX_CNTR64: { // Frank Fock (was empty before) Counter64 c64(((counter64*)vp->val.counter64)->high, ((counter64*)vp->val.counter64)->low); tempvb.set_value( c64); break; } case sNMP_SYNTAX_NULL: tempvb.set_null(); break; // v2 vb exceptions case sNMP_SYNTAX_NOSUCHOBJECT: case sNMP_SYNTAX_NOSUCHINSTANCE: case sNMP_SYNTAX_ENDOFMIBVIEW: tempvb.set_exception_status(vp->type); break; default: tempvb.set_null(); } // end switch // append the vb to the pdu pdu += tempvb; } snmp_free_pdu( raw_pdu); return SNMP_CLASS_SUCCESS; }