From bf83f301e4fdbf333240af7f316735e35634c5fd Mon Sep 17 00:00:00 2001 From: Ido Barnea Date: Tue, 15 Nov 2016 16:27:08 +0200 Subject: client config ARP resolve working. Still missing IPv6 support. Signed-off-by: Ido Barnea --- src/utl_ip.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/utl_ip.cpp (limited to 'src/utl_ip.cpp') diff --git a/src/utl_ip.cpp b/src/utl_ip.cpp new file mode 100644 index 00000000..c645c7ed --- /dev/null +++ b/src/utl_ip.cpp @@ -0,0 +1,122 @@ +/* + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2016-2016 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include +#include +#include +#include "utl_ip.h" + +void COneIPInfo::dump(FILE *fd, const char *offset) const { + uint8_t mac[ETHER_ADDR_LEN]; + m_mac.copyToArray(mac); + char ip_str[100]; + get_ip_str(ip_str); + std::string mac_str; + utl_macaddr_to_str(mac, mac_str); + const char *mac_char = resolve_needed() ? "Not resolved" : mac_str.c_str(); + fprintf(fd, "%sip: %s vlan: %d port: %d mac: %s\n", offset, ip_str, m_vlan, m_port, mac_char); +} + +bool COneIPInfo::resolve_needed() const { + return m_mac.isDefaultAddress(); +} + +/* + * Fill buffer p with arp request. + * port_id - port id we intend to send on + * sip - source IP/MAC information + */ +void COneIPv4Info::fill_arp_req_buf(uint8_t *p, uint16_t port_id, COneIPInfo *sip) { + uint8_t src_mac[ETHER_ADDR_LEN]; + sip->get_mac(src_mac); + + CTestPktGen::create_arp_req(p, ((COneIPv4Info *)sip)->get_ip(), m_ip, src_mac, m_vlan, port_id); +} + +void COneIPv4Info::fill_grat_arp_buf(uint8_t *p) { + uint8_t src_mac[ETHER_ADDR_LEN]; + get_mac(src_mac); + + CTestPktGen::create_arp_req(p, m_ip, m_ip, src_mac, m_vlan, 0); +} + +void COneIPv6Info::fill_arp_req_buf(uint8_t *p, uint16_t port_id, COneIPInfo *sip) { + //??? implement ipv6 +} + +void COneIPv6Info::fill_grat_arp_buf(uint8_t *p) { + //??? implement ipv6 +} + +const COneIPInfo *CManyIPInfo::get_next() { + COneIPInfo *ret; + + if (!m_iter_initiated) { + m_ipv4_iter = m_ipv4_resolve.begin(); + m_iter_initiated = true; + } + + if (m_ipv4_iter == m_ipv4_resolve.end()) { + m_ipv4_iter = m_ipv4_resolve.begin(); + return NULL; + } + + ret = &(m_ipv4_iter->second); + m_ipv4_iter++; + return ret; +} + +void CManyIPInfo::dump(FILE *fd) { + std::map, COneIPv4Info>::iterator it; + for (it = m_ipv4_resolve.begin(); it != m_ipv4_resolve.end(); it++) { + fprintf(fd, "IPv4 resolved list:\n"); + uint8_t mac[ETHER_ADDR_LEN]; + it->second.get_mac(mac); + fprintf(fd, "ip:%s vlan: %d resolved to mac %s\n", ip_to_str(it->first.first).c_str(), it->first.second + , utl_macaddr_to_str(mac).c_str()); + } +} + +void CManyIPInfo::insert(COneIPv4Info &ip_info) { + m_ipv4_resolve.insert(std::pair, COneIPv4Info> + (std::pair(ip_info.get_ip(), ip_info.get_vlan()), ip_info)); +} + +bool CManyIPInfo::lookup(uint32_t ip, uint16_t vlan, MacAddress &ret_mac) { + std::map, COneIPv4Info>::iterator it = m_ipv4_resolve.find(std::make_pair(ip, vlan)); + if (it != m_ipv4_resolve.end()) { + uint8_t mac[ETHER_ADDR_LEN]; + (*it).second.get_mac(mac); + ret_mac.set(mac); + return true; + } else { + return false; + } +} + +const COneIPInfo *CManyIPInfo::get_first() { + if (m_ipv4_resolve.size() == 0) { + return NULL; + } else { + m_ipv4_iter = m_ipv4_resolve.begin(); + return &(m_ipv4_iter->second); + } +} + + -- cgit 1.2.3-korg