Esempio n. 1
0
    def __init__(self,
                 sock,
                 messages,
                 peers,
                 start_time=None,
                 isVerbose=False,
                 msg_freq=1,
                 wait_time=20,
                 gtp_port=2123):
        threading.Thread.__init__(self)

        self.TAG_NAME = 'GTP SENDER'
        self.is_verbose = isVerbose

        self.sock = sock

        self.start_time = start_time
        self.msg_freq = msg_freq
        self.wait_time = wait_time
        self.peers = IP(peers)
        self.gtp_port = gtp_port
        self.messages = []
        if messages is None or len(messages) == 0:
            logErr("no messages", TAG=self.TAG_NAME)
            return

        if not isinstance(messages, list):
            self.messages = [messages]
        else:
            self.messages = messages
Esempio n. 2
0
    def run(self):
        self.is_running = True

        logNormal("--: Acting as SENDER :--",
                  verbose=self.is_verbose,
                  TAG=self.TAG_NAME)

        if self.is_running and self.sock is not None:
            ''' Prepare the messages to send '''

            logNormal("Preparing GTP messages",
                      verbose=self.is_verbose,
                      TAG=self.TAG_NAME)
            mdatas = []
            if self.messages is not None and len(self.messages) > 0:
                count = 0
                for m in self.messages:
                    logNormal("preparing msg #%d - type %d" %
                              (count, m.get_msg_type()),
                              verbose=self.is_verbose,
                              TAG=self.TAG_NAME)
                    mdatas.append(m.get_message())
                    count += 1
            tot_count = len(mdatas)
            logOk("Prepared %d GTP messages" % (tot_count),
                  verbose=self.is_verbose,
                  TAG=self.TAG_NAME)

            curr_count = 1

            for num, data in enumerate(mdatas):
                logNormal("Sending message (#%d of %d)..." %
                          (curr_count, tot_count),
                          verbose=self.is_verbose,
                          TAG=self.TAG_NAME)
                for ip in self.peers:
                    try:
                        ip_str = ip.strNormal()

                        msg_info = {'reply': 0}
                        msg_type = self.messages[num].get_msg_type()
                        if msg_type == 32 or msg_type == 34:
                            msg_info['local_teid'] = self.messages[
                                num].get_fteid()

                        if not message_queue.has_key(ip_str) or \
                            not message_queue[ip_str].has_key(msg_type):
                            message_queue[ip_str] = {}
                            message_queue[ip_str][msg_type] = [msg_info]
                        else:
                            message_queue[ip_str][msg_type].append(msg_info)

                        sent_bytes = self.sock.sendto(data,
                                                      (ip_str, self.gtp_port))

                        if sent_bytes is not None and sent_bytes > 0:
                            info_msg = "Bytes sent to %s %d" % (ip_str,
                                                                sent_bytes)
                        else:
                            info_msg = "NO bytes sent to %s" % (ip_str)
                        logNormal(info_msg,
                                  verbose=self.is_verbose,
                                  TAG=self.TAG_NAME)
                    except timeout, e:
                        logErr("%s TIMEOUT_ERROR" % (ip_str),
                               TAG=self.TAG_NAME)
                        pass
                    except error, e:
                        if e.errno == errno.ECONNREFUSED:
                            logErr("%s CONNECTION_REFUSED" % (ip_str),
                                   TAG=self.TAG_NAME)
                        elif e.errno == errno.EBADFD:
                            logErr("%s BAD_FILE_DESCRIPTOR_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                            break
                        elif e.errno == errno.EPIPE:
                            logErr("%s BROKEN_PIPE_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                            break
                        elif e.errno == errno.ECONNRESET:
                            logErr("%s CONNECTION_RESET_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                        else:
                            logErr("%s UNKNOWN_ERROR: %s" % (ip_str, e),
                                   TAG=self.TAG_NAME)
                            break
                        pass
                    except Exception, e:
                        logErr("%s GENERIC ERROR : reason %s" % (ip_str, e),
                               TAG=self.TAG_NAME)
                        break
Esempio n. 3
0
class Listener(threading.Thread):
    '''
    classdocs
    '''
    def __init__(self, open_sock, isVerbose=True):
        threading.Thread.__init__(self)

        self.TAG_NAME = 'GTP LISTENER'

        self.sock = open_sock
        self.is_verbose = isVerbose
        self.is_running = False

    ##
    ## @brief      Determines if the thread is running
    ##
    ## @param      self  refers to the class itself
    ##
    ## @return     True if running, False otherwise.
    ##
    def isRunning(self):
        return self.is_running

    def __getFTEID(self, data):
        if data is None or len(data) == 0:
            raise Exception("%s: invalid data" % self.TAG_NAME)
        i = 0
        teid = 0x00
        while i < len(data):
            (ie_type, ie_len,
             spare_instance) = struct.unpack("!BHB", data[i:i + 4])
            if ie_type != 87:
                i += (4 + ie_len)
            else:
                teid = struct.unpack("!L", data[i + 5:i + 9])[0]
                break
        return format(teid, '#08X')

    ##
    ## @brief      Starts the execution of the thread
    ##
    ## @param      self  refers to the class itself
    ##
    def run(self):
        self.is_running = True
        count = 0
        while self.sock is not None and self.is_running:
            try:
                data, addr = self.sock.recvfrom(1024)
                if len(data) > 8:
                    (flags, resp_msg_type, length,
                     sequence_or_teid) = struct.unpack("!BBHL", data[:8])
                    version = flags & 0xF0
                    if version != 0x40:
                        logWarn("Unsupported GTP version %02x" % (version),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue
                    if not message_queue.has_key(addr[0]):
                        logWarn("Unmanaged IP %s" % (addr[0]),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue
                    req_msg_type = GTPResponse2Request[resp_msg_type]

                    if not message_queue[addr[0]].has_key(req_msg_type):
                        logWarn("Unsolicited response msg %d" %
                                (resp_msg_type),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue

                    logWarn("Received response to sent msg %s from ip %s" %
                            (GTPmessageTypeStr[req_msg_type], addr[0]),
                            verbose=self.is_verbose,
                            TAG=self.TAG_NAME)
                    if req_msg_type == GTPmessageTypeDigit["echo-request"]:
                        message_queue[addr[0]][req_msg_type][0]['reply'] = 1
                    else:
                        for elem in message_queue[addr[0]][req_msg_type]:
                            if elem.has_key('local_teid') and \
                                elem['local_teid'] ==  sequence_or_teid :
                                elem['reply'] = 1
                                elem['remote_teid'] = self.__getFTEID(
                                    data[12:])
                                break
                count += 1
                logNormal("RECEIVED #%d messages" % (count),
                          verbose=self.is_verbose,
                          TAG=self.TAG_NAME)
            except timeout, e:
                if addr[0]:
                    logErr("%s TIMEOUT_ERROR" % (addr[0]), TAG=self.TAG_NAME)
                else:
                    logErr("TIMEOUT_ERROR", TAG=self.TAG_NAME)
                pass
            except error, e:
                if e.errno == errno.EBADFD:
                    if addr[0]:
                        logErr("%s BAD_FILE_DESCRIPTOR_ERROR" % (addr[0]),
                               TAG=self.TAG_NAME)
                    else:
                        logErr("BAD_FILE_DESCRIPTOR_ERROR", TAG=self.TAG_NAME)
                    break
                elif e.errno == errno.EPIPE:
                    if addr[0]:
                        logErr("%s BROKEN_PIPE_ERROR" % (addr[0]),
                               TAG=self.TAG_NAME)
                    else:
                        logErr("BROKEN_PIPE_ERROR", TAG=self.TAG_NAME)
                    break
                else:
                    logErr("UNKNOWN ERROR: %s" % (e), TAG=self.TAG_NAME)
                    break
            except Exception, e:
                logErr("GENERIC ERROR: %s" % (e), TAG=self.TAG_NAME)
                break
Esempio n. 4
0
    def run(self):
        self.is_running = True
        count = 0
        while self.sock is not None and self.is_running:
            try:
                data, addr = self.sock.recvfrom(1024)
                if len(data) > 8:
                    (flags, resp_msg_type, length,
                     sequence_or_teid) = struct.unpack("!BBHL", data[:8])
                    version = flags & 0xF0
                    if version != 0x40:
                        logWarn("Unsupported GTP version %02x" % (version),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue
                    if not message_queue.has_key(addr[0]):
                        logWarn("Unmanaged IP %s" % (addr[0]),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue
                    req_msg_type = GTPResponse2Request[resp_msg_type]

                    if not message_queue[addr[0]].has_key(req_msg_type):
                        logWarn("Unsolicited response msg %d" %
                                (resp_msg_type),
                                verbose=self.is_verbose,
                                TAG=self.TAG_NAME)
                        continue

                    logWarn("Received response to sent msg %s from ip %s" %
                            (GTPmessageTypeStr[req_msg_type], addr[0]),
                            verbose=self.is_verbose,
                            TAG=self.TAG_NAME)
                    if req_msg_type == GTPmessageTypeDigit["echo-request"]:
                        message_queue[addr[0]][req_msg_type][0]['reply'] = 1
                    else:
                        for elem in message_queue[addr[0]][req_msg_type]:
                            if elem.has_key('local_teid') and \
                                elem['local_teid'] ==  sequence_or_teid :
                                elem['reply'] = 1
                                elem['remote_teid'] = self.__getFTEID(
                                    data[12:])
                                break
                count += 1
                logNormal("RECEIVED #%d messages" % (count),
                          verbose=self.is_verbose,
                          TAG=self.TAG_NAME)
            except timeout, e:
                if addr[0]:
                    logErr("%s TIMEOUT_ERROR" % (addr[0]), TAG=self.TAG_NAME)
                else:
                    logErr("TIMEOUT_ERROR", TAG=self.TAG_NAME)
                pass
            except error, e:
                if e.errno == errno.EBADFD:
                    if addr[0]:
                        logErr("%s BAD_FILE_DESCRIPTOR_ERROR" % (addr[0]),
                               TAG=self.TAG_NAME)
                    else:
                        logErr("BAD_FILE_DESCRIPTOR_ERROR", TAG=self.TAG_NAME)
                    break
                elif e.errno == errno.EPIPE:
                    if addr[0]:
                        logErr("%s BROKEN_PIPE_ERROR" % (addr[0]),
                               TAG=self.TAG_NAME)
                    else:
                        logErr("BROKEN_PIPE_ERROR", TAG=self.TAG_NAME)
                    break
                else:
                    logErr("UNKNOWN ERROR: %s" % (e), TAG=self.TAG_NAME)
                    break
Esempio n. 5
0
    def run(self):
        self.is_running = True

        logNormal("--: Acting as SENDER :--",
                  verbose=self.is_verbose,
                  TAG=self.TAG_NAME)

        if self.is_running and self.sock is not None:
            ''' Prepare the messages to send '''

            logNormal("Preparing GTP messages",
                      verbose=self.is_verbose,
                      TAG=self.TAG_NAME)
            mdatas = []
            if self.messages is not None and len(self.messages) > 0:
                count = 0
                for m in self.messages:
                    logNormal("preparing msg #%d - type %d" %
                              (count, m.get_msg_type()),
                              verbose=self.is_verbose,
                              TAG=self.TAG_NAME)
                    mdatas.append(m.get_message())
                    count += 1
            tot_count = len(mdatas)
            logOk("Prepared %d GTP messages" % (tot_count),
                  verbose=self.is_verbose,
                  TAG=self.TAG_NAME)

            curr_count = 1

            for num, data in enumerate(mdatas):
                logNormal("Sending message (#%d of %d)..." %
                          (curr_count, tot_count),
                          verbose=self.is_verbose,
                          TAG=self.TAG_NAME)
                for ip in self.peers:
                    try:
                        ip_str = ip.strNormal()

                        msg_info = {'reply': 0}
                        msg_type = self.messages[num].get_msg_type()
                        if msg_type == 32 or msg_type == 34:
                            msg_info['local_teid'] = self.messages[
                                num].get_fteid()

                        if ip_str not in message_queue or \
                            msg_type not in message_queue[ip_str]:
                            message_queue[ip_str] = {}
                            message_queue[ip_str][msg_type] = [msg_info]
                        else:
                            message_queue[ip_str][msg_type].append(msg_info)

                        sent_bytes = self.sock.sendto(data,
                                                      (ip_str, self.gtp_port))

                        if sent_bytes is not None and sent_bytes > 0:
                            info_msg = "Bytes sent to %s %d" % (ip_str,
                                                                sent_bytes)
                        else:
                            info_msg = "NO bytes sent to %s" % (ip_str)
                        logNormal(info_msg,
                                  verbose=self.is_verbose,
                                  TAG=self.TAG_NAME)
                    except timeout as e:
                        logErr("%s TIMEOUT_ERROR" % (ip_str),
                               TAG=self.TAG_NAME)
                        pass
                    except error as e:
                        if e.errno == errno.ECONNREFUSED:
                            logErr("%s CONNECTION_REFUSED" % (ip_str),
                                   TAG=self.TAG_NAME)
                        elif e.errno == errno.EBADFD:
                            logErr("%s BAD_FILE_DESCRIPTOR_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                            break
                        elif e.errno == errno.EPIPE:
                            logErr("%s BROKEN_PIPE_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                            break
                        elif e.errno == errno.ECONNRESET:
                            logErr("%s CONNECTION_RESET_ERROR" % (ip_str),
                                   TAG=self.TAG_NAME)
                        else:
                            logErr("%s UNKNOWN_ERROR: %s" % (ip_str, e),
                                   TAG=self.TAG_NAME)
                            break
                        pass
                    except Exception as e:
                        logErr("%s GENERIC ERROR : reason %s" % (ip_str, e),
                               TAG=self.TAG_NAME)
                        break
                curr_count += 1
                time.sleep(self.msg_freq)

            if self.start_time is not None:
                stop_time = time.time()
                hours, rem = divmod(stop_time - self.start_time, 3600)
                minutes, seconds = divmod(rem, 60)
                logOk("Elapsed time: {:0>2}:{:0>2}:{:05.4f}".format(
                    int(hours), int(minutes), seconds),
                      verbose=self.is_verbose,
                      TAG=self.TAG_NAME)