net_udp.go 839 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. )
  7. type NetUDPSockets struct {
  8. Sockets []NetUDPSocket `json:"sockets"`
  9. }
  10. type NetUDPSocket struct {
  11. NetSocket
  12. Drops uint64 `json:"drops"`
  13. }
  14. func ReadNetUDPSockets(path string, ip NetIPDecoder) (*NetUDPSockets, error) {
  15. b, err := ioutil.ReadFile(path)
  16. if err != nil {
  17. return nil, err
  18. }
  19. lines := strings.Split(string(b), "\n")
  20. udp := &NetUDPSockets{}
  21. for i := 1; i < len(lines); i++ {
  22. line := lines[i]
  23. f := strings.Fields(line)
  24. if len(f) < 13 {
  25. continue
  26. }
  27. s, err := parseNetSocket(f, ip)
  28. if err != nil {
  29. return nil, err
  30. }
  31. e := &NetUDPSocket{
  32. NetSocket: *s,
  33. Drops: 0,
  34. }
  35. if e.Drops, err = strconv.ParseUint(f[12], 10, 64); err != nil {
  36. return nil, err
  37. }
  38. udp.Sockets = append(udp.Sockets, *e)
  39. }
  40. return udp, nil
  41. }