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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::{os::raw::{c_int, c_void}, mem::MaybeUninit, ffi::CString};
use filedesc::FileDesc;

use crate::CanId;

#[repr(C)]
#[derive(Copy, Clone, Debug)]
#[allow(non_camel_case_types)]
struct can_frame {
	pub can_id: u32,
	pub can_dlc: u8,
	_pad: u8,
	_res0: u8,
	pub len8_dlc: u8,
	pub data: [u8; 8],
}


#[derive(Debug)]
pub struct Socket {
	fd: FileDesc,
}

pub struct CanFrame {
	inner: can_frame
}

#[repr(transparent)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CanInterface {
	index: u32,
}

impl CanFrame {
	pub fn new(id: impl Into<CanId>, data: &[u8], data_length_code: Option<u8>) -> std::io::Result<Self> {
		let id = id.into();

		if data.len() > 8 {
			return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "maximum CAN data length is 8 bytes"));
		}
		if let Some(data_length_code) = data_length_code {
			if data.len() != 8 {
				return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "data_length_code can only be set if data length is 8"));
			}
			if !(9..=15).contains(&data_length_code) {
				return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "data_length_code must be in the range 9 to 15 (inclusive)"));
			}
		}

		let mut inner: can_frame = unsafe { std::mem::zeroed() };
		inner.can_id = match id {
			CanId::Extended(x) => x.as_u32() | libc::CAN_EFF_FLAG,
			CanId::Base(x) => x.as_u16().into(),
		};
		inner.can_dlc = data.len() as u8;
		inner.len8_dlc = data_length_code.unwrap_or(0);
		inner.data[..data.len()].copy_from_slice(data);
		Ok(Self { inner })
	}

	pub fn id(&self) -> CanId {
		// Unwrap should be fine: the kernel should never give us an invalid CAN ID,
		// and the Rust constructor doesn't allow it.
		if self.inner.can_id & libc::CAN_EFF_FLAG == 0 {
			CanId::new_base((self.inner.can_id & libc::CAN_SFF_MASK) as u16).unwrap()
		} else {
			CanId::new_extended(self.inner.can_id & libc::CAN_EFF_MASK).unwrap()
		}
	}

	pub fn data(&self) -> &[u8] {
		&self.inner.data[..self.inner.can_dlc as usize]
	}

	pub fn data_length_code(&self) -> Option<u8> {
		if self.inner.len8_dlc > 0 {
			Some(self.inner.len8_dlc)
		} else {
			None
		}
	}

	fn as_c_void_ptr(&self) -> *const c_void {
		(self as *const Self).cast()
	}
}

impl CanInterface {
	pub fn from_index(index: u32) -> Self {
		Self { index }
	}

	pub fn from_name(name: &str) -> std::io::Result<Self> {
		let name = CString::new(name)
			.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "interface name contain a null byte"))?;
		let index = unsafe { libc::if_nametoindex(name.as_ptr()) };
		if index == 0 {
			return Err(std::io::Error::last_os_error());
		}
		Ok(Self::from_index(index))
	}

	pub fn index(&self) -> u32 {
		self.index
	}

	pub fn get_name(&self) -> std::io::Result<String> {
		let mut buffer = vec![0u8; libc::IF_NAMESIZE];
		let name = unsafe { libc::if_indextoname(self.index, buffer.as_mut_ptr().cast()) };
		if name.is_null() {
			return Err(std::io::Error::last_os_error());
		}
		if let Some(len) = buffer.iter().position(|&byte| byte == 0) {
			buffer.truncate(len)
		}
		String::from_utf8(buffer)
			.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "interface name contains invalid UTF-8"))
	}

	fn to_address(&self) -> libc::sockaddr_can {
		unsafe {
			let mut addr: libc::sockaddr_can = std::mem::zeroed();
			addr.can_family = libc::AF_CAN as _;
			addr.can_ifindex = self.index as _;
			addr
		}
	}
}

impl Socket {
	pub fn new(non_blocking: bool) -> std::io::Result<Self> {
		let flags = match non_blocking {
			true => libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
			false => libc::SOCK_CLOEXEC,
		};
		unsafe {
			let fd = check_int(libc::socket(libc::PF_CAN, libc::SOCK_RAW | flags, libc::CAN_RAW))?;
			Ok(Self {
				fd: FileDesc::from_raw_fd(fd),
			})
		}
	}

	pub fn set_nonblocking(&self, non_blocking: bool) -> std::io::Result<()> {
		unsafe {
			let flags = check_int(libc::fcntl(self.fd.as_raw_fd(), libc::F_GETFL))?;
			let flags = match non_blocking {
				true => flags | libc::O_NONBLOCK,
				false => flags & !libc::O_NONBLOCK,
			};
			check_int(libc::fcntl(self.fd.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK))?;
		}
		Ok(())
	}

