summaryrefslogtreecommitdiffstats
path: root/src/utl_ip.cpp
blob: 5bd83f953d90edd8e51733d47affa084923461cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 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) {
    ip_vlan_to_many_ip_iter_t 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.get_ip()).c_str(), it->first.get_vlan()
                , utl_macaddr_to_str(mac).c_str());
    }
}

void CManyIPInfo::insert(COneIPv4Info &ip_info) {
    CIpVlan ip_vlan(ip_info.get_ip(), ip_info.get_vlan());

    m_ipv4_resolve.insert(std::make_pair(ip_vlan, ip_info));
}

bool CManyIPInfo::lookup(uint32_t ip, uint16_t vlan, MacAddress &ret_mac) {
    ip_vlan_to_many_ip_iter_t it = m_ipv4_resolve.find(CIpVlan(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);
    }
}