sQc@s`dZddlZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZddlmZmZmZdd lmZddlZdd lmZmZdd lmZd Zi d d6dd6dd6dd6dd6dd6dd6dd6dd6ZiZ x$ej!D]\Z"Z#e"e e#6d?d@6dAdB6dCdD6dEdF6dGdH6dIdJ6dKdL6dMdN6dOdP6dQdR6dSdT6dUdV6dWdX6dYdZ6d[d\6d]d^6d_d`6dadb6dcdd6dedf6dgdh6didj6dkdl6dmdn6dodp6dqdr6dsdt6dudv6dwdx6dydz6d{d|6d}d~6dd6dd6dd6dd6dCd6Z$idd6dd6dd6Z%dZ&dZ'dZ(eedddde(Z(dZ)eedddde)Z)e*Z+de*fdYZ,dZ-dddYZ.dddZ0dZ1ddddZ2de3fdYZ4de4fdYZ5dddYZ6de6fdYZ7de6fdYZ8dej9fdYZ:dej;fdYZ<de fdYZ=dddYZ>de fdYZ?de fdYZ@de<fdYZAde fdYZBdddYZCdefdYZDdddYZEdeAfdYZFdddYZGdS(sSession Initialization Protocol. Documented in RFC 2543. [Superceded by 3261] This module contains a deprecated implementation of HTTP Digest authentication. See L{twisted.cred.credentials} and L{twisted.cred._digest} for its new home. iN(tmd5(t implementst Interface(tlogtutil(t deprecated(tVersion(tprotocoltdefertreactor(tcred(tUsernameHashedPasswordtUsernamePassword(tbasicitiscall-idtmtcontacttescontent-encodingtlscontent-lengthtcs content-typetftfromtstsubjectttttotvtviatTryingidtRingingisCall Is Being ForwardeditQueuedisSession ProgressitOKisMultiple Choicesi,sMoved Permanentlyi-sMoved Temporarilyi.s See Otheri/s Use Proxyi1sAlternative Servicei|s Bad Requestit UnauthorizedisPayment Requiredit Forbiddenis Not FoundisMethod Not AllowedisNot AcceptableisProxy Authentication RequiredisRequest TimeoutitConflictitGoneisLength RequiredisRequest Entity Too LargeisRequest-URI Too LargeisUnsupported Media TypeisUnsupported URI Schemeis Bad ExtensionisExtension RequiredisInterval Too BriefisTemporarily UnavailableisCall/Transaction Does Not Existis Loop Detectedis Too Many HopsisAddress Incompleteit Ambiguousis Busy HereisRequest TerminatedisNot Acceptable HereisRequest PendingitUndecipherableisInternal Server ErrorisNot Implementedis Bad GatewayisService UnavailableisServer Time-outisSIP Version not supportedisMessage Too LargeisBusy EverywhereiXtDeclinei[sDoes not exist anywherei\i^tCSeqtcseqsCall-IDsWWW-Authenticateswww-authenticatecCs/djg|jdD]}|j^qS(sA Capitalize a string, making sure to treat - as a word seperator t-(tjointsplitt capitalize(Rtx((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytdashCapitalize{scCs3|d|dkodknr/|dd!S|S(Niit"i((R((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytunqs$ cCst}|j||jd|j||jd|j||j}|dkrt}|j||jd|j||jd|j||j}n|jdS(Nt:smd5-sessthex(Rtupdatetdigesttencode(tpszAlgt pszUserNametpszRealmt pszPasswordtpszNoncet pszCNonceRtHA1((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt DigestCalcHA1s               tTwistedi ic Cs.t}|j||jd|j||dkrY|jd|j|n|jjd} t}|j||jd|j||jd|r|r|j||jd|j||jd|j||jdn|j| |jjd} | S(NR1sauth-intR2(RR3R4R5( R<R:t pszNonceCountR;tpszQopt pszMethodt pszDigestUrit pszHEntityRtHA2thash((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytDigestCalcResponses.                  tViac BsYeZdZeddededddZdZdZ e ee Z dZ RS(s A L{Via} is a SIP Via header, representing a segment of the path taken by the request. See RFC 3261, sections 8.1.1.7, 18.2.2, and 20.42. @ivar transport: Network protocol used for this leg. (Probably either "TCP" or "UDP".) @type transport: C{str} @ivar branch: Unique identifier for this request. @type branch: C{str} @ivar host: Hostname or IP for this leg. @type host: C{str} @ivar port: Port used for this leg. @type port C{int}, or None. @ivar rportRequested: Whether to request RFC 3581 client processing or not. @type rportRequested: C{bool} @ivar rportValue: Servers wishing to honor requests for RFC 3581 processing should set this parameter to the source port the request was received from. @type rportValue: C{int}, or None. @ivar ttl: Time-to-live for requests on multicast paths. @type ttl: C{int}, or None. @ivar maddr: The destination multicast address, if any. @type maddr: C{str}, or None. @ivar hidden: Obsolete in SIP 2.0. @type hidden: C{bool} @ivar otherParams: Any other parameters in the header. @type otherParams: C{dict} tUDPc Ks||_||_||_||_||_||_|tkrmtjdt ddd|_ t|_ nT|dkrd|_ t|_ n3|t krd|_ t|_ n||_ t|_ ||_| |_| |_dS(s( Set parameters of this Via header. All arguments correspond to attributes of the same name. To maintain compatibility with old SIP code, the 'rport' argument is used to determine the values of C{rportRequested} and C{rportValue}. If None, C{rportRequested} is set to True. (The deprecated method for doing this is to pass True.) If an integer, C{rportValue} is set to the given value. Any arguments not explicitly named here are collected into the C{otherParams} dict. s+rport=True is deprecated since Twisted 9.0.t stackleveliN(t transportthosttporttttlthiddentreceivedtTruetwarningstwarntDeprecationWarningtNonet rportValuetrportRequestedt_absenttFalsetbranchtmaddrt otherParams( tselfRKRLRJRMRNROtrportRYRZtkw((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt__init__s0                   cCs1|jtkrtS|jdk r)|jSdSdS(sG Returns the rport value expected by the old SIP code. N(RVRPRURT(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt _getrports cCs||_t|_dS(s L{Base._fixupNAT} sets C{rport} directly, so this method sets C{rportValue} based on that. @param newRPort: The new rport value. @type newRPort: C{int} N(RURXRV(R\tnewRPort((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt _setrport!s cCsd|j|j|jf}|jr2|d7}nx@d D]8}t||}|d k r9|d||f7}q9q9W|jr|d7}n&|jd k r|d |jf7}n|j j }|j xE|D]=\}}|d kr|d |7}q|d||f7}qW|S( sI Serialize this header for use in a request or response. sSIP/2.0/%s %s:%ss;hiddenRMRYRZROs;%s=%ss;rports ;rport=%st;(sttlsbranchsmaddrsreceivedN( RJRKRLRNtgetattrRTRVRUR]R[titemstsort(R\RtntvaluetetctkR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyttoString/s$        N( t__name__t __module__t__doc__tPORTRTRXRWR_R`RbtpropertyR]Rk(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRGs   * cCs|jd}|d|d}}|jdd\}}|j}i}|jd\}}} |dks~|dkrtd|n| |d (RRRKRLRJ(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt__repr__sN(RlRmRnRTR_RkRR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRts    cCsi}|jds,td|d n|djd}|d|d}}|jdd}t|dkr|\}} |jd d} t| dkr| d|d RcRuN( RvRRR+tendswithRTRMRRJRZ( taddressRKRLtcleanR|RRt paramstringRRjR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt parseAddresss2         tSIPErrorcBseZddZRS(cCsI|dkrt|}ntj|d||f||_||_dS(NsSIP error (%d): %s(RTt statusCodest ExceptionR_tcodetphrase(R\RR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_ s    N(RlRmRTR_(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR stRegistrationErrorcBseZdZRS(sRegistration was not possible.(RlRmRn(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRstMessagecBsJeZdZdZdZdZdZdZdZ dZ RS(sA SIP message.cCs%tj|_d|_d|_dS(NRti(Rt OrderedDictRtbodytfinished(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_s cCs\|j}tj||}|dkr<t||_n|jj|gj|dS(Nscontent-length(tlowert longHeadersRRxtlengthRRR(R\RRh((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt addHeader#s   cCs|j|7_dS(N(R(R\tdata((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytbodyDataReceived*scCs@|jdkr3|jt|jkr3tdnd|_dS(Nswrong body lengthi(RRTRyRRwR(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytcreationFinished-s' cCsd|j}xW|jjD]F\}}x7|D]/}|dtj|pWt||f7}q3Wq W|d7}||j7}|S(Ns%s s%s: %s s (t_getHeaderLineRReRRR.R(R\RRgtvsR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRk2s 1  cCs tdS(N(tNotImplementedError(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR;sN( RlRmRnRTRR_RRRRkR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs     tRequestcBs,eZdZddZdZdZRS(sA Request for a URIsSIP/2.0cCsQtj|||_t|tr1||_nt||_t|jdS(N(RR_Rt isinstanceRturiRR(R\RRtversion((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_Cs    cCs#dt||j|jjfS(Ns(tidRRRk(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRLscCsd|j|jjfS(Ns %s %s SIP/2.0(RRRk(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyROs(RlRmRnR_RR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR?s tResponsecBs/eZdZdddZdZdZRS(sA Response to a URI RequestsSIP/2.0cCs<tj|||_|dkr/t|}n||_dS(N(RR_RRTRR(R\RRR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_Vs     cCsdt||jfS(Ns(RR(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR]scCsd|j|jfS(Ns SIP/2.0 %s %s(RR(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR`sN(RlRmRnRTR_RR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRSs tMessagesParsercBseZdZdZdZdZdZdZdZddZ dZ d Z d Z d Z d Zd ZddZdZRS(sA SIP messages parser. Expects dataReceived, dataDone repeatedly, in that order. Shouldn't be connected to actual transport. sSIP/2.0it firstlineicCs||_|jdS(N(tmessageReceivedtreset(R\tmessageReceivedCallback((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_rs RtcCs>d|_d|_d|_d|_d|_|j|dS(NRi(tstateRTRt bodyReceivedtmessageRt setLineMode(R\t remainingData((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRvs      cCsd|_|jdS(Ntinvalid(Rt setRawMode(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytinvalidMessage~s cCs|j|jdkrdS|jdkr:|jdS|jdkrV|jn(|j|jkru|jn tddS(NRRsthis should never happen(tclearLineBufferRRRRTt messageDoneRt RuntimeError(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytdataDones    cCs9ytjj||Wntj|jnXdS(N(R t LineReceivert dataReceivedRterrR(R\R((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs  cCs tdS(s Expected to create self.message.N(R(R\tline((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pythandleFirstLinescCs|jdS(N(R(R\R((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytlineLengthExceededsc Cs|jdkr$x,|jds0|jdr=|d}qW|sHdSy|jdd\}}}Wntk r|jdSX|dkr|jryt|}Wntk r|jdSXt|||_n8|dkr |j r t |||_n|jdSd|_dS|jdks9t |rP|jds]|jd r|j \}}|||j f|_ q|j r|jj|j d|_ ny|jd d\}}Wntk r|jdSX||j f|_ |jd kryt|j |_WqMtk rI|jdSXqnXd |_|j r|jj|j d|_ n|jd kr|jdS|jdS(NRs s iRqisSIP/2.0Rs R1scontent-lengthRi(RRR+RwRtacceptResponsesRxRRtacceptRequestsRtAssertionErrorRtlstripRRTRRRR(R\RtatbRRRRh((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt lineReceivedsf!                 cCsC|jdkst|jj|j|j|j|dS(NR(RRRRRR(R\R((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs cCs|jdkst|jdkr(dS|jdkrJ|jj|nt|}|j|j}||kr|jj|| |j||dS|j|7_|jj||j|jkr|jndS(NRR(sbodysinvalid( RRRRTRRRyRR(R\RtdataLent expectedLen((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytrawDataReceiveds  (RlRmRnRRRRtdebugR_RRRRRRRRR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRds         @ tBasecBskeZdZeZeZdZdZdZdZ dZ dZ dZ dZ d ZRS( s'Base class for SIP clients and servers.cCsg|_t|j|_dS(N(tmessagesRt addMessagetparser(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_s cCs|jj|dS(N(RR(R\tmsg((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRscCs|jj||jjx||jD]q}|j|||jrftjd|j|fnt |t r|j ||q'|j ||q'Wg|j(dS(NsReceived %r from %r( RRRRt _fixupNATRRRRkRRthandle_requestthandle_response(R\RtaddrR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytdatagramReceived s   cCs|\}}t|jdd}|j|krp||_|j|krV||_n|j|jddt|}x+dD]#}|jj|g|j|physical URL mapping.cCsdS(sRegister the physical address of a logical URL. @return: Deferred of C{Registration} or failure with RegistrationError. N((t domainURLt logicalURLt physicalURL((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytregisterAddress_scCsdS(sUnregister the physical address of a logical URL. @return: Deferred of C{Registration} or failure with RegistrationError. N((RR R ((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytunregisterAddressescCsdS(sGet registration info for logical URL. @return: Deferred of C{Registration} object or failure of LookupError. N((R ((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytgetRegistrationInfoks(RlRmRnR R R (((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR\s  tILocatorcBseZdZdZRS(s2Allow looking up physical address for logical URL.cCsdS(sReturn physical URL of server for logical URL of user. @param logicalURL: a logical C{URL}. @return: Deferred which becomes URL or fails with LookupError. N((R ((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt getAddressus(RlRmRnR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRrstProxycBsqeZdZeZd Zd edZdZdZdZ dZ dZ dZ dZ d ZRS( s SIP proxy.cCs/|ptj|_||_tj|dS(sCreate new instance. @param host: our hostname/IP as set in Via headers. @param port: our port as set in Via headers. N(tsockettgetfqdnRKRLRR_(R\RKRL((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyR_s cCstd|jd|jS(s*Return value of Via header for this proxy.RKRL(RGRKRL(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytgetViascstdjd}|dkr1j}ny||}WnXtk ru}jj|jnRtj jjdn)X|dk r|j fdndS(Nshandle_%s_requesticsjj|jS(N(RRR(R(RR\(s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyts( RdRRTthandle_request_defaultRRRRRRt addErrback(R\RRRRR((RR\s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs    c s|\}}fd}j}|j|jdkrQtjddS|jdjd|jt|jdddd\}}} jj|} | j j || j j |dS( sDefault request handler. Default behaviour for OPTIONS and unknown methods for proxies is to forward message on to the client. Since at the moment we are stateless proxy, thats basically everything. cs'|j|jdd<j||S(NRi(RkRR(RR(R\(s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyt_mungContactHeadersRsDropping looped message.NiRRi( RRkRRRtinsertRtlocatorRt addCallbackRRt_cantForwardRequest( R\RRRRRt viaHeaderRRttagsR((R\s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs   &cCs8|jt|jdd=|j|jd|dS(NRii(ttrapt LookupErrorRRR(R\terrorR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs cCsmt|jdd}|jp&|j}|jpA|jpA|j}td|d|}|j||dS(sFDeliver response. Destination is based on topmost Via header.RiRKRLN( RRRORKR]RLRoRR(R\RRRKRLR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyRs cCs>t|}x+dD]#}|jj|g|j|R?R@RA(R\((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pytgenerateOpaquebscCsF|j}|j}||j|t|jdRQthashlibRtzope.interfaceRRttwisted.pythonRRttwisted.python.deprecateRttwisted.python.versionsRttwisted.internetRRR ttwistedR ttwisted.cred.errorttwisted.cred.credentialsR R ttwisted.protocolsR Rot shortHeadersRReRjRRRR.R0R=RFtobjectRWRGRRRTRRRRRRRRRRRtDatagramProtocolRRRRRRR"R&R+R;RLRo(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/sip.pyts<        ! $!  +86 " &W o)'1