	pub fn get_interface_by_name(&self, name: &str) -> std::io::Result<CanInterface> {
		unsafe {
			let mut req: libc::ifreq = std::mem::zeroed();
			if name.len() + 1 > std::mem::size_of_val(&req.ifr_name) {
				return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "interface name too long"));
			}
			if name.as_bytes().iter().any(|&byte| byte == 0) {
				return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "interface name contain a null byte"));
			}
			std::ptr::copy(name.as_ptr().cast(), req.ifr_name.as_mut_ptr(), name.len());
			check_int(libc::ioctl(self.fd.as_raw_fd(), libc::SIOCGIFINDEX, &mut req))?;
			Ok(CanInterface::from_index(req.ifr_ifru.ifru_ifindex as u32))
		}
	}

	pub fn bind(&self, interface: &CanInterface) -> std::io::Result<()> {
		unsafe {
			let addr = interface.to_address();
			check_int(libc::bind(
				self.fd.as_raw_fd(),
				&addr as *const _ as *const _,
				std::mem::size_of_val(&addr) as _),
			)?;
			Ok(())
		}
	}

	pub fn send(&self, frame: &CanFrame) -> std::io::Result<()> {
		unsafe {
			let written = check_isize(libc::send(
				self.fd.as_raw_fd(),
				frame.as_c_void_ptr(),
				std::mem::size_of_val(frame),
				0,
			))?;
			debug_assert!(written as usize == std::mem::size_of_val(frame));
			Ok(())
		}
	}

	pub fn send_to(&self, frame: &CanFrame, interface: &CanInterface) -> std::io::Result<()> {
		unsafe {
			let address = interface.to_address();
			let written = check_isize(libc::sendto(
				self.fd.as_raw_fd(),
				&frame as *const _ as *const _,
				std::mem::size_of_val(frame),
				0,
				&address as *const _ as *const _,
				std::mem::size_of_val(&address) as _,
			))?;
			debug_assert!(written as usize == std::mem::size_of_val(frame));
			Ok(())
		}
	}

	pub fn recv(&self) -> std::io::Result<CanFrame> {
		unsafe {
			let mut frame: MaybeUninit<CanFrame> = MaybeUninit::uninit();
			let read = check_isize(libc::recv(
				self.fd.as_raw_fd(),
				frame.as_mut_ptr().cast(),
				std::mem::size_of_val(&frame),
				0,
			))?;
			debug_assert!(read as usize == std::mem::size_of_val(&frame));
			Ok(frame.assume_init())
		}
	}

	pub fn recv_from(&self) -> std::io::Result<(CanFrame, CanInterface)> {
		unsafe {
			let mut frame: MaybeUninit<CanFrame> = MaybeUninit::uninit();
			let mut addr: libc::sockaddr_can = std::mem::zeroed();
			let read = check_isize(libc::recvfrom(
				self.fd.as_raw_fd(),
				frame.as_mut_ptr().cast(),
				std::mem::size_of_val(&frame),
				0,
				&mut addr as *mut _ as *mut _,
				std::mem::size_of_val(&addr) as _,
			))?;
			debug_assert!(read as usize == std::mem::size_of_val(&frame));

			Ok((frame.assume_init(), CanInterface { index: addr.can_ifindex as u32 }))
		}
	}
}

fn check_int(return_value: c_int) -> std::io::Result<c_int> {
	if return_value == -1 {
		Err(std::io::Error::last_os_error())
	} else {
		Ok(return_value)
	}
}

fn check_isize(return_value: isize) -> std::io::Result<isize> {
	if return_value == -1 {
		Err(std::io::Error::last_os_error())
	} else {
		Ok(return_value)
	}
}

impl std::os::fd::AsFd for Socket {
	fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
		self.fd.as_fd()
	}
}

impl From<Socket> for std::os::fd::OwnedFd {
	fn from(value: Socket) -> Self {
		value.fd.into()
	}
}

impl From<std::os::fd::OwnedFd> for Socket {
	fn from(value: std::os::fd::OwnedFd) -> Self {
		Self {
			fd: FileDesc::from(value),
		}
	}
}

impl std::os::fd::AsRawFd for Socket {
	fn as_raw_fd(&self) -> std::os::fd::RawFd {
		self.fd.as_raw_fd()
	}
}

impl std::os::fd::IntoRawFd for Socket {
	fn into_raw_fd(self) -> std::os::fd::RawFd {
		self.fd.into_raw_fd()
	}
}

impl std::os::fd::FromRawFd for Socket {
	unsafe fn from_raw_fd(fd: std::os::fd::RawFd) -> Self {
		Self {
			fd: FileDesc::from_raw_fd(fd)
		}
	}
}