/** * Creates a listening point from the following address and attempts to * discover how it is mapped so that using inside the application is possible. * @param address the [address]:[port] pair where ther request should be * sent from. * @return a StunAddress object containing the mapped address or null if * discovery failed. * @throws StunException if something fails along the way. */ public virtual StunAddress GetMappingFor(StunAddress address) { NetAccessPointDescriptor apDesc = new NetAccessPointDescriptor(address); stunStack.InstallNetAccessPoint(apDesc); requestSender = new BlockingRequestSender(stunProvider, apDesc); StunMessageEvent evt = null; try { evt = requestSender.SendRequestAndWaitForResponse( MessageFactory.CreateBindingRequest(), serverAddress); } finally { //free the port to allow the application to use it. stunStack.RemoveNetAccessPoint(apDesc); } if (evt != null) { Response res = (Response)evt.GetMessage(); MappedAddressAttribute maAtt = (MappedAddressAttribute) res.GetAttribute(Attribute.MAPPED_ADDRESS); if (maAtt != null) { StunAddress sa = maAtt.GetAddress(); return(sa); } } return(null); }
/** * Creates a listening point for the specified socket and attempts to * discover how its local address is NAT mapped. * @param socket the socket whose address needs to be resolved. * @return a StunAddress object containing the mapped address or null if * discovery failed. * @throws StunException if something fails along the way. */ public virtual StunAddress GetMappingFor(MyUdpClient socket) { NetAccessPointDescriptor apDesc = stunStack.InstallNetAccessPoint(socket); requestSender = new BlockingRequestSender(stunProvider, apDesc); StunMessageEvent evt = null; try { evt = requestSender.SendRequestAndWaitForResponse( MessageFactory.CreateBindingRequest(), serverAddress); } finally { stunStack.RemoveNetAccessPoint(apDesc); } if (evt != null) { Response res = (Response)evt.GetMessage(); MappedAddressAttribute maAtt = (MappedAddressAttribute) res.GetAttribute(Attribute.MAPPED_ADDRESS); if (maAtt != null) { return(maAtt.GetAddress()); } } return(null); }
/** * Called to notify this provider for an incoming message. * @param event the event object that contains the new message. */ public virtual void HandleMessageEvent(StunMessageEvent @event) { Message msg = @event.GetMessage(); //request if (msg is Request) { TransactionID serverTid = TransactionID. CreateTransactionID(msg.GetTransactionID()); serverTransactions.Add(serverTid); if (requestListener != null) { requestListener.requestReceived(@event); } } //response else if (msg is Response) { TransactionID tid = TransactionID. CreateTransactionID(msg.GetTransactionID()); StunClientTransaction tran = (StunClientTransaction)clientTransactions[tid]; clientTransactions.Remove(tid); if (tran != null) { tran.HandleResponse(@event); } else { //do nothing - just drop the phantom response. } } }
/** * Called to notify this provider for an incoming message. * @param event the event object that contains the new message. */ public virtual void HandleMessageEvent(StunMessageEvent @event) { Message msg = @event.GetMessage(); //request if(msg is Request) { TransactionID serverTid = TransactionID. CreateTransactionID(msg.GetTransactionID()); serverTransactions.Add(serverTid); if(requestListener != null) requestListener.requestReceived(@event); } //response else if(msg is Response) { TransactionID tid = TransactionID. CreateTransactionID(msg.GetTransactionID()); StunClientTransaction tran = (StunClientTransaction)clientTransactions[tid]; clientTransactions.Remove(tid); if(tran != null) { tran.HandleResponse(@event); } else { //do nothing - just drop the phantom response. } } }
/** * Implements the discovery process itself (see class description). * @return a StunDiscoveryReport containing details about the network * configuration of the host where the class is executed. * @throws StunException ILLEGAL_STATE if the discoverer has not been started * NETWORK_ERROR or ILLEGAL_ARGUMENT if a failure occurs while executing * the discovery algorithm */ virtual public StunDiscoveryReport determineAddress() { checkStarted(); StunDiscoveryReport report = new StunDiscoveryReport(); StunMessageEvent evt = doTestI(serverAddress); if (evt == null) { //UDP Blocked report.SetNatType(StunDiscoveryReport.UDP_BLOCKING_FIREWALL); return(report); } else { StunAddress mappedAddress = ((MappedAddressAttribute)evt.GetMessage(). GetAttribute(Attribute.MAPPED_ADDRESS)).GetAddress(); StunAddress backupServerAddress = ((ChangedAddressAttribute)evt.GetMessage(). GetAttribute(Attribute.CHANGED_ADDRESS)).GetAddress(); report.SetPublicAddress(mappedAddress); if (mappedAddress.Equals(apDescriptor.GetAddress())) { evt = doTestII(serverAddress); if (evt == null) { //Sym UDP Firewall report.SetNatType(StunDiscoveryReport.SYMMETRIC_UDP_FIREWALL); return(report); } else { //open internet report.SetNatType(StunDiscoveryReport.OPEN_INTERNET); return(report); } } else { evt = doTestII(serverAddress); if (evt == null) { evt = doTestI(backupServerAddress); if (evt == null) { // System.oout.println("Failed to receive a response from backup stun server!"); return(report); } StunAddress mappedAddress2 = ((MappedAddressAttribute)evt.GetMessage(). GetAttribute(Attribute.MAPPED_ADDRESS)).GetAddress(); if (mappedAddress.Equals(mappedAddress2)) { evt = doTestIII(serverAddress); if (evt == null) { //port restricted cone report.SetNatType(StunDiscoveryReport.PORT_RESTRICTED_CONE_NAT); return(report); } else { //restricted cone report.SetNatType(StunDiscoveryReport.RESTRICTED_CONE_NAT); return(report); } } else { //Symmetric NAT report.SetNatType(StunDiscoveryReport.SYMMETRIC_NAT); return(report); } } else { //full cone report.SetNatType(StunDiscoveryReport.FULL_CONE_NAT); return(report); } } } }