added support for string type
[govpp.git] / vendor / github.com / google / gopacket / pcap / pcap_unix.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
3 //
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
6 // tree.
7 //
8 // +build !windows
9
10 package pcap
11
12 /*
13 #include <stdlib.h>
14 #include <pcap.h>
15
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) {
19         fd_set fds;
20         int fd;
21         struct timeval tv;
22
23         fd = pcap_get_selectable_fd(p);
24         if(fd < 0) {
25                 return fd;
26         }
27
28         FD_ZERO(&fds);
29         FD_SET(fd, &fds);
30
31         tv.tv_sec = 0;
32         tv.tv_usec = usec;
33
34         if(usec != 0) {
35                 return select(fd+1, &fds, NULL, NULL, &tv);
36         }
37
38         // block indefinitely if no timeout provided
39         return select(fd+1, &fds, NULL, NULL, NULL);
40 }
41 */
42 import "C"
43
44 import (
45         "errors"
46         "unsafe"
47 )
48
49 func (p *Handle) openLive() error {
50         buf := (*C.char)(C.calloc(errorBufferSize, 1))
51         defer C.free(unsafe.Pointer(buf))
52
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))
57         }
58
59         return nil
60 }
61
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
68         usec -= 100
69
70         C.pcap_wait(p.cptr, usec)
71 }