blob: d203883049b5025da966e214afcd03d59aa56a1e [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbarie35595062018-02-08 08:34:39 -050016package common
17
18import (
19 "github.com/google/gopacket"
20 "github.com/google/gopacket/layers"
21 "github.com/sirupsen/logrus"
22 "net"
23)
24
25func GetInterfaceIP(ifName string) string {
26 var err error
27 var netIf *net.Interface
28 var netAddrs []net.Addr
29 var netIp net.IP
30 var ipAddr string
31
32 if netIf, err = net.InterfaceByName(ifName); err == nil {
33 if netAddrs, err = netIf.Addrs(); err == nil {
34 for _, addr := range netAddrs {
35 Logger().WithFields(logrus.Fields{
36 "type": addr.Network(),
37 }).Debug("Address network type")
38 switch v := addr.(type) {
39 case *net.IPNet:
40 netIp = v.IP
41 case *net.IPAddr:
42 netIp = v.IP
43 }
44 if netIp == nil || netIp.IsLoopback() {
45 continue
46 }
47 netIp = netIp.To4()
48 if netIp == nil {
49 continue // not an ipv4 address
50 }
51 ipAddr = netIp.String()
52 break
53 }
54 }
55 }
56
57 return ipAddr
58}
59func GetHostIP(hostName string) string {
60 var err error
61 var ipAddrs []string
62 var ipAddr string
63
64 if ipAddrs, err = net.LookupHost(hostName); err == nil {
65 for _, ip := range ipAddrs {
66 if addr := net.ParseIP(ip); err == nil {
67 Logger().WithFields(logrus.Fields{
68 "ip": addr,
69 }).Debug("Host address")
70 if addr == nil /*|| addr.IsLoopback()*/ {
71 continue
72 }
73 ipAddr = ip
74 break
75 }
76 }
77 }
78
79 return ipAddr
80}
81func GetMacAddress(ifName string) net.HardwareAddr {
82 var err error
83 var netIf *net.Interface
84 var hwAddr net.HardwareAddr
85
86 if netIf, err = net.InterfaceByName(ifName); err == nil {
87 hwAddr = netIf.HardwareAddr
88 }
89
90 return hwAddr
91}
92
93func GetEthernetLayer(frame gopacket.Packet) *layers.Ethernet {
94 eth := &layers.Ethernet{}
95 if ethLayer := frame.Layer(layers.LayerTypeEthernet); ethLayer != nil {
96 eth, _ = ethLayer.(*layers.Ethernet)
97 }
98 return eth
99}
100func GetDot1QLayer(frame gopacket.Packet) *layers.Dot1Q {
101 var dot1q *layers.Dot1Q
102 //dot1q := &layers.Dot1Q{}
103 if dot1qLayer := frame.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil {
104 dot1q, _ = dot1qLayer.(*layers.Dot1Q)
105 }
106 return dot1q
107}
108
109func GetIpLayer(frame gopacket.Packet) *layers.IPv4 {
110 ip := &layers.IPv4{}
111 if ipLayer := frame.Layer(layers.LayerTypeIPv4); ipLayer != nil {
112 ip, _ = ipLayer.(*layers.IPv4)
113 }
114 return ip
115}
116func GetUdpLayer(frame gopacket.Packet) *layers.UDP {
117 udp := &layers.UDP{}
118 if udpLayer := frame.Layer(layers.LayerTypeUDP); udpLayer != nil {
119 udp, _ = udpLayer.(*layers.UDP)
120 }
121 return udp
122}