440 | 440 |
return errors.New(C.GoString(C.pcap_statustostr(status)))
|
441 | 441 |
}
|
442 | 442 |
|
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
|
444 | 444 |
// handle to set it up just the way you'd like.
|
445 | |
type Unactivated struct {
|
|
445 |
type InactiveHandle struct {
|
446 | 446 |
// cptr is the handle for the actual pcap C object.
|
447 | 447 |
cptr *C.pcap_t
|
448 | 448 |
blockForever bool
|
449 | 449 |
}
|
450 | 450 |
|
451 | |
// Activate activates the handle. The current Unactivated becomes invalid
|
|
451 |
// Activate activates the handle. The current InactiveHandle becomes invalid
|
452 | 452 |
// and all future function calls on it will fail.
|
453 | |
func (p *Unactivated) Activate() (*Handle, error) {
|
|
453 |
func (p *InactiveHandle) Activate() (*Handle, error) {
|
454 | 454 |
err := activateError(C.pcap_activate(p.cptr))
|
455 | 455 |
if err != aeNoError {
|
456 | 456 |
return nil, err
|
|
462 | 462 |
|
463 | 463 |
// CleanUp cleans up any stuff left over from a successful or failed building
|
464 | 464 |
// of a handle.
|
465 | |
func (p *Unactivated) CleanUp() {
|
|
465 |
func (p *InactiveHandle) CleanUp() {
|
466 | 466 |
if p.cptr != nil {
|
467 | 467 |
C.pcap_close(p.cptr)
|
468 | 468 |
}
|
469 | 469 |
}
|
470 | 470 |
|
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) {
|
476 | 476 |
buf := (*C.char)(C.calloc(errorBufferSize, 1))
|
477 | 477 |
defer C.free(unsafe.Pointer(buf))
|
478 | 478 |
dev := C.CString(device)
|
|
483 | 483 |
if cptr == nil {
|
484 | 484 |
return nil, errors.New(C.GoString(buf))
|
485 | 485 |
}
|
486 | |
return &Unactivated{cptr: cptr}, nil
|
|
486 |
return &InactiveHandle{cptr: cptr}, nil
|
487 | 487 |
}
|
488 | 488 |
|
489 | 489 |
// 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 {
|
491 | 491 |
if status := C.pcap_set_snaplen(p.cptr, C.int(snaplen)); status < 0 {
|
492 | 492 |
return statusError(status)
|
493 | 493 |
}
|
|
496 | 496 |
|
497 | 497 |
// SetPromisc sets the handle to either be promiscuous (capture packets
|
498 | 498 |
// unrelated to this host) or not.
|
499 | |
func (p *Unactivated) SetPromisc(promisc bool) error {
|
|
499 |
func (p *InactiveHandle) SetPromisc(promisc bool) error {
|
500 | 500 |
var pro C.int
|
501 | 501 |
if promisc {
|
502 | 502 |
pro = 1
|
|
510 | 510 |
// SetTimeout sets the read timeout for the handle.
|
511 | 511 |
//
|
512 | 512 |
// 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 {
|
514 | 514 |
p.blockForever = timeout < 0
|
515 | 515 |
if status := C.pcap_set_timeout(p.cptr, timeoutMillis(timeout)); status < 0 {
|
516 | 516 |
return statusError(status)
|
|
520 | 520 |
|
521 | 521 |
// SupportedTimestamps returns a list of supported timstamp types for this
|
522 | 522 |
// handle.
|
523 | |
func (p *Unactivated) SupportedTimestamps() (out []TimestampSource) {
|
|
523 |
func (p *InactiveHandle) SupportedTimestamps() (out []TimestampSource) {
|
524 | 524 |
var types *C.int
|
525 | 525 |
n := int(C.pcap_list_tstamp_types(p.cptr, &types))
|
526 | 526 |
defer C.pcap_free_tstamp_types(types)
|
|
533 | 533 |
|
534 | 534 |
// SetTimestampSource sets the type of timestamp generator PCAP uses when
|
535 | 535 |
// attaching timestamps to packets.
|
536 | |
func (p *Unactivated) SetTimestampSource(t TimestampSource) error {
|
|
536 |
func (p *InactiveHandle) SetTimestampSource(t TimestampSource) error {
|
537 | 537 |
if status := C.pcap_set_tstamp_type(p.cptr, C.int(t)); status < 0 {
|
538 | 538 |
return statusError(status)
|
539 | 539 |
}
|