Exemplo n.º 1
0
def ask_for_global_table():
    controller_index = config_data['controller_index']
    src_ip = ''
    dst_ip = ''
    netmask = ''
    for interface in config_data['interfaces']:
        if interface['counter_index'] != controller_index:
            continue
        # now counter_index is controller_index
        src_ip = interface['vip']
        dst_ip = interface['counter_vip']
        netmask = interface['netmask']
    logger.debug(
        'going to send route table request msg\n, \
        src_ip %s, dst_ip %s, netmask %s', src_ip, dst_ip, netmask)
    request_msg = {
        "code": 0,
        "msg": "request for route table",
        "src_index": ROUTER_INDEX
    }
    msg_bytes = utilities.objEncode(request_msg)
    pkg = route.IP_Package(src_ip, dst_ip, dst_ip, netmask, msg_bytes)
    pkg.protocol = 119
    errno = route.link_layer.send(pkg.to_bytes())
    if errno < 0:
        logger.warning('fail to send to link layer, errno: %d', errno)
Exemplo n.º 2
0
 def broadcastMsg(self, data, protocol=0):
     interfaces = self.network_layer.interfaces
     for interface in interfaces:
         sip = interface.vip
         dip = interface.counter_vip
         logger.debug('broadcast to ip %s', dip)
         self.network_layer.send(sip, dip, utilities.objEncode(data),
                                 protocol)
Exemplo n.º 3
0
 def run_ping(self):
     logger.info("tracking direct router neighbor threading run...")
     for interface in self.network_layer.interfaces:
         src_ip = interface.vip
         dst_ip = interface.counter_vip
         netmask = interface.netmask
         msg = {'code': 0, 'msg': "are you still here?"}
         self.network_layer.send(src_ip, dst_ip, utilities.objEncode(msg),
                                 100)
         logger.debug('send ping from %s to %s', src_ip, dst_ip)
Exemplo n.º 4
0
 def broadcast_logout(self, index):
     global msgID
     logger.debug('###########broadcasting!!!!!########')
     logger.debug('broadcast,  id is %d, from index %d', msgID,
                  ROUTER_INDEX)
     for interface in self.interfaces:
         src_ip, dst_ip = interface
         msg = {"type": "logout", "index": index, "id": msgID}
         msgID += V
         self.network_layer.send(src_ip, dst_ip, utilities.objEncode(msg),
                                 119)
Exemplo n.º 5
0
 def broadcast(self, interfaces):
     rip_msg = {
         "from" : self.route_name,
         "topo": self.topo,
         'tear_down': self.tear_down,
         "dv": self.dis_vec
     }
     s = utilities.objEncode(rip_msg)
     for inf in interfaces:
         pkg = route.IP_Package(inf.vip, inf.counter_vip, inf.counter_vip, 24, s)
         pkg.protocol = 120
         route.link_layer.send(pkg.to_bytes())
