private void StartNetifInformation(NetifConfiguration configuration) { byte[] dhcp = { 10, 0, 0, 0, 10, 0, 0, 1, 255, 255, 255, 0, 0, 1, 81, 128 }; byte[] ip = { 10, 0, 0, 1, 10, 0, 0, 0, 255, 255, 255, 0 }; byte[] dns = { 6, 8, 8, 8, 8, 8, 4, 4, 4, 4 }; byte[] status = { 1, 0, 0, 0 }; fixed(byte *pinned = dhcp) { DhcpNetifInfo *info = (DhcpNetifInfo *)pinned; info->ip = BitConverter.ToUInt32(configuration.Address.GetAddressBytes(), 0); info->gateway = BitConverter.ToUInt32(configuration.GatewayAddress.GetAddressBytes(), 0); info->netmask = BitConverter.ToUInt32(configuration.SubnetMask.GetAddressBytes(), 0); } fixed(byte *pinned = ip) { IpNetifInfo *info = (IpNetifInfo *)pinned; info->address = BitConverter.ToUInt32(configuration.Address.GetAddressBytes(), 0); info->gateway = BitConverter.ToUInt32(configuration.GatewayAddress.GetAddressBytes(), 0); info->subnetmask = BitConverter.ToUInt32(configuration.SubnetMask.GetAddressBytes(), 0); } fixed(byte *pinned = dns) { DnsNetifInfo *info = (DnsNetifInfo *)pinned; info->dns1 = BitConverter.ToUInt32(configuration.DnsAddress1.GetAddressBytes(), 0); info->dns2 = BitConverter.ToUInt32(configuration.DnsAddress2.GetAddressBytes(), 0); } DeviceIoControl(TAP_WIN_IOCTL_SET_MEDIA_STATUS, status); // netif-up DeviceIoControl(TAP_WIN_IOCTL_CONFIG_DHCP_MASQ, dhcp); // DHCP DeviceIoControl(TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT, dns); // DNS DeviceIoControl(TAP_WIN_IOCTL_CONFIG_TUN, ip); // IP mode 1 string commands = $"netsh interface ip set address {this.Index} static {configuration.Address} {configuration.SubnetMask} "; system(commands); }
public virtual void Listen(NetifConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configuration.Address == null) { throw new ArgumentNullException("The network interface device address is not allowed to be null"); } if (configuration.DnsAddress1 == null) { throw new ArgumentNullException("The network interface device dns address 1 is not allowed to be null"); } if (configuration.DnsAddress2 == null) { throw new ArgumentNullException("The network interface device dns address 2 is not allowed to be null"); } if (configuration.GatewayAddress == null) { throw new ArgumentNullException("The network interface device gateway address is not allowed to be null"); } if (configuration.SubnetMask == null) { throw new ArgumentNullException("The network interface device subnet mask is not allowed to be null"); } StartNetifInformation(configuration); ListenNetifLevel3Input((buffer, length) => { IPFrame ip = IPv4.Parse(new BufferSegment(new BufferSegment(buffer, length).ToArray())); if (ip != null) { IPv4.Input(ip); } }); }