simple-socket library 1.2.0
CANSocket.cpp
Go to the documentation of this file.
1#include "CANSocket.h"
2
3#include <linux/can.h>
4#include <sys/socket.h>
5#include <sys/ioctl.h>
6#include <net/if.h>
7
8#include <cstring>
9
10using namespace NET;
11
12CANSocket::CANSocket( int type, int protocol)
13: SimpleSocket( CAN, type, protocol)
14{}
15
16void CANSocket::connect( std::string_view interface)
17{
18 sockaddr_can addr;
19 addr.can_family = AF_CAN;
20 addr.can_ifindex = getInterfaceIndex(interface);
21
22 if( ::connect( m_socket, (sockaddr*) &addr, sizeof(addr)) < 0)
23 throw SocketException("Connect failed (connect)");
24}
25
26void CANSocket::bind( std::string_view interface)
27{
28 sockaddr_can addr;
29 addr.can_family = AF_CAN;
30 addr.can_ifindex = getInterfaceIndex(interface);
31
32 if( ::bind( m_socket, (sockaddr*) &addr, sizeof(addr)) < 0)
33 throw SocketException("Set of interface failed (bind)");
34}
35
37{
38 sockaddr_can addr;
39 socklen_t addr_len = sizeof(addr);
40
41 if( getsockname( m_socket, (sockaddr*) &addr, &addr_len) < 0)
42 throw SocketException("Fetch of interface failed (getsockname)");
43
44 return getInterfaceName(addr);
45}
46
48{
49 sockaddr_can addr;
50 socklen_t addr_len = sizeof(addr);
51
52 if( getpeername( m_socket, (sockaddr*) &addr, &addr_len) < 0)
53 throw SocketException("Fetch of interface failed (getsockname)");
54
55 return getInterfaceName(addr);
56}
57
58std::string CANSocket::getInterfaceName( const sockaddr_can& addr) const
59{
60 struct ifreq ifr;
61 ifr.ifr_ifindex = addr.can_ifindex;
62
63 if( ioctl( m_socket, SIOCGIFNAME, &ifr) < 0)
64 throw SocketException("ioctl failed (getInterfaceName)");
65
66 return std::string(ifr.ifr_name);
67}
68
69int CANSocket::getInterfaceIndex( std::string_view interface) const
70{
71 const int len = interface.length();
72 // binds to all interfaces
73 if( len == 0) return 0;
74
75 // needed space is size plus null character
76 if( len >= IF_NAMESIZE-1)
77 throw SocketException("Interface name is too long", false);
78
79 struct ifreq ifr;
80 std::memcpy( ifr.ifr_name, interface.data(), len);
81 ifr.ifr_name[len] = 0;
82
83 if( ioctl( m_socket, SIOCGIFINDEX, &ifr) < 0)
84 throw SocketException("ioctl failed (getInterfaceIndex)");
85
86 return ifr.ifr_ifindex;
87}
static const int len
Definition CANFrame.h:7
std::string getLocalInterface() const
Definition CANSocket.cpp:36
int getInterfaceIndex(std::string_view interface) const
get CAN interface index from interface name
Definition CANSocket.cpp:69
CANSocket(int type, int protocol)
allows a subclass to create new socket
Definition CANSocket.cpp:12
void bind(std::string_view interface="")
Definition CANSocket.cpp:26
std::string getInterfaceName(const sockaddr_can &addr) const
get CAN interface name from socket address structure
Definition CANSocket.cpp:58
std::string getForeignInterface() const
Definition CANSocket.cpp:47
void connect(std::string_view interface="")
Definition CANSocket.cpp:16
Signals a problem with the execution of a socket call.
SimpleSocket(int sockfd)
enables return of an accepted socket