예제 #1
0
    def handle_rpccmd_message(self, context, dest_id, src_id, msg_type,
                              payload):
        # logger.debug("got RPCCMD message from src_id %s:\n%s" % (MPTN.ID_TO_STRING(src_id), MPTN.formatted_print([payload])))
        if not self._is_id_valid(dest_id):
            logger.error("invalid RPCCMD dest ID %X: not found in network" %
                         dest_id)
            return None

        if src_id != MPTN.MASTER_ID:
            logger.error("invalid RPCCMD src ID %X: should be master" % src_id)
            return None

        if payload is None:
            logger.error("packet RPCCMD should have the payload")
            return None

        try:
            request = self._rpc_protocol.parse_request(payload)
        except RPCError as e:
            response = e.error_respond()
        else:
            response = self._rpc_dispatcher.dispatch(request)

        if response is None:
            response = str(None)
        else:
            response = response.serialize()

        message = MPTN.create_packet_to_str(src_id, dest_id,
                                            MPTN.MPTN_MSGTYPE_RPCREP, response)
        MPTN.socket_send(context, src_id, message)
        return
예제 #2
0
 def rtping_forever(self):
     while True:
         message = MPTN.create_packet_to_str(MPTN.MASTER_ID, self._id,
                                             MPTN.MPTN_MSGTYPE_RTPING,
                                             self._nexthop_hash)
         MPTN.socket_send(None, MPTN.MASTER_ID, message)
         gevent.sleep(5)
예제 #3
0
 def send(self,dest_id,payload):
     src_id = self.mptnaddr
     msg_type = MPTN.MPTN_MSGTYPE_FWDREQ
     # print src_id
     message = MPTN.create_packet_to_str(dest_id, src_id, msg_type, payload)
     payload_length = len(message)
     p = struct.pack('11B', 0xAA,0x55,src_id&0xff,dest_id&0xff,(dest_id>>8)&0xff,(dest_id>>16)&0xff,(dest_id>>24)&0xff,self.port%256,self.port/256,1,payload_length)
     p = p+message
     self.transport.write(p,(self.gtwaddr,MPTN.MPTN_UDP_PORT))
예제 #4
0
 def requestID(self):
     dest_id = MPTN.MASTER_ID
     src_id = 0xffffffff
     msg_type = MPTN.MPTN_MSGTYPE_IDREQ
     message = MPTN.create_packet_to_str(dest_id, src_id, msg_type, ''.join(map(chr,self.uuid)))
     payload_length = len(message)
     address = self.host
     port = self.port
     p = struct.pack('11B', 0xAA,0x55,self.nodeid,address&0xff,(address>>8)&0xff,(address>>16)&0xff,(address>>24)&0xff,port%256,port/256,1,payload_length)
     p = p+message
     self.transport.write(p,(self.gtwaddr,MPTN.MPTN_UDP_PORT))
     self.state = 'WAITADDR'
예제 #5
0
    def handle_rtping_message(self, context, dest_id, src_id, msg_type,
                              payload):
        if payload is None:
            logger.error("RTPING should have the payload")
            return

        if dest_id != self._id:
            logger.error("RTPING dest_id should be me")
            return

        if not self._is_id_master(src_id):
            logger.error("RTPING src ID %X %s should be Master 0" %
                         (src_id, MPTN.ID_TO_STRING(src_id)))
            return

        if payload != self._nexthop_hash:
            logger.debug(
                "RTPING got different hash %s. mine is %s. need to update routing table"
                % (str(map(ord, payload)), str(map(ord, self._nexthop_hash))))
            message = MPTN.create_packet_to_str(MPTN.MASTER_ID, self._id,
                                                MPTN.MPTN_MSGTYPE_RTREQ, None)
            MPTN.socket_send(None, MPTN.MASTER_ID, message)
