Exemple #1
0
 /**
  * Call this when you have a result packet belonging to this search result object
  * */
 public void PushResultPacket(G2PacketQH2 pack)
 {
     lock (LockBuffer)
     {
         Buffer.PushPacketToReceive(pack);
         Monitor.Pulse(LockBuffer);
     }
 }
Exemple #2
0
        private string getVendorCodeFromQueryHit(G2PacketQH2 qh2)
        {
            G2Packet vendorPacket = qh2.getFirstChildPacket(G2PacketType.V);

            if (vendorPacket == null)
            {
                return("");
            }
            return(((G2PacketV)vendorPacket).Str);
        }
Exemple #3
0
        private string getDNSNameFromQueryHit(G2PacketQH2 qh2)
        {
            G2Packet dns = qh2.getFirstChildPacket(G2PacketType.DNS);

            if (dns == null)
            {
                return("");
            }
            return(((G2PacketDNS)dns).Str);
        }
Exemple #4
0
        /** Search address of the servent
         *  may be null
         **/
        private static NodeAddress getAddressFromQueryHit(G2PacketQH2 qh2)
        {
            G2Packet p = qh2.getFirstChildPacket(G2PacketType.NA);

            if (p == null)
            {
                return(null);
            }
            G2PacketNA na = p as G2PacketNA;

            return(na.node);
        }
Exemple #5
0
 private void SearchResultThread()
 {
     while (ContinueRegrouping)
     {
         // take a packet from the buffer
         G2PacketQH2 resultPack = PollResultPacket();
         if (resultPack == null)
         {
             continue;
         }
         // append it to the global result of this search
         AppendResult(resultPack);
     }
     G2Log.Write("SearchResult (" + SearchedWord + ") STOPPING .....");
 }
Exemple #6
0
        /*		*
         * Return the nickname if there is one
         * otherwise return empty string
         * */
        private static String getNickFromQueryHit(G2PacketQH2 qh2)
        {
            G2Packet upro = qh2.getFirstChildPacket(G2PacketType.UPRO);

            if (upro == null)
            {
                return("");
            }
            G2Packet nick = upro.getFirstChildPacket(G2PacketType.NICK);

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

            return(((G2PacketNICK)nick).Str);
        }
Exemple #7
0
        /** G2Peer will parse the packet itself to get the relevant info
         * **/
        public static G2Peer ParseG2Peer(G2PacketQH2 resultPacket)
        {
            NodeAddress node = getAddressFromQueryHit(resultPacket);

            if (node == null)
            {
                return(null);
            }
            string nickname = getNickFromQueryHit(resultPacket);
            GUID   g        = ((G2PacketGU)(resultPacket.getFirstChildPacket(G2PacketType.GU))).nodeGuid;
            G2Peer peer     = new G2Peer(node.ipv4.ToString(), BitConverter.ToString(g.bytes), node.port, nickname);

            peer.Browsable      = resultPacket.getFirstChildPacket(G2PacketType.BH) == null ? false : true;
            peer.Firewalled     = resultPacket.getFirstChildPacket(G2PacketType.FW) == null ? false : true;
            peer.VendorCode     = resultPacket.getStringFromChildType(G2PacketType.V);
            peer.DNSName        = resultPacket.getStringFromChildType(G2PacketType.DNS);
            peer.AlternatePeers = getAlternateLocationsFromQueryHit(resultPacket);

            return(peer);
        }
        /**
         * Stop the connection to the peer,
         * and sends the results back
         * */
        private void SearchTimeOut(Object sender, EventArgs args)
        {
            G2Packet pack = null;

            while ((pack = Peer.Buffer.PollPacketToReceive()) != null)
            {
                G2PacketQH2 qh2 = pack as G2PacketQH2;
                if (qh2 == null)
                {
                    continue;
                }
                packetResults.Add(qh2);
            }

            Peer.Close();

            if (EndSearch != null)
            {
                EndSearch(referenceToPeer, referenceToSearchResults, packetResults);
            }
        }
 /**
  * Take a search related packets i.e. QA (ack) or QH2 (hit)
  * and stores it into SearchResults class
  * */
 public void EnqueueResultPacket(NodePeer p, G2Packet pack)
 {
     // a hub packet ACK a query
     if (pack.type == G2PacketType.QA)
     {
         G2PacketQA      qa     = pack as G2PacketQA;
         G2SearchResults res    = null;
         bool            exists = SearchResults.TryGetValue(qa.guid, out res);
         if (!exists)  // no entry => not a search we initiated
         {
             G2Log.Write("G2SearchManager : Received ACK of non asked Query");
         }
         else
         {
             res.SetAcknowledgement(qa);
             G2Log.Write("G2SearchManager Received ACK of search " + SearchDB[qa.guid].Keywords[0]);
         }
     }
     // Hit packet !
     else if (pack.type == G2PacketType.QH2)
     {
         G2PacketQH2     qh2    = pack as G2PacketQH2;
         G2SearchResults res    = null;
         bool            exists = SearchResults.TryGetValue(qh2.searchGuid, out res);
         if (exists)
         { // a new result packet coming for a requested query
             res.PushResultPacket(qh2);
             //if (res.TotalFiles > MAX_RESULTS)
             //    G2Network.Instance.StopNetwork();
         }
         else // got a response for a query we did not ask ?
         {
             G2Log.Write("G2SearchManager : Received a Hit on a NON ASKED Query");
         }
     }
 }
Exemple #10
0
        private static ActionInnocence.P2PScan.PeerCollection getAlternateLocationsFromQueryHit(G2PacketQH2 qh2)
        {
            G2Packet pack = qh2.getFirstChildPacket(G2PacketType.ALT);

            if (pack == null)
            {
                return(null);
            }
            G2PacketALT altPack = pack as G2PacketALT;

            ActionInnocence.P2PScan.PeerCollection coll = new ActionInnocence.P2PScan.PeerCollection();
            foreach (NodeAddress add in altPack.Addresses)
            {
                coll.Add(new ActionInnocence.P2PScan.Peer(add.ipv4.ToString(), add.ToString(), add.port, ""));
            }
            return(coll);
        }
Exemple #11
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);
            }
        }