def getResponse(self, buff, srcaddr): srcip, srcport, *_ = srcaddr uaname = 'unknown' buff = buff.decode('utf-8', 'ignore') if buff.startswith('OPTIONS ') \ or buff.startswith('INVITE ') \ or buff.startswith('REGISTER '): if self.externalip == srcip: self.log.debug("We received our own packet from %s:%s" % (str(srcip), srcport)) else: self.log.info( "Looks like we received a SIP request from %s:%s" % (str(srcip), srcport)) self.log.debug(buff.__repr__()) return self.log.debug("running fingerPrintPacket()") res = fingerPrintPacket(buff) if res is not None: if 'name' in res: uaname = res['name'][0] else: uaname = 'unknown' self.log.debug(buff.__repr__()) if not self.fpworks: fp = None if fp is None: if self.fpworks: fpname = 'unknown' else: fpname = 'disabled' else: fpname = ' / '.join(fp) self.log.debug('Fingerprint: %s' % fpname) self.log.debug("Uaname: %s" % uaname) #print buff originaldst = getTag(buff) try: dstip = socket.inet_ntoa(pack('!L', int(originaldst[:8], 16))) dstport = int(originaldst[8:12], 16) except (ValueError, TypeError, socket.error): self.log.debug( "original destination could not be decoded: %s" % (originaldst)) dstip, dstport = 'unknown', 'unknown' resultstr = '%s:%s\t->\t%s:%s\t->\t%s' % (dstip, dstport, srcip, srcport, uaname) self.log.info(resultstr) self.resultip['%s:%s' % (srcip, srcport)] = '%s:%s' % (dstip, dstport) self.resultua['%s:%s' % (srcip, srcport)] = uaname if self.sessionpath is not None and self.dbsyncs: self.resultip.sync() self.resultua.sync() else: self.log.info('Packet from %s:%s did not contain a SIP msg' % srcaddr) self.log.debug('Packet: %s' % buff.__repr__())
def getResponse(self): # we got stuff to read off the socket buff, srcaddr = self.sock.recvfrom(8192) if self.printdebug: print(srcaddr) print(buff) buff = buff.decode('utf-8') try: extension = getTag(buff).decode('utf-8', 'ignore') except (TypeError, AttributeError): self.log.error('could not decode to tag') extension = None if extension is None: self.nomore = True return try: firstline = buff.splitlines()[0] except (ValueError, IndexError, AttributeError): self.log.error("could not get the 1st line") return if self.enableack: # send an ack to any responses which match _tmp = parseHeader(buff) if not (_tmp and 'code' in _tmp): return if 699 > _tmp['code'] >= 200: self.log.debug('will try to send an ACK response') if 'headers' not in _tmp: self.log.debug('no headers?') return if 'from' not in _tmp['headers']: self.log.debug('no from?') return if 'cseq' not in _tmp['headers']: self.log.debug('no cseq') return if 'call-id' not in _tmp['headers']: self.log.debug('no caller id') return try: # _tmp['headers']['from'][0].split('"')[1] username = getTag(buff) except IndexError: self.log.warning('could not parse the from address %s' % _tmp[ 'headers']['from']) username = '******' cseq = _tmp['headers']['cseq'][0] cseqmethod = cseq.split()[1] if 'INVITE' == cseqmethod: cid = _tmp['headers']['call-id'][0] fromaddr = _tmp['headers']['from'][0] toaddr = _tmp['headers']['to'][0] ackreq = self.createRequest('ACK', cid=cid, cseq=cseq.replace( cseqmethod, ''), fromaddr=fromaddr, toaddr=toaddr, ) self.log.debug('here is your ack request: %s' % ackreq) mysendto(self.sock, ackreq, (self.dsthost, self.dstport)) # self.sock.sendto(ackreq,(self.dsthost,self.dstport)) if _tmp['code'] == 200: byemsg = self.createRequest('BYE', cid=cid, cseq='2', fromaddr=fromaddr, toaddr=toaddr, ) self.log.debug( 'sending a BYE to the 200 OK for the INVITE') mysendto(self.sock, byemsg, (self.dsthost, self.dstport)) if firstline != self.BADUSER: if buff.startswith(self.PROXYAUTHREQ) \ or buff.startswith(self.INVALIDPASS) \ or buff.startswith(self.AUTHREQ): if self.realm is None: self.realm = getRealm(buff) self.log.info( "extension '%s' exists - requires authentication" % extension) self.resultauth[extension] = 'reqauth' if self.sessionpath is not None and self.dbsyncs: self.resultauth.sync() elif buff.startswith(self.TRYING): pass elif buff.startswith(self.RINGING): pass elif buff.startswith(self.OKEY): self.log.info( "extension '%s' exists - authentication not required" % extension) self.resultauth[extension] = 'noauth' if self.sessionpath is not None and self.dbsyncs: self.resultauth.sync() else: self.log.warning( "extension '%s' probably exists but the response is unexpected" % extension) self.log.debug("response: %s" % firstline) self.resultauth[extension] = 'weird' if self.sessionpath is not None and self.dbsyncs: self.resultauth.sync() elif buff.startswith(self.NOTFOUND): self.log.debug("User '%s' not found" % extension) elif buff.startswith(self.INEXISTENTTRANSACTION): pass # Prefix not found, lets go to the next one. Should we add a warning # here??? elif buff.startswith(self.SERVICEUN): pass elif buff.startswith(self.TRYING): pass elif buff.startswith(self.RINGING): pass elif buff.startswith(self.OKEY): pass elif buff.startswith(self.DECLINED): pass elif buff.startswith(self.NOTALLOWED): self.log.warning("method not allowed") self.nomore = True return elif buff.startswith(self.BADREQUEST): self.log.error( "Protocol / interopability error! The remote side most probably has problems with parsing your SIP messages!") self.nomore = True return else: self.log.warning("We got an unknown response") self.log.error("Response: %s" % buff.__repr__()) self.log.debug("1st line: %s" % firstline.__repr__()) self.log.debug("Bad user: %s" % self.BADUSER.__repr__()) self.nomore = True