예제 #6
0
    def handle_gwdiscover_message(self, context, dest_id, src_id, msg_type,
                                  payload):
        if context.direction != MPTN.ONLY_FROM_TRANSPORT_INTERFACE:
            logger.error("GWDISCOVER cannot be from TCP Server")
            return

        if payload is not None:
            logger.error("GWDISCOVER should not have the payload")
            return

        if dest_id != MPTN.MPTN_MAX_ID:
            logger.error("GWDISCOVER dest_id should be 0xFFFFFFFF")
            return

        if not MPTN.IS_ID_IN_NETWORK(src_id, self._network):
            logger.error(
                "GWDISCOVER src ID %X %s does not belong to the network" %
                (src_id, MPTN.ID_TO_STRING(src_id)))
            return

        msg_type = MPTN.MPTN_MSGTYPE_GWOFFER
        message = MPTN.create_packet_to_str(dest_id, self._id, msg_type,
                                            uuid.uuid4().bytes)
        self._transport_if_send(self._get_address_from_id(src_id), message)
예제 #7
0
                        (address >> 24) & 0xff, port % 256, port / 256, 2,
                        payload_length)

        HOST = socket.gethostbyname(hardcode_ip)
        sock.sendto(p, (HOST, 5775))
        p = sock.recv(1000)
        if ord(p[0]) == 0xAA and ord(p[1]) == 0x55:
            print "Node ID is %d" % ord(p[2])
        else:
            print "Unknown message:", [p]

    elif sys.argv[3] == 'ID':
        dest_id = MPTN.MASTER_ID
        src_id = MPTN.MPTN_MAX_ID
        msg_type = MPTN.MPTN_MSGTYPE_IDREQ
        message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                            uuid.uuid4().bytes)
        payload_length = len(message)
        p = struct.pack('11B', 0xAA, 0x55, int(sys.argv[4]), address & 0xff,
                        (address >> 8) & 0xff, (address >> 16) & 0xff,
                        (address >> 24) & 0xff, port % 256, port / 256, 1,
                        payload_length)
        p = p + message

        HOST = socket.gethostbyname(hardcode_ip)
        sock.sendto(p, (HOST, 5775))

        p = sock.recv(1000)
        dest_id, src_id, msg_type, payload = MPTN.extract_packet_from_str(p)
        if msg_type == MPTN.MPTN_MSGTYPE_IDACK and src_id == MPTN.MASTER_ID:
            src_id = dest_id
            print "Your ID is %d of which dotted format is %s" % (
예제 #8
0
    def handle_fwdreq_message(self, context, dest_id, src_id, msg_type,
                              payload):
        if payload is None:
            logger.error("FWDREQ should have the payload")
            return

        if not self.is_id_valid(src_id):
            logger.error("invalid FWDREQ src ID %X %s: not found in network" %
                         (src_id, MPTN.ID_TO_STRING(src_id)))
            return

        if self._is_id_master(dest_id):
            logger.debug("FWDREQ is to master")
            message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                                payload)
            if not self._forward_to_next_hop(context, dest_id, message):
                logger.error("FWDREQ to master failed")
            return  # no need to return FWDACK back via transport interface

        if self._is_id_gwself(dest_id):
            logger.debug("FWDREQ the message is to me")
            if context.direction == MPTN.ONLY_FROM_TRANSPORT_INTERFACE:
                payload = map(ord, payload)
                handler = self._app_handler.get(payload[0])
                if handler is not None:
                    handler(context.address, payload[1:])
                    gevent.sleep(0.001)
                else:
                    logger.error(
                        "FWDREQ receives invalid gateway application %d" %
                        payload[0])
            else:
                logger.error(
                    "FWDREQ receives invalid message to me from Master or other gateways"
                )
            return

        if self._is_id_in_gwself_network(dest_id):
            logger.debug("FWDREQ to transport interface directly")
            message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                                payload)
            ret = self._transport_if_send(self._get_address_from_id(dest_id),
                                          message)

            msg_type = MPTN.MPTN_MSGTYPE_FWDACK
            if not ret[0]:
                msg_type = MPTN.MPTN_MSGTYPE_FWDNAK
                logger.error("FWDREQ to transport address %X fail" %
                             self._get_address_from_id(dest_id))

            if not self._is_id_in_gwself_network(src_id):
                message = MPTN.create_packet_to_str(src_id, dest_id, msg_type,
                                                    payload)
                MPTN.socket_send(context, context.id, message)

            return

        logger.debug("FWDREQ may be sent to other gateway's network")
        message = MPTN.create_packet_to_str(dest_id, src_id, msg_type, payload)
        if self._forward_to_next_hop(context, dest_id, message):
            return

        logger.error(
            "FWDREQ dest ID %X %s is neither the master, the gateway, nor within MPTN"
            % (dest_id, MPTN.ID_TO_STRING(dest_id)))
        return
