int load_libpcap(void) { char *error; char *file[] = {"libpcap.so", "libpcap.so.1", "libpcap.so.1.0", "libpcap.so.0.9", "libpcap.so.0.8"}; int i, count = 5; for (i=0; i<count && !libpcap; i++) { libpcap = dlopen(file[i], RTLD_LAZY); error = dlerror(); } if (libpcap == NULL) { printf("!! 打开libpcap失败,请检查是否已安装该库文件。\n"); return -1; } if ((pcap_findalldevs = dlsym(libpcap, "pcap_findalldevs"), error = dlerror()) != NULL || (pcap_freealldevs = dlsym(libpcap, "pcap_freealldevs"), error = dlerror()) != NULL || (pcap_open_live = dlsym(libpcap, "pcap_open_live"), error = dlerror()) != NULL || (pcap_compile = dlsym(libpcap, "pcap_compile"), error = dlerror()) != NULL || (pcap_setfilter = dlsym(libpcap, "pcap_setfilter"), error = dlerror()) != NULL || (pcap_geterr = dlsym(libpcap, "pcap_geterr"), error = dlerror()) != NULL || (pcap_freecode = dlsym(libpcap, "pcap_freecode"), error = dlerror()) != NULL || (pcap_loop = dlsym(libpcap, "pcap_loop"), error = dlerror()) != NULL || (pcap_close = dlsym(libpcap, "pcap_close"), error = dlerror()) != NULL || (pcap_breakloop = dlsym(libpcap, "pcap_breakloop"), error = dlerror()) != NULL || (pcap_sendpacket = dlsym(libpcap, "pcap_sendpacket"), error = dlerror()) != NULL) { printf("!! 从libpcap获取函数失败: %s\n", error); free_libpcap(); return -1; } return 0; }
static void exit_handle(void) { if (state != ID_DISCONNECT) switchState(ID_DISCONNECT); if (hPcap != NULL) pcap_close(hPcap); if (fillBuf != NULL) free(fillBuf); if (lockfd > -1) close(lockfd); #ifndef NO_NOTIFY free_libnotify(); #endif #ifndef NO_DYLOAD free_libpcap(); #endif printf(">! 认证已退出。\n"); fflush(stdout); }