private static extern int bind(int fd, ref sockaddr_can addr, int addrlen);
private UnixStream OpenSocket(uint?mtu) { /* * sockaddr_ll sa = new sockaddr_ll(); * sa.sll_family = AF_CAN; * sa.sll_protocol = CAN_RAW; * sa.sll_ifindex = if_nametoindex(Interface); */ sockaddr_can addr = new sockaddr_can(); addr.can_family = AF_CAN; int fd = -1, ret = -1; try { fd = socket(AF_CAN, SOCK_RAW, CAN_RAW); UnixMarshal.ThrowExceptionForLastErrorIf(fd); ifreq ifr = new ifreq(Interface); /* Doesn't seem to work * ret = ioctl(fd, SIOCGIFINDEX, ref ifr); * UnixMarshal.ThrowExceptionForLastErrorIf(ret); */ addr.can_ifindex = if_nametoindex(Interface); if (addr.can_ifindex == 0) { throw new ArgumentException("The interface \"" + Interface + "\" is not valid."); } ret = bind(fd, ref addr, Marshal.SizeOf(addr)); UnixMarshal.ThrowExceptionForLastErrorIf(ret); /* * if (orig_mtu == 0) * { * ret = ioctl(fd, SIOCGIFMTU, ref ifr); * UnixMarshal.ThrowExceptionForLastErrorIf(ret); * orig_mtu = ifr.ifru_mtu; * } * * if (mtu != null) * { * ifr.ifru_mtu = mtu.Value; * //ret = ioctl(fd, SIOCSIFMTU, ref ifr); * ret = ioctl(fd, SIOCGIFMTU, ref ifr); * UnixMarshal.ThrowExceptionForLastErrorIf(ret); * } * */ ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); /* * if (mtu != null && ifr.ifru_mtu != mtu.Value) * throw new PeachException("MTU change did not take effect."); */ _mtu = ifr.ifru_mtu; /* * * if (ifr.ifru_mtu > (MaxMTU - EthernetHeaderSize)) * _bufferSize = (int)MaxMTU; * else * _bufferSize = (int)(ifr.ifru_mtu + EthernetHeaderSize); */ _bufferSize = CAN_MTU; var stream = new UnixStream(fd); fd = -1; return(stream); } catch (InvalidOperationException ex) { if (ex.InnerException != null) { var inner = ex.InnerException as UnixIOException; if (inner != null && inner.ErrorCode == Errno.EPERM) { throw new PeachException("Access denied when opening the raw ethernet publisher. Ensure the user has the appropriate permissions.", ex); } } throw; } finally { if (fd != -1) { Syscall.close(fd); } } }
private UnixStream OpenSocket(uint? mtu) { /* sockaddr_ll sa = new sockaddr_ll(); sa.sll_family = AF_CAN; sa.sll_protocol = CAN_RAW; sa.sll_ifindex = if_nametoindex(Interface); */ sockaddr_can addr = new sockaddr_can(); addr.can_family = AF_CAN; int fd = -1, ret = -1; try { fd = socket(AF_CAN, SOCK_RAW, CAN_RAW); UnixMarshal.ThrowExceptionForLastErrorIf(fd); ifreq ifr = new ifreq(Interface); /* Doesn't seem to work ret = ioctl(fd, SIOCGIFINDEX, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); */ addr.can_ifindex = if_nametoindex(Interface); if (addr.can_ifindex == 0) throw new ArgumentException("The interface \"" + Interface + "\" is not valid."); ret = bind(fd, ref addr, Marshal.SizeOf(addr)); UnixMarshal.ThrowExceptionForLastErrorIf(ret); /* if (orig_mtu == 0) { ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); orig_mtu = ifr.ifru_mtu; } if (mtu != null) { ifr.ifru_mtu = mtu.Value; //ret = ioctl(fd, SIOCSIFMTU, ref ifr); ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); } */ ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); /* if (mtu != null && ifr.ifru_mtu != mtu.Value) throw new PeachException("MTU change did not take effect."); */ _mtu = ifr.ifru_mtu; /* if (ifr.ifru_mtu > (MaxMTU - EthernetHeaderSize)) _bufferSize = (int)MaxMTU; else _bufferSize = (int)(ifr.ifru_mtu + EthernetHeaderSize); */ _bufferSize = CAN_MTU; var stream = new UnixStream(fd); fd = -1; return stream; } catch (InvalidOperationException ex) { if (ex.InnerException != null) { var inner = ex.InnerException as UnixIOException; if (inner != null && inner.ErrorCode == Errno.EPERM) throw new PeachException("Access denied when opening the raw ethernet publisher. Ensure the user has the appropriate permissions.", ex); } throw; } finally { if (fd != -1) Syscall.close(fd); } }