def flow(users, pilot, param): parameters = copy(param) usera = next(users) parameters["userA"] = usera parameters["userB"] = pilot parameters["source_port"] = link[usera].port Invite = buildMessage(message["Invite_SDP_1"], parameters) #print(Invite) link[usera].send(Invite.contents()) inBytes = link[usera].waitForData() inmessage = handleDA(Invite, parseBytes(inBytes), user) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="100 Trying",\ "Sent:\n{}Received:\n{}".format(Invite,inmessage) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="180 Ringing",\ "A side received Trying and then :\n{}".format(inmessage) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="200 OK",\ "A side received Ringing and then:\n{}".format(inmessage) m = buildMessage(message["Ack_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] #print(m) link[usera].send(m.contents()) sleep(parameters["talkDuration"]) m = buildMessage(message["Bye_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] #print(m) link[usera].send(m.contents()) inBytes = link[usera].waitForData() inmessage = handleDA(m, parseBytes(inBytes), user) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="200 OK",\ "Sent:\n{}Received:\n{}".format(m,inmessage)
def flow(users, pilot): usera = next(users) data.parameters["userA"] = usera data.parameters["userB"] = pilot data.parameters["source_port"] = link[usera].port Invite = buildMessage(message["Invite_SDP_1"], data.parameters) #print(Invite) link[usera].send(Invite.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="100 Trying",\ "Sent:\n{}Received:\n{}".format(Invite,inmessage) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="180 Ringing",\ "B side sent:\n{}but A side received:\n{}".format(Ringing,inmessage) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="200 OK",\ "B side got:\n{}but A side received:\n{}".format(ack,inmessage) data.parameters["source_port"] = link[usera].port m = buildMessage(message["Ack_1"], data.parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] #print(m) link[usera].send(m.contents()) sleep(talkDuration) data.parameters["source_port"] = link[usera].port m = buildMessage(message["Bye_1"], data.parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] #print(m) link[usera].send(m.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) #print("IN:",inmessage) assert inmessage.type=="Response" and inmessage.status=="200 OK",\ "Sent:\n{}Received:\n{}".format(m,inmessage)
def Register(user): parameters["user"] = user m = buildMessage(message["Register_1"], parameters) print(m) link[user].send(m.contents()) inBytes = link[user].waitForData() inmessage = parseBytes(inBytes) print(inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK"
def handleDA(request, response, user, pwd=None): "Add DA to message and send again" # Usual case in lab, password same as username if not pwd: pwd = user if response.type == "Response" and response.status == "401 Unauthorized": request.addAuthorization(response["WWW-Authenticate"], user, pwd) link[user].send(request.contents()) inBytes = link[user].waitForData() return parseBytes(inBytes) else: return response
def Register(user): data.parameters["user"] = user L = link[user] data.parameters["source_port"] = L.port m = buildMessage(message["Register_1"], data.parameters) #print(m) L.send(m.contents()) inBytes = L.waitForData() inmessage = parseBytes(inBytes) #print(inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK", "{}\n{}".format( user, inmessage)
def handleDA(self, request, response): """" Add DA to message and send again """ # Usual case in lab, password same as username if "da_pass" not in self.parameters or "da_user" not in self.parameters: self.set_digest_credentials(self.number, self.number, "") user, pwd = self.parameters["da_user"], self.parameters["da_pass"] if response.type == "Response" and response.status == "401 Unauthorized": request.addAuthorization(response["WWW-Authenticate"], user, pwd) self.link.send(request.contents()) inBytes = self.link.waitForSipData() return parseBytes(inBytes) else: return response
def waitForMessage(self, message_type, ignore_messages=[]): """ Wait for a specific type of SIP message. :message_type is a string that we will make sure is contained in the received message request or response line """ inmessage = None while not inmessage or inmessage.get_status_or_method( ) in ignore_messages: inbytes = self.link.waitForSipData() inmessage = self.handleDA(self.last_sent_message, parseBytes(inbytes)) self.last_received_message = inmessage assert message_type in inmessage.get_status_or_method(), \ '{}: Got "{}" while expecting "{}"'.format(self.number, inmessage.get_status_or_method(), message_type)
def Register(user): parameters["user"] = user L = link[user] parameters["source_port"] = L.port parameters["source_ip"] = L.ip parameters["dest_ip"] = L.rip parameters["dest_port"] = L.rport parameters["epid"] = util.epid(user) m = buildMessage(message["Register_2"], parameters) #print(m) L.send(m.contents()) inBytes = L.waitForData() try: inmessage = handleDA(m, parseBytes(inBytes), user) assert inmessage.type == "Response" and inmessage.status == "200 OK", "{}\n{}".format( user, inmessage) except: traceback.print_exc()
"epid": "SC" + util.randHex(3), "expires": "360" } # Open the connection C = TCPClient(parameters["sourceIP"], parameters["sourcePort"]) C.connect(parameters["dest_ip"], parameters["dest_port"]) # Register m = prepareMessage(message["Register_1"], parameters) print(m) C.send(m) inmessage = C.waitForData() response = parseBytes(inmessage) print(response) assert response.status == "200 OK" # Unregister parameters["expires"] = "0" m = prepareMessage(message["Register_1"], parameters) print(m) C.send(m) inmessage = C.waitForData() response = parseBytes(inmessage) print(response) assert response.status == "200 OK"
def WaitForCall(user): " Start an agent. Wait for INVITE messages" L = link[user] expectedMessage = "INVITE" while L: # Will stop when we set the link of the user to None try: inBytes = L.waitForData() inmessage = parseBytes(inBytes) assert inmessage.type==expectedEvent,\ "User {} expected {} but got {}".format(user,expectedEvent,inmessage.event) #print("User:{} received {}".format(user,inmessage)) except timeout: pass finally: L = link[user] try: data.parameters["userB"] = user data.parameters["source_port"] = link[user].port m = buildMessage(message["Trying_1"], data.parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = inmessageb[h] #print(m) link[user].send(m.contents()) data.parameters["source_port"] = link[user].port Ringing = buildMessage(message["Ringing_1"], data.parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): Ringing[h] = inmessageb[h] toTag = ";tag=" + util.randStr(8) Ringing["To"] = Ringing["To"] + toTag #print(Ringing) link[user].send(Ringing.contents()) data.parameters["source_port"] = link[user].port m = buildMessage(message["200_OK_SDP_1"], data.parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Ringing[h] m["To"] = m["To"] + toTag #print(m) link[user].send(m.contents()) inBytes = link[user].waitForData() ack = parseBytes(inBytes) #print("IN:",ack) assert ack.type=="Request" and ack.method=="ACK",\ "Sent:\n{}Received:\n{}".format(m,ack) inBytes = link[user].waitForData() Bye = parseBytes(inBytes) #print("IN:",Bye) assert Bye.type=="Request" and Bye.method=="BYE",\ "A side sent:\n{}B side received:\n{}".format(m,inmessage) data.parameters["source_port"] = link[user].port m = buildMessage(message["200_OK_1"], data.parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Bye[h] #print(m) link[user].send(m.contents()) finally: # When call is done wait for next call AgentState(user)
def flow(users): usera = next(users) userb = next(users) parameters["userA"] = usera parameters["userB"] = userb serverThreads.append( util.serverThread(WaitForCstaEvents, usera, assertLeg="A")) serverThreads.append( util.serverThread(WaitForCstaEvents, userb, assertLeg="B")) parameters["source_port"] = link[usera].port Invite = buildMessage(message["Invite_SDP_1"], parameters) # print(Invite) link[usera].send(Invite.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) # print("IN:",inmessage) assert inmessage.type == "Response" and inmessage.status == "100 Trying", \ "Sent:\n{}Received:\n{}".format(Invite, inmessage) # inBytes=linkCsta[usera].waitForData() # cstaevent=parseBytes_csta(inBytes) # assert cstaevent.event=="ServiceInitiated", "Expected:{} Received:{}".format("ServiceInitiated",str(cstaevent)) inBytes = link[userb].waitForData() inmessageb = parseBytes(inBytes) # print("IN:",inmessageb) assert inmessageb.type == "Request" and inmessageb.method == "INVITE", \ "A side sent:\n{}and got Trying, but B side received:\n{}".format(Invite, inmessage) parameters["source_port"] = link[userb].port m = buildMessage(message["Trying_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = inmessageb[h] # print(m) link[userb].send(m.contents()) parameters["source_port"] = link[userb].port Ringing = buildMessage(message["Ringing_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): Ringing[h] = inmessageb[h] toTag = ";tag=" + util.randStr(8) Ringing["To"] = Ringing["To"] + toTag # print(Ringing) link[userb].send(Ringing.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) # print("IN:",inmessage) assert inmessage.type == "Response" and inmessage.status == "180 Ringing", \ "B side sent:\n{}but A side received:\n{}".format(Ringing, inmessage) parameters["source_port"] = link[userb].port m = buildMessage(message["200_OK_SDP_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Ringing[h] m["To"] = m["To"] + toTag # print(m) link[userb].send(m.contents()) inBytes = link[userb].waitForData() ack = parseBytes(inBytes) # print("IN:",ack) assert ack.type == "Request" and ack.method == "ACK", \ "Sent:\n{}Received:\n{}".format(m, ack) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) # print("IN:",inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK", \ "B side got:\n{}but A side received:\n{}".format(ack, inmessage) parameters["source_port"] = link[usera].port m = buildMessage(message["Ack_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] # print(m) link[usera].send(m.contents()) sleep(talkDuration) parameters["source_port"] = link[usera].port m = buildMessage(message["Bye_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] # print(m) link[usera].send(m.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) # print("IN:",inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK", \ "Sent:\n{}Received:\n{}".format(m, inmessage) inBytes = link[userb].waitForData() Bye = parseBytes(inBytes) # print("IN:",Bye) assert Bye.type == "Request" and Bye.method == "BYE", \ "A side sent:\n{}B side received:\n{}".format(m, inmessage) parameters["source_port"] = link[userb].port m = buildMessage(message["200_OK_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Bye[h] # print(m) link[userb].send(m.contents())
def WaitForCall(user, param): " Start an agent. Wait for INVITE messages" parameters = copy(param) parameters["number"] = 0 L = link[user] expectedMessage = "INVITE" while L: # Will stop when we set the link of the user to None try: inBytes = L.waitForData() invite = parseBytes(inBytes) assert invite.type=="Request" and invite.method==expectedMessage,\ "User {} expected {} but got:\n{}".format(user,expectedMessage,invite) #print("User:{} received {}".format(user,inmessage)) break except timeout: pass finally: L = link[user] if not L: return try: parameters["userB"] = user parameters["source_port"] = link[user].port m = buildMessage(message["Trying_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = invite[h] #print(m) link[user].send(m.contents()) Ringing = buildMessage(message["Ringing_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): Ringing[h] = invite[h] for h in ("Allow", "Allow-Events", "X-Siemens-Call-Type"): if h in invite.headers: Ringing[h] = invite[h] toTag = ";tag=" + util.randStr(8) Ringing["To"] = Ringing["To"] + toTag #print(Ringing) link[user].send(Ringing.contents()) sleep(0.5) m = buildMessage(message["200_OK_SDP_1"], parameters) # Preserve Content-Length bkp = m["Content-Length"] for h in Ringing.headers: #("To", "From", "CSeq","Via","Call-ID","Contact"): m[h] = Ringing[h] m["Content-Length"] = bkp link[user].send(m.contents()) inBytes = link[user].waitForData() ack = parseBytes(inBytes) #print("IN:",ack) assert ack.type=="Request" and ack.method=="ACK",\ "Sent:\n{}Received:\n{}".format(m,ack) nobye = True acceptable = ("BYE", "UPDATE") while nobye: inBytes = link[user].waitForData(2 * parameters["talkDuration"]) inmessage = parseBytes(inBytes) nobye = not (inmessage.type == "Request" and inmessage.method == "BYE") assert inmessage.type=="Request" and inmessage.method in acceptable,\ "Expected one of {} but received :\n{}".format(str(acceptable),inmessage) m = buildMessage(message["200_OK_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = inmessage[h] #print(m) link[user].send(m.contents()) except: traceback.print_exc() finally: # When call is done wait for next call del parameters WaitForCall(user, param)
def flow(): parameters["userA"] = usera parameters["userB"] = userb parameters["expires"] = "0" Invite = buildMessage(message["Invite_SDP_1"], parameters) print(Invite) link[usera].send(Invite.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) print("IN:", inmessage) assert inmessage.type == "Response" and inmessage.status == "100 Trying" inBytes = link[userb].waitForData() inmessageb = parseBytes(inBytes) print("IN:", inmessageb) assert inmessageb.type == "Request" and inmessageb.method == "INVITE" parameters["callId"] = inmessage["Call-ID"] m = buildMessage(message["Trying_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = inmessageb[h] print(m) link[userb].send(m.contents()) Ringing = buildMessage(message["Ringing_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): Ringing[h] = inmessageb[h] toTag = ";tag=" + util.randStr(8) Ringing["To"] = Ringing["To"] + toTag print(Ringing) link[userb].send(Ringing.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) print("IN:", inmessage) assert inmessage.type == "Response" and inmessage.status == "180 Ringing" m = buildMessage(message["200_OK_SDP_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Ringing[h] m["To"] = m["To"] + toTag print(m) link[userb].send(m.contents()) inBytes = link[userb].waitForData() inmessage = parseBytes(inBytes) print("IN:", inmessage) assert inmessage.type == "Request" and inmessage.method == "ACK" inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) print("IN:", inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK" m = buildMessage(message["Ack_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] print(m) link[usera].send(m.contents()) sleep(talkDuration) m = buildMessage(message["Bye_1"], parameters) for h in ("To", "From", "Call-ID"): m[h] = inmessage[h] print(m) link[usera].send(m.contents()) inBytes = link[usera].waitForData() inmessage = parseBytes(inBytes) print("IN:", inmessage) assert inmessage.type == "Response" and inmessage.status == "200 OK" inBytes = link[userb].waitForData() Bye = parseBytes(inBytes) print("IN:", Bye) assert Bye.type == "Request" and Bye.method == "BYE" m = buildMessage(message["200_OK_1"], parameters) for h in ("To", "From", "CSeq", "Via", "Call-ID"): m[h] = Bye[h] print(m) link[userb].send(m.contents())
def wait_for_message(self, message_type, dialog=None, ignore_messages=(), link=None, timeout=5.0): """ Wait for a specific type of SIP message. :param message_type: is a string that we will make sure is contained in the received message request or response line Set this to None or "" to accept any incoming message if a list or tuple is given any of the contained message types will be accepted :param dialog: Set the dialog to expect the message in. If None will expect a message in current dialog :param ignore_messages: a list of message_types to ignore if they come before the expected message_type :param timeout: Defined timeout in seconds. :return: A SipMessage constructed from the incoming message """ if not link: link = self.link if not dialog: dialog = self.current_dialog else: dialog = self.get_complete_dialog(dialog) inmessage = None last_sent_message = self.get_last_message_in(dialog) if last_sent_message: transaction = last_sent_message.get_transaction() len_buffer = len(self.message_buffer) count = 0 while not inmessage: if count < len_buffer: # first get a message from the buffer inmessage = self.get_buffered_message(dialog) count += 1 if not inmessage: # no (more) buffered messages. try the network inbytes = self.link.waitForSipData(timeout=timeout, client=link) inmessage = self.handleDA(last_sent_message, parseBytes(inbytes)) inmessage_type = inmessage.get_status_or_method() inmessage_dialog = inmessage.get_dialog() inmessage.cseq_method = inmessage.get_transaction()["method"] if inmessage_type in ignore_messages: inmessage = None continue if message_type and \ ((isinstance(message_type, str) and message_type not in inmessage_type) or (type(message_type) in (list, tuple) and not any([m in inmessage_type for m in message_type])) or (inmessage.type == "Response" and inmessage.cseq_method != transaction["method"])): # we have received an unexpected message. buffer it if there is an active dialog for it if inmessage_dialog in self.dialogs or inmessage_type == "INVITE": # message is part of another active dialog or a new call, so buffer it self.message_buffer.append(inmessage) # print("Appended {} with {} to buffer. Will keep waiting for {} in {} ".format(inmessage_type, # inmessage_dialog, # message_type, # dialog)) inmessage = None else: d = ["sip:{}@".format(line.number) in inmessage["To"] for line in self.secondary_lines] if any(d): # message is meant for another line in this device self.secondary_lines[d.index(True)].message_buffer.append(inmessage) inmessage = None else: raise AssertionError('{}: Got "{}" in {} while expecting "{}" in {}. ' 'Other active dialogs:{}.'.format(self.number, inmessage_type, inmessage_dialog, message_type, dialog, self.dialogs)) self.save_message(inmessage) if inmessage.type == "Request": self.set_transaction(inmessage.get_transaction()) else: assert inmessage.cseq_method == transaction["method"], \ "Got {} to {} instead of {} to {}".format(inmessage.get_status_or_method(), inmessage.cseq_method, message_type, transaction["method"]) self.update_to_tag(inmessage.get_dialog()) self.set_dialog(inmessage.get_dialog()) return inmessage