📄 frame_relay.c
字号:
netio_release(vc->input->name); } /* release output NIO */ if (vc->output) netio_release(vc->output->name); }}/* Free resources used by a Frame-Relay switch */static int frsw_free(void *data,void *arg){ frsw_table_t *t = data; frsw_conn_t *vc; int i; for(i=0;i<FRSW_HASH_SIZE;i++) for(vc=t->dlci_table[i];vc;vc=vc->hash_next) frsw_release_vc(vc); mp_free_pool(&t->mp); free(t); return(TRUE);}/* Delete a Frame-Relay switch */int frsw_delete(char *name){ return(registry_delete_if_unused(name,OBJ_TYPE_FRSW,frsw_free,NULL));}/* Delete all Frame-Relay switches */int frsw_delete_all(void){ return(registry_delete_type(OBJ_TYPE_FRSW,frsw_free,NULL));}/* Create a switch connection */int frsw_create_vc(frsw_table_t *t,char *nio_input,u_int dlci_in, char *nio_output,u_int dlci_out){ frsw_conn_t *vc,**p; u_int hbucket; FRSW_LOCK(t); /* Allocate a new VC */ if (!(vc = mp_alloc(&t->mp,sizeof(*vc)))) { FRSW_UNLOCK(t); return(-1); } vc->input = netio_acquire(nio_input); vc->output = netio_acquire(nio_output); vc->dlci_in = dlci_in; vc->dlci_out = dlci_out; /* Check these NIOs are valid and the input VC does not exists */ if (!vc->input || !vc->output) goto error; if (frsw_dlci_lookup(t,vc->input,dlci_in)) { fprintf(stderr,"FRSW %s: switching for VC %u on IF %s " "already defined.\n",t->name,dlci_in,vc->input->name); goto error; } /* Add as a RX listener */ if (netio_rxl_add(vc->input,(netio_rx_handler_t)frsw_recv_pkt,t,NULL) == -1) goto error; hbucket = frsw_dlci_hash(dlci_in); vc->hash_next = t->dlci_table[hbucket]; t->dlci_table[hbucket] = vc; for(p=(frsw_conn_t **)&vc->input->fr_conn_list;*p;p=&(*p)->next) if ((*p)->dlci_in > dlci_in) break; vc->next = *p; if (*p) (*p)->pprev = &vc->next; vc->pprev = p; *p = vc; FRSW_UNLOCK(t); return(0); error: FRSW_UNLOCK(t); frsw_release_vc(vc); mp_free(vc); return(-1);}/* Remove a switch connection */int frsw_delete_vc(frsw_table_t *t,char *nio_input,u_int dlci_in, char *nio_output,u_int dlci_out){ netio_desc_t *input,*output; frsw_conn_t **vc,*p; u_int hbucket; FRSW_LOCK(t); input = registry_exists(nio_input,OBJ_TYPE_NIO); output = registry_exists(nio_output,OBJ_TYPE_NIO); if (!input || !output) { FRSW_UNLOCK(t); return(-1); } hbucket = frsw_dlci_hash(dlci_in); for(vc=&t->dlci_table[hbucket];*vc;vc=&(*vc)->hash_next) { p = *vc; if ((p->input == input) && (p->output == output) && (p->dlci_in == dlci_in) && (p->dlci_out == dlci_out)) { /* Found a matching VC, remove it */ *vc = (*vc)->hash_next; frsw_unlink_vc(p); FRSW_UNLOCK(t); /* Release NIOs */ frsw_release_vc(p); mp_free(vc); return(0); } } FRSW_UNLOCK(t); return(-1);}/* Save the configuration of a Frame-Relay switch */void frsw_save_config(frsw_table_t *t,FILE *fd){ frsw_conn_t *vc; int i; fprintf(fd,"frsw create %s\n",t->name); FRSW_LOCK(t); for(i=0;i<FRSW_HASH_SIZE;i++) { for(vc=t->dlci_table[i];vc;vc=vc->next) { fprintf(fd,"frsw create_vc %s %s %u %s %u\n", t->name,vc->input->name,vc->dlci_in, vc->output->name,vc->dlci_out); } } FRSW_UNLOCK(t); fprintf(fd,"\n");}/* Save configurations of all Frame-Relay switches */static void frsw_reg_save_config(registry_entry_t *entry,void *opt,int *err){ frsw_save_config((frsw_table_t *)entry->data,(FILE *)opt);}void frsw_save_config_all(FILE *fd){ registry_foreach_type(OBJ_TYPE_FRSW,frsw_reg_save_config,fd,NULL);}/* Create a new interface */int frsw_cfg_create_if(frsw_table_t *t,char **tokens,int count){ netio_desc_t *nio = NULL; int nio_type; /* at least: IF, interface name, NetIO type */ if (count < 3) { fprintf(stderr,"frsw_cfg_create_if: invalid interface description\n"); return(-1); } nio_type = netio_get_type(tokens[2]); switch(nio_type) { case NETIO_TYPE_UNIX: if (count != 5) { fprintf(stderr,"FRSW: invalid number of arguments " "for UNIX NIO '%s'\n",tokens[1]); break; } nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]); break; case NETIO_TYPE_UDP: if (count != 6) { fprintf(stderr,"FRSW: invalid number of arguments " "for UDP NIO '%s'\n",tokens[1]); break; } nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]), tokens[4],atoi(tokens[5])); break; case NETIO_TYPE_TCP_CLI: if (count != 5) { fprintf(stderr,"FRSW: invalid number of arguments " "for TCP CLI NIO '%s'\n",tokens[1]); break; } nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]); break; case NETIO_TYPE_TCP_SER: if (count != 4) { fprintf(stderr,"FRSW: invalid number of arguments " "for TCP SER NIO '%s'\n",tokens[1]); break; } nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]); break; default: fprintf(stderr,"FRSW: unknown/invalid NETIO type '%s'\n", tokens[2]); } if (!nio) { fprintf(stderr,"FRSW: unable to create NETIO descriptor of " "interface %s\n",tokens[1]); return(-1); } netio_release(nio->name); return(0);}/* Create a new virtual circuit */int frsw_cfg_create_vc(frsw_table_t *t,char **tokens,int count){ /* 5 parameters: "VC", InputIF, InDLCI, OutputIF, OutDLCI */ if (count != 5) { fprintf(stderr,"FRSW: invalid VPC descriptor.\n"); return(-1); } return(frsw_create_vc(t,tokens[1],atoi(tokens[2]), tokens[3],atoi(tokens[4])));}#define FRSW_MAX_TOKENS 16/* Handle a FRSW configuration line */int frsw_handle_cfg_line(frsw_table_t *t,char *str){ char *tokens[FRSW_MAX_TOKENS]; int count; if ((count = m_strsplit(str,':',tokens,FRSW_MAX_TOKENS)) <= 1) return(-1); if (!strcmp(tokens[0],"IF")) return(frsw_cfg_create_if(t,tokens,count)); else if (!strcmp(tokens[0],"VC")) return(frsw_cfg_create_vc(t,tokens,count)); fprintf(stderr,"FRSW: Unknown statement \"%s\" (allowed: IF,VC)\n", tokens[0]); return(-1);}/* Read a FRSW configuration file */int frsw_read_cfg_file(frsw_table_t *t,char *filename){ char buffer[1024],*ptr; FILE *fd; if (!(fd = fopen(filename,"r"))) { perror("fopen"); return(-1); } while(!feof(fd)) { if (!fgets(buffer,sizeof(buffer),fd)) break; /* skip comments and end of line */ if ((ptr = strpbrk(buffer,"#\r\n")) != NULL) *ptr = 0; /* analyze non-empty lines */ if (strchr(buffer,':')) frsw_handle_cfg_line(t,buffer); } fclose(fd); return(0);}/* Start a virtual Frame-Relay switch */int frsw_start(char *filename){ frsw_table_t *t; if (!(t = frsw_create_table("default"))) { fprintf(stderr,"FRSW: unable to create virtual fabric table.\n"); return(-1); } if (frsw_read_cfg_file(t,filename) == -1) { fprintf(stderr,"FRSW: unable to parse configuration file.\n"); return(-1); } frsw_release("default"); return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -