示例#1
0
    def SendSLPStatus(self, bridge, msg, dest, destGuid, code, phrase):
        target = dest.name.lower()
        if msg.Version == P2P.Version.V2:
            target += ';{' + str(destGuid).lower() + '}'

        slp = MSNSLP.SLPStatusMessage(target, code, phrase)
        if msg.IsSLPData:
            msgSLP = SLPMessage.Parse(msg.InnerMessage)
            slp.Branch = msgSLP.Branch
            slp.Source = msgSLP.Target
            slp.CallId = msgSLP.CallId
            slp.ContentType = msgSLP.ContentType
        else:
            slp.ContentType = 'null'

        response = P2PMessage(msg.Version)
        response.InnerMessage = slp

        if msg.Version == P2P.Version.V1:
            response.VHeader.Flags = P2P.Flags.MSNSLPInfo
        else:
            response.VHeader.OperationCode = P2P.OperationCode.NONE
            response.VHeader.TFCombination = P2P.TFCombination.First

        bridge.Send(None, dest, destGuid, response, None)
示例#2
0
    def ProcessP2PMessage(self, bridge, message, slp):
        #log.info("Got message for %r: %r", self, message)
        self.ResetTimeoutTimer()

        self.RemoteIdentifier = message.Header.Identifier
        if self.Version == P2P.Version.V2:
            self.RemoteIdentifier += message.Header.MessageSize

        if self.Status in (P2PSessionStatus.Closed, P2PSessionStatus.Error):
            log.info("Session is closed / error'd. Not handling message %r", message)
            return False

        if slp is not None:
            if hasattr(slp, 'Method'):
                # Request
                if slp.ContentType == 'application/x-msnmsgr-sessionclosebody' and slp.Method == 'BYE':
                    log.info("Got BYE message from %r", self.Remote.account)
                    if message.Version == P2P.Version.V1:
                        byeAck = message.CreateAck()
                        byeAck.Header.Flags = P2P.Flags.CloseSession
                        self.Send(byeAck)
                    else:
                        slpMessage = MSNSLP.SLPRequestMessage(self.RemoteContactId, 'BYE')
                        slpMessage.Target = self.RemoteContactId
                        slpMessage.Source = self.LocalContactId
                        slpMessage.Branch = self.Invitation.Branch
                        slpMessage.CallId = self.Invitation.CallId
                        slpMessage.ContentType = 'application/x-msnmsgr-sessionclosebody'
                        slpMessage.BodyValues['SessionID'] = str(self.SessionId)

                        log.info("Sending my own BYE message")
                        self.Send(self.WrapSLPMessage(slpMessage))

                    self.OnClosed(self.Remote)
                    return True

                elif (slp.ContentType == 'application/x-msnmsgr-sessionreqbody') and (slp.Method == 'INVITE'):
                    slpMessage = MSNSLP.SLPStatusMessage(self.RemoteContactId, 500, 'Internal Error')
                    slpMessage.Target = self.RemoteContactId
                    slpMessage.Source = self.LocalContactId
                    slpMessage.Branch = self.Invitation.Branch
                    slpMessage.CallId = self.Invitation.CallId
                    slpMessage.ContentType = 'application/x-msnmsgr-sessionreqbody'
                    slpMessage.BodyValues['SessionID'] = str(self.SessionId)

                    errorMessage = self.WrapSLPMessage(slpMessage)
                    bridge.Send(None, self.Remote, self.RemoteContactEPID, errorMessage)
                    return True
                elif slp.ContentType in ('application/x-msnmsgr-transreqbody',
                                         'application/x-msnmsgr-transrespbody',
                                         'application/x-msnmsgr-transdestaddrupdate'):
                        ProcessDirectInvite(slp, self.protocol, self)
                        return True
            else:
                if slp.Code == 200:
                    if slp.ContentType == 'application-x-msnmsgr-transrespbody':
                        ProcessDirectInvite(slp, self.protocol, self)

                    else:
                        log.info("Got 200 OK for invite. Starting app = %r", self.App)
                        self.OnActive()
                        self.App.Start()

                    return True

                elif slp.Code == 603:
                    self.OnClosed(self.Remote)
                    return True

                elif slp.Code == 500:
                    return True

        if self.App is None:
            log.error("No app set up. Ignoring message = %r", message)
            return False

        if message.Header.MessageSize > 0 and message.Header.SessionId > 0:
            reset = False
            appData = io.BytesIO()
            if message.Header.MessageSize == 4 and message.InnerBody == ('\0'*4):
                reset = True

            else:
                appData.write(message.InnerBody)

            if message.Version == P2P.Version.V2 and P2P.TFCombination.First == (message.Header.TFCombination & P2P.TFCombination.First):
                reset = True

            return self.App.ProcessData(bridge, appData, reset)

        log.error("Nothing to do for message = %r", message)
        return False
