Ejemplo n.º 1
0
 def dataReceived(self, data):
     TwistedBaseClient.dataReceived(self, data)
     msg = SipMessage.parse(data)[0]
     if msg.is_request and msg.method == "INVITE":
         # get RTP connection properties, connect rtp fake rtp client
         self.factory.connect_udp(*self.factory.parse_sdp(msg))
         headers = {}
         for header_name in ["Via", "From", "To", "Call-ID", "CSeq"]:
             headers[header_name] = msg.headers[header_name]
         headers["Content-Length"] = "0"
         method = "INVITE"
         res = SipMessage(status=100, is_request=False, method=method,
                          headers=headers)
         self.do_request(res)
         res = SipMessage(status=180, is_request=False, method=method,
                          headers=headers)
         self.do_request(res)
         headers["Content-Type"] = "application/sdp"
         data = SDP_DATA % self.factory.to_dict()
         headers["Content-Length"] = len(data)
         res = SipMessage(status=200, body=data,
                          is_request=False, method=method, headers=headers)
         self.do_request(res)
     elif msg.is_request and msg.method == "BYE":
         headers = {}
         for header_name in ["Via", "From", "To", "Call-ID", "CSeq",
                             "Content-Length"]:
             headers[header_name] = msg.headers[header_name]
             res = SipMessage(status=200, is_request=False, method="BYE",
                              headers=headers)
         self.do_request(res)
         self.factory.successes += 1
         self.transport.loseConnection()
Ejemplo n.º 2
0
 def dataReceived(self, data):
     """
     Get answer against REGISTER request, count digest and make registration
     """
     self.factory.responses_received += 1
     resps = SipMessage.parse(data)
     for res in resps:
         self.factory.log(res, send=False)
     if self.factory.registration_done:
         return
     for res in resps:
         if not res.is_request and res.status == 407 \
                 and "REGISTER" in res.headers.get("CSeq"):
             req = self.factory.messages['register_no_auth'].copy()
             client_header = req.gen_auth_header(
                 res.headers.get("Proxy-Authenticate", ""),
                 self.factory.msisdn,
                 self.factory.password
             )
             req.headers.update(client_header)
             req.headers["CSeq"] = "2 REGISTER"
             self.do_request(req)
         if not res.is_request and res.status == 200 \
                 and "REGISTER" in res.headers.get("CSeq"):
             self.factory.registration_done = True
Ejemplo n.º 3
0
 def dataReceived(self, data):
     TwistedBaseClient.dataReceived(self, data)
     # must be registred for doing call
     if not self.factory.registration_done:
         return
     # receiver can get any count of sip packages in one tcp message
     resps = SipMessage.parse(data)
     for res in resps:
         # do nothing on 180 ringing
         if not res.is_request and res.status == 180 \
                 and res.method == "INVITE":
             return
         # disconnect
         if not res.is_request and res.method == "BYE" \
                 and res.status == 200:
             self.factory.successes += 1
             self.factory.active_calls -= 1
             self.transport.loseConnection()
         # got timeout from server for call
         if res.is_request and res.method == "BYE":
             headers = {}
             res_headers = {}
             res_headers.update(res.headers)
             for header_name in ["Via", "To", "From", "Call-ID", "CSeq",
                                 "Content-Length"]:
                 headers[header_name] = res_headers[header_name]
                 res = SipMessage(status=200, is_request=False, method="BYE",
                                  headers=headers)
             self.do_request(res)
             if self.factory.call_connected:
                 self.factory.successes += 1
                 self.factory.active_calls -= 1
                 self.transport.loseConnection()
         if self.factory.call_connected:
             return
         if not res.is_request and res.status == 200 \
                 and res.method == "INVITE":
             # get RTP connection properties, connect rtp fake rtp client
             self.factory.connect_udp(*self.factory.parse_sdp(res))
             self.factory.call_connected = True
             self.factory.active_calls += 1
             # wait for end of call
             headers = {}
             for header_name in ["Via", "From", "To", "Call-ID"]:
                 headers[header_name] = res.headers[header_name]
             headers["Content-Length"] = "0"
             method = "ACK"
             headers["CSeq"] = "4 ACK"
             req = SipMessage(is_request=True, method=method,
                              headers=headers)
             # self.do_request(req)
             # self.send_deferred_bye(req, self.factory.options.duration)
             method = "BYE"
             headers["CSeq"] = "4 BYE"
             req = SipMessage(is_request=True, method=method,
                              headers=headers)
             # wait then send for non blocking purposes
             self.send_deferred(req, self.factory.options.duration + 1)
         if self.factory.call_connected:
             return
         if not res.status == 100 and not res.status == 407:
             # send first INVITE. Resend if smth goes wrong
             # count sent INVITES
             self.factory.invites_count += 1
             # generate new Call-ID and tag
             self.factory.new_call()
             data = SDP_DATA % self.factory.to_dict()
             headers = OrderedDict()
             headers.update({
                 'Via': 'SIP/2.0/TCP 82.144.65.34;branch=%(branch)s;rport',
                 'From': '<sip:%(msisdn)s@%(domain)s>;tag=%(tag)s',
                 'To': '<sip:*%(to)s@%(domain)s>',
                 'CSeq': '1 INVITE',
                 'Call-ID': '%(call_id)s',
                 'Max-Forwards': '70',
                 'Supported': 'replaces, outbound',
                 'Allow': 'INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, '
                          'NOTIFY, MESSAGE, SUBSCRIBE, INFO',
                 'Content-Type': "application/sdp",
                 'Content-Length': "%s" % len(data),
                 'Contact':  '<sip:%(msisdn)s@%(domain)s;transport=tcp>',
                 'User-Agent': 'TestAgent/4.0.1',
             })
             for key, value in headers.items():
                 headers[key] = value % self.factory.to_dict()
             req = SipMessage(method="INVITE",
                              headers=headers,
                              body=data,
                              is_request=True,
                              to=self.factory.to)
             self.factory.log(req, send=True)
             self.do_request(req)
             self.factory.messages["invite_no_auth"] = req
         # send INVITE with digest
         elif res.status == 407 and "INVITE" in res.headers.get("CSeq"):
             req = self.factory.messages['invite_no_auth'].copy()
             client_header = req.gen_auth_header(
                 res.headers.get("Proxy-Authenticate", ""),
                 self.factory.msisdn,
                 self.factory.password
             )
             req.headers.update(client_header)
             req.headers["CSeq"] = "2 INVITE"
             self.do_request(req)