summaryrefslogtreecommitdiffstats
path: root/src/utl_ip.cpp
diff options
context:
space:
mode:
authorIdo Barnea <ibarnea@cisco.com>2016-11-15 16:27:08 +0200
committerIdo Barnea <ibarnea@cisco.com>2016-11-21 12:56:28 +0200
commitbf83f301e4fdbf333240af7f316735e35634c5fd (patch)
tree667c001d36a1b1a95b0f6da82d9919977543746c /src/utl_ip.cpp
parentc39cdf89bee7999ba714eb31e8ec4bc8b5d52a60 (diff)
client config ARP resolve working. Still missing IPv6 support.
Signed-off-by: Ido Barnea <ibarnea@cisco.com>
Diffstat (limited to 'src/utl_ip.cpp')
-rw-r--r--src/utl_ip.cpp122
1 files changed, 122 insertions, 0 deletions
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 <string>
+#include <iostream>
+#include <pkt_gen.h>
+#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<std::pair<uint32_t, uint16_t>, 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<std::pair<uint32_t, uint16_t>, COneIPv4Info>
+ (std::pair<uint32_t, uint16_t>(ip_info.get_ip(), ip_info.get_vlan()), ip_info));
+}
+
+bool CManyIPInfo::lookup(uint32_t ip, uint16_t vlan, MacAddress &ret_mac) {
+ std::map<std::pair<uint32_t, uint16_t>, 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);
+ }
+}
+
+