예제 #9
0
    def handle_idreq_message(self, context, dest_id, src_id, msg_type,
                             payload):
        if not self._is_id_master(dest_id):
            logger.error("IDREQ dest ID should be 0 not %X (%s)" %
                         (dest_id, MPTN.ID_TO_STRING(dest_id)))
            return

        if src_id != MPTN.MPTN_MAX_ID:
            logger.error("IDREQ src ID should be 0xFFFFFFFF not %X (%s)" %
                         (dest_id, MPTN.ID_TO_STRING(dest_id)))
            return

        if payload is None or len(payload) != MPTN.IDREQ_PAYLOAD_LEN:
            logger.error("IDREQ length of payload %d should be %d" %
                         (len(payload), MPTN.IDREQ_PAYLOAD_LEN))
            return

        uuid = payload

        try:
            temp_addr = int(context.address)
        except Exception as e:
            logger.error(
                "IDREQ cannot turn interface address %s into integer" %
                str(context.address))
            exit(-1)

        if self._transport_if_addr_len == 1 or self._transport_if_addr_len == 2:
            temp_id = self._id_prefix | temp_addr

        elif self._transport_if_addr_len == 4:
            temp_id = temp_addr

        dest_id = self._id
        src_id = temp_id

        # New addresss from transport interface
        if not self.is_addr_valid(temp_addr):
            if CONFIG.UNITTEST_MODE:
                message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                                    uuid)
                self._id_req_queue.put_nowait(message)

                dest_id = temp_id
                src_id = MPTN.MASTER_ID
                msg_type = MPTN.MPTN_MSGTYPE_IDACK
                message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                                    uuid)
                self._transport_if_send(temp_addr, message)
                return

            else:
                message = MPTN.create_packet_to_str(dest_id, src_id, msg_type,
                                                    uuid)
                packet = MPTN.socket_send(context,
                                          MPTN.MASTER_ID,
                                          message,
                                          expect_reply=True)
                if packet is None:
                    logger.error(
                        "IDREQ cannot be confirmed ID=%d (%s) Addr=%d" %
                        (temp_id, MPTN.ID_TO_STRING(temp_id), temp_addr))
                    return
                dest_id, src_id, msg_type, payload = packet
                if dest_id != temp_id or src_id != MPTN.MASTER_ID or (
                        msg_type not in [
                            MPTN.MPTN_MSGTYPE_IDACK, MPTN.MPTN_MSGTYPE_IDNAK
                        ]):
                    logger.error(
                        "IDREQ invalid response for dest ID=%X (%s), src ID=%X (%s), msg_type=%X"
                        % (dest_id, MPTN.ID_TO_STRING(dest_id), src_id,
                           MPTN.ID_TO_STRING(src_id), msg_type))
                    return

                if msg_type == MPTN.MPTN_MSGTYPE_IDNAK:
                    logger.error("IDREQ for %X (%s) is refused by Master" %
                                 (temp_id, MPTN.ID_TO_STRING(temp_id)))
                    return

                self._alloc_address(temp_addr, uuid)
                message = MPTN.create_packet_to_str(dest_id, src_id,
                                                    MPTN.MPTN_MSGTYPE_IDACK,
                                                    None)
                self._transport_if_send(temp_addr, message)
                return

        # Known address to check uuid
        elif True:  #self._addr_db[temp_addr] == payload:
            dest_id = temp_id
            message = MPTN.create_packet_to_str(dest_id, MPTN.MASTER_ID,
                                                MPTN.MPTN_MSGTYPE_IDACK, None)
            self._transport_if_send(temp_addr, message)
            return

        else:
            logger.error(
                "IDREQ comes with a valid addr=%d, ID=%d or %s, but an unknown uuid %s"
                % (temp_addr, temp_id, MPTN.ID_TO_STRING(temp_id),
                   str(map(ord, uuid))))
