net_tcp.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. )
  7. type NetTCPSockets struct {
  8. Sockets []NetTCPSocket `json:"sockets"`
  9. }
  10. type NetTCPSocket struct {
  11. NetSocket
  12. RetransmitTimeout uint64 `json:"retransmit_timeout"`
  13. PredictedTick uint64 `json:"predicted_tick"`
  14. AckQuick uint8 `json:"ack_quick"`
  15. AckPingpong bool `json:"ack_pingpong"`
  16. SendingCongestionWindow uint64 `json:"sending_congestion_window"`
  17. SlowStartSizeThreshold int64 `json:"slow_start_size_threshold"`
  18. }
  19. func ReadNetTCPSockets(path string, ip NetIPDecoder) (*NetTCPSockets, error) {
  20. b, err := ioutil.ReadFile(path)
  21. if err != nil {
  22. return nil, err
  23. }
  24. lines := strings.Split(string(b), "\n")
  25. tcp := &NetTCPSockets{}
  26. for i := 1; i < len(lines); i++ {
  27. line := lines[i]
  28. f := strings.Fields(line)
  29. if len(f) < 17 {
  30. continue
  31. }
  32. s, err := parseNetSocket(f, ip)
  33. if err != nil {
  34. return nil, err
  35. }
  36. var n int64
  37. e := &NetTCPSocket{
  38. NetSocket: *s,
  39. }
  40. if e.RetransmitTimeout, err = strconv.ParseUint(f[12], 10, 64); err != nil {
  41. return nil, err
  42. }
  43. if e.PredictedTick, err = strconv.ParseUint(f[13], 10, 64); err != nil {
  44. return nil, err
  45. }
  46. if n, err = strconv.ParseInt(f[14], 10, 8); err != nil {
  47. return nil, err
  48. }
  49. e.AckQuick = uint8(n >> 1)
  50. e.AckPingpong = ((n & 1) == 1)
  51. if e.SendingCongestionWindow, err = strconv.ParseUint(f[15], 10, 64); err != nil {
  52. return nil, err
  53. }
  54. if e.SlowStartSizeThreshold, err = strconv.ParseInt(f[16], 10, 32); err != nil {
  55. return nil, err
  56. }
  57. tcp.Sockets = append(tcp.Sockets, *e)
  58. }
  59. return tcp, nil
  60. }