Exemplo n.º 6
0
    def run(self):
        logger.info('network layer listener begin to work...')
        while True:
            ospf_pkg = self.network_layer.recv_ospf()
            ordinary_pkg = self.network_layer.recv()
            if ospf_pkg is None and ordinary_pkg is None:
                time.sleep(0.01)
                continue
            if ospf_pkg:
                src_ip = ospf_pkg.dest_ip
                dest_ip = ospf_pkg.src_ip
                netmask = ospf_pkg.net_mask
                if ospf_pkg.protocol != 119:
                    logger.error(
                        'error! get ospf pkg, protocol is %d instead of 119',
                        ospf_pkg.protocol)
                ospf_msg = utilities.objDecode(ospf_pkg.data)
                logger.info("get ospf msg\n%s", ospf_msg)

                if ospf_msg['code'] == 0:
                    if not is_controller:
                        logger.warn(
                            "IM NOT CONTROLLER. get request msg. ignore.")
                        continue
                    src_index = ospf_msg['src_index']
                    init_global_route_table(GLOBAL_ROUTE_INFORMATIOIN_FILE,
                                            src_index)
                    sp = calculate_shortest_path(src_index)
                    response_msg = {
                        "code": 1,
                        "msg": "here's your route table",
                        "route_table": sp
                    }
                    response_msg_bytes = utilities.objEncode(response_msg)
                    pkg = route.IP_Package(src_ip, dest_ip, dest_ip, netmask,
                                           response_msg_bytes)
                    pkg.protocol = 119
                    errno = route.link_layer.send(pkg.to_bytes())
                    if errno < 0:
                        logger.warning(
                            'fail to send to link layer. errno is %d\n', errno)
                elif ospf_msg['code'] == 1:
                    logger.info('get route table response msg\n%s', ospf_msg)
                    route_table = ospf_msg['route_table']
                    for dest_net, netmask, dest_ip in route_table:
                        route.my_route_table.update_item(
                            dest_net, netmask, dest_ip)

            if ordinary_pkg:
                #TODO: refine here
                logger.info("get odinary msg\n%s", ordinary_pkg)
Exemplo n.º 7
0
    def task(self):
        time.sleep(0.1)
        msg_queue = []
        msg_queue.append(self.network_layer.recv_ospf())
        msg_queue.append(self.network_layer.recv())
        msg_queue.append(self.network_layer.recv_ping())

        available_msg = [msg for msg in msg_queue if not msg is None]
        for msg in available_msg:
            if msg.protocol == 100:
                data = utilities.objDecode(msg.data)
                sip = msg.src_ip
                dip = msg.dest_ip
                netmask = msg.net_mask
                if data['code'] == 0:
                    # get ping request from other
                    logger.info('get ping from %s\n', sip)
                    msg = {'code': 1, 'msg': 'I am here.'}
                    self.network_layer.send(dip, sip, utilities.objEncode(msg),
                                            100)
                if data['code'] == 1:
                    # get ping response from it
                    logger.info('ping response. I know %s reachable', sip)
                    self.tracking_neighbour_alive.wakeup(dip, sip)
            elif msg.protocol == 119:
                data = utilities.objDecode(msg.data)
                msgid = data['id']
                if msgid in known_msgid_list:
                    # logger.debug('already know %d, continue', msgid)
                    continue
                known_msgid_list.append(msgid)
                self.broadcastMsg(data, 119)
                msg_type = data['type']
                if msg_type == 'logout':
                    # pass #TODO: here, maybe bugs
                    lgout_index = data['index']
                    logger.info(
                        'get logout broadcast. now refresh route table according to broadcast %d',
                        lgout_index)
                    logout_refresh_route_table(lgout_index)
            else:
                logger.debug('recv msg!!\n%s', msg)
Exemplo n.º 8
0
             logger.setLevel(logging.INFO)
         elif main_action == 'o':
             logger.setLevel(logging.DEBUG)
         elif main_action == 'cost':
             _vip = user_args[1]
             _cost = int(user_args[2])
             found = False
             for intf in rip_worker.interfaces:
                 if intf.vip == _vip:
                     print('Cost {} -> {} changed to {}'.format(rip_worker.route_name, intf.counter_name, _cost))
                     rip_worker.dis_vec[intf.counter_name]['cost'] = _cost
                     cost_msg = {
                         "vip": intf.counter_vip,
                         "cost": _cost
                     }
                     s = utilities.objEncode(cost_msg)
                     pkg = route.IP_Package(intf.vip, intf.counter_vip, intf.counter_vip, 24, s)
                     pkg.protocol = 121
                     route.link_layer.send(pkg.to_bytes())
                     found = True
                     break
             if found == False:
                 print('{} is not a on-link interface'.format(_vip))
         elif main_action == 'q':
             os._exit(0)
 except IndexError:
     print('invalid command!')
     continue
 except Exception as e:
     # 捕获所有异常,并且打印错误信息
     print(traceback.format_exc())