예제 #10
0
    def _init_get_prefix_from_master(self):
        if "GTWSELF_ID" not in self._settings_db:
            self._settings_db["GTWSELF_ID"] = MPTN.MPTN_MAX_ID
        self._id = self._settings_db["GTWSELF_ID"]
        MPTN.set_self_id(self._id)
        if self._id != MPTN.MPTN_MAX_ID:
            self._network = MPTN.ID_NETWORK_FROM_TUPLE(
                MPTN.ID_TO_STRING(self._id), str(self._id_prefix_len))
            self._id_prefix = self._id & self._id_netmask

        dest_id = MPTN.MASTER_ID
        src_id = self._id
        msg_type = MPTN.MPTN_MSGTYPE_GWIDREQ

        payload = json.dumps({
            "IFADDR": self._transport_if_addr,
            "IFADDRLEN": self._transport_if_addr_len,
            "IFNETMASK": self._id_netmask,
            "PORT": CONFIG.SELF_TCP_SERVER_PORT,
            "UUID": self._settings_db["GTWSELF_UNIQUE_VALUE"]
        })
        # payload = "IFADDR=%d;IFADDRLEN=%d;IFNETMASK=%d;PORT=%d;VAL=%s" % (self._transport_if_addr,
        #     self._transport_if_addr_len, self._id_netmask, CONFIG.SELF_TCP_SERVER_PORT,
        #     struct.pack("!%dB"%MPTN.GWIDREQ_PAYLOAD_LEN, *self._settings_db["GTWSELF_UNIQUE_VALUE"]))
        message = MPTN.create_packet_to_str(dest_id, src_id, msg_type, payload)

        packet = MPTN.socket_send(None,
                                  MPTN.MASTER_ID,
                                  message,
                                  expect_reply=True)
        if packet is None:
            logger.error(
                "GWIDREQ cannot get GWIDACK/NAK from master due to network problem"
            )
            return None

        # log_msg = MPTN.formatted_print(MPTN.split_packet_to_list(message))
        dest_id, src_id, msg_type, payload = packet

        if msg_type == MPTN.MPTN_MSGTYPE_GWIDNAK:
            logger.error("GWIDREQ GWIDREQ is refused by master")
            return None

        elif msg_type != MPTN.MPTN_MSGTYPE_GWIDACK:
            logger.error(
                "GWIDREQ get unexpected msg type (%d) instead of GWIDACK/NAK from master"
                % msg_type)
            return None

        elif dest_id != self._id:
            if self._id == MPTN.MPTN_MAX_ID:
                self._settings_db["GTWSELF_ID"] = dest_id
                self._id = dest_id
                MPTN.set_self_id(self._id)
                self._network = MPTN.ID_NETWORK_FROM_TUPLE(
                    MPTN.ID_TO_STRING(self._id), str(self._id_prefix_len))
                self._id_prefix = dest_id & self._id_netmask
                logger.info(
                    "GWIDREQ successfully get new ID %s including prefix" %
                    MPTN.ID_TO_STRING(dest_id))
            else:
                logger.error(
                    "GWIDREQ get an ID %d %s different from old one %d %s" %
                    (dest_id, MPTN.ID_TO_STRING(dest_id), self._id,
                     MPTN.ID_TO_STRING(self._id)))
                exit(-1)
        else:
            logger.info("GWIDREQ successfully check the ID %s with master" %
                        MPTN.ID_TO_STRING(dest_id))