saleks28

pasoib8_fields

Dec 15th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.58 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5.     "net"
  6.     "strings"
  7.  
  8.     "github.com/google/gopacket/layers"
  9. )
  10.  
  11. //Protocols bla-bla
  12. var Protocols = []layers.IPProtocol{
  13.     layers.IPProtocolIPv6HopByHop,
  14.     layers.IPProtocolICMPv4,
  15.     layers.IPProtocolIGMP,
  16.     layers.IPProtocolIPv4,
  17.     layers.IPProtocolTCP,
  18.     layers.IPProtocolUDP,
  19.     layers.IPProtocolRUDP,
  20.     layers.IPProtocolIPv6,
  21.     layers.IPProtocolIPv6Routing,
  22.     layers.IPProtocolIPv6Fragment,
  23.     layers.IPProtocolGRE,
  24.     layers.IPProtocolESP,
  25.     layers.IPProtocolAH,
  26.     layers.IPProtocolICMPv6,
  27.     layers.IPProtocolNoNextHeader,
  28.     layers.IPProtocolIPv6Destination,
  29.     layers.IPProtocolOSPF,
  30.     layers.IPProtocolIPIP,
  31.     layers.IPProtocolEtherIP,
  32.     layers.IPProtocolVRRP,
  33.     layers.IPProtocolSCTP,
  34.     layers.IPProtocolUDPLite,
  35.     layers.IPProtocolMPLSInIP,
  36. }
  37.  
  38. //EtherTypes bla-bla
  39. var EtherTypes = []layers.EthernetType{
  40.     layers.EthernetTypeLLC,
  41.     layers.EthernetTypeIPv4,
  42.     layers.EthernetTypeARP,
  43.     layers.EthernetTypeIPv6,
  44.     layers.EthernetTypeCiscoDiscovery,
  45.     layers.EthernetTypeNortelDiscovery,
  46.     layers.EthernetTypeTransparentEthernetBridging,
  47.     layers.EthernetTypeDot1Q,
  48.     layers.EthernetTypePPP,
  49.     layers.EthernetTypePPPoEDiscovery,
  50.     layers.EthernetTypePPPoESession,
  51.     layers.EthernetTypeMPLSUnicast,
  52.     layers.EthernetTypeMPLSMulticast,
  53.     layers.EthernetTypeEAPOL,
  54.     layers.EthernetTypeQinQ,
  55.     layers.EthernetTypeLinkLayerDiscovery,
  56.     layers.EthernetTypeEthernetCTP,
  57. }
  58.  
  59. //SetIPFlag function uses const values to set field of IP header
  60. func SetIPFlag(str string) layers.IPv4Flag {
  61.     switch str {
  62.     case "IPv4EvilBit":
  63.         return layers.IPv4EvilBit
  64.     case "IPv4DontFragment":
  65.         return layers.IPv4DontFragment
  66.     case "IPv4MoreFragments":
  67.         return layers.IPv4MoreFragments
  68.     }
  69.     return 0
  70. }
  71.  
  72. //SetIPProtocol function compares const values with input
  73. func SetIPProtocol(str string) layers.IPProtocol {
  74.     for _, protocol := range Protocols {
  75.         if protocol.String() == str {
  76.             return protocol
  77.         }
  78.     }
  79.     return 0
  80. }
  81.  
  82. //AutoMAC function returns a mac addr of interface finding it by ip
  83. func AutoMAC(ip net.IP, interfArray []net.Interface) net.HardwareAddr {
  84.     for _, inter := range interfArray {
  85.         addrs, err := inter.Addrs()
  86.         if err != nil {
  87.             log.Fatal(err)
  88.         }
  89.         println()
  90.         for _, addr := range addrs {
  91.             str := addr.String()
  92.             ippart := strings.Split(str, "/")
  93.             if ip.String() == ippart[0] {
  94.                 return inter.HardwareAddr
  95.             }
  96.         }
  97.     }
  98.     return net.HardwareAddr{}
  99. }
  100.  
  101. //SetEthType function finds a ethernet type in const enum
  102. func SetEthType(str string) layers.EthernetType {
  103.     for _, i := range EtherTypes {
  104.         if i.String() == str {
  105.             return i
  106.         }
  107.     }
  108.     return 0
  109. }
Add Comment
Please, Sign In to add comment