示例#3
0
def ProcessDCReqInvite(message, ns, session):
    if session is not None and session.Bridge is not None and session.Bridge.BridgeType == 'TCPv1':
        return

    if 'TCPv1' not in message.BodyValues.get('Bridges', message.BodyValues.get('Bridge', '')):
        if session is not None:
            session.DirectNegotiationFailed()
        return

    remoteGuid = message.FromEndPoint
    remote = ns.contact_list.GetContact(message.FromEmailAccount, MSNAB.IMAddressInfoType.WindowsLive)

    dcNonceType, remoteNonce = ParseDCNonce(message.BodyValues)
    if remoteNonce == uuid.UUID(int = 0):
        remoteNonce = remote.dcPlainKey

    hashed = dcNonceType == DCNonceType.Sha1
    nonceFieldName = 'Hashed-Nonce' if hashed else 'Nonce'
    myHashedNonce = remote.dcLocalHashedNonce if hashed else remoteNonce
    myPlainNonce = remote.dcPlainKey

    if dcNonceType == DCNonceType.Sha1:
        remote.dcType = dcNonceType
        remote.dcRemoteHashedNonce = remoteNonce

    else:
        remote.dcType = DCNonceType.Plain
        myPlainNonce = remote.dcPlainKey = remote.dcLocalHashedNonce = remote.dcRemoteHashedNonce = remoteNonce

    ipAddress = util.ip_from_bytes(util.myip())
    port = 0

    if (message.FromEndPoint != uuid.UUID(int=0) and message.ToEndPoint != uuid.UUID(int=0)):
        ver = P2P.Version.V2
    else:
        ver = P2P.Version.V1

    try:
        remote.DirectBridge = ListenForDirectConnection(remote, remoteGuid, ns, ver, session, ipAddress, port, myPlainNonce, remoteNonce, hashed)
    except Exception as e:
        import traceback; traceback.print_exc()
        log.error("Error setting up direct bridge: %r", e)
        port = 0
    else:
        port = remote.DirectBridge.LocalEndPoint[1]

    slp = MSNSLP.SLPStatusMessage(message.Source, 200, 'OK')
    slp.Target = message.Source
    slp.Source = message.Target
    slp.Branch = message.Branch
    slp.CSeq = 1
    slp.CallId = message.CallId
    slp.MaxForwards = 0
    slp.ContentType = 'application/x-msnmsgr-transrespbody'
    slp.BodyValues['Bridge'] = 'TCPv1'

    log.info("port = %r, ipaddress = %r, protocol.ip == %r, other_listening = %r", port, ipAddress, ns.protocol.ip, message.BodyValues.get("Listening", None))
    if port == 0 and message.BodyValues.get("Listening", None) != "false":
        slp.BodyValues['Listening'] = 'false'
        slp.BodyValues[nonceFieldName] = '{%s}' % str(uuid.UUID(int = 0))

    else:
        slp.BodyValues['Listening'] = 'true'
        slp.BodyValues['Capabilities-Flags'] = '1'
        slp.BodyValues['IPv6-global'] = ''
        slp.BodyValues['Nat-Trav-Msg-Type'] = 'WLX-Nat-Trav-Msg-Direct-Connect-Resp'
        slp.BodyValues['UPnPNat'] = 'false'

        slp.BodyValues['NeedConnectingEndpointInfo'] = 'true'
        slp.BodyValues['Conn-Type'] = 'Direct-Connect'
        slp.BodyValues['TCP-Conn-Type'] = 'Direct-Connect'

        slp.BodyValues[nonceFieldName] = '{%s}' % str(myHashedNonce).upper()
        slp.BodyValues['IPv4Internal-Addrs'] = ipAddress
        slp.BodyValues['IPv4Internal-Port'] = str(port)

        if ipAddress != ns.protocol.ip:
            slp.BodyValues['IPv4External-Addrs'] = ns.protocol.ip
            slp.BodyValues['IPv4External-Port'] = str(port)

    p2pmessage = P2PMessage.P2PMessage(ver)
    p2pmessage.InnerMessage = slp

    if ver == P2P.Version.V2:
        p2pmessage.Header.TFCombination = P2P.TFCombination.First
    else:
        p2pmessage.Header.Flags = P2P.Flags.MSNSLPInfo

    if session is not None:
        session.ResetDCTimer()
        session.Bridge.Send(None, session.Remote, session.RemoteContactID, p2pmessage)
        session.Bridge.StopSending(session)
    else:
        ns.SDGBridge.Send(None, remote, remoteGuid, p2pmessage)
示例#4
0
 def _MakeSLPStatusMessage(self, for_who, code, phrase):
     slpMessage = MSNSLP.SLPStatusMessage(for_who, code, phrase)
     self._PrepSLPMessage(slpMessage)
     return slpMessage