github.com/la5nta/wl2k-go@v0.11.8/transport/ax25/heard_linux.go (about)

     1  // Copyright 2015 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
     2  // Use of this source code is governed by the MIT-license that can be
     3  // found in the LICENSE file.
     4  
     5  //go:build libax25 && cgo
     6  // +build libax25,cgo
     7  
     8  package ax25
     9  
    10  //#include <time.h>
    11  //#include <netax25/axlib.h>
    12  //#include <netax25/mheard.h>
    13  import "C"
    14  
    15  import (
    16  	"io"
    17  	"os"
    18  	"strings"
    19  	"time"
    20  	"unsafe"
    21  )
    22  
    23  const MheardDataFile = "/var/ax25/mheard/mheard.dat"
    24  
    25  // Heard returns all stations heard via the given axport.
    26  //
    27  // This function parses the content of MhardDataFile which
    28  // is normally written by mheardd. The mheardd daemon must
    29  // be running on the system to record heard stations.
    30  func Heard(axPort string) (map[string]time.Time, error) {
    31  	var mheard C.struct_mheard_struct
    32  
    33  	f, err := os.Open(MheardDataFile)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	heard := make(map[string]time.Time)
    39  	for {
    40  		data := (*(*[999]byte)(unsafe.Pointer(&mheard)))[0:unsafe.Sizeof(mheard)]
    41  		if _, err := f.Read(data); err == io.EOF {
    42  			break
    43  		} else if err != nil {
    44  			return heard, err
    45  		}
    46  
    47  		port := C.GoString((*C.char)(unsafe.Pointer(&mheard.portname[0])))
    48  		if !strings.EqualFold(port, axPort) {
    49  			continue
    50  		}
    51  
    52  		from := (*C.ax25_address)(unsafe.Pointer(&mheard.from_call))
    53  		fromAddr := AddressFromString(C.GoString(C.ax25_ntoa(from)))
    54  		t := time.Unix(int64(mheard.last_heard), 0)
    55  
    56  		heard[fromAddr.String()] = t
    57  	}
    58  
    59  	return heard, f.Close()
    60  }