package gotoc import ( "fmt" "unsafe" "github.com/vmware/leap/util" ) /* #include "../../../../../Include/sigar.h" #include #include typedef unsigned char byte; void translateNetConfig(sigar_t *sigar){ sigar_net_interface_list_t net_iflist; size_t i; sigar_net_interface_list_get(sigar, &net_iflist); printf("number: %lu\n", net_iflist.number) ; for (i = 0; i < net_iflist.number; i++) { char *ifname = net_iflist.data[i]; sigar_net_interface_stat_t ifstat; sigar_net_interface_config_t config; int ret=sigar_net_interface_config_get(sigar, ifname, &(config)); if(ret!=SIGAR_OK){ printf("error number: %d\n",ret); return; } sigar_uint32_t a=config.address.addr.in; char buf[180]; unsigned char* ucp = (unsigned char *)&a; sprintf(buf, "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff); printf("%s: %s\n",ifname, buf); } sigar_net_interface_config_t netconfPrimary; sigar_net_interface_config_primary_get(sigar, &netconfPrimary); sigar_uint32_t a=netconfPrimary.address.addr.in; char buf[18]; unsigned char* ucp = (unsigned char *)&a; sprintf(buf, "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, ucp[2] & 0xff, ucp[3] & 0xff); printf("%s\n",buf); sigar_net_interface_list_destroy(sigar, &net_iflist); //char *name=netinfo->name; //char *type=netinfo->type; //sigar_uint32_t a=netinfo->address.addr.in; //printf("%s broadcast:\n", netinfo->address.addr.mac[0]); //printf("%d.%d.%d.%d", (byte)a>>24 & 0xFF, (byte)a>>16 & 0xFF, (byte)a>>8& 0xFF, (byte)a& 0xFF); } */ import "C" type NetConfig struct { } type NetAddress struct{ Family string In uint32 In6 uint32 Mac string } func (this *NetAddress) String() string { return fmt.Sprintf("{ Family: %v, In: %v, in6: %v, Mac: %v }", this.Family, this.In, this.In6, this.Mac) } type InterfaceConfigInfo struct { Name string Type string Description string Hwaddr *NetAddress Address *NetAddress Destination *NetAddress Broadcast *NetAddress Netmask *NetAddress Address6 *NetAddress Prefix6Length int Scope6 int Flags uint64 Mtu uint64 Metric uint64 TxQueueLen int } func (this *InterfaceConfigInfo) String() string { return fmt.Sprintf(`Name: %v, Type: %v, Description: %v, Hwaddr: %v, Address: %v, Destination: %v, Broadcast: %v, Netmask: %v, Address6: %v, Prefix6Length: %v, Scope6: %v, Flags: %v, Mtu: %v, Metric: %v, TxQueueLen: %v`, this.Name, this.Type, this.Description, this.Hwaddr, this.Address, this.Destination, this.Broadcast, this.Netmask, this.Address6, this.Prefix6Length, this.Scope6, this.Flags, this.Mtu, this.Metric, this.TxQueueLen, ) } type NetIfaceUsageInfo struct { RxPackets uint64 RxBytes uint64 RxErrors uint64 RxDropped uint64 RxOverruns uint64 RxFrame uint64 /* transmitted */ TxPackets uint64 TxBytes uint64 TxErrors uint64 TxDropped uint64 TxOverruns uint64 TxCollisions uint64 TxCarrier uint64 Speed uint64 } //---------------------------------------------------------------------------------------- //Top-Level Top-Level Top-Level Top-Level Top-Level Top-Level Top-Level Top-Level Top-Level //---------------------------------------------------------------------------------------- func GetNetInterfaceConfig() (result []*InterfaceConfigInfo, err error) { return getNetInterfaceConfigWithSigar(GetSigarHandle()), nil } func GetNetIfaceUsageInfo(netIfaceName string) (netifaceInfo *NetIfaceUsageInfo, err error) { defer util.Panic2Error(&err) c_netifaceName := C.CString(netIfaceName) defer Free(c_netifaceName) var c_sigar_net_interface_stat_t C.sigar_net_interface_stat_t if status := int(C.sigar_net_interface_stat_get(GetSigarHandle(), c_netifaceName, &c_sigar_net_interface_stat_t)) ; status != SIGAR_OK { return nil,fmt.Errorf("Failed to rertieve network interface status for: %v with error: %v", netIfaceName, status) } return &NetIfaceUsageInfo{ RxPackets: uint64(c_sigar_net_interface_stat_t.rx_packets), RxBytes: uint64(c_sigar_net_interface_stat_t.rx_bytes), RxErrors: uint64(c_sigar_net_interface_stat_t.rx_errors), RxDropped: uint64(c_sigar_net_interface_stat_t.rx_dropped), RxOverruns: uint64(c_sigar_net_interface_stat_t.rx_overruns), RxFrame: uint64(c_sigar_net_interface_stat_t.rx_frame), TxPackets: uint64(c_sigar_net_interface_stat_t.tx_packets), TxBytes: uint64(c_sigar_net_interface_stat_t.tx_bytes), TxErrors: uint64(c_sigar_net_interface_stat_t.tx_errors), TxDropped: uint64(c_sigar_net_interface_stat_t.tx_dropped), TxOverruns: uint64(c_sigar_net_interface_stat_t.tx_overruns), TxCollisions: uint64(c_sigar_net_interface_stat_t.tx_collisions), TxCarrier: uint64(c_sigar_net_interface_stat_t.tx_carrier), Speed: uint64(c_sigar_net_interface_stat_t.speed), },nil } func GetNetConfig()(C.sigar_net_interface_config_t,[]C.sigar_net_interface_config_t,[]C.sigar_net_interface_stat_t){ var sigar *C.sigar_t=GetSigarHandle() var netlistconf C.sigar_net_interface_list_t var netconfPrimary C.sigar_net_interface_config_t C.sigar_net_interface_config_primary_get(sigar, &netconfPrimary) C.sigar_net_interface_list_get(sigar, &netlistconf) var length int=int(netlistconf.number) goInterfacesNames := ((*[1 << 30]*C.char)(unsafe.Pointer(netlistconf.data)))[:length] //cinteraces:=GetGoSlice(length, unsafe.Pointer(netlistconf.data)) //goInterfacesNames := *(*[]C.char)(unsafe.Pointer(&cinteraces)) var netconf =make([]C.sigar_net_interface_config_t,length) var netstat =make([]C.sigar_net_interface_stat_t,length) for i:=0;i