Ejemplo n.º 1
0
 public SocketClientConnector(Socket socket)
 {
     if (this.tcpOptions == null)
     {
         this.tcpOptions = new TCPOption();
     }
     this.Socket = socket;
     this.Socket.ReceiveTimeout = tcpOptions.ReceiveTimeOut;
 }
Ejemplo n.º 2
0
 public TCPClientConnector(Socket socket)
 {
     if (this.tcpOptions == null)
     {
         this.tcpOptions = new TCPOption();
     }
     this.tcpClient          = new TcpClient(socket.AddressFamily);
     this.tcpClient.Client   = socket;
     this.stream             = this.tcpClient.GetStream();
     this.stream.ReadTimeout = tcpOptions.ReceiveTimeOut;
 }
Ejemplo n.º 3
0
        public TCPH(FileStream fs, PcapFile pfh, Packet pkt, uint i)
        {
            uint temp;
            uint optionbytes;
            H containingheader = pkt.phlist[pkt.phlist.Count()-1];
            TCPOption thisoption;
            List<TCPOption> options = new List<TCPOption>();

            // if containing protocol does not know it's payload length, return with no result - need to add code to handle this case
            // TCP needs to be told the length of its payload by the outer protocol
            // it may not be all of the remaining bytes in the packet, at least due to cases where the ethernet frame includes padding bytes that are captured by wireshark/pcap
            if (containingheader.payloadlen == -1) { MessageBox.Show("Trying to parse TCP header where containing protocol did not set payloadlen - need to add code to handle this"); return; }

            // if not enough data remaining, return without reading anything
            // note that we have not added the header to the packet's header list yet, so we are not leaving an invalid header in the packet
            if ((pkt.Len - i) < 0x14) return;

            // read in the fixed header data
            SrcPort = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            DestPort = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            SeqNo = (uint)pkt.PData[i++]  * 0x1000000 + (uint)pkt.PData[i++]  * 0x10000 + (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            AckNo = (uint)pkt.PData[i++]  * 0x1000000 + (uint)pkt.PData[i++]  * 0x10000 + (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            temp = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            DataOffset = temp / 0x1000;
            Flags = temp & 0xfff;
            WindowSize = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            Checksum = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
            UrgentPtr = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;

            optionbytes = (DataOffset * 4) - 0x14;     // number of bytes of options plus any padding to get TCP header to 32 bit boundary
            if ((pkt.Len - i) < (optionbytes + 0x14)) return;     // if not enough bytes to fill options fields, return without adding header to packet

            for (uint ob = 0; ob < optionbytes; )
            {
                thisoption = new TCPOption();
                thisoption.Kind = (uint)pkt.PData[i++] ; i++;
                switch (thisoption.Kind)
                {
                    case 0:         // end of options list
                        thisoption.Length = 1;
                        i += optionbytes - i;    // skip any remaining padding bytes
                        ob = optionbytes;
                        break;
                    case 1:         // NOP, just eat the byte
                        thisoption.Length = 1;
                        break;
                    case 2:         // maximum segment size, len is 4, segment size is 32 bits
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        thisoption.Data = new uint[1]; thisoption.Data[0] = (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
                        ob += 3;
                        break;
                    case 3:         // window scale
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        thisoption.Data = new uint[1]; thisoption.Data[0] = (uint)pkt.PData[i++] ;
                        ob += 2;
                        break;
                    case 4:         // selective acknowledgement permitted
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        ob++;
                        thisoption.Data = null;
                        break;
                    case 5:         // selective acknowledgement
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        if (thisoption.Length > 0x22) MessageBox.Show("TCP packet with bad Selective Acknowlegement option");
                        thisoption.Data = new uint[(thisoption.Length - 2) / 4];
                        for (int ii = 0; ii < (thisoption.Length - 2) / 4; ii++) thisoption.Data[ii] = (uint)pkt.PData[i++]  * 0x1000000 + (uint)pkt.PData[i++]  * 0x10000 + (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
                        ob += thisoption.Length - 1;
                        break;
                    case 8:         // timestamp and echo of previous timestamp
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        thisoption.Data = new uint[2];
                        thisoption.Data[0] = (uint)pkt.PData[i++]  * 0x1000000 + (uint)pkt.PData[i++]  * 0x10000 + (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
                        thisoption.Data[1] = (uint)pkt.PData[i++]  * 0x1000000 + (uint)pkt.PData[i++]  * 0x10000 + (uint)pkt.PData[i++]  * 0x100 + (uint)pkt.PData[i++] ;
                        ob += 9;
                        break;
                    case 0x0e:         // TCP alternate checksum request
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        thisoption.Data = new uint[1];
                        thisoption.Data[0] = (uint)pkt.PData[i++] ;
                        ob += 2;
                        break;
                    case 0x0f:         // TCP alternate checksum data
                        thisoption.Length = (uint)pkt.PData[i++] ;
                        thisoption.Data = new uint[thisoption.Length];
                        for (int ii = 0; ii < thisoption.Length; ii++) thisoption.Data[ii] = (uint)pkt.PData[i++] ;   // just naively read each byte into a uint - this option is considered "historic" and probably will never be encountered
                        ob += thisoption.Length - 1;
                        break;
                    default:
                        MessageBox.Show("Unknown TCP header option type");
                        break;
                }
                options.Add(thisoption);
            }
            Options = new TCPOption[options.Count];
            // copy options into TCPH.Options
            for (int ii = 0; ii < options.Count; ii++) Options[ii] = options[ii];

            // set generic header properties
            headerprot = Protocols.TCP;
            payloadindex = i;
            payloadlen = containingheader.payloadlen - 0x14 - (int)optionbytes;

            // set packet-level convenience properties
            pkt.tcphdr = this;
            pkt.Prots |= Protocols.TCP;
            pkt.SrcPort = SrcPort;
            pkt.DestPort = DestPort;

            // add header to packet's header list
            pkt.phlist.Add(this);

            // determine which header constructor to call next, if any, and call it
            switch (1)
            {
                case 0x01:
                    break;

                default:
                    break;
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public SocketClientConnector(TCPOption tcpOptions, Socket socket) : this(socket)
 {
     this.tcpOptions = tcpOptions;
 }
Ejemplo n.º 6
0
 public SocketClientConnector()
 {
     this.tcpOptions = new TCPOption();
 }
Ejemplo n.º 7
0
 public TCPClientConnector()
 {
     this.tcpOptions = new TCPOption();
     this.tcpClient  = new TcpClient();
 }