Exemple #1
0
 public int sceNetResolverStartNtoA(int ResolverId, string HostName, in_addr* Address, uint Timeout, int Retries)
 {
     var Resolver = Resolvers.Get(ResolverId);
     var ResolvedAddress = Dns.GetHostEntry(HostName).AddressList[0];
     var Bytes = ResolvedAddress.GetAddressBytes();
     (*Address).AddressAsBytes = Bytes;
     //(*addr).Address =
     //Resolver.Resolve();
     //throw(new NotImplementedException());
     return 0;
 }
Exemple #2
0
 public static PhysicalAddress SendARP(IPAddress destIP, IPAddress srcIP = null)
 {
     in_addr srcAddr = new in_addr((srcIP ?? IPAddress.Parse("0.0.0.0")).ToString());
     in_addr destAddr = new in_addr(destIP.ToString());
     UInt64 macAddr = 0;
     uint phyAddrLen = 8;
     ERROR result = Iphlpapi.SendARP(destAddr, srcAddr, ref macAddr, ref phyAddrLen);
     PhysicalAddress mac = new PhysicalAddress(new byte[] { });
     if (result == ERROR.ERROR_SUCCESS)
         mac = new PhysicalAddress(BitConverter.GetBytes(macAddr));
     return mac;
 }
Exemple #3
0
        public static PhysicalAddress SendARP(IPAddress destIP, IPAddress srcIP = null)
        {
            in_addr         srcAddr    = new in_addr((srcIP ?? IPAddress.Parse("0.0.0.0")).ToString());
            in_addr         destAddr   = new in_addr(destIP.ToString());
            UInt64          macAddr    = 0;
            uint            phyAddrLen = 8;
            ERROR           result     = Iphlpapi.SendARP(destAddr, srcAddr, ref macAddr, ref phyAddrLen);
            PhysicalAddress mac        = new PhysicalAddress(new byte[] { });

            if (result == ERROR.ERROR_SUCCESS)
            {
                mac = new PhysicalAddress(BitConverter.GetBytes(macAddr));
            }
            return(mac);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            tErr err = 0;

            // initialize the API.
            if ((err = Pv.Initialize()) == 0)
            {
                in_addr Camera = new in_addr();

                ListingCameras(ref Camera);

                // Uninitialize the API.
                Pv.UnInitialize();
            }
            else
            {
                Console.WriteLine("Failed to initialize.");
            }
        }
Exemple #5
0
        public static int Ping(IPAddress ip)
        {
            in_addr destinationAddress = new in_addr((ip ?? IPAddress.Parse("0.0.0.0")).ToString());
            IntPtr  icmpHandle         = IcmpCreateFile();

            byte[] requestData = new byte[] { 0 };
            byte[] replyByte   = new byte[1000];
            uint   result      = IcmpSendEcho(icmpHandle, destinationAddress, requestData, (ushort)requestData.Length, IntPtr.Zero, replyByte, (uint)replyByte.Length, 1000);

            if (result > 0)
            {
                GCHandle        handle = GCHandle.Alloc(replyByte, GCHandleType.Pinned);
                ICMP_ECHO_REPLY reply  = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY));
                return((int)reply.RoundTripTime);
            }
            ERROR error = (ERROR)GetLastError();

            return(-1);
        }
Exemple #6
0
        private void Start(ushort port, byte address1, byte address2, byte address3, byte address4)
        {
            // BIND
            in_addr inAddress = new in_addr();

            inAddress.s_b1 = address1;
            inAddress.s_b2 = address2;
            inAddress.s_b3 = address3;
            inAddress.s_b4 = address4;

            sockaddr_in sa = new sockaddr_in();

            sa.sin_family = ADDRESS_FAMILIES.AF_INET;
            sa.sin_port   = SocketImports.htons(port);
            sa.sin_addr   = inAddress;

            int result;

            unsafe
            {
                var size = sizeof(sockaddr_in);
                result = SocketImports.bind(_handle, ref sa, size);
            }
            if (result == SocketImports.SOCKET_ERROR)
            {
                SocketImports.WSACleanup();
                throw new Exception("bind failed");
            }

            // LISTEN
            result = SocketImports.listen(_handle, 10);
            if (result == SocketImports.SOCKET_ERROR)
            {
                SocketImports.WSACleanup();
                throw new Exception("listen failed");
            }
        }
