A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
1.7 KiB

  1. package utils
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "strings"
  20. "os"
  21. "bufio"
  22. "errors"
  23. "net"
  24. )
  25. // Arpscan will read the ARP table and return the
  26. // hardware address for the specified source address
  27. func Arpscan(srcIP net.IP) (net.HardwareAddr, error) {
  28. procArp, err := os.Open("/proc/net/arp")
  29. if err != nil {
  30. return nil, err
  31. }
  32. defer procArp.Close()
  33. scanner := bufio.NewScanner(procArp)
  34. scanner.Scan() // skip header row
  35. for scanner.Scan() {
  36. line := scanner.Text()
  37. fields := strings.Fields(line)
  38. // IP-address HW-type Flags HW-address Mask Device
  39. if len(fields) == 6 {
  40. ipField, macField := fields[0], fields[3]
  41. // check if the MAC address is valid
  42. mac, err := net.ParseMAC(macField)
  43. if err != nil {
  44. continue
  45. }
  46. // check if it is a real IP address and
  47. // if it is the actual one we are looking for
  48. ip := net.ParseIP(ipField)
  49. if ip == nil || !ip.Equal(srcIP) {
  50. continue
  51. }
  52. return mac, nil
  53. }
  54. }
  55. return nil, errors.New("cannot find MAC address")
  56. }