LoadWinPCAP attempts to dynamically load the wpcap DLL and resolve necessary functions
()
| 160 | |
| 161 | // LoadWinPCAP attempts to dynamically load the wpcap DLL and resolve necessary functions |
| 162 | func LoadWinPCAP() error { |
| 163 | if pcapLoaded { |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | kernel32, err := syscall.LoadLibrary("kernel32.dll") |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("couldn't load kernel32.dll") |
| 170 | } |
| 171 | defer syscall.FreeLibrary(kernel32) |
| 172 | |
| 173 | initDllPath(kernel32) |
| 174 | |
| 175 | if haveSearch, _ := syscall.GetProcAddress(kernel32, "AddDllDirectory"); haveSearch != 0 { |
| 176 | // if AddDllDirectory is present, we can use LOAD_LIBRARY_* stuff with LoadLibraryEx to avoid wpcap.dll hijacking |
| 177 | // see: https://msdn.microsoft.com/en-us/library/ff919712%28VS.85%29.aspx |
| 178 | const LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400 |
| 179 | const LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 |
| 180 | wpcapHandle, err = windows.LoadLibraryEx("wpcap.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS|LOAD_LIBRARY_SEARCH_SYSTEM32) |
| 181 | if err != nil { |
| 182 | return fmt.Errorf("couldn't load wpcap.dll") |
| 183 | } |
| 184 | } else { |
| 185 | // otherwise fall back to load it with the unsafe search cause by SetDllDirectory |
| 186 | wpcapHandle, err = windows.LoadLibrary("wpcap.dll") |
| 187 | if err != nil { |
| 188 | return fmt.Errorf("couldn't load wpcap.dll") |
| 189 | } |
| 190 | } |
| 191 | initLoadedDllPath(kernel32) |
| 192 | msvcrtHandle, err = syscall.LoadLibrary("msvcrt.dll") |
| 193 | if err != nil { |
| 194 | return fmt.Errorf("couldn't load msvcrt.dll") |
| 195 | } |
| 196 | callocPtr, err = syscall.GetProcAddress(msvcrtHandle, "calloc") |
| 197 | if err != nil { |
| 198 | return fmt.Errorf("couldn't get calloc function") |
| 199 | } |
| 200 | |
| 201 | pcapStrerrorPtr = mustLoad("pcap_strerror") |
| 202 | pcapStatustostrPtr = mightLoad("pcap_statustostr") // not available on winpcap |
| 203 | pcapOpenLivePtr = mustLoad("pcap_open_live") |
| 204 | pcapOpenOfflinePtr = mustLoad("pcap_open_offline") |
| 205 | pcapClosePtr = mustLoad("pcap_close") |
| 206 | pcapGeterrPtr = mustLoad("pcap_geterr") |
| 207 | pcapStatsPtr = mustLoad("pcap_stats") |
| 208 | pcapCompilePtr = mustLoad("pcap_compile") |
| 209 | pcapFreecodePtr = mustLoad("pcap_freecode") |
| 210 | pcapLookupnetPtr = mustLoad("pcap_lookupnet") |
| 211 | pcapOfflineFilterPtr = mustLoad("pcap_offline_filter") |
| 212 | pcapSetfilterPtr = mustLoad("pcap_setfilter") |
| 213 | pcapListDatalinksPtr = mustLoad("pcap_list_datalinks") |
| 214 | pcapFreeDatalinksPtr = mustLoad("pcap_free_datalinks") |
| 215 | pcapDatalinkValToNamePtr = mustLoad("pcap_datalink_val_to_name") |
| 216 | pcapDatalinkValToDescriptionPtr = mustLoad("pcap_datalink_val_to_description") |
| 217 | pcapOpenDeadPtr = mustLoad("pcap_open_dead") |
| 218 | pcapNextExPtr = mustLoad("pcap_next_ex") |
| 219 | pcapDatalinkPtr = mustLoad("pcap_datalink") |
no test coverage detected
searching dependent graphs…