private bool isLookupQueryValid(SignedQueryByName signedQuery) { UserEntry responder = ServerApp._pkiCommunicator.GetVerifiedUserPublicKey(whoSentQuery(signedQuery.Query)); if (responder != null) { RSACryptoServiceProvider responderProvider = new RSACryptoServiceProvider(); responderProvider.FromXmlString(responder.PubKey); byte[] data = Encoding.Default.GetBytes(signedQuery.Query.ToString()); return responderProvider.VerifyData(data, "SHA1", signedQuery.Signature); } else return false; }
public void lookupname(SignedQueryByName incomingQuery) { ServerToServerServices friend; RemoteAsyncLookupNameDelegate remoteDel; QueryByName q = incomingQuery.Query; QueryByName newQ = new QueryByName(q.Name, q.Uris, new List<string>(),q.Id); newQ.ContactingServerUri.Clear(); newQ.ContactingServerUri.Add(ServerApp._primaryURI); bool sendMessage = false; System.Windows.Forms.MessageBox.Show(ServerApp._user.Username + " : Received the request."); if (ServerApp._user.SentMessages.Contains(incomingQuery.Query.Id)) { MessageBox.Show(ServerApp._user.Username + " : Message was already sent so the request was discarted!"); return; } if (ServerApp._user.ReceivedNameMessages.Contains(incomingQuery.Query)) { MessageBox.Show(ServerApp._user.Username + " : Message was already received so the request was discarted!"); return; } //only accepts messages from predecessors! if (!isPredecessor(incomingQuery.Query)) { MessageBox.Show(ServerApp._user.Username + " says " + whoSentQuery(incomingQuery.Query) + " is trying to screw the comunication!"); return; } if (!isLookupQueryValid(incomingQuery)) { MessageBox.Show(ServerApp._user.Username + " Could not verify lookup message"); return; } ServerApp._user.ReceivedNameMessages.Add(q); //Is it receiving this message from the original requester? if so doesnt need consensus. (check expl @ shareObject()) if (incomingQuery.Query.ContactingServerUri.ElementAt(0).CompareTo(incomingQuery.Query.Uris.ElementAt(0)) == 0) { //Does not need consensus MessageBox.Show(ServerApp._user.Username + ": Does not need consensus!"); sendMessage = true; } else { //consensus sendMessage = consensus(incomingQuery.Query); } if (sendMessage) { MessageBox.Show(ServerApp._user.Username + ": Reached consensus!"); if (ServerApp._user.Username[0] == q.Name[0]) { sendResponseToLookupQuery(q); return; } /* sign the query which we send to our friends */ byte[] queryData = Encoding.Default.GetBytes(newQ.ToString()); byte[] signature = ServerApp._rsaProvider.SignData(queryData, "SHA1"); SignedQueryByName signedNewNameQuery = new SignedQueryByName(newQ, signature); foreach (Friend i in ServerApp._user.Friends) { if (i.Uris.ElementAt(0) != null && i.SucessorSwarm) { if (i.Uris.ElementAt(0).CompareTo(q.ContactingServerUri.ElementAt(0)) != 0) { System.Windows.Forms.MessageBox.Show(ServerApp._user.Username + " : forwarding to : " + i.Name); friend = ((ServerToServerServices)Activator.GetObject(typeof(ServerToServerServices), i.Uris.ElementAt(0) + "/" + ServicesNames.ServerToServerServicesName)); remoteDel = new RemoteAsyncLookupNameDelegate(friend.lookupname); remoteDel.BeginInvoke(signedNewNameQuery, null, null); } } } ServerApp._user.SentMessages.Add(incomingQuery.Query.Id); } else MessageBox.Show(ServerApp._user + ": Didn't reach consensus yet!"); }
public void lookupname(QueryByName q) { System.Windows.Forms.MessageBox.Show(ServerApp._user.Username + " lookupname " + q.Name); ServerToServerServices friend; RemoteAsyncLookupNameDelegate remoteDel; ClientServices client; if (!ServerApp._serviceAvailable) { client = ((ClientServices)Activator.GetObject(typeof(ClientServices), ServerApp._clientUri + "/" + ServicesNames.ClientServicesName)); new RemoteAsyncServiceUnavailableDelegate(client.serviceUnavailable).BeginInvoke(null, null); return; } //One of this is the response uri and the other is the previous uri q.Uris.Add(ServerApp._primaryURI); q.ContactingServerUri.Add(ServerApp._primaryURI); byte[] data = Encoding.Default.GetBytes(q.ToString()); byte[] signature = ServerApp._rsaProvider.SignData(data, "SHA1"); SignedQueryByName signedQuery = new SignedQueryByName(q, signature); foreach (Friend i in ServerApp._user.Friends) { if (i.Uris.ElementAt(0) != null && i.SucessorSwarm) //only sends to the sucessor nodes { friend = ((ServerToServerServices)Activator.GetObject(typeof(ServerToServerServices), i.Uris.ElementAt(0) + "/" + ServicesNames.ServerToServerServicesName)); remoteDel = new RemoteAsyncLookupNameDelegate(friend.lookupname); remoteDel.BeginInvoke(signedQuery, null, null); } } }