Exemple #7
0
 public static extern IntPtr inet_ntoa(in_addr a);
Exemple #8
0
 public static extern uint IcmpSendEcho(IntPtr IcmpHandle, in_addr DestinationAddress, byte[] RequestData, ushort RequestSize, ref IP_OPTION_INFORMATION RequestOptions, byte[] ReplyBuffer, uint ReplySize, uint Timeout);
Exemple #9
0
 public int sceNetResolverStartAtoN(int rid, in_addr* addr, byte *hostname, int hostname_len, uint timeout, int retry)
 {
     throw(new NotImplementedException());
 }
Exemple #10
0
 // http://www.beej.us/guide/bgnet/output/html/multipage/inet_ntoaman.html
 public static string inet_ntoa(this in_addr cp)
 {
     return(default(string));
 }
Exemple #11
0
 public static int Ping(IPAddress ip)
 {
     in_addr destinationAddress = new in_addr((ip ?? IPAddress.Parse("0.0.0.0")).ToString());
     IntPtr icmpHandle = IcmpCreateFile();
     byte[] requestData = new byte[] { 0 };
     byte[] replyByte = new byte[1000];
     uint result = IcmpSendEcho(icmpHandle, destinationAddress, requestData, (ushort)requestData.Length, IntPtr.Zero, replyByte, (uint)replyByte.Length, 1000);
     if (result > 0)
     {
         GCHandle handle = GCHandle.Alloc(replyByte, GCHandleType.Pinned);
         ICMP_ECHO_REPLY reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY));
         return (int)reply.RoundTripTime;
     }
     ERROR error = (ERROR)GetLastError();
     return -1;
 }
Exemple #12
0
        static void ListingCameras(ref in_addr Camera)
        {
            UInt32 MaxCameraList = 20;

            tCameraInfo[] CameraList = new tCameraInfo[MaxCameraList];
            UInt32        CameraNum  = 0;
            UInt32        CameraRle;
            UInt32        Zero  = 0;
            bool          gstop = true;


            while (gstop)
            {
                Console.WriteLine("***********************************");

                // Get list of reachable cameras.
                CameraNum = Pv.CameraList(CameraList, MaxCameraList, ref Zero);

                // Store how many cameras listed are reachable.
                CameraRle = CameraNum;

                // Append the list of unreachable cameras.
                if (CameraNum < MaxCameraList)
                {
                    CameraNum += Pv.CameraListUnreachable(CameraList, (MaxCameraList - CameraNum), ref Zero);
                }

                if (CameraNum > 0)
                {
                    // Go through the number of cameras detected.
                    for (UInt32 i = 0; i < CameraNum; i++)
                    {
                        if (i < CameraRle)
                        {
                            // Get the camera's IP configuration.
                            if ((Camera.lErr = Pv.CameraIpSettingsGet(CameraList[i].UniqueId, ref Camera.Conf)) == tErr.eErrSuccess)
                            {
                                Camera.S_addr = Camera.Conf.CurrentIpAddress;
                                System.Net.IPAddress Address = new IPAddress(Camera.S_addr);

                                // Display the camera info.
                                Console.Write("{0}- {1} - Unique ID = {2} IP@ = {3}", CameraList[i].SerialString, CameraList[i].DisplayName, CameraList[i].UniqueId, Address);

                                // Check to see if the camera is being used.
                                if (CameraList[i].PermittedAccess == 6)
                                {
                                    Console.WriteLine(" [Available]");
                                }
                                else
                                {
                                    Console.WriteLine(" [In use]");
                                }

                                Console.WriteLine();
                            }
                            else
                            {
                                Console.Write("{0}- {1} - Unique ID = {2} [Unavailable] ", CameraList[i].SerialString, CameraList[i].DisplayName, CameraList[i].UniqueId);
                            }
                        }
                        else
                        {
                            Console.Write("{0}- {1} - Unique ID = {2}", CameraList[i].SerialString, CameraList[i].DisplayName, CameraList[i].UniqueId);
                        }
                    }
                    if (CameraNum != CameraRle)
                    {
                        Console.WriteLine("Camera is unreachable.");
                    }
                }
                else
                {
                    Console.WriteLine("There is no camera detected.");
                }

                // Wait a certain amount of time before checking again.
                Thread.Sleep(1500);
            }
        }
