void read_blif(char* blif_file, int lut_size) { char buffer[BUFSIZE]; int pass, done, doall; blif = my_fopen(blif_file, "r", 0); for (doall = 0; doall <= 1; doall++) { init_parse(doall); /* Three passes to ensure inputs are first blocks, outputs second and * * LUTs and latches third. Just makes the output netlist more readable. */ for (pass = 1; pass <= 3; pass++) { linenum = 0; /* Reset line number. */ done = 0; while ((my_fgets(buffer, BUFSIZE, blif) != NULL) && !done) { get_tok(buffer, pass, doall, &done, lut_size); } rewind(blif); /* Start at beginning of file again */ } } fclose(blif); check_net(lut_size); free_parse(); }
void read_blif(char *blif_file, boolean sweep_hanging_nets_and_inputs, t_model *user_models, t_model *library_models) { char buffer[BUFSIZE]; int pass, doall; boolean done; boolean add_truth_table; t_model *inpad_model, *outpad_model, *logic_model, *latch_model; blif = fopen(blif_file, "r"); if(blif == NULL) { printf("Failed to open blif file %s\n", blif_file); exit(1); } load_default_models(library_models, &inpad_model, &outpad_model, &logic_model, &latch_model); /* doall = 0 means do a counting pass, doall = 1 means allocate and load data structures */ for(doall = 0; doall <= 1; doall++) { init_parse(doall); /* Three passes to ensure inputs are first blocks, outputs second and * * LUTs and latches third, subckts last. Just makes the output netlist more readable. */ for(pass = 0; pass <= 4; pass++) { linenum = 0; /* Reset line number. */ done = FALSE; add_truth_table = FALSE; model_lines = 0; while(my_fgets(buffer, BUFSIZE, blif) != NULL) { get_tok(buffer, pass, doall, &done, &add_truth_table, inpad_model, outpad_model, logic_model, latch_model, user_models); } rewind(blif); /* Start at beginning of file again */ } } fclose(blif); check_net(sweep_hanging_nets_and_inputs); free_parse(); }
unsigned int nf_hook_func( unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) { struct sk_buff *skb_ = skb; #else unsigned int nf_hook_func( unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) { struct sk_buff *skb_ = *skb; #endif struct iphdr *ip_header_ = NULL; struct tcphdr *tcp_header_ = NULL; unsigned char *tcp_data_ = NULL; if (!skb_) return NF_ACCEPT; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) ip_header_ = ip_hdr(skb_); #else ip_header_ = skb_->h.ipiph; #endif // 仅仅过滤 TCP 协议 if (!ip_header_ || ip_header_->protocol != IPPROTO_TCP) return NF_ACCEPT; // 已知在 2.6.18/3.19.2 上,tcp_hdr(skb_) 会返回预期的 tcp 头偏移量。 // 已知在 2.6.32 上,tcp_hdr(skb_) 与 ip_hdr(skb_) 会返回相同的指针地址。 // // 2.6.32: // TCP 包来自 1/2 层,netfilter/ipv4 钩子会在第 3 层被注入。 // 因此,此时使用 tcp_hdr 函数不会获得预期的 tcp 头偏移量。 // 直接根据 IP 头偏移量计算 TCP 头偏移量是最合适的 tcp_header_ = (struct tcphdr *)((__u32 *)ip_header_ + ip_header_->ihl); // 仅仅过滤指定的 HTTP 端口 if (htons(tcp_header_->dest) != HTTP_PORT) return NF_ACCEPT; // 如果是信任的网络,则放行。不做任何过滤 // 因此,如果本机作为转发网关,必须添加内网网段,内网网络才能访问公网。 if (check_net(ip_header_->saddr, g_net_array, g_net_array_size)) return NF_ACCEPT; // GET / HTTP/1.1\r\n // User-Agent: curl/7.41.0\r\n // Host: abc.com\r\n // Accept: */*\r\n // \r\n tcp_data_ = get_tcp_data(skb_, ip_header_, tcp_header_); // 跳过非 HTTP 头相关包 if (!check_http_header(tcp_data_)) return NF_ACCEPT; // 如果是信任的主机,则放行 if (check_host(tcp_data_, g_hosts, g_hosts_size)) return NF_ACCEPT; return NF_DROP; } static struct nf_hook_ops g_nf_hook = { .hook = (nf_hookfn*) nf_hook_func, #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) .hooknum = NF_INET_PRE_ROUTING, #else .hooknum = NF_IP_PRE_ROUTING, #endif .pf = PF_INET, .priority = NF_IP_PRI_FIRST }; int init_module() { #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) pr_err("[%s] Linux Kernel Version does not supported.\n", MODOUBLE_NAME); return -1; #endif pr_info("Loading module \"%s\"\n", MODOUBLE_NAME); if (!init_host_str(&g_hosts, &g_hosts_size)) { pr_err("Cannot initialize host whitelist.\n"); pr_err("Cannot load module \"%s\"\n", MODOUBLE_NAME); return -1; } if (!init_net_array(&g_net_array, &g_net_array_size)) { pr_err("Cannot initialize network whitelist.\n"); pr_err("Cannot load module \"%s\"\n", MODOUBLE_NAME); return -1; } if (nf_register_hook(&g_nf_hook) < 0) { pr_err("Cannot register netfilter hook.\n"); pr_err("Cannot load module \"%s\"\n", MODOUBLE_NAME); return -1; } return 0; } void cleanup_module() { pr_info("Unloading module \"%s\"\n", MODOUBLE_NAME); if (g_hosts) kfree(g_hosts); if (g_net_array) kfree(g_net_array); nf_unregister_hook(&g_nf_hook); }
void main_loop (void) { int activity=0, sleep_now=0, total_unused=0; int sleep_battery=0; int prev_ac_line_status=0; time_t nowtime, oldtime=0; apm_info ai; double loadavg[1]; if (use_events) { pthread_t emthread; pthread_create(&emthread, NULL, eventMonitor, NULL); } while (1) { activity=0; if (use_events) { pthread_mutex_lock(&condition_mutex); pthread_cond_signal(&condition_cond); pthread_mutex_unlock(&condition_mutex); } if (use_acpi) { acpi_read(1, &ai); } #ifdef HAL else if (use_simplehal) { simplehal_read(1, &ai); } #endif else { apm_read(&ai); } if (min_batt != -1 && ai.ac_line_status != 1 && ai.battery_percentage < min_batt && ai.battery_status != BATTERY_STATUS_ABSENT) { sleep_battery = 1; } if (sleep_battery && ! require_unused_and_battery) { syslog(LOG_NOTICE, "battery level %d%% is below %d%%; forcing hibernation", ai.battery_percentage, min_batt); if (system(hibernate_command) != 0) syslog(LOG_ERR, "%s failed", hibernate_command); /* This counts as activity; to prevent double sleeps. */ if (debug) printf("sleepd: activity: just woke up\n"); activity=1; oldtime=0; sleep_battery=0; } /* Rest is only needed if sleeping on inactivity. */ if (! max_unused && ! ac_max_unused) { sleep(sleep_time); continue; } if (autoprobe || have_irqs) { activity=check_irqs(activity, autoprobe); } if (use_net) { activity=check_net(activity); } if ((max_loadavg != 0) && (getloadavg(loadavg, 1) == 1) && (loadavg[0] >= max_loadavg)) { /* If the load average is too high */ if (debug) printf("sleepd: activity: load average %f\n", loadavg[0]); activity=1; } if (use_utmp == 1) { total_unused=check_utmp(total_unused); } if (ai.ac_line_status != prev_ac_line_status) { /* AC plug/unplug counts as activity. */ if (debug) printf("sleepd: activity: AC status change\n"); activity=1; } prev_ac_line_status=ai.ac_line_status; sleep(sleep_time); if (use_events) { pthread_mutex_lock(&activity_mutex); if (eventData.emactivity == 1) { if (debug) printf("sleepd: activity: keyboard/mouse events\n"); activity=1; } pthread_mutex_unlock(&activity_mutex); } if (activity) { total_unused = 0; } else { total_unused += sleep_time; if (ai.ac_line_status == 1) { /* On wall power. */ if (ac_max_unused > 0) { sleep_now = total_unused >= ac_max_unused; } } else if (max_unused > 0) { sleep_now = total_unused >= max_unused; } if (sleep_now && ! no_sleep && ! require_unused_and_battery) { syslog(LOG_NOTICE, "system inactive for %ds; forcing sleep", total_unused); if (system(sleep_command) != 0) syslog(LOG_ERR, "%s failed", sleep_command); total_unused=0; oldtime=0; sleep_now=0; } else if (sleep_now && ! no_sleep && sleep_battery) { syslog(LOG_NOTICE, "system inactive for %ds and battery level %d%% is below %d%%; forcing hibernaton", total_unused, ai.battery_percentage, min_batt); if (system(hibernate_command) != 0) syslog(LOG_ERR, "%s failed", hibernate_command); total_unused=0; oldtime=0; sleep_now=0; sleep_battery=0; } } /* * Keep track of how long it's been since we were last * here. If it was much longer than sleep_time, the system * was probably suspended, or this program was, (or the * kernel is thrashing :-), so clear idle counter. */ nowtime=time(NULL); /* The 1 is a necessary fudge factor. */ if (oldtime && nowtime - sleep_time > oldtime + 1) { no_sleep=0; /* reset, since they must have put it to sleep */ writecontrol(no_sleep); syslog(LOG_NOTICE, "%i sec sleep; resetting timer", (int)(nowtime - oldtime)); total_unused=0; } oldtime=nowtime; } }