Пример #1
0
        public override bool Send(IPPayload payload)
        {
            DHCP dhcp = new DHCP(payload.GetPayload());

            hType  = dhcp.HardwareType;
            hLen   = dhcp.HardwareAddressLength;
            xID    = dhcp.TransactionID;
            cMac   = dhcp.ClientHardwareAddress;
            cookie = dhcp.MagicCookie;

            DHCPopClientID clientID = null;

            byte msg = 0;

            byte[] reqList = null;

            uint leaseTime = 86400;

            for (int i = 0; i < dhcp.Options.Count; i++)
            {
                switch (dhcp.Options[i].Code)
                {
                case 0:
                    //Error.WriteLine("Got NOP");
                    continue;

                case 1:
                    Log_Info("Got SubnetMask?");
                    if (Utils.memcmp(NetMask, 0, ((DHCPopSubnet)dhcp.Options[i]).SubnetMask, 0, 4) == false)
                    {
                        throw new Exception("SubnetMask missmatch");
                    }
                    break;

                case 3:
                    Log_Info("Got Router?");
                    if (((DHCPopRouter)dhcp.Options[i]).RouterIPs.Count != 1)
                    {
                        throw new Exception("RouterIPs count missmatch");
                    }
                    if (Utils.memcmp(Gateway, 0, ((DHCPopRouter)dhcp.Options[i]).RouterIPs[0], 0, 4) == false)
                    {
                        throw new Exception("RouterIPs missmatch");
                    }
                    break;

                case 6:
                    Log_Info("Got DNS?");
                    if ((((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 0 & DNS1 == null) ||
                        (((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 1 & DNS2 == null) ||
                        (((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 2 & DNS2 != null))
                    {
                        throw new Exception("DNS count missmatch");
                    }
                    if ((DNS1 != null && Utils.memcmp(DNS1, 0, ((DHCPopDNS)dhcp.Options[i]).DNSServers[0], 0, 4) == false) ||
                        (DNS2 != null && Utils.memcmp(DNS2, 0, ((DHCPopDNS)dhcp.Options[i]).DNSServers[1], 0, 4) == false))
                    {
                        throw new Exception("DNS missmatch");
                    }
                    break;

                case 12:
                    Log_Info("Got HostName");
                    //TODO use name?
                    break;

                case 50:
                    Log_Info("Got Request IP");
                    if (Utils.memcmp(PS2IP, 0, ((DHCPopREQIP)dhcp.Options[i]).IPaddress, 0, 4) == false)
                    {
                        throw new Exception("ReqIP missmatch");
                    }
                    break;

                case 51:
                    Log_Info("Got Requested Lease Time");
                    leaseTime = ((DHCPopIPLT)(dhcp.Options[i])).IPLeaseTime;
                    break;

                case 53:
                    msg = ((DHCPopMSG)(dhcp.Options[i])).Message;
                    Log_Info("Got MSG ID = " + msg);
                    break;

                case 54:
                    Log_Info("Got Server IP");
                    if (Utils.memcmp(DefaultDHCPConfig.DHCP_IP, 0, ((DHCPopSERVIP)dhcp.Options[i]).ServerIP, 0, 4) == false)
                    {
                        throw new Exception("ServIP missmatch");
                    }
                    break;

                case 55:
                    reqList = ((DHCPopREQLIST)(dhcp.Options[i])).RequestList;
                    Log_Verb("Got Request List of length " + reqList.Length);
                    for (int rID = 0; rID < reqList.Length; rID++)
                    {
                        Log_Verb("Requested : " + reqList[rID]);
                    }
                    break;

                case 56:
                    Log_Verb("Got String Message of " + ((DHCPopMSGStr)dhcp.Options[i]).Message);
                    break;

                case 57:
                    maxMs = ((DHCPopMMSGS)(dhcp.Options[i])).MaxMessageSize;
                    Log_Verb("Got Max Message Size of " + maxMs);
                    break;

                case 60:
                    Log_Verb("Got Class Id of " + ((DHCPopClassID)dhcp.Options[i]).ClassID);
                    break;

                case 61:
                    Log_Verb("Got Client ID");
                    clientID = (DHCPopClientID)dhcp.Options[i];
                    //Ignore
                    break;

                case 255:
                    Log_Verb("Got END");
                    break;

                default:
                    Log_Error("Got Unknown Option " + dhcp.Options[i].Code);
                    throw new Exception("Got Unknown Option " + dhcp.Options[i].Code);
                    //break;
                }
            }
            DHCP retPay = new DHCP
            {
                OP                    = 2,
                HardwareType          = hType,
                HardwareAddressLength = hLen,
                TransactionID         = xID,

                YourIP   = PS2IP,//IPaddress.GetAddressBytes();
                ServerIP = DefaultDHCPConfig.DHCP_IP,

                ClientHardwareAddress = cMac,
                MagicCookie           = cookie
            };

            if (msg == 1 || msg == 3) //Fill out Requests
            {
                if (msg == 1)
                {
                    retPay.Options.Add(new DHCPopMSG(2));
                }
                if (msg == 3)
                {
                    retPay.Options.Add(new DHCPopMSG(5));
                }

                if (reqList != null)
                {
                    for (int i = 0; i < reqList.Length; i++)
                    {
                        switch (reqList[i])
                        {
                        case 1:
                            Log_Verb("Sending Subnet");
                            //retPay.Options.Add(new DHCPopSubnet(NetMask.GetAddressBytes()));
                            retPay.Options.Add(new DHCPopSubnet(NetMask));
                            break;

                        case 3:
                            Log_Verb("Sending Router");
                            if (Gateway != null)
                            {
                                retPay.Options.Add(new DHCPopRouter(new List <byte[]>()
                                {
                                    Gateway
                                }));
                            }
                            break;

                        case 6:
                            Log_Verb("Sending DNS");
                            if (DNS1 != null)
                            {
                                if (DNS2 != null)
                                {
                                    retPay.Options.Add(new DHCPopDNS(new List <byte[]>()
                                    {
                                        DNS1, DNS2
                                    }));
                                }
                                else
                                {
                                    retPay.Options.Add(new DHCPopDNS(new List <byte[]>()
                                    {
                                        DNS1
                                    }));
                                }
                            }
                            break;

                        case 15:
                            Log_Verb("Sending Domain Name");
                            //retPay.Options.Add(new DHCPopDNSNAME(Dns.GetHostName()));
                            retPay.Options.Add(new DHCPopDNSNAME("PCSX2-CLRDEV9"));
                            break;

                        case 28:
                            Log_Verb("Sending Broadcast Addr");
                            retPay.Options.Add(new DHCPopBCIP(Broadcast));
                            break;

                        case 50:
                            Log_Verb("Sending PS2 IP Addr");
                            retPay.Options.Add(new DHCPopREQIP(PS2IP));
                            break;

                        case 53:
                            Log_Verb("Sending MSG (Already Added)");
                            break;

                        case 54:
                            Log_Verb("Sending Server Identifier (Already Added)");
                            break;

                        case 77:
                            //Isn't this surpossed to be sent by the client?
                            Log_Error("Request for User-Class, Ignoring");
                            break;

                        default:
                            Log_Error("Got Unknown Req " + reqList[i]);
                            throw new Exception("Got Unknown Req " + reqList[i]);
                        }
                    }
                    retPay.Options.Add(new DHCPopIPLT(leaseTime));
                }
            }

            if (msg == 7)
            {
                Log_Info("PS2 has Disconnected");
                return(true);
            }

            retPay.Options.Add(new DHCPopSERVIP(DefaultDHCPConfig.DHCP_IP));
            retPay.Options.Add(new DHCPopEND());

            byte[] udpPayload = retPay.GetBytes((UInt16)(maxMs - (8 + 20)));
            UDP    retudp     = new UDP(udpPayload)
            {
                SourcePort      = 67,
                DestinationPort = 68
            };

            recvBuff.Enqueue(retudp);
            return(true);
        }
Пример #2
0
        public override bool send(IPPayload payload)
        {
            #region "Get Network Info"
            //Get comp IP
            IPAddress IPaddress = null;

            //IPAddress NetMask = null;
            //IPAddress GatewayIP = null;
            List <IPAddress> DNS_IP = new List <IPAddress>();
            //IPAddress BroadCastIP = null;
            NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();

            bool FoundAdapter = false;

            foreach (NetworkInterface adapter in Interfaces)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                {
                    continue;
                }
                if (adapter.OperationalStatus == OperationalStatus.Up)
                {
                    UnicastIPAddressInformationCollection IPInfo = adapter.GetIPProperties().UnicastAddresses;
                    IPInterfaceProperties properties             = adapter.GetIPProperties();
                    //GatewayIPAddressInformationCollection myGateways = properties.GatewayAddresses;
                    foreach (UnicastIPAddressInformation IPAddressInfo in IPInfo)
                    {
                        if (IPAddressInfo.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred &
                            IPAddressInfo.AddressPreferredLifetime != UInt32.MaxValue &
                            IPAddressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            Console.Error.WriteLine("Matched Adapter");
                            IPaddress    = IPAddressInfo.Address;;
                            FoundAdapter = true;
                            break;
                        }
                    }
                    //foreach (GatewayIPAddressInformation Gateway in myGateways) //allow more than one gateway?
                    //{
                    //    if (FoundAdapter == true)
                    //    {
                    //        GatewayIP = Gateway.Address;
                    //        GATEWAY_IP = GatewayIP.GetAddressBytes();
                    //        break;
                    //    }
                    //}
                    foreach (IPAddress DNSaddress in properties.DnsAddresses) //allow more than one DNS address?
                    {
                        if (FoundAdapter == true)
                        {
                            if (!(DNSaddress.AddressFamily == AddressFamily.InterNetworkV6))
                            {
                                DNS_IP.Add(DNSaddress);
                            }
                        }
                    }
                }
                if (FoundAdapter == true)
                {
                    Console.Error.WriteLine(adapter.Name);
                    Console.Error.WriteLine(adapter.Description);
                    Console.Error.WriteLine("IP Address :" + IPaddress.ToString());
                    Console.Error.WriteLine("Domain Name :" + Dns.GetHostName());
                    //Console.Error.WriteLine("Subnet Mask :" + NetMask.ToString());
                    //Console.Error.WriteLine("Gateway IP :" + GatewayIP.ToString());
                    Console.Error.WriteLine("DNS 1 : " + DNS_IP[0].ToString());
                    break;
                }
            }
            #endregion

            DHCP dhcp = new DHCP(payload.GetPayload());
            HType  = dhcp.HardwareType;
            Hlen   = dhcp.HardwareAddressLength;
            xID    = dhcp.TransactionID;
            cMac   = dhcp.ClientHardwareAddress;
            cookie = dhcp.MagicCookie;

            TCPOption clientID = null;

            byte   msg     = 0;
            byte[] reqList = null;

            for (int i = 0; i < dhcp.Options.Count; i++)
            {
                switch (dhcp.Options[i].Code)
                {
                case 0:
                    //Console.Error.WriteLine("Got NOP");
                    continue;

                case 50:
                    Console.Error.WriteLine("Got Request IP");
                    if (Utils.memcmp(PS2_IP, 0, ((DHCPopREQIP)dhcp.Options[i]).IPaddress, 0, 4) == false)
                    {
                        throw new Exception("ReqIP missmatch");
                    }
                    break;

                case 53:
                    msg = ((DHCPopMSG)(dhcp.Options[i])).Message;
                    Console.Error.WriteLine("Got MSG ID = " + msg);
                    break;

                case 54:
                    Console.Error.WriteLine("Got Server IP");
                    if (Utils.memcmp(DHCP_IP, 0, ((DHCPopSERVIP)dhcp.Options[i]).IPaddress, 0, 4) == false)
                    {
                        throw new Exception("ServIP missmatch");
                    }
                    break;

                case 55:
                    reqList = ((DHCPopREQLIST)(dhcp.Options[i])).RequestList;
                    Console.Error.WriteLine("Got Request List of length " + reqList.Length);
                    for (int rID = 0; rID < reqList.Length; rID++)
                    {
                        Console.Error.WriteLine("Requested : " + reqList[rID]);
                    }
                    break;

                case 56:
                    Console.Error.WriteLine("Got String Message");
                    break;

                case 57:
                    maMs = ((DHCPopMMSGS)(dhcp.Options[i])).MaxMessageSize;
                    Console.Error.WriteLine("Got Max Message Size of " + maMs);
                    break;

                case 61:
                    Console.Error.WriteLine("Got Client ID");
                    clientID = dhcp.Options[i];
                    //Ignore
                    break;

                case 255:
                    Console.Error.WriteLine("Got END");
                    break;

                default:
                    Console.Error.WriteLine("Got Unknown Option " + dhcp.Options[i].Code);
                    throw new Exception();
                    //break;
                }
            }
            DHCP retPay = new DHCP();
            retPay.OP                    = 2;
            retPay.HardwareType          = HType;
            retPay.HardwareAddressLength = Hlen;
            retPay.TransactionID         = xID;

            retPay.YourIP   = PS2_IP;//IPaddress.GetAddressBytes();
            retPay.ServerIP = DHCP_IP;

            retPay.ClientHardwareAddress = cMac;
            retPay.MagicCookie           = cookie;

            if (msg == 1 || msg == 3) //Fill out Requests
            {
                if (msg == 1)
                {
                    retPay.Options.Add(new DHCPopMSG(2));
                }
                if (msg == 3)
                {
                    retPay.Options.Add(new DHCPopMSG(5));
                }


                for (int i = 0; i < reqList.Length; i++)
                {
                    switch (reqList[i])
                    {
                    case 1:
                        Console.Error.WriteLine("Sending Subnet");
                        //retPay.Options.Add(new DHCPopSubnet(NetMask.GetAddressBytes()));
                        retPay.Options.Add(new DHCPopSubnet(NETMASK));
                        break;

                    case 3:
                        Console.Error.WriteLine("Sending Router");
                        retPay.Options.Add(new DHCPopRouter(GATEWAY_IP));
                        break;

                    case 6:
                        Console.Error.WriteLine("Sending DNS");     //TODO support more than 1
                        retPay.Options.Add(new DHCPopDNS(DNS_IP[0]));
                        //retPay.Options.Add(new DHCPopDNS(IPAddress.Parse("1.1.1.1")));
                        break;

                    case 15:
                        Console.Error.WriteLine("Sending Domain Name");
                        //retPay.Options.Add(new DHCPopDNSNAME(Dns.GetHostName()));
                        retPay.Options.Add(new DHCPopDNSNAME("PCSX2-CLRDEV9"));
                        break;

                    case 28:
                        Console.Error.WriteLine("Sending Broadcast Addr");
                        for (int i2 = 0; i2 < 4; i2++)
                        {
                            BROADCAST[i2] = (byte)((PS2_IP[i2]) | (~NETMASK[i2]));
                        }
                        retPay.Options.Add(new DHCPopBCIP(BROADCAST));
                        break;

                    default:
                        Console.Error.WriteLine("Got Unknown Option " + reqList[i]);
                        throw new Exception();
                    }
                }
                retPay.Options.Add(new DHCPopIPLT(86400));
            }

            if (msg == 7)
            {
                Console.Error.WriteLine("PS2 has Disconnected");
                return(true);
            }

            retPay.Options.Add(new DHCPopSERVIP(DHCP_IP));
            retPay.Options.Add(new DHCPopEND());

            byte[] udpPayload = retPay.GetBytes((UInt16)(maMs - (8 + 20)));
            UDP    retudp     = new UDP(udpPayload);
            retudp.SourcePort      = 67;
            retudp.DestinationPort = 68;
            recvbuff.Add(retudp);
            return(true);
        }