Agora
1.2.0
Agora project
|
Go to the documentation of this file.
23 #include <arpa/inet.h>
25 #include <linux/if_packet.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
42 std::ostringstream ret;
43 for (
size_t i = 0;
i < 6;
i++) {
44 ret << std::hex << static_cast<uint32_t>(mac[
i]);
45 if (
i != 5) ret <<
":";
53 int ret = inet_pton(AF_INET, ip, &addr);
54 RtAssert(ret == 1,
"inet_pton() failed for " + std::string(ip));
60 char str[INET_ADDRSTRLEN];
61 const char* ret = inet_ntop(AF_INET, &ipv4_addr, str,
sizeof(str));
63 str[INET_ADDRSTRLEN - 1] = 0;
76 std::ostringstream ret;
80 return std::string(ret.str());
92 std::ostringstream ret;
115 std::ostringstream ret;
135 std::ostringstream ret;
151 auto* eth_hdr =
reinterpret_cast<const eth_hdr_t*
>(buf);
152 auto* ipv4_hdr =
reinterpret_cast<const ipv4_hdr_t*
>(ð_hdr[1]);
153 auto* udp_hdr =
reinterpret_cast<const udp_hdr_t*
>(&ipv4_hdr[1]);
155 return eth_hdr->
to_string() +
", " + ipv4_hdr->to_string() +
", " +
156 udp_hdr->to_string();
176 ipv4_hdr->
id = htons(0);
197 struct ifaddrs *ifaddr, *ifa;
199 uint32_t ipv4_addr = 0;
201 for (ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
202 if (strcmp(ifa->ifa_name, interface.c_str()) != 0)
continue;
205 if (ifa->ifa_addr ==
nullptr || ifa->ifa_addr->sa_family != AF_INET) {
209 auto sin_addr =
reinterpret_cast<sockaddr_in*
>(ifa->ifa_addr);
210 ipv4_addr = ntohl(*
reinterpret_cast<uint32_t*
>(&sin_addr->sin_addr));
214 std::string(
"Failed to find interface ") + interface);
224 ifr.ifr_addr.sa_family = AF_INET;
225 strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ - 1);
227 int fd = socket(AF_INET, SOCK_DGRAM, 0);
230 int ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
231 RtAssert(ret == 0,
"MAC address IOCTL failed");
234 for (
size_t i = 0;
i < 6;
i++) {
235 mac[
i] =
static_cast<uint8_t
>(ifr.ifr_hwaddr.sa_data[
i]);
239 #endif // ETH_COMMON_H_
std::string to_string()
Definition: eth_common.h:75
uint8_t version
Definition: eth_common.h:102
uint16_t eth_type
Definition: eth_common.h:89
uint8_t protocol
Definition: eth_common.h:109
uint32_t ipv4_addr
Definition: eth_common.h:72
uint8_t dscp
Definition: eth_common.h:104
static std::string ipv4_to_string(uint32_t ipv4_addr)
Convert a network-byte-order IPv4 address to a human-readable IP string.
Definition: eth_common.h:59
uint8_t src_mac[6]
Definition: eth_common.h:61
static constexpr uint16_t kIPHdrProtocol
Definition: eth_common.h:39
uint16_t src_port
Definition: eth_common.h:60
uint16_t tot_len
Definition: eth_common.h:105
uint16_t len
Definition: eth_common.h:131
uint16_t udp_port
Definition: eth_common.h:73
Definition: eth_common.h:86
uint32_t src_ip
Definition: eth_common.h:70
uint16_t dst_port
Definition: eth_common.h:61
uint8_t mac[6]
Definition: eth_common.h:71
static void gen_ipv4_header(ipv4_hdr_t *ipv4_hdr, uint32_t src_ip, uint32_t dst_ip, uint16_t data_size)
Definition: eth_common.h:169
std::string to_string() const
Definition: eth_common.h:114
static uint32_t ipv4_from_str(const char *ip)
Get the network-byte-order IPv4 address from a human-readable IP string.
Definition: eth_common.h:51
static std::string frame_header_to_string(const uint8_t *buf)
Definition: eth_common.h:150
void close()
Definition: matplotlibcpp.h:2567
uint8_t ttl
Definition: eth_common.h:108
uint32_t dst_ip
Definition: eth_common.h:112
for i
Definition: generate_data.m:107
uint16_t id
Definition: eth_common.h:106
uint8_t dst_mac[6]
Definition: eth_common.h:60
std::string to_string() const
Definition: eth_common.h:134
uint8_t src_mac[6]
Definition: eth_common.h:88
struct eth_hdr_t __attribute__((packed))
uint16_t frag_off
Definition: eth_common.h:107
uint16_t check
Definition: eth_common.h:132
uint8_t ecn
Definition: eth_common.h:103
Definition: eth_common.h:128
static constexpr size_t kInetHdrsTotSize
Definition: eth_common.h:144
static void fill_interface_mac(const std::string &interface, uint8_t *mac)
Fill the MAC address of kernel-visible interface.
Definition: eth_common.h:221
std::string to_string() const
Definition: eth_common.h:91
data_size
Definition: inspect_agora_results.m:16
uint16_t check
Definition: eth_common.h:110
uint8_t ihl
Definition: eth_common.h:101
uint16_t src_port
Definition: eth_common.h:129
static void RtAssert(bool condition, const char *throw_str)
Definition: utils.h:104
static uint32_t get_interface_ipv4_addr(const std::string &interface)
Return the IPv4 address of a kernel-visible interface in host-byte order.
Definition: eth_common.h:196
std::string to_string() const
Definition: eth_common.h:64
uint32_t src_ip
Definition: eth_common.h:111
static constexpr uint16_t kIPEtherType
Definition: eth_common.h:38
Utility functions for file and text processing.
uint16_t dst_port
Definition: eth_common.h:130
static void gen_eth_header(eth_hdr_t *eth_header, const uint8_t *src_mac, const uint8_t *dst_mac)
Fill in Ethernet header eth_header using the source and destination MAC.
Definition: eth_common.h:160
Definition: eth_common.h:100
uint32_t dst_ip
Definition: eth_common.h:71
static void gen_udp_header(udp_hdr_t *udp_hdr, uint16_t src_port, uint16_t dst_port, uint16_t data_size)
Definition: eth_common.h:187
Definition: eth_common.h:70
static std::string mac_to_string(const uint8_t *mac)
Definition: eth_common.h:41
uint8_t dst_mac[6]
Definition: eth_common.h:87