Skip to content

Commit

Permalink
change net hardware adress to string.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Jun 2, 2014
1 parent 9b46031 commit 6ecec79
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
24 changes: 11 additions & 13 deletions net.go
Expand Up @@ -17,7 +17,6 @@ type NetIOCountersStat struct {
Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD)
}


// Addr is implemented compatibility to psutil
type Addr struct {
IP string `json:"ip"`
Expand All @@ -40,10 +39,10 @@ type NetInterfaceAddr struct {
}

type NetInterfaceStat struct {
MTU int `json:"mtu"` // maximum transmission unit
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
HardwareAddr []byte `json:"hardwareaddr"` // IEEE MAC-48, EUI-48 and EUI-64 form
Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast
MTU int `json:"mtu"` // maximum transmission unit
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
HardwareAddr string `json:"hardwareaddr"` // IEEE MAC-48, EUI-48 and EUI-64 form
Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast
Addrs []NetInterfaceAddr `json:"addrs"`
}

Expand All @@ -67,12 +66,11 @@ func (n NetInterfaceStat) String() string {
return string(s)
}

func (n NetInterfaceAddr) String() string{
func (n NetInterfaceAddr) String() string {
s, _ := json.Marshal(n)
return string(s)
}


func NetInterfaces() ([]NetInterfaceStat, error) {
is, err := net.Interfaces()
if err != nil {
Expand All @@ -85,29 +83,29 @@ func NetInterfaces() ([]NetInterfaceStat, error) {
if ifi.Flags&net.FlagUp != 0 {
flags = append(flags, "up")
}
if ifi.Flags & net.FlagBroadcast != 0 {
if ifi.Flags&net.FlagBroadcast != 0 {
flags = append(flags, "broadcast")
}
if ifi.Flags & net.FlagLoopback != 0{
if ifi.Flags&net.FlagLoopback != 0 {
flags = append(flags, "loopback")
}
if ifi.Flags & net.FlagPointToPoint != 0{
if ifi.Flags&net.FlagPointToPoint != 0 {
flags = append(flags, "pointtopoint")
}
if ifi.Flags & net.FlagMulticast != 0{
if ifi.Flags&net.FlagMulticast != 0 {
flags = append(flags, "multicast")
}

r := NetInterfaceStat{
Name: ifi.Name,
MTU: ifi.MTU,
HardwareAddr: ifi.HardwareAddr,
HardwareAddr: ifi.HardwareAddr.String(),
Flags: flags,
}
addrs, err := ifi.Addrs()
if err == nil {
r.Addrs = make([]NetInterfaceAddr, 0, len(addrs))
for _, addr := range addrs{
for _, addr := range addrs {
r.Addrs = append(r.Addrs, NetInterfaceAddr{
Addr: addr.String(),
})
Expand Down
10 changes: 5 additions & 5 deletions net_test.go
Expand Up @@ -53,16 +53,16 @@ func TestNetIOCounters(t *testing.T) {
}
}

func TestNetInterfaces(t *testing.T){
func TestNetInterfaces(t *testing.T) {
v, err := NetInterfaces()
if err != nil{
if err != nil {
t.Errorf("Could not get NetInterfaceStat: %v", err)
}
if len(v) == 0{
if len(v) == 0 {
t.Errorf("Could not get NetInterfaceStat: %v", err)
}
for _, vv := range v{
if vv.Name == ""{
for _, vv := range v {
if vv.Name == "" {
t.Errorf("Invalid NetInterface: %v", vv)
}
}
Expand Down

0 comments on commit 6ecec79

Please sign in to comment.