utl_ip.cpp revision 4c8363ea
1/*
2 Cisco Systems, Inc.
3*/
4
5/*
6Copyright (c) 2016-2016 Cisco Systems, Inc.
7
8Licensed under the Apache License, Version 2.0 (the "License");
9you may not use this file except in compliance with the License.
10You may obtain a copy of the License at
11
12    http://www.apache.org/licenses/LICENSE-2.0
13
14Unless required by applicable law or agreed to in writing, software
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19*/
20#include <string>
21#include <iostream>
22#include <pkt_gen.h>
23#include "utl_ip.h"
24
25void COneIPInfo::dump(FILE *fd, const char *offset) const {
26    uint8_t mac[ETHER_ADDR_LEN];
27    m_mac.copyToArray(mac);
28    char ip_str[100];
29    get_ip_str(ip_str);
30    std::string mac_str;
31    utl_macaddr_to_str(mac, mac_str);
32    const char *mac_char = resolve_needed() ?  "Unknown" : mac_str.c_str();
33    fprintf(fd, "%sip: %s ", offset, ip_str);
34    if (m_vlan != 0)
35        fprintf(fd, "vlan: %d ", m_vlan);
36    if (m_port != UINT8_MAX)
37        fprintf(fd, "port: %d ", m_port);
38    fprintf(fd, "mac: %s", mac_char);
39    fprintf(fd, "\n");
40}
41
42bool COneIPInfo::resolve_needed() const {
43    return m_mac.isDefaultAddress();
44}
45
46/*
47 * Fill buffer p with arp request.
48 * port_id - port id we intend to send on
49 * sip - source IP/MAC information
50 */
51void COneIPv4Info::fill_arp_req_buf(uint8_t *p, uint16_t port_id, COneIPInfo *sip) {
52    uint8_t src_mac[ETHER_ADDR_LEN];
53    sip->get_mac(src_mac);
54
55    CTestPktGen::create_arp_req(p, ((COneIPv4Info *)sip)->get_ip(), m_ip, src_mac, m_vlan, port_id);
56}
57
58void COneIPv4Info::fill_grat_arp_buf(uint8_t *p) {
59    uint8_t src_mac[ETHER_ADDR_LEN];
60    get_mac(src_mac);
61
62    CTestPktGen::create_arp_req(p, m_ip, m_ip, src_mac, m_vlan, 0);
63}
64
65void COneIPv6Info::fill_arp_req_buf(uint8_t *p, uint16_t port_id, COneIPInfo *sip) {
66    //??? implement ipv6
67}
68
69void COneIPv6Info::fill_grat_arp_buf(uint8_t *p) {
70    //??? implement ipv6
71}
72
73const COneIPInfo *CManyIPInfo::get_next() {
74    COneIPInfo *ret;
75
76    if (!m_iter_initiated) {
77        m_ipv4_iter = m_ipv4_resolve.begin();
78        m_iter_initiated = true;
79    }
80
81    if (m_ipv4_iter == m_ipv4_resolve.end()) {
82        m_ipv4_iter = m_ipv4_resolve.begin();
83        return NULL;
84    }
85
86    ret = &(m_ipv4_iter->second);
87    m_ipv4_iter++;
88    return ret;
89}
90
91void CManyIPInfo::dump(FILE *fd) {
92    ip_vlan_to_many_ip_iter_t it;
93    for (it = m_ipv4_resolve.begin(); it != m_ipv4_resolve.end(); it++) {
94        fprintf(fd, "IPv4 resolved list:\n");
95        uint8_t mac[ETHER_ADDR_LEN];
96        it->second.get_mac(mac);
97        fprintf(fd, "ip:%s vlan: %d resolved to mac %s\n", ip_to_str(it->first.get_ip()).c_str(), it->first.get_vlan()
98                , utl_macaddr_to_str(mac).c_str());
99    }
100}
101
102void CManyIPInfo::insert(COneIPv4Info &ip_info) {
103    CIpVlan ip_vlan(ip_info.get_ip(), ip_info.get_vlan());
104
105    m_ipv4_resolve.insert(std::make_pair(ip_vlan, ip_info));
106}
107
108bool CManyIPInfo::lookup(uint32_t ip, uint16_t vlan, MacAddress &ret_mac) {
109    ip_vlan_to_many_ip_iter_t it = m_ipv4_resolve.find(CIpVlan(ip, vlan));
110    if (it != m_ipv4_resolve.end()) {
111        uint8_t mac[ETHER_ADDR_LEN];
112        (*it).second.get_mac(mac);
113        ret_mac.set(mac);
114        return true;
115    } else {
116        return false;
117    }
118}
119
120const COneIPInfo *CManyIPInfo::get_first() {
121    if (m_ipv4_resolve.size() == 0) {
122        return NULL;
123    } else {
124        m_ipv4_iter = m_ipv4_resolve.begin();
125        return &(m_ipv4_iter->second);
126    }
127}
128