⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dev_c3600.c

📁 思科路由器仿真器,用来仿7200系列得,可以在电脑上模拟路由器-Cisco router simulator, used to fake a 7200 series can be simulated
💻 C
📖 第 1 页 / 共 3 页
字号:
   for(nb=bay->nio_list;nb;nb=nb->next)      if (nb->port_id == port_id)         return nb;   return NULL;}/* Add a network IO binding */int c3600_nm_add_nio_binding(c3600_t *router,u_int nm_bay,u_int port_id,                             char *nio_name){   struct c3600_nio_binding *nb;   struct c3600_nm_bay *bay;   netio_desc_t *nio;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* check that a NIO is not already bound to this port */   if (c3600_nm_find_nio_binding(router,nm_bay,port_id) != NULL) {      vm_error(router->vm,"a NIO already exists for interface %u/%u.\n",               nm_bay,port_id);      return(-1);   }   /* acquire a reference on the NIO object */   if (!(nio = netio_acquire(nio_name))) {      vm_error(router->vm,"unable to find NIO '%s'.\n",nio_name);      return(-1);   }   /* create a new binding */   if (!(nb = malloc(sizeof(*nb)))) {      vm_error(router->vm,"unable to create NIO binding "               "for interface %u/%u.\n",nm_bay,port_id);      netio_release(nio_name);      return(-1);   }   memset(nb,0,sizeof(*nb));   nb->nio       = nio;   nb->port_id   = port_id;   nb->next      = bay->nio_list;   if (nb->next) nb->next->prev = nb;   bay->nio_list = nb;   return(0);}/* Remove a NIO binding */int c3600_nm_remove_nio_binding(c3600_t *router,u_int nm_bay,u_int port_id){   struct c3600_nio_binding *nb;   struct c3600_nm_bay *bay;      if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   if (!(nb = c3600_nm_find_nio_binding(router,nm_bay,port_id)))      return(-1);   /* no nio binding for this slot/port */   /* tell the NM driver to stop using this NIO */   if (bay->nm_driver)      bay->nm_driver->nm_unset_nio(router,nm_bay,port_id);   /* remove this entry from the double linked list */   if (nb->next)      nb->next->prev = nb->prev;   if (nb->prev) {      nb->prev->next = nb->next;   } else {      bay->nio_list = nb->next;   }   /* unreference NIO object */   netio_release(nb->nio->name);   free(nb);   return(0);}/* Remove all NIO bindings for the specified NM */int c3600_nm_remove_all_nio_bindings(c3600_t *router,u_int nm_bay){     struct c3600_nio_binding *nb,*next;   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   for(nb=bay->nio_list;nb;nb=next) {      next = nb->next;      /* tell the NM driver to stop using this NIO */      if (bay->nm_driver)         bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id);      /* unreference NIO object */      netio_release(nb->nio->name);      free(nb);   }   bay->nio_list = NULL;   return(0);}/* Enable a Network IO descriptor for a Network Module */int c3600_nm_enable_nio(c3600_t *router,u_int nm_bay,u_int port_id){   struct c3600_nio_binding *nb;   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* check that we have an NIO binding for this interface */   if (!(nb = c3600_nm_find_nio_binding(router,nm_bay,port_id)))      return(-1);   /* check that the driver is defined and successfully initialized */   if (!bay->nm_driver || !bay->drv_info)      return(-1);   return(bay->nm_driver->nm_set_nio(router,nm_bay,port_id,nb->nio));}/* Disable Network IO descriptor of a Network Module */int c3600_nm_disable_nio(c3600_t *router,u_int nm_bay,u_int port_id){   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* check that the driver is defined and successfully initialized */   if (!bay->nm_driver || !bay->drv_info)      return(-1);   return(bay->nm_driver->nm_unset_nio(router,nm_bay,port_id));}/* Enable all NIO of the specified NM */int c3600_nm_enable_all_nio(c3600_t *router,u_int nm_bay){   struct c3600_nio_binding *nb;   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* check that the driver is defined and successfully initialized */   if (!bay->nm_driver || !bay->drv_info)      return(-1);   for(nb=bay->nio_list;nb;nb=nb->next)      bay->nm_driver->nm_set_nio(router,nm_bay,nb->port_id,nb->nio);   return(0);}/* Disable all NIO of the specified NM */int c3600_nm_disable_all_nio(c3600_t *router,u_int nm_bay){   struct c3600_nio_binding *nb;   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* check that the driver is defined and successfully initialized */   if (!bay->nm_driver || !bay->drv_info)      return(-1);   for(nb=bay->nio_list;nb;nb=nb->next)      bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id);   return(0);}/* Initialize a Network Module */int c3600_nm_init(c3600_t *router,u_int nm_bay){      struct c3600_nm_bay *bay;   size_t len;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* Check that a device type is defined for this bay */   if (!bay->dev_type || !bay->nm_driver) {      vm_error(router->vm,"trying to init empty slot %u.\n",nm_bay);      return(-1);   }   /* Allocate device name */   len = strlen(bay->dev_type) + 10;   if (!(bay->dev_name = malloc(len))) {      vm_error(router->vm,"unable to allocate device name.\n");      return(-1);   }   snprintf(bay->dev_name,len,"%s(%u)",bay->dev_type,nm_bay);   /* Initialize NM driver */   if (bay->nm_driver->nm_init(router,bay->dev_name,nm_bay) == 1) {      vm_error(router->vm,"unable to initialize NM %u.\n",nm_bay);      return(-1);   }   /* Enable all NIO */   c3600_nm_enable_all_nio(router,nm_bay);   return(0);}/* Shutdown a Network Module */int c3600_nm_shutdown(c3600_t *router,u_int nm_bay){   struct c3600_nm_bay *bay;   if (!(bay = c3600_nm_get_info(router,nm_bay)))      return(-1);   /* Check that a device type is defined for this bay */      if (!bay->dev_type || !bay->nm_driver) {      vm_error(router->vm,"trying to shut down empty slot %u.\n",nm_bay);      return(-1);   }   /* Disable all NIO */   c3600_nm_disable_all_nio(router,nm_bay);   /* Shutdown the NM driver */   if (bay->drv_info && (bay->nm_driver->nm_shutdown(router,nm_bay) == -1)) {      vm_error(router->vm,"unable to shutdown NM %u.\n",nm_bay);      return(-1);   }   free(bay->dev_name);   bay->dev_name = NULL;   bay->drv_info = NULL;   return(0);}/* Shutdown all NM of a router */int c3600_nm_shutdown_all(c3600_t *router){   int i;   for(i=0;i<C3600_MAX_NM_BAYS;i++) {      if (!router->nm_bay[i].dev_type)          continue;      c3600_nm_shutdown(router,i);   }   return(0);}/* Show info about all NMs */int c3600_nm_show_all_info(c3600_t *router){   struct c3600_nm_bay *bay;   int i;   for(i=0;i<C3600_MAX_NM_BAYS;i++) {      if (!(bay = c3600_nm_get_info(router,i)) || !bay->nm_driver)         continue;      if (bay->nm_driver->nm_show_info != NULL)         bay->nm_driver->nm_show_info(router,i);   }   return(0);}/* Maximum number of tokens in a NM description */#define NM_DESC_MAX_TOKENS  8/* Create a Network Module (command line) */int c3600_cmd_nm_create(c3600_t *router,char *str){   char *tokens[NM_DESC_MAX_TOKENS];   int i,count,res;   u_int nm_bay;   /* A port adapter description is like "1:NM-1FE" */   if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) != 2) {      vm_error(router->vm,"unable to parse NM description '%s'.\n",str);      return(-1);   }   /* Parse the NM bay id */   nm_bay = atoi(tokens[0]);   /* Add this new NM to the current NM list */   res = c3600_nm_add_binding(router,tokens[1],nm_bay);   /* The complete array was cleaned by strsplit */   for(i=0;i<NM_DESC_MAX_TOKENS;i++)      free(tokens[i]);   return(res);}/* Add a Network IO descriptor binding (command line) */int c3600_cmd_add_nio(c3600_t *router,char *str){   char *tokens[NM_DESC_MAX_TOKENS];   int i,count,nio_type,res=-1;   u_int nm_bay,port_id;   netio_desc_t *nio;   char nio_name[128];   /* A port adapter description is like "1:3:tap:tap0" */   if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) < 3) {      vm_error(router->vm,"unable to parse NIO description '%s'.\n",str);      return(-1);   }   /* Parse the NM bay */   nm_bay = atoi(tokens[0]);   /* Parse the NM port id */   port_id = atoi(tokens[1]);   /* Autogenerate a NIO name */   snprintf(nio_name,sizeof(nio_name),"c3600-i%u/%u/%u",            router->vm->instance_id,nm_bay,port_id);   /* Create the Network IO descriptor */   nio = NULL;   nio_type = netio_get_type(tokens[2]);   switch(nio_type) {      case NETIO_TYPE_UNIX:         if (count != 5) {            vm_error(router->vm,                     "invalid number of arguments for UNIX NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_unix(nio_name,tokens[3],tokens[4]);         break;      case NETIO_TYPE_VDE:         if (count != 5) {            vm_error(router->vm,                     "invalid number of arguments for VDE NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_vde(nio_name,tokens[3],tokens[4]);         break;      case NETIO_TYPE_TAP:         if (count != 4) {            vm_error(router->vm,                     "invalid number of arguments for TAP NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_tap(nio_name,tokens[3]);         break;      case NETIO_TYPE_UDP:         if (count != 6) {            vm_error(router->vm,                     "invalid number of arguments for UDP NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_udp(nio_name,atoi(tokens[3]),                                     tokens[4],atoi(tokens[5]));         break;      case NETIO_TYPE_TCP_CLI:         if (count != 5) {            vm_error(router->vm,                     "invalid number of arguments for TCP CLI NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_tcp_cli(nio_name,tokens[3],tokens[4]);         break;      case NETIO_TYPE_TCP_SER:         if (count != 4) {            vm_error(router->vm,                     "invalid number of arguments for TCP SER NIO '%s'\n",str);            goto done;         }         nio = netio_desc_create_tcp_ser(nio_name,tokens[3]);         break;      case NETIO_TYPE_NULL:         nio = netio_desc_create_null(nio_name);         break;#ifdef LINUX_ETH      case NETIO_TYPE_LINUX_ETH:         if (count != 4) {            vm_error(router->vm,                     "invalid number of arguments for Linux Eth NIO '%s'\n",                     str);            goto done;         }                  nio = netio_desc_create_lnxeth(nio_name,tokens[3]);         break;#endif#ifdef GEN_ETH      case NETIO_TYPE_GEN_ETH:         if (count != 4) {            vm_error(router->vm,                     "invalid number of arguments for Generic Eth NIO '%s'\n",                     str);            goto done;         }                  nio = netio_desc_create_geneth(nio_name,tokens[3]);         break;#endif      default:         vm_error(router->vm,"unknown NETIO type '%s'\n",tokens[2]);         goto done;   }   if (!nio) {      vm_error(router->vm,"unable to create NETIO "              "descriptor for NM slot %u\n",nm_bay);      goto done;   }   if (c3600_nm_add_nio_binding(router,nm_bay,port_id,nio_name) == -1) {      vm_error(router->vm,"unable to add NETIO binding for slot %u\n",nm_bay);      netio_release(nio_name);      netio_delete(nio_name);      goto done;   }      netio_release(nio_name);   res = 0; done:   /* The complete array was cleaned by strsplit */   for(i=0;i<NM_DESC_MAX_TOKENS;i++)      free(tokens[i]);   return(res);}/* Show the list of available NM drivers */void c3600_nm_show_drivers(void){   int i;   printf("Available C3600 Network Module drivers:\n");   for(i=0;nm_drivers[i];i++) {      printf("  * %s %s\n",             nm_drivers[i]->dev_type,             !nm_drivers[i]->supported ? "(NOT WORKING)" : "");   }      printf("\n");}/* Get a chassis driver */struct c3600_chassis_driver *c3600_chassis_get_driver(char *chassis_type){   int i;   for(i=0;chassis_drivers[i].chassis_type;i++)      if (!strcmp(chassis_drivers[i].chassis_type,chassis_type))         return(&chassis_drivers[i]);   return NULL;}/* Set the base MAC address of the chassis */static int c3600_burn_mac_addr(c3600_t *router,n_eth_addr_t *addr){   m_uint8_t eeprom_ver;   size_t offset;   /* Read EEPROM format version */   cisco_eeprom_get_byte(&router->mb_eeprom,0,&eeprom_ver);   switch(eeprom_ver) {      case 0:         cisco_eeprom_set_region(&router->mb_eeprom,2,addr->eth_addr_byte,6);         break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -