Codebase list gopacket / 2c5b054
Fix stupid english mistakes. For some reason I totally forgot that Unactivated isn't a word... it's "Inactive". Sigh. Fixed. Graeme Connell 9 years ago
4 changed file(s) with 34 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
3636 // This is a little complicated because we want to allow all possible options
3737 // for creating the packet capture handle... instead of all this you can
3838 // just call pcap.OpenLive if you want a simple handle.
39 unactivated, err := pcap.Create(*iface)
39 inactive, err := pcap.NewInactiveHandle(*iface)
4040 if err != nil {
4141 log.Fatal("could not create: %v", err)
4242 }
43 defer unactivated.CleanUp()
44 if err = unactivated.SetSnapLen(*snaplen); err != nil {
43 defer inactive.CleanUp()
44 if err = inactive.SetSnapLen(*snaplen); err != nil {
4545 log.Fatal("could not set snap length: %v", err)
46 } else if err = unactivated.SetPromisc(*promisc); err != nil {
46 } else if err = inactive.SetPromisc(*promisc); err != nil {
4747 log.Fatal("could not set promisc mode: %v", err)
48 } else if err = unactivated.SetTimeout(time.Second); err != nil {
48 } else if err = inactive.SetTimeout(time.Second); err != nil {
4949 log.Fatal("could not set timeout: %v", err)
5050 }
5151 if *tstype != "" {
5252 if t, err := pcap.TimestampSourceFromString(*tstype); err != nil {
53 log.Fatalf("Supported timestamp types: %v", unactivated.SupportedTimestamps())
54 } else if err := unactivated.SetTimestampSource(t); err != nil {
55 log.Fatalf("Supported timestamp types: %v", unactivated.SupportedTimestamps())
53 log.Fatalf("Supported timestamp types: %v", inactive.SupportedTimestamps())
54 } else if err := inactive.SetTimestampSource(t); err != nil {
55 log.Fatalf("Supported timestamp types: %v", inactive.SupportedTimestamps())
5656 }
5757 }
58 if handle, err = unactivated.Activate(); err != nil {
58 if handle, err = inactive.Activate(); err != nil {
5959 log.Fatal("PCAP Activate error:", err)
6060 }
6161 defer handle.Close()
4040 }
4141 }
4242
43 Unactivated Handles
43 Inactive Handles
4444
45 Newer PCAP functionality requires the concept of an 'unactivated' PCAP handle.
45 Newer PCAP functionality requires the concept of an 'inactive' PCAP handle.
4646 Instead of constantly adding new arguments to pcap_open_live, users now call
4747 pcap_create to create a handle, set it up with a bunch of optional function
4848 calls, then call pcap_activate to activate it. This library mirrors that
4949 mechanism, for those that want to expose/use these new features:
5050
51 unactivated, err := pcap.Create(deviceName)
51 inactive, err := pcap.NewInactiveHandle(deviceName)
5252 if err != nil {
5353 log.Fatal(err)
5454 }
55 defer unactivated.CleanUp()
55 defer inactive.CleanUp()
5656
57 // Call various functions on unactivated to set it up the way you'd like:
58 if err = unactivated.SetTimeout(time.Minute); err != nil {
57 // Call various functions on inactive to set it up the way you'd like:
58 if err = inactive.SetTimeout(time.Minute); err != nil {
5959 log.Fatal(err)
60 } else if err = unactivated.SetTimestampSource("foo"); err != nil {
60 } else if err = inactive.SetTimestampSource("foo"); err != nil {
6161 log.Fatal(err)
6262 }
6363
6464 // Finally, create the actual handle by calling Activate:
65 handle, err := unactivated.Activate() // after this, unactivated is no longer valid
65 handle, err := inactive.Activate() // after this, inactive is no longer valid
6666 if err != nil {
6767 log.Fatal(err)
6868 }
440440 return errors.New(C.GoString(C.pcap_statustostr(status)))
441441 }
442442
443 // Unactivated allows you to call pre-pcap_activate functions on your pcap
443 // InactiveHandle allows you to call pre-pcap_activate functions on your pcap
444444 // handle to set it up just the way you'd like.
445 type Unactivated struct {
445 type InactiveHandle struct {
446446 // cptr is the handle for the actual pcap C object.
447447 cptr *C.pcap_t
448448 blockForever bool
449449 }
450450
451 // Activate activates the handle. The current Unactivated becomes invalid
451 // Activate activates the handle. The current InactiveHandle becomes invalid
452452 // and all future function calls on it will fail.
453 func (p *Unactivated) Activate() (*Handle, error) {
453 func (p *InactiveHandle) Activate() (*Handle, error) {
454454 err := activateError(C.pcap_activate(p.cptr))
455455 if err != aeNoError {
456456 return nil, err
462462
463463 // CleanUp cleans up any stuff left over from a successful or failed building
464464 // of a handle.
465 func (p *Unactivated) CleanUp() {
465 func (p *InactiveHandle) CleanUp() {
466466 if p.cptr != nil {
467467 C.pcap_close(p.cptr)
468468 }
469469 }
470470
471 // Create creates a new Unactivated, which wraps an un-activated PCAP handle.
472 // Callers of Create should immediately defer 'CleanUp', as in:
473 // unactivated := Create("eth0")
474 // defer unactivated.CleanUp()
475 func Create(device string) (*Unactivated, error) {
471 // NewInactiveHandle creates a new InactiveHandle, which wraps an un-activated PCAP handle.
472 // Callers of NewInactiveHandle should immediately defer 'CleanUp', as in:
473 // inactive := NewInactiveHandle("eth0")
474 // defer inactive.CleanUp()
475 func NewInactiveHandle(device string) (*InactiveHandle, error) {
476476 buf := (*C.char)(C.calloc(errorBufferSize, 1))
477477 defer C.free(unsafe.Pointer(buf))
478478 dev := C.CString(device)
483483 if cptr == nil {
484484 return nil, errors.New(C.GoString(buf))
485485 }
486 return &Unactivated{cptr: cptr}, nil
486 return &InactiveHandle{cptr: cptr}, nil
487487 }
488488
489489 // SetSnapLen sets the snap length (max bytes per packet to capture).
490 func (p *Unactivated) SetSnapLen(snaplen int) error {
490 func (p *InactiveHandle) SetSnapLen(snaplen int) error {
491491 if status := C.pcap_set_snaplen(p.cptr, C.int(snaplen)); status < 0 {
492492 return statusError(status)
493493 }
496496
497497 // SetPromisc sets the handle to either be promiscuous (capture packets
498498 // unrelated to this host) or not.
499 func (p *Unactivated) SetPromisc(promisc bool) error {
499 func (p *InactiveHandle) SetPromisc(promisc bool) error {
500500 var pro C.int
501501 if promisc {
502502 pro = 1
510510 // SetTimeout sets the read timeout for the handle.
511511 //
512512 // See the package documentation for important details regarding 'timeout'.
513 func (p *Unactivated) SetTimeout(timeout time.Duration) error {
513 func (p *InactiveHandle) SetTimeout(timeout time.Duration) error {
514514 p.blockForever = timeout < 0
515515 if status := C.pcap_set_timeout(p.cptr, timeoutMillis(timeout)); status < 0 {
516516 return statusError(status)
520520
521521 // SupportedTimestamps returns a list of supported timstamp types for this
522522 // handle.
523 func (p *Unactivated) SupportedTimestamps() (out []TimestampSource) {
523 func (p *InactiveHandle) SupportedTimestamps() (out []TimestampSource) {
524524 var types *C.int
525525 n := int(C.pcap_list_tstamp_types(p.cptr, &types))
526526 defer C.pcap_free_tstamp_types(types)
533533
534534 // SetTimestampSource sets the type of timestamp generator PCAP uses when
535535 // attaching timestamps to packets.
536 func (p *Unactivated) SetTimestampSource(t TimestampSource) error {
536 func (p *InactiveHandle) SetTimestampSource(t TimestampSource) error {
537537 if status := C.pcap_set_tstamp_type(p.cptr, C.int(t)); status < 0 {
538538 return statusError(status)
539539 }
7171 return fmt.Errorf("setbpf: %v", err)
7272 }
7373 case "timestamp":
74 u, err := pcap.Create(iface.Name)
74 u, err := pcap.NewInactiveHandle(iface.Name)
7575 if err != nil {
7676 return err
7777 }