1 // Copyright 2012 Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
4 // Use of this source code is governed by a BSD-style license
5 // that can be found in the LICENSE file in the root of the source
16 // pcap_wait returns when the next packet is available or the timeout expires.
17 // Since it uses pcap_get_selectable_fd, it will not work in Windows.
18 int pcap_wait(pcap_t *p, int usec) {
23 fd = pcap_get_selectable_fd(p);
35 return select(fd+1, &fds, NULL, NULL, &tv);
38 // block indefinitely if no timeout provided
39 return select(fd+1, &fds, NULL, NULL, NULL);
49 func (p *Handle) openLive() error {
50 buf := (*C.char)(C.calloc(errorBufferSize, 1))
51 defer C.free(unsafe.Pointer(buf))
53 // Change the device to non-blocking, we'll use pcap_wait to wait until the
54 // handle is ready to read.
55 if v := C.pcap_setnonblock(p.cptr, 1, buf); v == -1 {
56 return errors.New(C.GoString(buf))
62 // waitForPacket waits for a packet or for the timeout to expire.
63 func (p *Handle) waitForPacket() {
64 // need to wait less than the read timeout according to pcap documentation.
65 // timeoutMillis rounds up to at least one millisecond so we can safely
66 // subtract up to a millisecond.
67 usec := timeoutMillis(p.timeout) * 1000
70 C.pcap_wait(p.cptr, usec)