can_address.cpp
1.24 KB
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
#include "socket-cpp/can_address.h"
#include "socket-cpp/socket.h"
#include <cstring>
#include <stdexcept>
#include <sys/ioctl.h>
#include <net/if.h>
using namespace std;
using namespace osdev::components::socket-cpp;
constexpr sa_family_t can_address::ADDRESS_FAMILY;
// --------------------------------------------------------------------------
can_address::can_address(unsigned ifindex) : addr_{}
{
addr_.can_family = AF_CAN;
addr_.can_ifindex = ifindex;
}
can_address::can_address(const string& iface) : addr_{}
{
unsigned idx = if_nametoindex(iface.c_str());
if (idx != 0) {
addr_.can_family = AF_CAN;
addr_.can_ifindex = idx;
}
}
can_address::can_address(const sockaddr& addr)
{
auto domain = addr.sa_family;
if (domain != AF_CAN)
throw std::invalid_argument("Not a SocketCAN address");
std::memcpy(&addr_, &addr, sizeof(sockaddr));
}
string can_address::iface() const
{
if (addr_.can_family == AF_UNSPEC)
return string("none");
if (addr_.can_ifindex == 0)
return string("any");
char buf[IF_NAMESIZE];
const char* iface = if_indextoname(addr_.can_ifindex, buf);
return string(iface ? iface : "unknown");
}
ostream& operator<<(ostream& os, const can_address& addr)
{
os << "can:" << addr.iface();
return os;
}