Пример #1
0
        /**
         * Return the size of the file and the name with the out parameters name
         * */
        private static long getNameAndSizeFromHit(G2PacketH hit, out string name)
        {
            G2Packet SZ, DN;

            DN   = hit.getFirstChildPacket(G2PacketType.DN);
            SZ   = hit.getFirstChildPacket(G2PacketType.SZ);
            name = "";
            if (DN == null)             // no name
            {
                return(0);
            }
            // object size packet is not present
            // so we parse DN with a 32 bit integer before
            if (SZ == null)
            {
                byte[] bytes = ((G2PacketDN)DN).bytes;
                byte[] bName = bytes.SubArray(4, bytes.Length - 4);
                long   size  = BinaryUtils.getVariableIntLE(bytes, 4);
                name = BinaryUtils.getStringFromBytes(bName, (int)(bytes.Length - 4));
                return(size);
            }
            else
            {
                name = ((G2PacketDN)DN).Str;
                return(((G2PacketSZ)SZ).Size);
            }
        }
Пример #2
0
        private static bool isPartialFile(G2PacketH hit)
        {
            G2Packet part = hit.getFirstChildPacket(G2PacketType.PART);

            if (part != null)
            {
                return(true);
            }
            return(false);
        }
Пример #3
0
        /**
         * Return creation time in DateTime format if present
         * otherwise return current time
         * */
        private static DateTime getCreationTimeFromHit(G2PacketH hit)
        {
            G2Packet pack = hit.getFirstChildPacket(G2PacketType.CT);

            if (pack == null)
            {
                return(DateTime.Now);
            }
            G2PacketCT ct = pack as G2PacketCT;

            return(BinaryUtils.UnixTimeStampToDateTime(ct.Timestamp));
        }
Пример #4
0
        ParseHit(G2PacketH hit, FileLocationFound location)
        {
            // for now we dont take into accounts host of partial files
            if (isPartialFile(hit) && !Settings.ACCEPT_PARTIAL_FILE)
            {
                return(null);
            }

            string FileName = "";
            long   size     = getNameAndSizeFromHit(hit, out FileName);

            if (size == 0)
            {
                return(null);                // no size, no name or bug
            }
            List <URN> Hashes     = getHashesFromHit(hit);
            TypeHash   type       = TypeHash.Md4EDonkey;
            URN        SingleHash = Hashes.Find(FindED2KHash);

            if (SingleHash == null)
            {
                SingleHash = Hashes.Find(FindSHA1Hash);
                type       = TypeHash.Sha1;
            }
            // if there is not the hashes we want , we dont want this packet
            // for now. ActionInnoncence DLL needs to take care of more hashes
            if (SingleHash == null)
            {
                return(null);
            }
            // gives a text representation of the hash
            string strHash = BitConverter.ToString(SingleHash.Digest).Replace("-", string.Empty).ToLower();


            string   link = getDownloadLink(hit);
            Metadata meta = Metadata.ParseMetadata(hit.getStringFromChildType(G2PacketType.MD));


            G2File f = new G2File(FileName, strHash, SingleHash.Digest, type, size,
                                  FileType.Unknow, meta.Codec, location, meta.Length, Settings.PROTOCOL_NAME, link);

            f.Hashes       = Hashes;
            f.CreationTime = getCreationTimeFromHit(hit);
            f.Metadata     = meta;
            f.UserComment  = hit.getStringFromChildType(G2PacketType.COM);
            f.PreviewUrl   = hit.getStringFromChildType(G2PacketType.PVU);
            return(f);
        }
Пример #5
0
        /**
         * Get a URL link if present
         * IF not present , it has to beretrieved by HTTP request
         * */
        private static string getDownloadLink(G2PacketH hit)
        {
            G2Packet pack = hit.getFirstChildPacket(G2PacketType.URL);

            if (pack == null)
            {
                return("");
            }
            G2PacketURL url = pack as G2PacketURL;

            if (url == null)
            {
                return("");
            }

            return(url.Str);
        }
Пример #6
0
        /**
         * Parse all hashes contained for this file
         * */
        private static List <URN> getHashesFromHit(G2PacketH hit)
        {
            List <URN> hashes = new List <URN> ();

            foreach (G2Packet child in hit.children)
            {
                if (child.type != G2PacketType.URN)
                {
                    continue;
                }
                G2PacketURN UrnPacket = child as G2PacketURN;
                if (UrnPacket == null)
                {
                    continue;
                }
                hashes.Add(UrnPacket.urn);
            }
            return(hashes);
        }
