Example #1
0
        internal void Discover(NetPeer peer)
        {
            string content =
                "M-SEARCH * HTTP/1.1\r\n" +
                "HOST: 239.255.255.250:1900\r\n" +
                "ST:upnp:rootdevice\r\n" +
                "MAN:\"ssdp:discover\"\r\n" +
                "MX:3\r\n\r\n";

            m_discoveryResponseDeadline = NetTime.Now + 6.0; // arbitrarily chosen number, router gets 6 seconds to respond
            m_status = UPnPStatus.Discovering;

            byte[] raw = System.Text.Encoding.UTF8.GetBytes(content);

            m_peer.LogDebug("Attempting UPnP discovery");
            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);

            if (peer.Configuration.BroadcastAddress.Equals(IPAddress.Broadcast))
            {
                foreach (IPAddress address in NetUtility.GetBroadcastAddresses())
                {
                    peer.RawSend(raw, 0, raw.Length, new NetEndPoint(address, 1900));
                }
            }
            else
            {
                peer.RawSend(raw, 0, raw.Length, new NetEndPoint(peer.Configuration.BroadcastAddress, 1900));
            }

            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);
        }
Example #2
0
        internal void ExtractServiceUrl(string resp)
        {
#if !DEBUG
            try
            {
#endif
            XmlDocument desc = new XmlDocument();
            desc.Load(WebRequest.Create(resp).GetResponse().GetResponseStream());
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable);
            nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
            XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr);
            if (!typen.Value.Contains("InternetGatewayDevice"))
            {
                return;
            }
            XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:controlURL/text()", nsMgr);
            if (node == null)
            {
                return;
            }
            m_serviceUrl = CombineUrls(resp, node.Value);
            m_peer.LogDebug("UPnP service ready");
            m_discoveryComplete.Set();
#if !DEBUG
        }

        catch { return; }
#endif
        }
Example #3
0
        internal void Discover(NetPeer peer)
        {
            string str =
                "M-SEARCH * HTTP/1.1\r\n" +
                "HOST: 239.255.255.250:1900\r\n" +
                "ST:upnp:rootdevice\r\n" +
                "MAN:\"ssdp:discover\"\r\n" +
                "MX:3\r\n\r\n";

            m_discoveryResponseDeadline = NetTime.Now + 6.0;             // arbitrarily chosen number, router gets 6 seconds to respond
            m_status = UPnPStatus.Discovering;

            byte[] arr = System.Text.Encoding.UTF8.GetBytes(str);

            m_peer.LogDebug("Attempting UPnP discovery");
            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
            peer.RawSend(arr, 0, arr.Length, new NetEndPoint(NetUtility.GetBroadcastAddress(), 1900));
            peer.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);
        }
        // received a library message while Connected
        internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength)
        {
            m_peer.VerifyNetworkThread();

            float now = (float)NetTime.Now;

            switch (tp)
            {
            case NetMessageType.Connect:
                m_peer.LogDebug("Received handshake message (" + tp + ") despite connection being in place");
                break;

            case NetMessageType.ConnectResponse:
                // handshake message must have been lost
                HandleConnectResponse(now, tp, ptr, payloadLength);
                break;

            case NetMessageType.ConnectionEstablished:
                // do nothing, all's well
                break;

            case NetMessageType.LibraryError:
                m_peer.ThrowOrLog("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
                break;

            case NetMessageType.Disconnect:
                NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);

                m_disconnectRequested  = true;
                m_disconnectMessage    = msg.ReadString();
                m_disconnectReqSendBye = false;
                //ExecuteDisconnect(msg.ReadString(), false);
                break;

            case NetMessageType.Acknowledge:
                for (int i = 0; i < payloadLength; i += 3)
                {
                    NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++];                             // netmessagetype
                    int            seqNr = m_peer.m_receiveBuffer[ptr++];
                    seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8);

                    // need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends
                    m_queuedIncomingAcks.Enqueue(new NetTuple <NetMessageType, int>(acktp, seqNr));
                }
                break;

            case NetMessageType.Ping:
                int pingNr = m_peer.m_receiveBuffer[ptr++];
                SendPong(pingNr);
                break;

            case NetMessageType.Pong:
                NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                int   pongNr            = pmsg.ReadByte();
                float remoteSendTime    = pmsg.ReadSingle();
                ReceivedPong(now, pongNr, remoteSendTime);
                break;

            case NetMessageType.ExpandMTURequest:
                SendMTUSuccess(payloadLength);
                break;

            case NetMessageType.ExpandMTUSuccess:
                if (m_peer.Configuration.AutoExpandMTU == false)
                {
                    m_peer.LogDebug("Received ExpandMTURequest altho AutoExpandMTU is turned off!");
                    break;
                }
                NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                int size = emsg.ReadInt32();
                HandleExpandMTUSuccess(now, size);
                break;

            case NetMessageType.NatIntroduction:
                // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual
                m_peer.HandleNatIntroduction(ptr);
                break;

            default:
                m_peer.LogWarning("Connection received unhandled library message: " + tp);
                break;
            }
        }
        // received a library message while Connected
        internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength)
        {
            m_peer.VerifyNetworkThread();

            float now = (float)NetTime.Now;

            switch (tp)
            {
            case NetMessageType.Disconnect:
                NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                ExecuteDisconnect(msg.ReadString(), false);
                break;

            case NetMessageType.Acknowledge:
                for (int i = 0; i < payloadLength; i += 3)
                {
                    NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++];     // netmessagetype
                    int            seqNr = m_peer.m_receiveBuffer[ptr++];
                    seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8);

                    // need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends
                    m_queuedIncomingAcks.Enqueue(new NetTuple <NetMessageType, int>(acktp, seqNr));
                }
                break;

            case NetMessageType.Ping:
                int pingNr = m_peer.m_receiveBuffer[ptr++];
                SendPong(pingNr);
                break;

            case NetMessageType.Pong:
                NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                int   pongNr            = pmsg.ReadByte();
                float remoteSendTime    = pmsg.ReadSingle();
                ReceivedPong(now, pongNr, remoteSendTime);
                break;

            case NetMessageType.ExpandMTURequest:
                SendMTUSuccess(payloadLength);
                break;

            case NetMessageType.ExpandMTUSuccess:
                NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                int size = emsg.ReadInt32();
                HandleExpandMTUSuccess(now, size);
                break;

            case NetMessageType.NatIntroduction:
                // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual
                m_peer.HandleNatIntroduction(ptr);
                break;

            case NetMessageType.ConnectResponse:     // Lidgren bug fix -AR
                // ConnectionEstablished (from handshake) must have been lost, send another one
                m_peer.LogDebug("Resending ConnectionEstablished...");
                SendConnectionEstablished();
                break;

            default:
                m_peer.LogWarning("Connection received unhandled library message: " + tp);
                break;
            }
        }