static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) { _cleanup_free_ uint16_t *options = NULL; int n, i; n = efi_get_boot_options(&options); if (n < 0) return n; /* find already existing systemd-boot entry */ for (i = 0; i < n; i++) if (same_entry(options[i], uuid, path)) { *id = options[i]; return 1; } /* find free slot in the sorted BootXXXX variable list */ for (i = 0; i < n; i++) if (i != options[i]) { *id = i; return 1; } /* use the next one */ if (i == 0xffff) return -ENOSPC; *id = i; return 0; }
static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) { uint16_t *options = NULL; int n_options; int i; uint16_t new_id = 0; bool existing = false; n_options = efi_get_boot_options(&options); if (n_options < 0) return n_options; /* find already existing systemd-boot entry */ for (i = 0; i < n_options; i++) if (same_entry(options[i], uuid, path)) { new_id = options[i]; existing = true; goto finish; } /* find free slot in the sorted BootXXXX variable list */ for (i = 0; i < n_options; i++) if (i != options[i]) { new_id = i; goto finish; } /* use the next one */ if (i == 0xffff) return -ENOSPC; new_id = i; finish: *id = new_id; free(options); return existing; }
//RETURN 0 WHEN NO TRIGGER UPDATE IS NEEDED (equal entries) //RETURN 1 WHEN TRIGGER UPDATE IS NEEDED (not equal entries) // and replace when better cost int compare_entries (rip_table_t * table, rip_route_t * route_aux, ipv4_addr_t src ) { int index; int i; for (i=0; i<RIP_ROUTE_TABLE_SIZE; i++){ if (table->routes[i] != NULL){ //IS THERE AN EXISTENT ROUTE?? //WE COMPARE DESTINATION ADDRESS if (same_entry (table->routes[i]->ipv4_addr, route_aux->ipv4_addr)){ //THERE IS AN EXISTENT ROUTE //COMPARE METRICS int cmp_metrics = compare_metrics (table->routes[i]->metric, route_aux->metric); //WE CHECK IF THIS DATAGRAM IS FROM THE SAME ROUTER AS THE EXISTING ROUTE if (same_entry (table->routes[i]->ipv4_next, src)){ //THIS DATAGRAM IS FROM THE SAME ROUTER AS THE EXISTING ROUTE //WE REINITIALIZE TIMEOUT //reset_entry_timer (table->routes[i]); //WE CHECK IF METRICS ARE DIFFERENT if (cmp_metrics != 0){ //METRICS ARE DIFFERENT //CHECK IF METRIC IS 16 if (metric_is_inf (route_aux->metric)){ //METRIC IS 16 //SUBSTITUTE ROUTE AND SET DELETE FLAG set_delete_flag (table->routes[i]); index = rip_replace_entry (table, table->routes[i], route_aux, i); memcpy (table->routes[index]->ipv4_next, src, IPv4_ADDR_SIZE); return 1; } else { //METRIC IS LOWER THAN 16 //SUBSTITUTE ROUTE AND RESET TIMER index = rip_replace_entry (table, table->routes[i], route_aux, i); memcpy (table->routes[index]->ipv4_next, src, IPv4_ADDR_SIZE); reset_entry_timer (table->routes[index]); return 1; } }else{ //METRICS ARE THE SAME if (!metric_is_inf (route_aux->metric)){ //METRIC IS NOT 16 //RESET TIMER reset_entry_timer (table->routes[i]); return 0; }else{ //METRIC IS 16 set_delete_flag (table->routes[i]); return 0; } } }else{ //THIS DATAGRAM IS NOT FROM THE SAME ROUTER AS THE EXISTING ROUTE //WE CHECK IF METRICS ARE DIFFERENT if (cmp_metrics != 0){ //METRICS ARE DIFFERENT //WE CHECK IF THE NEW METRIC IS LOWER THAN THE ACTUAL ONE if (cmp_metrics < 0) //THE ACTUAL METRIC IS A BETTER OPTION, DO NOTHING return 0; else{ //THE NEW METRIC IS A BETTER OPTION, WE ADOPT THE NEW ROUTE index = rip_replace_entry (table, table->routes[i], route_aux, i); memcpy (table->routes[index]->ipv4_next, src, IPv4_ADDR_SIZE); reset_entry_timer (table->routes[index]); return 1; } }else{ //METRICS ARE THE SAME //WE CHECK IF THE TIMER FOR THE ACTUAL ENTRY IS HALFWAY OR LOWER if (timer_halfway(table->routes[i])){ //TIMER HALFWAY OR LOWER, SUBSTITUTE ROUTE index = rip_replace_entry (table, table->routes[i], route_aux, i); memcpy (table->routes[index]->ipv4_next, src, IPv4_ADDR_SIZE); reset_entry_timer (table->routes[index]); return 0; }else //TIMER IS FINE, DO NOTHING return 0; } } } } } //THERE IS NOT AN EXISTENT ROUTE if (route_aux->metric < 16){ index = rip_route_table_add (table, route_aux); memcpy (table->routes[index]->ipv4_next, src, IPv4_ADDR_SIZE); reset_entry_timer (table->routes[index]); return 1; } else return 0; }