示例#1
0
 def handle_vclient_listen(self, s):
     chunk = s.recv(1024)
     if chunk == '':
         print "UDP error", s
         sys.exit(1)
     else:
         logger.debug('recv from vclient ' + chunk)
         msg = DTNMessage()
         msg.handle(chunk, self.server_sh)
         msg.src = self.sh
         if msg.type == 'PING':
             # Notify Monitors
             self.notify_monitors(chunk)
             # Save into database
             try:
                 self.db.insert_msg(msg)
             except:
                 logger.debug('error in inserting PING')
示例#2
0
 def handle_vclient_listen(self, s):
     chunk = s.recv(1024)
     if chunk == '':
         print "UDP error", s
         sys.exit(1)
     else:
         logger.debug('recv from vclient ' + chunk)
         msg = DTNMessage()
         msg.handle(chunk, self.server_sh)
         msg.src = self.sh
         if msg.type == 'PING':
             # Notify Monitors
             self.notify_monitors(chunk)
             # Save into database
             try:
                 self.db.insert_msg(msg)
             except:
                 logger.debug('error in inserting PING')
示例#3
0
    def select_msg(self, where):
        msgs = self.select('select hash, time, ip, port, src, dst, type, data, ttl, id from data where %s' % where)

        res = list()
        for msg in msgs:
            m = DTNMessage()
            m.hash = msg[0]
            m.time = msg[1]
            m.ip = msg[2]
            m.port = msg[3]
            m.src = msg[4]
            m.dst = msg[5]
            m.type = msg[6]
            m.data = msg[7]
            m.ttl = msg[8]
            m.id = msg[9]

            res.append(m)
        return res
示例#4
0
    def _recv_handle(self, msg):
        dtn_msg = DTNMessage()
        if not dtn_msg.handle(msg):
            logger.debug('cannot recognize message ' + msg)
            self._send(self.conn_recv, UNKNOWN_MSG)
            return

        if dtn_msg.type == 'DST_ACK':
            self.sm.db.insert_dst_ack(dtn_msg)
            # set the match data ack=1
            self.sm.db.update('ack=1', "hash='%s'" % dtn_msg.data)
        else:
            self.sm.db.insert_msg(dtn_msg)

        logger.debug('recv %s : %s' % (dtn_msg.type, msg))

        # Any messages but ACK should send ACK
        if dtn_msg.type != 'ACK':
            self._send(self.conn_recv, 'ACK ' + dtn_msg.hash)
            logger.debug('send ACK ' + dtn_msg.hash)
        else:  # ACK
            return

        # Message reaches destination
        if dtn_msg.dst == self.my_sh:
            self.sm.db.update('ack=1', "hash='%s'" % dtn_msg.hash)

            # New DST_ACK message
            if dtn_msg.type != 'DST_ACK':

                dst_msg = DTNMessage()
                dst_msg.time = int(time.time() * 1000)
                dst_msg.ack = 1
                dst_msg.ip = self.sm.my_ip
                dst_msg.port = self.sm.dtn_port
                dst_msg.dst = dtn_msg.src
                dst_msg.src = self.my_sh
                dst_msg.type = 'DST_ACK'
                dst_msg.data = dtn_msg.hash
                dst_msg.hash = dst_msg.get_hash()

                self.sm.db.insert_dst_ack(dst_msg)

                self.msgs.append(dst_msg)

            # CMD
            if dtn_msg.type == 'CMD':

                # Run
                cmd = dtn_msg.data
                logger.debug('recv CMD : %s', cmd)
                import commands
                res = commands.getstatusoutput(cmd)
                logger.debug('CMD result :' + str(res))

                # Send CMD_RES
                res_msg = DTNMessage()
                res_msg.time = int(time.time() * 1000)
                res_msg.ack = 0
                res_msg.ip = self.sm.my_ip
                res_msg.port = self.sm.dtn_port
                res_msg.dst = dtn_msg.src
                res_msg.src = self.my_sh
                res_msg.type = 'CMD_RES'
                res_msg.data = str(res)
                res_msg.hash = res_msg.get_hash()

                self.sm.db.insert_msg(res_msg)

                self.msgs.append(res_msg)

        return True
示例#5
0
    def _recv_handle(self, msg):
        dtn_msg = DTNMessage()
        if not dtn_msg.handle(msg):
            logger.debug('cannot recognize message ' + msg)
            self._send(self.conn_recv, UNKNOWN_MSG)
            return 

        if dtn_msg.type == 'DST_ACK':
            self.sm.db.insert_dst_ack(dtn_msg)
            # set the match data ack=1
            self.sm.db.update('ack=1', "hash='%s'"%dtn_msg.data)
        else:
            self.sm.db.insert_msg(dtn_msg)

        logger.debug('recv %s : %s' % (dtn_msg.type, msg))

        # Any messages but ACK should send ACK
        if dtn_msg.type != 'ACK':
            self._send(self.conn_recv, 'ACK ' + dtn_msg.hash)
            logger.debug('send ACK ' + dtn_msg.hash)
        else: # ACK
            return

        # Message reaches destination
        if dtn_msg.dst == self.my_sh:
            self.sm.db.update('ack=1', "hash='%s'"%dtn_msg.hash)

            # New DST_ACK message
            if dtn_msg.type != 'DST_ACK':

                dst_msg = DTNMessage()
                dst_msg.time = int(time.time()*1000)
                dst_msg.ack = 1
                dst_msg.ip = self.sm.my_ip
                dst_msg.port = self.sm.dtn_port
                dst_msg.dst = dtn_msg.src
                dst_msg.src = self.my_sh
                dst_msg.type = 'DST_ACK'
                dst_msg.data = dtn_msg.hash
                dst_msg.hash = dst_msg.get_hash()

                self.sm.db.insert_dst_ack(dst_msg)

                self.msgs.append(dst_msg)

            # CMD 
            if dtn_msg.type == 'CMD':

                # Run
                cmd = dtn_msg.data
                logger.debug('recv CMD : %s', cmd)
                import commands
                res = commands.getstatusoutput(cmd)
                logger.debug('CMD result :' + str(res))

                # Send CMD_RES
                res_msg = DTNMessage()
                res_msg.time = int(time.time()*1000)
                res_msg.ack = 0
                res_msg.ip = self.sm.my_ip
                res_msg.port = self.sm.dtn_port
                res_msg.dst = dtn_msg.src
                res_msg.src = self.my_sh
                res_msg.type = 'CMD_RES'
                res_msg.data = str(res)
                res_msg.hash = res_msg.get_hash()

                self.sm.db.insert_msg(res_msg)

                self.msgs.append(res_msg)

        return True