void parse_configuration_args(int argc, char** argv) { // Scan options. // char **args = &argv[1]; while (*args) { char *opt = NULL; //TODO handle conflicts between args & conf file ? if ((opt = shift_option(&args, "--conf="))) confFilename = opt; else if ((opt = shift_option(&args, "--ip-conf="))) config_ip_file_name = opt; else if ((opt = shift_option(&args, "--discard"))) packet_drop = 1; #if OFP_LOOP_STATISTICS else if ((opt = shift_option(&args, "--limit-packets="))) limit_packets = strtoul(opt,NULL,0); #endif else if ((opt = shift_option(&args, "--workers="))) work_size = atoi(opt); #if TILEGX else if ((opt = shift_option(&args, "--links="))) parse_links(0, opt); #else #if TWOINTERFACE else if ((opt = shift_option(&args, "--interface1="))) interface1 = opt; else if ((opt = shift_option(&args, "--interface2="))) interface2 = opt; #else else if ((opt = shift_option(&args, "--interface="))) interface1 = opt; #endif #endif #if MODE_VLAN else if ((opt = shift_option(&args, "--vlan1="))) packet_vlan_swap1 = strtoul(opt,NULL,0); else if ((opt = shift_option(&args, "--vlan2="))) packet_vlan_swap2 = strtoul(opt,NULL,0); #endif else if ((opt = shift_option(&args, "--daemon"))) config_daemonize = 1; else tmc_task_die("Unknown option '%s'.\n", args[0]); } }
int main(int argc, char *argv[]) { // part 1: read in lnx file and char *linkFile = argv[1]; printf(_NORMAL_"Link File -> \"%s\"\n", argv[1]); ref = parse_links(linkFile); node_t *curr; int i = 1; for (curr = ref->head; curr != NULL; curr = curr->next) { printf(_GREEN_"\n\tINTERFACE [%d]----- Local Physical Info -----\n", i); link_t *sing = (link_t *)curr->data; printf(_BLUE_"\tHost\t\tPort\t\tIPv4\t\n"); printf(_BLUE_"\t%s\t%d\t\t%s\t\n"_BLUE_, sing->local_phys_host,sing->local_phys_port, inet_ntoa(sing->local_virt_ip)); printf(_NORMAL_); printf(_RED_"\tINTERFACE [%d]----- Remote Physical Info -----\n", i++); printf(_BLUE_"\tHost\t\tPort\t\tIPv4\t\n"); printf(_BLUE_"\t%s\t%d\t\t%s\t\n"_BLUE_, sing->remote_phys_host,sing->remote_phys_port, inet_ntoa(sing->remote_virt_ip)); printf(_NORMAL_); } printf("\n"); //part 2 int sockfd; int yes = 1; link_t *tmp = (link_t *)ref->head->data; struct sockaddr_in addrsock; memset(&addrsock, 0, sizeof(addrsock)); addrsock.sin_family = AF_INET; addrsock.sin_port = htons(tmp->local_phys_port); (addrsock.sin_addr).s_addr = INADDR_ANY; //inet_aton(inet_ntoa(tmp->local_virt_ip), &addrsock.sin_addr); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("ERROR : Socket"); return -1; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { perror("ERROR : Socket Option"); close(sockfd); return -1; } if (bind(sockfd, (struct sockaddr *)&addrsock, sizeof(addrsock)) < 0) { perror("ERROR : Bind"); close(sockfd); return -1; } // 2. Build Routing table list_init(&routes); struct route_entry *ent; for (curr = ref->head; curr != NULL; curr = curr->next) { link_t *sing = (link_t *)curr->data; ent = (struct route_entry *)malloc(sizeof(struct route_entry)); ent->rtu_id = num_entries; ent->rtu_dst.s_addr = sing->local_virt_ip.s_addr; ent->rtu_cost = 0; ent->rtu_status = (uint8_t)1; list_append(routes, (void *)ent); } // 3 select int maxfd; fd_set master, read_fds; FD_ZERO(&master); maxfd = sockfd + 1; FD_SET(sockfd, &master); FD_SET(0, &master); char input[BUFSIZE]; //int n; for (;;) { read_fds = master; if (select(maxfd, &master, NULL, NULL, NULL) == -1) { perror("Select"); exit(4); } if (FD_ISSET(0, &read_fds)) { if (fgets(input, BUFSIZE, stdin) != NULL) { if (strcmp(input, "routes\n") == 0) { rout_table_print(); } } } } return 0; }
// reload : set to 1 if we are reloading the conf while running // When we are reloading, only some options can be changed. E.g. 'workers' can not, it needs a restart void parse_main_configuration_file(int reload) { if (confFilename == NULL) { PRINT_D2("No config file provided\n"); return; } FILE *confFile = fopen(confFilename, "r"); if (confFile == NULL) tmc_task_die("Error opening config file %s\n", confFilename); PRINT_INFO("Loading config file %s\n", confFilename); char * line = NULL; size_t len = 0; ssize_t read; int lineCount = 0; while ((read = getline(&line, &len, confFile)) != -1) { lineCount++; int lineNumber = lineCount; // Ignore empty lines and lines starting with '#' if (strcmp(line, "\n") == 0 || line[0] == '#') continue; // Duplicate line since strtok modifies its arg char *dup_line = strdup(line); char *param = strtok(dup_line, "="); char *value = strtok(NULL, "\n"); if (value == NULL) tmc_task_die("Error parsing config file(%d), param '%s' has no value\n", lineNumber, param); if (!reload && !strcmp(param, "workers")) work_size = atoi(value); #if TILEGX else if (!reload && !strcmp(param, "links")) parse_links(lineNumber, value); #endif else if (!strcmp(param, "monitoring_ip_port")) { ip_port_tuple ip_port = {0}; if(!parse_ip(value, 0, &ip_port)) { tmc_task_die("Error parsing config file(%d), param '%s' parsing ip '%s'\n", lineNumber, param, value); } ofp_logger_addr.sin_port = htons(ip_port.port); ofp_logger_addr.sin_addr.s_addr = htonl(ip_port.ip); } else if (!strcmp(param, "bridge_mode") && !reload) { PRINT_D5("read bridge_mode = '%s'\n", value); config_bridge_mode = atoi(value); } OVH_FREE(dup_line); } if (line != NULL) OVH_FREE(line); fclose(confFile); PRINT_INFO("config_bridge_mode = %d\n", config_bridge_mode); }
int main(int argc, char *argv[]) { int n = 0; char *url; char command[512]; char buffer[512]; FILE *c; if (argc != 2) { printf("Usage :\ndownloader <url>\n"); return -1; } url = (char *) calloc (strlen(argv[1])+1, sizeof(char)); strcpy(url, argv[1]); /* Once i have the url, i need to download the html content to a file, then read it, and parse any possible links to other webs. Loop then until i finish downloading their respective content to other files. */ sprintf(command, "wget -qO page.html %s", url); c = popen(command, "r"); pclose(c); c = fopen("page.html", "r"); if (c == NULL) { printf("Error abriendo comando"); return -1; } while (fgets(buffer, BUFFSIZE, c)) { char *slink = NULL; parse_links(buffer, &slink); if (slink) { if (strstr(slink, "http://")) { FILE *p; sprintf(command, "wget -qO %s %s", slink, slink); p = popen(command, "r"); pclose(p); } else { FILE *p; char web[512]; char buff[512]; get_parent(url, buff); strcpy(web, buff); strcat(web, slink); sprintf(command, "wget -qO %s %s", slink, web); p = popen(command, "r"); pclose(p); remove_html_tags(slink); } free(slink); } } fclose(c); free(url); return 0; }