Пример #7
0
        /**
         * Get the results from browsing and put it into the sharedFileList folder of the peer.
         * Should maybe change the key to a Peer , but since implementation is unknown
         * no assurance that hashcode will be the same ...
         * */
        public void BrowsingTerminated(Peer peer, SearchResult Results, List <G2PacketQH2> resultPackets)
        {
            if (resultPackets.Count == 0)
            {
                RegroupResults(Results); // if no results from browsing directly sends results
                return;
            }
            Results.PeerCollection.Add(peer);
            Peer p = Results.PeerCollection.Find(peer);


            int fileCount = 0;

            // add results !!
            foreach (G2PacketQH2 qh2 in resultPackets)
            {
                foreach (G2Packet child in qh2.children)
                {
                    if (!child.type.Equals(G2PacketType.H))
                    {
                        continue;
                    }
                    G2PacketH hit  = child as G2PacketH;
                    G2File    file = G2File.ParseHit(hit, FileLocationFound.SharedLocalComputer);
                    if (file == null)
                    {
                        continue;
                    }
                    p.SharedLocalfilesList.Add(file);
                    file.PeerDiffusedFiles.Add(p);
                    SharedTotalFiles++;
                    fileCount++;
                }
            }

            G2Log.Write("SearchResults : New Browsing Result from " + p.Ip + ":" + p.Port + " ==> " + p.Files.Count + " files && " + p.SharedLocalfilesList.Count + " shared files ...");

            RegroupResults(Results);
        }
Пример #8
0
        private void AppendResult(G2PacketQH2 res)
        {
            G2Peer g2peer = G2Peer.ParseG2Peer(res);

            if (g2peer == null)
            {
                return;
            }

            /** Create a new search result from the hub ip  and the search transaction */
            SearchResult Results = new SearchResult(new System.Net.IPEndPoint(res.RemotePeer.Address, res.RemotePeer.Port), Transaction);

            bool          PeerBrowsable = false;
            bool          isFirewalled  = false;
            int           fileCount     = 0;
            List <G2File> files         = new List <G2File>();

            // add the files to the collections.
            foreach (G2Packet child in res.children)
            {
                if (child.type.Equals(G2PacketType.BH))
                {
                    PeerBrowsable = true;
                    continue;
                }
                if (child.type.Equals(G2PacketType.FW))
                {
                    isFirewalled = true;
                    continue;
                }
                if (!child.type.Equals(G2PacketType.H))
                {
                    continue;
                }


                G2PacketH hit  = child as G2PacketH;
                G2File    file = G2File.ParseHit(hit, FileLocationFound.ServerIndex);
                if (file == null)
                {
                    continue;
                }
                files.Add(file);

                fileCount += 1;
            }
            if (fileCount == 0)
            {
                return;                 // may have some hosts having only partial file,so no file for the application ...
            }
            Results.PeerCollection.Add(g2peer);
            Peer p = Results.PeerCollection.Find(g2peer); // to get the right object reference may not be needed .. ?

            if (p.Ip.StartsWith("192.168"))
            {
                return;
            }
            foreach (G2File file in files)
            {
                p.Files.Add(file);
                file.PeerDiffusedFiles.Add(p); // ?? necessary or wrong ?
                Results.FileCollection.Add(file);
            }
            files.Clear();
            files = null;

            TotalFiles += fileCount;


            G2Log.Write("SearchResults : New result from " + res.RemotePeer.ToString() + " for " + SearchedWord + " ==> " + fileCount + " files");

            // try to see if we already have launched an browsing research on this peer or not
            bool notAlreadySearched = !PeersBrowsed.Contains(p);
            // make sure that the browsing will not increase the waiting time of the application for the result
            bool notTooLate = StartSearchTime.AddMilliseconds(Settings.WAIT_TIME_BROWSING_MS)
                              < StartSearchTime.AddMilliseconds(Settings.SEARCH_TIME_OUT_MS + 100); // +100 to make sure browsing has time to regroup all
            bool notConnectedHub = res.RemotePeer.Address.ToString() != p.Ip;

            // verify that we dont search the hub, taht the peer is browsable and NOT firewalled
            // TODO can send a PUSH packet to the hub that will bring the peer to establish a connection to us
            if (notConnectedHub && PeerBrowsable && !isFirewalled && notAlreadySearched && notTooLate)
            {
                PeersBrowsed.Add(p);
                G2BrowseSearch browser = new G2BrowseSearch(p, Results);
                browser.EndSearch += new BrowseSearchEnd(BrowsingTerminated);
                browser.StartBrowsing();
            }
            else // sends results to application
            {
                RegroupResults(Results);
            }
        }