示例#1
0
 def Register(self, extension, remotehost, auth=None, cid=None):
     from svhelper import makeRequest
     from svhelper import createTag
     m = 'REGISTER'
     if cid is None:
         cid = '%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     cseq = 1
     localtag = str(random.getrandbits(32))
     contact = 'sip:%s@%s' % (extension, remotehost)
     if auth is not None:
         cseq = 2
         localtag = createTag(
             '%s:%s' % (self.auth['username'], self.auth['password']))
     register = makeRequest(
         m,
         '"%s" <sip:%s@%s>' % (extension, extension, self.domain),
         '"%s" <sip:%s@%s>' % (extension, extension, self.domain),
         self.domain,
         self.dstport,
         callid=cid,
         srchost=self.externalip,
         branchunique=branchunique,
         cseq=cseq,
         auth=auth,
         localtag=localtag,
         compact=self.compact,
         localport=self.localport)
     return register
示例#2
0
 def createRequest(self, m, username, auth=None, cid=None, cseq=1):
     from base64 import b64encode
     from svhelper import makeRequest
     from svhelper import createTag
     if cid is None:
         cid = '%s' % str(random.getrandbits(32))
     branchunique = '%s' % random.getrandbits(32)
     localtag = createTag(username)
     contact = 'sip:%s@%s' % (username, self.domain)
     request = makeRequest(
         m,
         '"%s"<sip:%s@%s>' % (username, username, self.domain),
         '"%s"<sip:%s@%s>' % (username, username, self.domain),
         self.domain,
         self.dstport,
         cid,
         self.externalip,
         branchunique,
         cseq,
         auth,
         localtag,
         self.compact,
         contact=contact,
         localport=self.localport,
         extension=username)
     return request
示例#3
0
    def start(self):
        from svhelper import makeRequest, createTag
        from svhelper import mysendto
        import socket
        # bind to 5060 - the reason is to maximize compatability with
        # devices that disregard the source port and send replies back
        # to port 5060
        self.log.debug("binding to %s:%s" % (self.bindingip, self.localport))
        while 1:
            if self.localport > 65535:
                self.log.critical("Could not bind to any port")
                return
            try:
                self.sock.bind((self.bindingip, self.localport))
                break
            except socket.error:
                self.log.debug("could not bind to %s" % self.localport)
                self.localport += 1
        if self.originallocalport != self.localport:
            self.log.warn(
                "could not bind to %s:%s - some process might already be listening on this port. Listening on port %s instead"
                % (self.bindingip, self.originallocalport, self.localport))
            self.log.info(
                "Make use of the -P option to specify a port to bind to yourself"
            )
        while 1:
            r, w, e = select.select(self.rlist, self.wlist, self.xlist,
                                    self.selecttime)
            if r:
                # we got stuff to read off the socket
                try:
                    buff, srcaddr = self.sock.recvfrom(8192)
                    self.log.debug('got data from %s:%s' % srcaddr)
                    self.log.debug('data: %s' % ` buff `)
                    if self.printdebug:
                        print srcaddr
                        print buff
                except socket.error:
                    continue
                self.getResponse(buff, srcaddr)
            else:
                # no stuff to read .. its our turn to send back something
                if self.nomoretoscan:
                    try:
                        # having the final sip
                        self.log.debug("Making sure that no packets get lost")
                        self.log.debug("Come to daddy")
                        while 1:
                            buff, srcaddr = self.sock.recvfrom(8192)
                            if self.printdebug:
                                print srcaddr
                                print buff
                            self.getResponse(buff, srcaddr)
                    except socket.error:
                        break
                try:
                    nextscan = self.scaniter.next()
                except StopIteration:
                    self.log.debug('no more hosts to scan')
                    self.nomoretoscan = True
                    continue
                dstip, dstport, method = nextscan
                self.nextip = dstip
                dsthost = (dstip, dstport)
                branchunique = '%s' % random.getrandbits(32)

                localtag = createTag('%s%s' % (''.join(
                    map(lambda x: '%02x' % int(x),
                        dsthost[0].split('.'))), '%04x' % dsthost[1]))
                cseq = 1
                fromaddr = '"%s"<%s>' % (self.fromname, self.fromaddr)
                toaddr = fromaddr
                callid = '%s' % random.getrandbits(80)
                contact = None
                if method != 'REGISTER':
                    contact = 'sip:%s@%s:%s' % (
                        self.extension, self.externalip, self.localport)
                data = makeRequest(method,
                                   fromaddr,
                                   toaddr,
                                   dsthost[0],
                                   dsthost[1],
                                   callid,
                                   self.externalip,
                                   branchunique,
                                   compact=self.compact,
                                   localtag=localtag,
                                   contact=contact,
                                   accept='application/sdp',
                                   localport=self.localport,
                                   extension=self.extension)
                try:
                    self.log.debug("sending packet to %s:%s" % dsthost)
                    self.log.debug("packet: %s" % ` data `)
                    mysendto(self.sock, data, dsthost)
                    self.sentpackets += 1
                    #self.sock.sendto(data,dsthost)
                    if self.sessionpath is not None:
                        if self.packetcount.next():
                            try:
                                f = open(
                                    os.path.join(self.sessionpath,
                                                 'lastip.pkl'), 'w')
                                pickle.dump(self.nextip, f)
                                f.close()
                                self.log.debug('logged last ip %s' %
                                               self.nextip)
                            except IOError:
                                self.log.warn(
                                    'could not log the last ip scanned')
                    if self.first is not None:
                        if self.sentpackets >= self.first:
                            self.log.info(
                                'Reached the limit to scan the first %s packets'
                                % self.first)
                            self.nomoretoscan = True
                except socket.error, err:
                    self.log.error(
                        "socket error while sending to %s:%s -> %s" %
                        (dsthost[0], dsthost[1], err))
                    pass