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
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
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
def collectpackets(dstaddrs,localport,fromaddr,toaddr,bindingip,selecttime=0.005,noisy=False,samples=1000,method='OPTIONS',localip=None): import logging, socket, select, random from time import sleep from sys import stdout log = logging.getLogger('collectpackets') s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.settimeout(2) bindresult = bindto(bindingip,localport,s) if bindresult is None: sys.exit(1) localport,s = bindresult if localip is None: log.debug("local ip was not set") if bindingip != '0.0.0.0': log.debug("but bindingip was set! we'll set it to the binding ip") localip = bindingip else: try: log.info("trying to get self ip .. might take a while") localip = socket.gethostbyname(socket.gethostname()) log.info("ok got %s" % localip) except socket.error: localip = '127.0.0.1' else: log.debug("external ip was set") myaddr = ':'.join((localip,str(localport))) externalip = bindingip rlist = [s] wlist = list() xlist = list() i = 0 while samples > i: for dstaddr in dstaddrs: r, w, e = select.select( rlist, wlist, xlist, selecttime ) if r: try: buff,srcaddr = s.recvfrom(8192) if not buff.startswith('SIP/2.0 10'): log.debug('got data from %s:%s' % srcaddr) log.debug('data: %s' % `buff`) responses.append(buff) if noisy: stdout.write(".") stdout.flush() except socket.error: continue else: if samples < i: break i += 1 branchunique = '%s' % random.getrandbits(32) callid = '%s' % random.getrandbits(80) localtag = '%s%s' % (''.join(map(lambda x: '%02x' % int(x), \ socket.gethostbyname(dstaddr[0]).split('.'))),'%04x' % dstaddr[1]) request = makeRequest( method, fromaddr, toaddr, dstaddr[0], dstaddr[1], callid, externalip, branchunique, localtag=localtag, accept='application/sdp', localport=localport, ) log.debug("sending request: %s" % `request`) if noisy: stdout.write("_") stdout.flush() s.sendto(request,dstaddr) try: # having the final sip log.debug("Making sure that no packets get lost") log.debug("Come to daddy") while 1: buff,srcaddr = s.recvfrom(8192) if not buff.startswith('SIP/2.0 10'): log.debug('got data from %s:%s' % srcaddr) log.debug('data: %s' % `buff`) responses.append(buff) if noisy: stdout.write(".") stdout.flush() except socket.error: return