private static unsafe int GetInterfaceIndex(int fd, string name) { const uint SIOCGIFINDEX = 0x8933; const int MaxLen = ifreq.IFNAMSIZ - 1; if (name.Length >= MaxLen) { throw new ArgumentException($"`{name}` exceeds maximum allowed length of {MaxLen} size", nameof(name)); } ifreq ifr = new ifreq(); fixed(char *inIntefaceName = name) { int written = Encoding.ASCII.GetBytes(inIntefaceName, name.Length, ifr.ifr_name, MaxLen); ifr.ifr_name[written] = 0; } int ret = Ioctl3(fd, SIOCGIFINDEX, ref ifr); if (ret == -1) { throw new IOException($"Could not get interface index for `{name}`"); } return(ifr.ifr_ifindex); }
private static extern int ioctl(int fd, int request, ref ifreq mtu);
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); } } }
public NetworkAdapterImpl(string name) : base(name) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); ifr = new ifreq(name); }
private static extern int Ioctl3(int fd, uint request, ref ifreq ifr);
private UnixStream OpenSocket(uint?mtu) { sockaddr_ll sa = new sockaddr_ll(); sa.sll_family = AF_PACKET; sa.sll_protocol = (ushort)IPAddress.HostToNetworkOrder((short)Protocol); sa.sll_ifindex = if_nametoindex(Interface); if (sa.sll_ifindex == 0) { throw new ArgumentException("The interface \"" + Interface + "\" is not valid."); } int fd = -1, ret = -1; try { fd = socket(AF_PACKET, SOCK_RAW, sa.sll_protocol); UnixMarshal.ThrowExceptionForLastErrorIf(fd); ret = bind(fd, ref sa, Marshal.SizeOf(sa)); UnixMarshal.ThrowExceptionForLastErrorIf(ret); ifreq ifr = new ifreq(Interface); 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); 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); } 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); } }