Exemple #13
0
 /// <summary>
 /// Resolves an IPv4 address to a MAC address
 /// </summary>
 /// <param name="sDestIP"></param>
 /// <param name="sSrcIP"></param>
 /// <returns></returns>
 public static string GetMacAddress(string sDestIP, string sSrcIP = "0.0.0.0")
 {
     in_addr destIP = new in_addr(sDestIP);
     in_addr srcIP = new in_addr(sSrcIP);
     UInt64 macAddr = 0;
     uint phyAddrLen = 8;
     ERROR result = SendARP(destIP, srcIP, ref macAddr, ref phyAddrLen);
     string sMac = null;
     if (result == ERROR.ERROR_SUCCESS)
         sMac = BitConverter.ToString(BitConverter.GetBytes(macAddr).Take((int)phyAddrLen).ToArray()).Replace("-", ":");
     return sMac;
 }
Exemple #14
0
 public static extern ERROR SendARP(in_addr DestIP, in_addr SrcIP, ref UInt64 MacAddr, ref uint PhyAddrLen);
 public static extern IntPtr inet_ntoa(in_addr a);
Exemple #16
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    Console.WriteLine(
                        "Command line syntax:\n\tfilter.exe index scenario \n\tindex - network interface index.\n\tscenario - sample set of filters to load.\n\tYou can use ListAdapters to determine correct index.");
                    Console.WriteLine("Available Scenarios:");
                    Console.WriteLine("1 - Redirect only IPv4 DNS packets for processing in user mode.");
                    Console.WriteLine("2 - Redirect only HTTP(TCP port 80) packets for processing in user mode. Both IPv4 and IPv6 protocols.");
                    Console.WriteLine("3 - Drop all IPv4 ICMP packets. Redirect all other packets to user mode (default behaviour).");
                    Console.WriteLine("4 - Block IPv4 access to http://www.ntkernel.com. Pass all other packets without processing in user mode.");
                    Console.WriteLine("5 - Redirect only ARP/RARP packets to user mode. Pass all others.");
                    return;
                }

                var adapterIndex = uint.Parse(args[0]) - 1;
                var scena        = uint.Parse(args[1]);

                var driverPtr = Ndisapi.OpenFilterDriver();
                if (!Ndisapi.IsDriverLoaded(driverPtr))
                {
                    Console.WriteLine("Driver not installed on this system of failed to load.");
                    return;
                }

                // Retrieve adapter list
                var adapters = new TCP_AdapterList();
                Ndisapi.GetTcpipBoundAdaptersInfo(driverPtr, ref adapters);

                // Set tunnel mode for the selected network interface
                var mode = new ADAPTER_MODE
                {
                    dwFlags        = Ndisapi.MSTCP_FLAG_SENT_TUNNEL | Ndisapi.MSTCP_FLAG_RECV_TUNNEL,
                    hAdapterHandle = adapters.m_nAdapterHandle[adapterIndex]
                };

                Ndisapi.SetAdapterMode(driverPtr, ref mode);

                // Create and set event for the adapter
                var manualResetEvent = new ManualResetEvent(false);
                Ndisapi.SetPacketEvent(driverPtr, adapters.m_nAdapterHandle[adapterIndex], manualResetEvent.SafeWaitHandle);

                var filtersTable = new STATIC_FILTER_TABLE();
                filtersTable.m_StaticFilters = new STATIC_FILTER[256];

                switch (scena)
                {
                case 1:
                    filtersTable.m_TableSize = 3;

                    //**************************************************************************************
                    // 1. Outgoing DNS requests filter: REDIRECT OUT UDP packets with destination PORT 53
                    // Common values
                    filtersTable.m_StaticFilters[0].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND;

                    // Network layer filter
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_Protocol    = 17;  //IPPROTO_UDP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_dwUnionSelector                = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_ValidFields           = Ndisapi.TCPUDP_DEST_PORT;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_StartRange = 53;     // DNS
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_EndRange   = 53;

                    //****************************************************************************************
                    // 2. Incoming DNS responses filter: REDIRECT IN UDP packets with source PORT 53
                    // Common values
                    filtersTable.m_StaticFilters[1].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[1].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[1].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[1].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE;

                    // Network layer filter
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_Protocol    = 17; //IPPROTO_UDP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_dwUnionSelector                  = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_ValidFields             = Ndisapi.TCPUDP_SRC_PORT;
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_SourcePort.m_StartRange = 53;     // DNS
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_SourcePort.m_EndRange   = 53;

                    //***************************************************************************************
                    // 3. Pass all packets (skipped by previous filters) without processing in user mode
                    // Common values
                    filtersTable.m_StaticFilters[2].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[2].m_ValidFields      = 0;
                    filtersTable.m_StaticFilters[2].m_FilterAction     = Ndisapi.FILTER_PACKET_PASS;
                    filtersTable.m_StaticFilters[2].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE | Ndisapi.PACKET_FLAG_ON_SEND;

                    break;

                case 2:
                    filtersTable.m_TableSize = 5;

                    //**************************************************************************************
                    // 1. Outgoing HTTP requests filter: REDIRECT OUT TCP packets with destination PORT 80 IPv4
                    // Common values
                    filtersTable.m_StaticFilters[0].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND;

                    // Network layer filter
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_Protocol    = 6; //IPPROTO_TCP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_dwUnionSelector                = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_ValidFields           = Ndisapi.TCPUDP_DEST_PORT;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_StartRange = 80;     // HTTP
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_EndRange   = 80;

                    //****************************************************************************************
                    // 2. Incoming HTTP responses filter: REDIRECT IN TCP packets with source PORT 80 IPv4
                    // Common values
                    filtersTable.m_StaticFilters[1].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[1].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[1].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[1].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE;

                    // Network layer filter
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_Protocol    = 6;  //IPPROTO_TCP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_dwUnionSelector                  = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_ValidFields             = Ndisapi.TCPUDP_SRC_PORT;
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_SourcePort.m_StartRange = 80;     // HTTP
                    filtersTable.m_StaticFilters[1].m_TransportFilter.m_TcpUdp.m_SourcePort.m_EndRange   = 80;

                    //****************************************************************************************
                    // 3. Outgoing HTTP requests filter: REDIRECT OUT TCP packets with destination PORT 80 IPv6
                    // Common values
                    filtersTable.m_StaticFilters[2].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[2].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[2].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[2].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND;

                    // Network layer filter
                    filtersTable.m_StaticFilters[2].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV6;
                    filtersTable.m_StaticFilters[2].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V6_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[2].m_NetworkFilter.m_IPv4.m_Protocol    = 6;  //IPPROTO_TCP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[2].m_TransportFilter.m_dwUnionSelector                = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[2].m_TransportFilter.m_TcpUdp.m_ValidFields           = Ndisapi.TCPUDP_DEST_PORT;
                    filtersTable.m_StaticFilters[2].m_TransportFilter.m_TcpUdp.m_DestPort.m_StartRange = 80;     // HTTP
                    filtersTable.m_StaticFilters[2].m_TransportFilter.m_TcpUdp.m_DestPort.m_EndRange   = 80;

                    //****************************************************************************************
                    // 4. Incoming HTTP responses filter: REDIRECT IN TCP packets with source PORT 80 IPv6
                    // Common values
                    filtersTable.m_StaticFilters[3].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[3].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[3].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[3].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE;

                    // Network layer filter
                    filtersTable.m_StaticFilters[3].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV6;
                    filtersTable.m_StaticFilters[3].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V6_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[3].m_NetworkFilter.m_IPv4.m_Protocol    = 6; // IPPROTO_TCP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[3].m_TransportFilter.m_dwUnionSelector                  = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[3].m_TransportFilter.m_TcpUdp.m_ValidFields             = Ndisapi.TCPUDP_SRC_PORT;
                    filtersTable.m_StaticFilters[3].m_TransportFilter.m_TcpUdp.m_SourcePort.m_StartRange = 80;     // HTTP
                    filtersTable.m_StaticFilters[3].m_TransportFilter.m_TcpUdp.m_SourcePort.m_EndRange   = 80;

                    //***************************************************************************************
                    // 5. Pass all packets (skipped by previous filters) without processing in user mode
                    // Common values
                    filtersTable.m_StaticFilters[4].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[4].m_ValidFields      = 0;
                    filtersTable.m_StaticFilters[4].m_FilterAction     = Ndisapi.FILTER_PACKET_PASS;
                    filtersTable.m_StaticFilters[4].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE | Ndisapi.PACKET_FLAG_ON_SEND;

                    break;

                case 3:
                    filtersTable.m_TableSize = 5;

                    //**************************************************************************************
                    // 1. Block all ICMP packets
                    // Common values
                    filtersTable.m_StaticFilters[0].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction     = Ndisapi.FILTER_PACKET_DROP;
                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND | Ndisapi.PACKET_FLAG_ON_RECEIVE;

                    // Network layer filter
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_Protocol    = 1; //IPPROTO_ICMP

                    break;

                case 4:

                    filtersTable.m_TableSize = 2;

                    //**************************************************************************************
                    // 1. Outgoing HTTP requests filter: DROP OUT TCP packets with destination IP 104.196.49.47 PORT 80 - 443 (http://www.ntkernel.com)
                    // Common values
                    filtersTable.m_StaticFilters[0].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields      = Ndisapi.NETWORK_LAYER_VALID | Ndisapi.TRANSPORT_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction     = Ndisapi.FILTER_PACKET_DROP;
                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND;

                    // Network layer filter
                    var address = new in_addr();
                    var mask    = new in_addr();

                    // IP address 104.196.49.47
                    address.s_b1 = 104;
                    address.s_b2 = 196;
                    address.s_b3 = 49;
                    address.s_b4 = 47;

                    // Network mask 255.255.255.255
                    mask.s_b1 = 255;
                    mask.s_b2 = 255;
                    mask.s_b3 = 255;
                    mask.s_b4 = 255;

                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_PROTOCOL | Ndisapi.IP_V4_FILTER_DEST_ADDRESS;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_DestAddress.m_AddressType       = Ndisapi.IP_SUBNET_V4_TYPE;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_DestAddress.m_IpSubnet.m_Ip     = address.s_addr; // IP address
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_DestAddress.m_IpSubnet.m_IpMask = mask.s_addr;    // network mask
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_Protocol = 6;                                     //IPPROTO_TCP

                    // Transport layer filter
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_dwUnionSelector                = Ndisapi.TCPUDP;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_ValidFields           = Ndisapi.TCPUDP_DEST_PORT;
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_StartRange = 80;    // HTTP
                    filtersTable.m_StaticFilters[0].m_TransportFilter.m_TcpUdp.m_DestPort.m_EndRange   = 443;   //HTTPS

                    //***************************************************************************************
                    // 2. Pass all packets (skipped by previous filters) without processing in user mode
                    // Common values
                    filtersTable.m_StaticFilters[1].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[1].m_ValidFields      = 0;
                    filtersTable.m_StaticFilters[1].m_FilterAction     = Ndisapi.FILTER_PACKET_PASS;
                    filtersTable.m_StaticFilters[1].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE | Ndisapi.PACKET_FLAG_ON_SEND;

                    break;

                case 5:

                    filtersTable.m_TableSize = 3;

                    //**************************************************************************************
                    // 1. Redirects all ARP packets to be processes by user mode application
                    // Common values
                    filtersTable.m_StaticFilters[0].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields      = Ndisapi.DATA_LINK_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND | Ndisapi.PACKET_FLAG_ON_RECEIVE;
                    filtersTable.m_StaticFilters[0].m_DataLinkFilter.m_dwUnionSelector             = Ndisapi.ETH_802_3;
                    filtersTable.m_StaticFilters[0].m_DataLinkFilter.m_Eth8023Filter.m_ValidFields = Ndisapi.ETH_802_3_PROTOCOL;
                    filtersTable.m_StaticFilters[0].m_DataLinkFilter.m_Eth8023Filter.m_Protocol    = 0x0806;  // ETH_P_ARP;


                    //**************************************************************************************
                    // 1. Redirects all RARP packets to be processes by user mode application
                    // Common values
                    filtersTable.m_StaticFilters[1].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[1].m_ValidFields      = Ndisapi.DATA_LINK_LAYER_VALID;
                    filtersTable.m_StaticFilters[1].m_FilterAction     = Ndisapi.FILTER_PACKET_REDIRECT;
                    filtersTable.m_StaticFilters[1].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND | Ndisapi.PACKET_FLAG_ON_RECEIVE;
                    filtersTable.m_StaticFilters[1].m_DataLinkFilter.m_dwUnionSelector             = Ndisapi.ETH_802_3;
                    filtersTable.m_StaticFilters[1].m_DataLinkFilter.m_Eth8023Filter.m_ValidFields = Ndisapi.ETH_802_3_PROTOCOL;
                    filtersTable.m_StaticFilters[1].m_DataLinkFilter.m_Eth8023Filter.m_Protocol    = 0x0806;  // ETH_P_ARP;


                    //***************************************************************************************
                    // 2. Pass all packets (skipped by previous filters) without processing in user mode
                    // Common values
                    filtersTable.m_StaticFilters[2].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[2].m_ValidFields      = 0;
                    filtersTable.m_StaticFilters[2].m_FilterAction     = Ndisapi.FILTER_PACKET_PASS;
                    filtersTable.m_StaticFilters[2].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE | Ndisapi.PACKET_FLAG_ON_SEND;

                    break;

                default:
                    Console.WriteLine("Unknown test scenario specified. Exiting.");
                    return;
                }

                // Load filters into driver
                Ndisapi.SetPacketFilterTable(driverPtr, ref filtersTable);

                // Allocate and initialize packet structures
                var request   = new ETH_REQUEST();
                var buffer    = new INTERMEDIATE_BUFFER();
                var bufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));

                Win32Api.ZeroMemory(bufferPtr, Marshal.SizeOf(buffer));

                request.hAdapterHandle   = adapters.m_nAdapterHandle[adapterIndex];
                request.EthPacket.Buffer = bufferPtr;

                while (true)
                {
                    manualResetEvent.WaitOne();

                    while (Ndisapi.ReadPacket(driverPtr, ref request))
                    {
                        buffer = (INTERMEDIATE_BUFFER)Marshal.PtrToStructure(bufferPtr, typeof(INTERMEDIATE_BUFFER));

                        WriteToConsole(buffer, bufferPtr);

                        if (buffer.m_dwDeviceFlags == Ndisapi.PACKET_FLAG_ON_SEND)
                        {
                            Ndisapi.SendPacketToAdapter(driverPtr, ref request);
                        }
                        else
                        {
                            Ndisapi.SendPacketToMstcp(driverPtr, ref request);
                        }
                    }

                    manualResetEvent.Reset();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemple #17
0
 public static extern uint IcmpSendEcho(IntPtr IcmpHandle, in_addr DestinationAddress, byte[] RequestData, ushort RequestSize, IntPtr pRequestOptions, ref ICMP_ECHO_REPLY ReplyBuffer, uint ReplySize, uint Timeout);
Exemple #18
0
        unsafe static void android_main(android_native_app_glue.android_app state)
        {
            // http://elfsharp.hellsgate.pl/examples.shtml
            // https://msdn.microsoft.com/en-us/library/dd554932(VS.100).aspx
            // http://mobilepearls.com/labs/native-android-api/

            // X:\jsc.internal.git\compiler\jsc.meta\jsc.meta\Library\Templates\Java\InternalPopupWebView\XWindow.cs
            // http://stackoverflow.com/questions/13249164/android-using-jni-from-nativeactivity

            android_native_app_glue.app_dummy();
            //Action<

            trace("enter TestNDKUDP");

            // listen to sockets.
            // X:\jsc.svn\examples\javascript\chrome\apps\ChromeUDPNotification\ChromeUDPNotification\Application.cs
            // http://stackoverflow.com/questions/10408980/android-ndk-sockets-network-unreachable
            // http://stackoverflow.com/questions/1593946/what-is-af-inet-and-why-do-i-need-it
            // https://msdn.microsoft.com/en-us/library/windows/hardware/ff543744(v=vs.85).aspx
            // http://stackoverflow.com/questions/6033581/using-socket-in-android-ndk
            // can we load apk from udp? and reload on update?
            var s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

            tracei("socket ", (int)s);



            // http://stackoverflow.com/questions/8330808/bind-with-so-reuseaddr-fails

            // https://books.google.ee/books?id=ptSC4LpwGA0C&pg=PA610&lpg=PA610&dq=SO_REUSEADDR+-1+errno+22&source=bl&ots=Ks2AUohlOn&sig=5ytq_BKAlj1sbZVNSGaaqPhM4lg&hl=en&sa=X&ei=glLgVM_SOsjCOa2RgfgN&ved=0CCQQ6AEwATgK#v=onepage&q=SO_REUSEADDR%20-1%20errno%2022&f=false



            //byte hopLimit = 2;
            //  The default value is one for all IP multicast datagrams.
            //setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &hopLimit, sizeof(byte));

            // http://www.tldp.org/HOWTO/Multicast-HOWTO-6.html
            var localAddr = new in_addr();

            localAddr.s_addr = INADDR_ANY;


            // For multicast sending use an IP_MULTICAST_IF flag with the setsockopt() call. This specifies the interface to be used.
            {
                var status = s.setsockopt(IPPROTO_IP, IP_MULTICAST_IF, &localAddr, sizeof(in_addr));

                // anonymous types like linq expressions?
                tracei("setsockopt IP_MULTICAST_IF: ", status);
            }

            // http://www.phonesdevelopers.com/1817807/

            // http://www.infres.enst.fr/~dax/polys/multicast/api_en.html

            //ip_mreq mreq;
            var mreq = new ip_mreq();

            // "239.1.2.3"
            // ip_mreq3->imr_multiaddr.s_addr = inet_addr((char*)"239.1.2.3");
            mreq.imr_multiaddr.s_addr = "239.1.2.3".inet_addr();

            {
                var status = s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(ip_mreq));
                tracei("setsockopt IP_ADD_MEMBERSHIP: ", status);
            }


            //var bAllowMultiple = true;
            //{
            //    var status = s.setsockopt(SOL_SOCKET, SO_REUSEADDR, &bAllowMultiple, sizeof(bool));

            //    // anonymous types like linq expressions?
            //    tracei("setsockopt SO_REUSEADDR: ", status);
            //}

            // Create the local endpoint
            sockaddr_in localEndPoint;
            ushort      gport = 40804;

            localEndPoint.sin_family      = AF_INET;
            localEndPoint.sin_addr.s_addr = INADDR_ANY.htonl();
            localEndPoint.sin_port        = gport.htons();


            // Bind the socket to the port
            {
                int bindret = s.bind((sockaddr *)&localEndPoint, sizeof(sockaddr_in));

                tracei("bind: ", bindret);
            }

            var ok = true;


            while (ok)
            {
                var buff = stackalloc byte[0xfff];

                sockaddr_in sender;
                var         sizeof_sender = sizeof(sockaddr_in);

                trace("before recvfrom");

                // http://pubs.opengroup.org/onlinepubs/009695399/functions/recvfrom.html
                // Upon successful completion, recvfrom() shall return the length of the message in bytes.
                var recvfromret = s.recvfrom(buff, 0xfff, 0, (sockaddr *)&sender, &sizeof_sender);

                //I/xNativeActivity(24024): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:167 recvfrom:  116 errno: 22 Invalid argument
                //I/xNativeActivity(24024): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:168 SenderAddrSize:  16 errno: 22 Invalid argument
                tracei("recvfrom: ", recvfromret);
                //tracei("sockaddr_in: ", sizeof_sender);

                buff[recvfromret] = 0;


                //trace(sender.sin_addr.inet_ntoa());
                trace(buff);
            }

            // do we have XElement in native mode yet?



            // http://pubs.opengroup.org/onlinepubs/7908799/xns/arpainet.h.html
            // http://stackoverflow.com/questions/15569012/android-udp-client-not-able-to-receive-data-on-non-rooted-phone
            // http://www.phonesdevelopers.com/1817807/

            // !!1
            // https://www.mail-archive.com/[email protected]/msg115225.html

            // http://www.gta.ufrj.br/ensino/eel878/sockets/inet_ntoaman.html

            // could jsc web apps be turned into ndk servers?


            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:74 enter TestNDKUDP
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:85 socket  28 errno: 0
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:112 setsockopt SO_REUSEADDR:  -1 errno: 22
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:129 setsockopt IP_MULTICAST_IF:  -1 errno: 99
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:143 setsockopt IP_ADD_MEMBERSHIP:  -1 errno: 19
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:159 bind:  -1 errno: 98
            //I/xNativeActivity(23301): X:\jsc.svn\examples\c\android\Test\TestNDKUDP\TestNDKUDP\xNativeActivity.cs:168 before recvfrom
            //I/CwMcuSensor(  461): CwMcuSensor::flush: fd = 194, sensors_id = 0, path = /sys/class/htc_sensorhub/sensor_hub/flush, err = 0
        }
Exemple #19
0
 public Pro_Head(string IP, int Port, int Func)
 {
     func     = (byte)Func;
     addr     = new in_addr(IP.Split('.'));
     sin_port = (short)Port;
 }